Skip to content

Commit 3dd09b9

Browse files
refactor SFTP to use NoticeError
1 parent ceb4158 commit 3dd09b9

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

examples/sftpclient/sftpclient.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ static int doCmds(func_args* args)
11011101
/* alternate main loop for the autopilot get/receive */
11021102
static int doAutopilot(int cmd, char* local, char* remote)
11031103
{
1104-
int err;
1104+
int err = 0;
11051105
int ret = WS_SUCCESS;
11061106
char fullpath[128] = ".";
11071107
WS_SFTPNAME* name = NULL;
@@ -1138,6 +1138,12 @@ static int doAutopilot(int cmd, char* local, char* remote)
11381138
}
11391139

11401140
do {
1141+
if (err == WS_REKEYING) { /* handle rekeying state */
1142+
do {
1143+
ret = wolfSSH_worker(ssh, NULL);
1144+
} while (ret == WS_REKEYING);
1145+
}
1146+
11411147
if (cmd == AUTOPILOT_PUT) {
11421148
ret = wolfSSH_SFTP_Put(ssh, local, fullpath, 0, NULL);
11431149
}
@@ -1146,7 +1152,8 @@ static int doAutopilot(int cmd, char* local, char* remote)
11461152
}
11471153
err = wolfSSH_get_error(ssh);
11481154
} while ((err == WS_WANT_READ || err == WS_WANT_WRITE ||
1149-
err == WS_CHAN_RXD) && ret == WS_FATAL_ERROR);
1155+
err == WS_CHAN_RXD || err == WS_REKEYING) &&
1156+
ret == WS_FATAL_ERROR);
11501157

11511158
if (ret != WS_SUCCESS) {
11521159
if (cmd == AUTOPILOT_PUT) {

src/ssh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ int wolfSSH_stream_send(WOLFSSH* ssh, byte* buf, word32 bufSz)
11691169

11701170
if (ssh->isKeying) {
11711171
ssh->error = WS_REKEYING;
1172-
return WS_REKEYING;
1172+
return WS_FATAL_ERROR;
11731173
}
11741174

11751175
bytesTxd = SendChannelData(ssh, ssh->channelList->channel, buf, bufSz);

src/wolfsftp.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,8 +1400,13 @@ int wolfSSH_SFTP_read(WOLFSSH* ssh)
14001400
ret = wolfSSH_SFTP_buffer_read(ssh, &state->buffer,
14011401
state->buffer.sz);
14021402
if (ret < 0) {
1403-
if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE)
1404-
wolfSSH_SFTP_ClearState(ssh, STATE_ID_RECV);
1403+
if (NoticeError(ssh)) {
1404+
/* keep state for returning to */
1405+
ret = WS_FATAL_ERROR;
1406+
}
1407+
else {
1408+
wolfSSH_SFTP_ClearState(ssh, STATE_ID_RECV);
1409+
}
14051410
return ret;
14061411
}
14071412

@@ -7246,8 +7251,7 @@ int wolfSSH_SFTP_SendWritePacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
72467251
/* send header and type specific data */
72477252
ret = wolfSSH_SFTP_buffer_send(ssh, &state->buffer);
72487253
if (ret < 0) {
7249-
if (ssh->error == WS_WANT_READ ||
7250-
ssh->error == WS_WANT_WRITE) {
7254+
if (NoticeError(ssh)) {
72517255
return WS_FATAL_ERROR;
72527256
}
72537257
state->state = STATE_SEND_WRITE_CLEANUP;
@@ -7259,12 +7263,8 @@ int wolfSSH_SFTP_SendWritePacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
72597263
case STATE_SEND_WRITE_SEND_BODY:
72607264
WLOG(WS_LOG_SFTP, "SFTP SEND_WRITE STATE: SEND_BODY");
72617265
state->sentSz = wolfSSH_stream_send(ssh, in, inSz);
7262-
if (state->sentSz == WS_WINDOW_FULL ||
7263-
state->sentSz == WS_REKEYING ||
7264-
state->sentSz == WS_WANT_READ ||
7265-
state->sentSz == WS_WANT_WRITE) {
7266-
ret = wolfSSH_worker(ssh, NULL);
7267-
continue; /* skip past rest and send more */
7266+
if (NoticeError(ssh)) {
7267+
return WS_FATAL_ERROR;
72687268
}
72697269
if (state->sentSz <= 0) {
72707270
ssh->error = state->sentSz;
@@ -7290,8 +7290,7 @@ int wolfSSH_SFTP_SendWritePacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
72907290
state->maxSz = SFTP_GetHeader(ssh, &state->reqId, &type,
72917291
&state->buffer);
72927292
if (state->maxSz <= 0) {
7293-
if (ssh->error == WS_WANT_READ ||
7294-
ssh->error == WS_WANT_WRITE) {
7293+
if (NoticeError(ssh)) {
72957294
return WS_FATAL_ERROR;
72967295
}
72977296
ssh->error = WS_SFTP_BAD_HEADER;
@@ -8954,10 +8953,9 @@ int wolfSSH_SFTP_Put(WOLFSSH* ssh, char* from, char* to, byte resume,
89548953
state->handle, state->handleSz, state->pOfst,
89558954
state->r, state->rSz);
89568955
if (sz <= 0) {
8957-
if (ssh->error == WS_WANT_READ ||
8958-
ssh->error == WS_WANT_WRITE ||
8959-
ssh->error == WS_WINDOW_FULL)
8956+
if (NoticeError(ssh)) {
89608957
return WS_FATAL_ERROR;
8958+
}
89618959
}
89628960
else {
89638961
AddAssign64(state->pOfst, sz);

0 commit comments

Comments
 (0)