Skip to content

Commit

Permalink
Nonblocking SCP
Browse files Browse the repository at this point in the history
1. Splitting the top level SCP functions for either from or to, and
   incorporating the sub-functions in appropriately.
  • Loading branch information
ejohnstown committed Jul 2, 2024
1 parent 4dabe1c commit 31d5813
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 47 deletions.
98 changes: 52 additions & 46 deletions src/wolfscp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1665,70 +1665,76 @@ int wolfSSH_SCP_connect(WOLFSSH* ssh, byte* cmd)
return ret;
}

static int wolfSSH_SCP_cmd(WOLFSSH* ssh, const char* localName,
const char* remoteName, byte dir)

int wolfSSH_SCP_to(WOLFSSH* ssh, const char* src, const char* dst)
{
char* cmd = NULL;
word32 remoteNameSz, cmdSz;
char* cmd;
word32 cmdSz;
int ret = WS_SUCCESS;

if (ssh == NULL || localName == NULL || remoteName == NULL)
return WS_BAD_ARGUMENT;
/* dst is passed to the server in the scp -t command */
/* src is used locally to fopen and read for copy to */

if (dir != 't' && dir != 'f')
if (ssh == NULL || src == NULL || dst == NULL)
return WS_BAD_ARGUMENT;

remoteNameSz = (word32)WSTRLEN(remoteName);
cmdSz = remoteNameSz + (word32)WSTRLEN("scp -5 ") + 1;
cmd = (char*)WMALLOC(cmdSz, ssh->ctx->heap, DYNTYPE_STRING);

/* Need to set up the context for the local interaction callback. */

if (cmd != NULL) {
WSNPRINTF(cmd, cmdSz, "scp -%c %s", dir, remoteName);
ssh->scpBasePath = localName;
ret = wolfSSH_SCP_connect(ssh, (byte*)cmd);
if (ret == WS_SUCCESS) {
if (dir == 't') {
ssh->scpState = SCP_SOURCE_BEGIN;
ssh->scpRequestState = SCP_SOURCE;
ret = DoScpSource(ssh);
}
else {
cmdSz = (word32)WSTRLEN(localName);
ret = ParseBasePathHelper(ssh, cmdSz);
if (ret == WS_SUCCESS) {
ssh->scpState = SCP_SINK_BEGIN;
ssh->scpRequestState = SCP_SINK;
ret = DoScpSink(ssh);
}
}
if (ssh->scpState == SCP_SETUP) {
cmdSz = (word32)WSNPRINTF(NULL, 0, "scp -%c %s", 't', dst) + 1;
cmd = (char*)WMALLOC(cmdSz, ssh->ctx->heap, DYNTYPE_STRING);
if (cmd == NULL) {
return WS_MEMORY_E;
}
WFREE(cmd, ssh->ctx->heap, DYNTYPE_STRING);
WSNPRINTF(cmd, cmdSz, "scp -%c %s", 't', dst);
ssh->scpBasePath = src;
ret = wolfSSH_SCP_connect(ssh, (byte*)cmd);
ssh->scpState = SCP_SOURCE_BEGIN;
ssh->scpRequestState = SCP_SOURCE;
}
else {
WLOG(WS_LOG_SCP, "Cannot build scp command");
ssh->error = WS_MEMORY_E;
ret = WS_ERROR;
if (ssh->scpState != SCP_SETUP) {
ret = DoScpSource(ssh);
WFREE(cmd, ssh->ctx->heap, DYNTYPE_STRING);
}

return ret;
}


int wolfSSH_SCP_to(WOLFSSH* ssh, const char* src, const char* dst)
{
return wolfSSH_SCP_cmd(ssh, src, dst, 't');
/* dst is passed to the server in the scp -t command */
/* src is used locally to fopen and read for copy to */
}


int wolfSSH_SCP_from(WOLFSSH* ssh, const char* src, const char* dst)
{
return wolfSSH_SCP_cmd(ssh, dst, src, 'f');
char* cmd;
word32 cmdSz;
int ret = WS_SUCCESS;

/* src is passed to the server in the scp -f command */
/* dst is used locally to fopen and write for copy from */

if (ssh == NULL || src == NULL || dst == NULL)
return WS_BAD_ARGUMENT;

if (ssh->scpState == SCP_SETUP) {
cmdSz = (word32)WSNPRINTF(NULL, 0, "scp -%c %s", 'f', src) + 1;
cmd = (char*)WMALLOC(cmdSz, ssh->ctx->heap, DYNTYPE_STRING);
if (cmd == NULL) {
WLOG(WS_LOG_SCP, "Cannot allocate scp command");
ssh->error = WS_MEMORY_E;
return WS_ERROR;
}
WSNPRINTF(cmd, cmdSz, "scp -%c %s", 'f', src);
ssh->scpBasePath = dst;
ret = wolfSSH_SCP_connect(ssh, (byte*)cmd);
ssh->scpState = SCP_SINK_BEGIN;
ssh->scpRequestState = SCP_SINK;
cmdSz = (word32)WSTRLEN(src);
ret = ParseBasePathHelper(ssh, cmdSz);
}
if (ssh->scpState != SCP_SETUP) {
if (ret == WS_SUCCESS) {
ret = DoScpSink(ssh);
}
WFREE(cmd, ssh->ctx->heap, DYNTYPE_STRING);
}

return ret;
}
#endif /* ! NO_WOLFSSH_CLIENT */

Expand Down
3 changes: 2 additions & 1 deletion wolfssh/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,8 @@ enum WS_BufferTypes {
#define SCP_CONFIRM_FATAL 0x02 /* binary 2 */

enum WS_ScpStates {
SCP_PARSE_COMMAND = 0,
SCP_SETUP = 0,
SCP_PARSE_COMMAND,
SCP_SINK,
SCP_SINK_BEGIN,
SCP_TRANSFER,
Expand Down

0 comments on commit 31d5813

Please sign in to comment.