Skip to content

Commit

Permalink
add more test debug prints and better rekeying handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed Sep 29, 2023
1 parent 7578a7d commit bbc9f9e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 41 deletions.
61 changes: 43 additions & 18 deletions examples/echoserver/echoserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1161,24 +1161,47 @@ static int sftp_worker(thread_ctx_t* threadCtx)
s = (WS_SOCKET_T)wolfSSH_get_fd(ssh);

do {
if (wolfSSH_SFTP_PendingSend(ssh)) {
/* Yes, process the SFTP data. */
ret = wolfSSH_SFTP_read(ssh);
error = wolfSSH_get_error(ssh);
timeout = (ret == WS_REKEYING) ?
TEST_SFTP_TIMEOUT : TEST_SFTP_TIMEOUT_NONE;
if (error == WS_WANT_READ || error == WS_WANT_WRITE ||
error == WS_CHAN_RXD || error == WS_REKEYING ||
error == WS_WINDOW_FULL)
ret = error;
if (error == WS_WANT_WRITE && wolfSSH_SFTP_PendingSend(ssh)) {
continue; /* no need to spend time attempting to pull data
* if there is still pending sends */
}
if (error == WS_EOF) {
break;
}
}

selected = tcp_select(s, timeout);
if (selected == WS_SELECT_ERROR_READY) {
break;
}

if (selected == WS_SELECT_RECV_READY) {
if (ret == WS_WANT_READ || ret == WS_WANT_WRITE ||
selected == WS_SELECT_RECV_READY) {
ret = wolfSSH_worker(ssh, NULL);
error = wolfSSH_get_error(ssh);
if (ret == WS_REKEYING) {
/* In a rekey, keep turning the crank. */
/* In a rekey, keeping turning the crank. */
timeout = TEST_SFTP_TIMEOUT;
continue;
}
if (error == WS_WANT_READ) {
/* If would block, keep turning the crank. */

if (error == WS_WANT_READ || error == WS_WANT_WRITE ||
error == WS_WINDOW_FULL) {
timeout = TEST_SFTP_TIMEOUT;
ret = error;
continue;
}

if (error == WS_EOF) {
break;
}
Expand All @@ -1188,39 +1211,41 @@ static int sftp_worker(thread_ctx_t* threadCtx)
}
}

if (wolfSSH_SFTP_PendingSend(ssh)) {
/* Yes, process the SFTP data. */
ret = wolfSSH_SFTP_read(ssh);
timeout = (ret == WS_REKEYING) ?
TEST_SFTP_TIMEOUT : TEST_SFTP_TIMEOUT_NONE;
continue;
}

ret = wolfSSH_stream_peek(ssh, peek_buf, sizeof(peek_buf));
if (ret > 0) {
/* Yes, process the SFTP data. */
ret = wolfSSH_SFTP_read(ssh);
error = wolfSSH_get_error(ssh);
timeout = (ret == WS_REKEYING) ?
TEST_SFTP_TIMEOUT : TEST_SFTP_TIMEOUT_NONE;
if (error == WS_WANT_READ || error == WS_WANT_WRITE ||
error == WS_CHAN_RXD || error == WS_REKEYING ||
error == WS_WINDOW_FULL)
ret = error;
if (error == WS_EOF)
break;
continue;
}
else if (ret == WS_REKEYING) {
timeout = TEST_SFTP_TIMEOUT;
continue;
}
else if (ret < 0) {
error = wolfSSH_get_error(ssh);
if (error == WS_EOF)
break;
}

/* Old check for EOF here */
{
if (ret == WS_FATAL_ERROR && error == 0) {
WOLFSSH_CHANNEL* channel =
wolfSSH_ChannelNext(ssh, NULL);
wolfSSH_ChannelNext(ssh, NULL);
if (channel && wolfSSH_ChannelGetEof(channel)) {
ret = WS_EOF;
ret = 0;
break;
}
}

timeout = TEST_SFTP_TIMEOUT;
} while (1);
} while (ret != WS_FATAL_ERROR);

return ret;
}
Expand Down
54 changes: 40 additions & 14 deletions examples/sftpclient/sftpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,18 +503,19 @@ static int doCmds(func_args* args)
}

do {
ret = wolfSSH_SFTP_Get(ssh, pt, to, resume, &myStatusCb);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}

ret = wolfSSH_SFTP_Get(ssh, pt, to, resume, &myStatusCb);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
} while (ret == WS_WANT_READ || ret == WS_WANT_WRITE ||
ret == WS_CHAN_RXD);
ret == WS_CHAN_RXD || ret == WS_REKEYING);

#ifndef WOLFSSH_NO_TIMESTAMP
WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME);
Expand Down Expand Up @@ -607,11 +608,19 @@ static int doCmds(func_args* args)
}

do {
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}

ret = wolfSSH_SFTP_Put(ssh, pt, to, resume, &myStatusCb);
err = wolfSSH_get_error(ssh);
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
err == WS_CHAN_RXD || err == WS_REKEYING) &&
ret == WS_FATAL_ERROR);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
} while (ret == WS_WANT_READ || ret == WS_WANT_WRITE ||
ret == WS_CHAN_RXD || ret == WS_REKEYING);

#ifndef WOLFSSH_NO_TIMESTAMP
WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME);
Expand Down Expand Up @@ -921,10 +930,19 @@ static int doCmds(func_args* args)
}

do {
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}

ret = wolfSSH_SFTP_Rename(ssh, pt, to);
err = wolfSSH_get_error(ssh);
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
&& ret != WS_SUCCESS);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
} while (ret == WS_WANT_READ || ret == WS_WANT_WRITE ||
ret == WS_CHAN_RXD || ret == WS_REKEYING);
if (ret != WS_SUCCESS) {
if (SFTP_FPUTS(args, "Error with rename\n") < 0) {
err_msg("fputs error");
Expand All @@ -942,10 +960,18 @@ static int doCmds(func_args* args)
WS_SFTPNAME* current;

do {
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
ret = wolfSSH_worker(ssh, NULL);
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
ret = wolfSSH_get_error(ssh);
}
}

current = wolfSSH_SFTP_LS(ssh, workingDir);
err = wolfSSH_get_error(ssh);
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
&& current == NULL && err != WS_SUCCESS);
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
err == WS_REKEYING) &&
(current == NULL && err != WS_SUCCESS));

if (WSTRNSTR(msg, "-s", MAX_CMD_SZ) != NULL) {
char tmpStr[WOLFSSH_MAX_FILENAME];
Expand Down
21 changes: 14 additions & 7 deletions src/wolfsftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -6265,7 +6265,8 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir)
case STATE_LS_REALPATH:
state->name = wolfSSH_SFTP_RealPath(ssh, dir);
if (state->name == NULL) {
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE) {
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE &&
ssh->error != WS_REKEYING) {
wolfSSH_SFTP_ClearState(ssh, STATE_ID_LS);
}
return NULL;
Expand All @@ -6277,7 +6278,8 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir)
if (wolfSSH_SFTP_OpenDir(ssh, (byte*)state->name->fName,
state->name->fSz) != WS_SUCCESS) {
WLOG(WS_LOG_SFTP, "Unable to open directory");
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE) {
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE &&
ssh->error != WS_REKEYING) {
wolfSSH_SFTPNAME_list_free(state->name); state->name = NULL;
wolfSSH_SFTP_ClearState(ssh, STATE_ID_LS);
}
Expand All @@ -6293,7 +6295,8 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir)
if (wolfSSH_SFTP_GetHandle(ssh, state->handle, (word32*)&state->sz)
!= WS_SUCCESS) {
WLOG(WS_LOG_SFTP, "Unable to get handle");
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE) {
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE &&
ssh->error != WS_REKEYING) {
wolfSSH_SFTP_ClearState(ssh, STATE_ID_LS);
}
return NULL;
Expand All @@ -6306,7 +6309,8 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir)
* times so we have to assign to state->name later. */
names = wolfSSH_SFTP_ReadDir(ssh, state->handle, state->sz);
if (names == NULL) {
if (ssh->error == WS_WANT_READ || ssh->error == WS_WANT_WRITE) {
if (ssh->error == WS_WANT_READ || ssh->error == WS_WANT_WRITE ||
ssh->error == WS_REKEYING) {
return NULL;
}
WLOG(WS_LOG_SFTP, "Error reading directory");
Expand All @@ -6328,7 +6332,8 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir)
names = wolfSSH_SFTP_ReadDir(ssh, state->handle, state->sz);
if (names == NULL) {
if (ssh->error == WS_WANT_READ
|| ssh->error == WS_WANT_WRITE) {
|| ssh->error == WS_WANT_WRITE
|| ssh->error == WS_REKEYING) {
/* State does not change so we will get back to this case
* clause in non-blocking mode. */
return NULL;
Expand All @@ -6346,7 +6351,8 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir)
if (wolfSSH_SFTP_Close(ssh, state->handle, state->sz)
!= WS_SUCCESS) {
WLOG(WS_LOG_SFTP, "Error closing handle");
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE) {
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE &&
ssh->error != WS_REKEYING) {
wolfSSH_SFTPNAME_list_free(state->name);
state->name = NULL;
wolfSSH_SFTP_ClearState(ssh, STATE_ID_LS);
Expand Down Expand Up @@ -8692,7 +8698,8 @@ int wolfSSH_SFTP_Put(WOLFSSH* ssh, char* from, char* to, byte resume,
state->handleSz);
if (ret != WS_SUCCESS) {
if (ssh->error == WS_WANT_READ ||
ssh->error == WS_WANT_WRITE) {
ssh->error == WS_WANT_WRITE ||
ssh->error == WS_REKEYING) {
return WS_FATAL_ERROR;
}
WLOG(WS_LOG_SFTP, "Error closing handle");
Expand Down
15 changes: 13 additions & 2 deletions tests/sftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,23 @@ static int Expected(int command)
char expt1[] = ".\n..\nwolfSSH sftp> ";
char expt2[] = "..\n.\nwolfSSH sftp> ";
if (WMEMCMP(expt1, inBuf, sizeof(expt1)) != 0 &&
WMEMCMP(expt2, inBuf, sizeof(expt2)) != 0)
WMEMCMP(expt2, inBuf, sizeof(expt2)) != 0) {
fprintf(stderr, "Unexpected string of %s\n", inBuf);
return -1;
}
else
return 0;

}

case 6:
return (WSTRNSTR(inBuf, "configure", sizeof(inBuf)) == NULL);
if (WSTRNSTR(inBuf, "configure", sizeof(inBuf)) == NULL) {
fprintf(stderr, "configure not found in %s\n", inBuf);
return 1;
}
else {
return 0;
}

case 10:
return (WSTRNSTR(inBuf, "test-get", sizeof(inBuf)) == NULL);
Expand Down Expand Up @@ -135,6 +143,7 @@ static int commandCb(const char* in, char* out, int outSz)
}

if (Expected(commandIdx) != 0) {
fprintf(stderr, "Failed on command index %d\n", commandIdx);
exit(1); /* abort out */
}
commandIdx++;
Expand Down Expand Up @@ -163,6 +172,8 @@ int wolfSSH_SftpTest(int flag)
WMEMSET(&cli, 0, sizeof(func_args));
commandIdx = 0;

wolfSSH_Debugging_ON();

argsCount = 0;
args[argsCount++] = ".";
args[argsCount++] = "-1";
Expand Down

0 comments on commit bbc9f9e

Please sign in to comment.