Echoserver Maintenance

1. When printing server worker errors, print out the error string on the
stored error code, not the return value.
2. If the error is a socket error, don't try to send a shutdown message
to the peer.
3. Update the echoserver to close the connection cleanly when the SFTP
peer exits.
4. Added a function to check a WOLFSSL_CHANNEL if it is in eof.
pull/201/head
John Safranek 2019-10-10 10:57:39 -07:00
parent 5fe810aa96
commit 744a67e53b
No known key found for this signature in database
GPG Key ID: 8CE817DE0D3CCB4A
4 changed files with 56 additions and 13 deletions

View File

@ -325,6 +325,16 @@ static int sftp_worker(thread_ctx_t* threadCtx) {
if (error == WS_WANT_READ || error == WS_WANT_WRITE)
ret = WS_WANT_READ;
if (ret == WS_FATAL_ERROR && error == 0) {
WOLFSSH_CHANNEL* channel =
wolfSSH_ChannelNext(threadCtx->ssh, NULL);
if (channel && wolfSSH_ChannelGetEof(channel)) {
ret = 0;
error = 0;
break;
}
}
} while (ret != WS_FATAL_ERROR);
return ret;
@ -372,7 +382,7 @@ wolfSSL_Mutex doneLock;
static THREAD_RETURN WOLFSSH_THREAD server_worker(void* vArgs)
{
int ret = 0;
int ret = 0, error = 0;
thread_ctx_t* threadCtx = (thread_ctx_t*)vArgs;
if (!threadCtx->nonBlock)
@ -399,19 +409,31 @@ static THREAD_RETURN WOLFSSH_THREAD server_worker(void* vArgs)
break;
}
if (ret == WS_FATAL_ERROR && wolfSSH_get_error(threadCtx->ssh) ==
WS_VERSION_E) {
ret = 0; /* don't break out of loop with version miss match */
printf("Unsupported version error\n");
}
else if (ret == WS_FATAL_ERROR && wolfSSH_get_error(threadCtx->ssh) ==
WS_USER_AUTH_E) {
ret = 0; /* don't break out of loop with user auth error */
printf("User Authentication error\n");
if (ret == WS_FATAL_ERROR) {
const char* errorStr;
error = wolfSSH_get_error(threadCtx->ssh);
errorStr = wolfSSH_ErrorToName(error);
if (error == WS_VERSION_E) {
ret = 0; /* don't break out of loop with version miss match */
printf("%s\n", errorStr);
}
else if (error == WS_USER_AUTH_E) {
ret = 0; /* don't break out of loop with user auth error */
printf("%s\n", errorStr);
}
else if (error == WS_SOCKET_ERROR_E) {
ret = 0;
printf("%s\n", errorStr);
}
}
if (wolfSSH_shutdown(threadCtx->ssh) != WS_SUCCESS) {
fprintf(stderr, "Error with SSH shutdown.\n");
if (error != WS_SOCKET_ERROR_E && error != WS_FATAL_ERROR)
{
if (wolfSSH_shutdown(threadCtx->ssh) != WS_SUCCESS) {
fprintf(stderr, "Error with SSH shutdown.\n");
}
}
WCLOSESOCKET(threadCtx->fd);
@ -419,7 +441,7 @@ static THREAD_RETURN WOLFSSH_THREAD server_worker(void* vArgs)
if (ret != 0) {
fprintf(stderr, "Error [%d] \"%s\" with handling connection.\n", ret,
wolfSSH_ErrorToName(ret));
wolfSSH_ErrorToName(error));
#ifndef WOLFSSH_NO_EXIT
wc_LockMutex(&doneLock);
quit = 1;

View File

@ -1574,6 +1574,21 @@ WOLFSSH_CHANNEL* wolfSSH_ChannelNext(WOLFSSH* ssh, WOLFSSH_CHANNEL* channel)
}
int wolfSSH_ChannelGetEof(WOLFSSH_CHANNEL* channel)
{
int eof = 1;
WLOG(WS_LOG_DEBUG, "Entering wolfSSH_ChannelGetEof()");
if (channel)
eof = (int)channel->receivedEof;
WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_ChannelGetEof(), %s",
eof ? "true" : "false");
return eof;
}
/* Protocols that have loops over wolfSSH_stream_send without doing any reads
* will run into the issue of not checking for a peer window adjustment packet.
* This function allows for checking for a peer window adjustment packet without

View File

@ -553,6 +553,7 @@ static int SFTP_GetHeader(WOLFSSH* ssh, word32* reqId, byte* type)
int ret;
word32 len;
byte buf[WOLFSSH_SFTP_HEADER];
WLOG(WS_LOG_SFTP, "Entering SFTP_GetHeader()");
if (type == NULL || reqId == NULL || ssh == NULL) {
WLOG(WS_LOG_SFTP, "NULL argument error");
@ -573,6 +574,8 @@ static int SFTP_GetHeader(WOLFSSH* ssh, word32* reqId, byte* type)
*type = buf[LENGTH_SZ];
ato32(buf + UINT32_SZ + MSG_ID_SZ, reqId);
WLOG(WS_LOG_SFTP, "Leaving SFTP_GetHeader(), %d",
len - UINT32_SZ - MSG_ID_SZ);
return len - UINT32_SZ - MSG_ID_SZ;
}
@ -1007,6 +1010,8 @@ int wolfSSH_SFTP_read(WOLFSSH* ssh)
int maxSz, ret = WS_SUCCESS;
WS_SFTP_RECV_STATE* state = NULL;
WLOG(WS_LOG_DEBUG, "Entering wolfSSH_SFTP_read()");
if (ssh == NULL)
return WS_BAD_ARGUMENT;

View File

@ -92,6 +92,7 @@ WOLFSSH_API int wolfSSH_ChannelGetFwdFd(const WOLFSSH_CHANNEL*);
WOLFSSH_API int wolfSSH_ChannelRead(WOLFSSH_CHANNEL*, byte*, word32);
WOLFSSH_API int wolfSSH_ChannelSend(WOLFSSH_CHANNEL*, const byte*, word32);
WOLFSSH_API int wolfSSH_ChannelExit(WOLFSSH_CHANNEL*);
WOLFSSH_API int wolfSSH_ChannelGetEof(WOLFSSH_CHANNEL*);
WOLFSSH_API int wolfSSH_get_error(const WOLFSSH*);