Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce SCP Allocations #717

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,8 @@ WOLFSSH* SshInit(WOLFSSH* ssh, WOLFSSH_CTX* ctx)
ssh->scpRequestState = SCP_PARSE_COMMAND;
ssh->scpConfirmMsg = NULL;
ssh->scpConfirmMsgSz = 0;
ssh->scpRecvMsg = NULL;
ssh->scpRecvMsgSz = 0;
ssh->scpRecvCtx = NULL;
#if !defined(WOLFSSH_SCP_USER_CALLBACKS) && !defined(NO_FILESYSTEM)
ssh->scpSendCtx = &(ssh->scpSendCbCtx);
Expand Down Expand Up @@ -7143,7 +7145,7 @@ static int DoUserAuthRequestPublicKey(WOLFSSH* ssh, WS_UserAuthData* authData,
case WOLFSSH_USERAUTH_PARTIAL_SUCCESS:
WLOG(WS_LOG_DEBUG, "DUARPK: user auth partial success");
break;

case WOLFSSH_USERAUTH_WOULD_BLOCK:
WLOG(WS_LOG_DEBUG, "DUARPK: userauth callback would block");
break;
Expand Down
36 changes: 13 additions & 23 deletions src/wolfscp.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ int DoScpSink(WOLFSSH* ssh)

ssh->scpConfirm = ssh->ctx->scpRecvCb(ssh,
WOLFSSH_SCP_NEW_REQUEST, ssh->scpBasePath,
NULL, 0, 0, 0, 0, NULL, 0, 0, wolfSSH_GetScpRecvCtx(ssh));
NULL, 0, 0, 0, 0, NULL, 0, 0,
wolfSSH_GetScpRecvCtx(ssh));
continue;

case SCP_RECEIVE_MESSAGE:
Expand Down Expand Up @@ -188,12 +189,9 @@ int DoScpSink(WOLFSSH* ssh)
ssh->scpATime, ssh->scpFileSz, ssh->scpFileBuffer,
ssh->scpFileBufferSz, ssh->scpFileOffset,
wolfSSH_GetScpRecvCtx(ssh));

ssh->scpFileOffset += ssh->scpFileBufferSz;

/* shrink and reset recv buffer */
WFREE(ssh->scpFileBuffer, ssh->ctx->heap, DYNTYPE_BUFFER);
ssh->scpFileBuffer = NULL;
/* reset recv buffer */
ssh->scpFileBufferSz = 0;

if (ssh->scpConfirm != WS_SCP_CONTINUE) {
Expand Down Expand Up @@ -1445,8 +1443,6 @@ int ReceiveScpMessage(WOLFSSH* ssh)
break;
}

WFREE(ssh->scpRecvMsg, ssh->ctx->heap, DYNTYPE_STRING);
ssh->scpRecvMsg = NULL;
ssh->scpRecvMsgSz = 0;

return ret;
Expand All @@ -1455,34 +1451,28 @@ int ReceiveScpMessage(WOLFSSH* ssh)
int ReceiveScpFile(WOLFSSH* ssh)
{
int partSz, ret = WS_SUCCESS;
byte* part;

if (ssh == NULL)
return WS_BAD_ARGUMENT;

/* We don't want to over-read the buffer. The file data is
* terminated by the sender with a nul which is checked later. */
partSz = min(ssh->scpFileSz - ssh->scpFileOffset, DEFAULT_SCP_BUFFER_SZ);

/* don't even bother reading if read size is 0 */
if (partSz == 0) return ret;

part = (byte*)WMALLOC(partSz, ssh->ctx->heap, DYNTYPE_BUFFER);
if (part == NULL)
ret = WS_MEMORY_E;
if (ssh->scpFileBuffer == NULL) {
ssh->scpFileBuffer = (byte*)WMALLOC(DEFAULT_SCP_BUFFER_SZ,
ssh->ctx->heap, DYNTYPE_BUFFER);
if (ssh->scpFileBuffer == NULL)
ret = WS_MEMORY_E;
}

if (ret == WS_SUCCESS) {
WMEMSET(part, 0, partSz);

ret = wolfSSH_stream_read(ssh, part, partSz);
ret = wolfSSH_stream_read(ssh, ssh->scpFileBuffer, partSz);
if (ret > 0) {
if (ssh->scpFileBuffer != NULL) {
WFREE(ssh->scpFileBuffer, ssh->ctx->heap, DYNTYPE_BUFFER);
ssh->scpFileBuffer = NULL;
ssh->scpFileBufferSz = 0;
}
ssh->scpFileBuffer = part;
ssh->scpFileBufferSz = ret;
} else {
WFREE(part, ssh->ctx->heap, DYNTYPE_BUFFER);
}
}

Expand Down Expand Up @@ -2122,7 +2112,7 @@ static int GetFileStats(void *fs, ScpSendCtx* ctx, const char* fileName,
word64* mTime, word64* aTime, int* fileMode)
{
int ret = WS_SUCCESS;

WOLFSSH_UNUSED(fs);

if (ctx == NULL || fileName == NULL || mTime == NULL ||
Expand Down
Loading