Skip to content

Commit 2a11471

Browse files
refactor introducing more use of NoticeError
1 parent ff95f3c commit 2a11471

4 files changed

Lines changed: 100 additions & 26 deletions

File tree

examples/echoserver/echoserver.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,8 +1416,11 @@ static int sftp_worker(thread_ctx_t* threadCtx)
14161416
}
14171417
else if (ret < 0) {
14181418
error = wolfSSH_get_error(ssh);
1419-
if (error == WS_EOF)
1419+
if (error == WS_EOF) {
1420+
/* shutdown is happening, clear peek error */
1421+
ret = 0;
14201422
break;
1423+
}
14211424
}
14221425

14231426
if (ret == WS_FATAL_ERROR && error == 0) {

examples/sftpclient/sftpclient.c

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,8 @@ static int doCmds(func_args* args)
566566
}
567567

568568
do {
569-
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
569+
while (wolfSSH_get_error(ssh) == WS_REKEYING) {
570570
ret = wolfSSH_worker(ssh, NULL);
571-
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
572-
ret = wolfSSH_get_error(ssh);
573-
}
574571
}
575572

576573
ret = wolfSSH_SFTP_Get(ssh, pt, to, resume, &myStatusCb);
@@ -747,6 +744,13 @@ static int doCmds(func_args* args)
747744

748745
/* check directory is valid */
749746
do {
747+
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
748+
ret = wolfSSH_worker(ssh, NULL);
749+
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
750+
ret = wolfSSH_get_error(ssh);
751+
}
752+
}
753+
750754
ret = wolfSSH_SFTP_STAT(ssh, pt, &atrb);
751755
err = wolfSSH_get_error(ssh);
752756
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
@@ -828,6 +832,13 @@ static int doCmds(func_args* args)
828832

829833
/* update permissions */
830834
do {
835+
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
836+
ret = wolfSSH_worker(ssh, NULL);
837+
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
838+
ret = wolfSSH_get_error(ssh);
839+
}
840+
}
841+
831842
ret = wolfSSH_SFTP_CHMOD(ssh, pt, mode);
832843
err = wolfSSH_get_error(ssh);
833844
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
@@ -878,6 +889,13 @@ static int doCmds(func_args* args)
878889
}
879890

880891
do {
892+
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
893+
ret = wolfSSH_worker(ssh, NULL);
894+
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
895+
ret = wolfSSH_get_error(ssh);
896+
}
897+
}
898+
881899
ret = wolfSSH_SFTP_RMDIR(ssh, pt);
882900
err = wolfSSH_get_error(ssh);
883901
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
@@ -924,6 +942,13 @@ static int doCmds(func_args* args)
924942
}
925943

926944
do {
945+
while (ret == WS_REKEYING || ssh->error == WS_REKEYING) {
946+
ret = wolfSSH_worker(ssh, NULL);
947+
if (ret != WS_SUCCESS && ret == WS_FATAL_ERROR) {
948+
ret = wolfSSH_get_error(ssh);
949+
}
950+
}
951+
927952
ret = wolfSSH_SFTP_Remove(ssh, pt);
928953
err = wolfSSH_get_error(ssh);
929954
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE)
@@ -1458,14 +1483,52 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args)
14581483

14591484
WFREE(workingDir, NULL, DYNAMIC_TYPE_TMP_BUFFER);
14601485
if (ret == WS_SUCCESS) {
1461-
if (wolfSSH_shutdown(ssh) != WS_SUCCESS) {
1462-
int rc;
1463-
rc = wolfSSH_get_error(ssh);
1486+
int err;
1487+
ret = wolfSSH_shutdown(ssh);
1488+
1489+
/* peer hung up, stop trying to shutdown */
1490+
if (ret == WS_SOCKET_ERROR_E) {
1491+
ret = 0;
1492+
}
1493+
1494+
err = wolfSSH_get_error(ssh);
1495+
if (err != WS_SOCKET_ERROR_E &&
1496+
(err == WS_WANT_READ || err == WS_WANT_WRITE)) {
1497+
int maxAttempt = 10; /* make 10 attempts max before giving up */
1498+
int attempt;
1499+
1500+
for (attempt = 0; attempt < maxAttempt; attempt++) {
1501+
ret = wolfSSH_worker(ssh, NULL);
1502+
err = wolfSSH_get_error(ssh);
1503+
1504+
/* peer succesfully closed down gracefully */
1505+
if (ret == WS_CHANNEL_CLOSED) {
1506+
ret = 0;
1507+
break;
1508+
}
14641509

1465-
if (rc != WS_SOCKET_ERROR_E && rc != WS_EOF)
1466-
printf("error with wolfSSH_shutdown()\n");
1510+
/* peer hung up, stop shutdown */
1511+
if (ret == WS_SOCKET_ERROR_E) {
1512+
ret = 0;
1513+
break;
1514+
}
1515+
1516+
if (err == WS_WANT_READ || err == WS_WANT_WRITE) {
1517+
/* Wanting read or wanting write. Clear ret. */
1518+
ret = 0;
1519+
}
1520+
else {
1521+
break;
1522+
}
1523+
}
1524+
1525+
if (attempt == maxAttempt) {
1526+
printf("SFTP client gave up on gracefull shutdown,"
1527+
"closing the socket\n");
1528+
}
14671529
}
14681530
}
1531+
14691532
WCLOSESOCKET(sockFd);
14701533
wolfSSH_free(ssh);
14711534
wolfSSH_CTX_free(ctx);

src/wolfsftp.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ static int SFTP_GetHeader(WOLFSSH* ssh, word32* reqId, byte* type,
865865
*/
866866
static int SFTP_SetHeader(WOLFSSH* ssh, word32 reqId, byte type, word32 len,
867867
byte* buf) {
868+
868869
c32toa(len + LENGTH_SZ + MSG_ID_SZ, buf);
869870
buf[LENGTH_SZ] = type;
870871
c32toa(reqId, buf + LENGTH_SZ + MSG_ID_SZ);
@@ -1170,8 +1171,9 @@ int wolfSSH_SFTP_accept(WOLFSSH* ssh)
11701171
case SFTP_EXT:
11711172
ret = SFTP_ServerRecvInit(ssh);
11721173
if (ret != WS_SUCCESS) {
1173-
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE)
1174+
if (!NoticeError(ssh)) {
11741175
wolfSSH_SFTP_ClearState(ssh, STATE_ID_ALL);
1176+
}
11751177
return ret;
11761178
}
11771179
ssh->sftpState = SFTP_RECV;
@@ -1573,8 +1575,9 @@ int wolfSSH_SFTP_read(WOLFSSH* ssh)
15731575

15741576
/* break out if encountering an error with nothing stored to send */
15751577
if (ret < 0 && !state->toSend) {
1576-
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE)
1578+
if (!NoticeError(ssh)) {
15771579
wolfSSH_SFTP_ClearState(ssh, STATE_ID_RECV);
1580+
}
15781581
return ret;
15791582
}
15801583
state->buffer.idx = 0;
@@ -7674,8 +7677,8 @@ int wolfSSH_SFTP_SendReadPacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
76747677
/* send header and type specific data */
76757678
ret = wolfSSH_SFTP_buffer_send(ssh, &state->buffer);
76767679
if (ret < 0) {
7677-
if (ret == WS_REKEYING) {
7678-
return ret;
7680+
if (NoticeError(ssh)) {
7681+
return WS_FATAL_ERROR;
76797682
}
76807683
if (ssh->error != WS_WANT_READ &&
76817684
ssh->error != WS_WANT_WRITE) {
@@ -7693,14 +7696,12 @@ int wolfSSH_SFTP_SendReadPacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
76937696
/* Get response */
76947697
if ((ret = SFTP_GetHeader(ssh, &state->reqId, &state->type,
76957698
&state->buffer)) <= 0) {
7696-
if (ssh->error != WS_WANT_READ &&
7697-
ssh->error != WS_WANT_WRITE) {
7699+
if (!NoticeError(ssh)) {
76987700
state->state = STATE_SEND_READ_CLEANUP;
76997701
continue;
77007702
}
77017703
return WS_FATAL_ERROR;
77027704
}
7703-
77047705
ret = wolfSSH_SFTP_buffer_create(ssh, &state->buffer, ret);
77057706
if (ret != WS_SUCCESS) {
77067707
state->state = STATE_SEND_READ_CLEANUP;
@@ -7718,8 +7719,9 @@ int wolfSSH_SFTP_SendReadPacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
77187719
state->state = STATE_SEND_READ_CLEANUP;
77197720
continue;
77207721
}
7721-
else
7722+
else {
77227723
ssh->reqId++;
7724+
}
77237725

77247726
if (state->type == WOLFSSH_FTP_DATA)
77257727
state->state = STATE_SEND_READ_FTP_DATA;
@@ -7737,8 +7739,7 @@ int wolfSSH_SFTP_SendReadPacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
77377739
/* get size of string and place it into out buffer */
77387740
ret = wolfSSH_stream_read(ssh, szFlat, UINT32_SZ);
77397741
if (ret < 0) {
7740-
if (ssh->error != WS_WANT_READ &&
7741-
ssh->error != WS_WANT_WRITE) {
7742+
if (!NoticeError(ssh)) {
77427743
state->state = STATE_SEND_READ_CLEANUP;
77437744
continue;
77447745
}
@@ -7917,8 +7918,9 @@ int wolfSSH_SFTP_MKDIR(WOLFSSH* ssh, char* dir, WS_SFTP_FILEATRB* atr)
79177918
/* send header and type specific data */
79187919
ret = wolfSSH_SFTP_buffer_send(ssh, &state->buffer);
79197920
if (ret < 0) {
7920-
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE)
7921+
if (!NoticeError(ssh)) {
79217922
wolfSSH_SFTP_ClearState(ssh, STATE_ID_MKDIR);
7923+
}
79227924
return ret;
79237925
}
79247926

@@ -7931,8 +7933,9 @@ int wolfSSH_SFTP_MKDIR(WOLFSSH* ssh, char* dir, WS_SFTP_FILEATRB* atr)
79317933
/* Get response */
79327934
if ((ret = SFTP_GetHeader(ssh, &state->reqId, &type,
79337935
&state->buffer)) <= 0) {
7934-
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE)
7936+
if (!NoticeError(ssh)) {
79357937
wolfSSH_SFTP_ClearState(ssh, STATE_ID_MKDIR);
7938+
}
79367939
return WS_FATAL_ERROR;
79377940
}
79387941

@@ -7963,8 +7966,9 @@ int wolfSSH_SFTP_MKDIR(WOLFSSH* ssh, char* dir, WS_SFTP_FILEATRB* atr)
79637966
ret = wolfSSH_SFTP_buffer_read(ssh, &state->buffer,
79647967
wolfSSH_SFTP_buffer_size(&state->buffer));
79657968
if (ret < 0) {
7966-
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE)
7967-
wolfSSH_SFTP_ClearState(ssh, STATE_ID_MKDIR);
7969+
if (!NoticeError(ssh)) {
7970+
wolfSSH_SFTP_ClearState(ssh, STATE_ID_MKDIR);
7971+
}
79687972
return WS_FATAL_ERROR;
79697973
}
79707974

@@ -8031,8 +8035,7 @@ WS_SFTPNAME* wolfSSH_SFTP_ReadDir(WOLFSSH* ssh, byte* handle,
80318035
case STATE_READDIR_NAME:
80328036
name = wolfSSH_SFTP_DoName(ssh);
80338037
if (name == NULL) {
8034-
if (ssh->error != WS_WANT_READ
8035-
&& ssh->error != WS_WANT_WRITE) {
8038+
if (!NoticeError(ssh)) {
80368039
wolfSSH_SFTP_ClearState(ssh, STATE_ID_READDIR);
80378040
}
80388041
return NULL;

tests/api.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,11 @@ static void test_wolfSSH_SFTP_SendReadPacket(void)
10751075
}
10761076
}
10771077

1078+
/* take care of re-keying state before shutdown call */
1079+
while (wolfSSH_get_error(ssh) == WS_REKEYING) {
1080+
wolfSSH_worker(ssh, NULL);
1081+
}
1082+
10781083
argsCount = wolfSSH_shutdown(ssh);
10791084
if (argsCount == WS_SOCKET_ERROR_E) {
10801085
/* If the socket is closed on shutdown, peer is gone, this is OK. */

0 commit comments

Comments
 (0)