From f8b092c5b43ec70855f04add4552c625275826e9 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Thu, 17 Sep 2020 10:43:38 -0600 Subject: [PATCH 1/3] add example timeout to sftp example --- examples/sftpclient/sftpclient.c | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index c980f04..c8c45f3 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -51,12 +51,54 @@ static void err_msg(const char* s) printf("%s\n", s); } + +#ifndef WOLFSSH_NO_TIMESTAMP + #include + + static char currentFile[WOLFSSH_MAX_FILENAME] = ""; + static double startTime; + #define TIMEOUT_VALUE 60 + + double current_time(int); + double current_time(int reset) + { + struct timeval tv; + + (void)reset; + + gettimeofday(&tv, 0); + + return (double)tv.tv_sec + (double)tv.tv_usec / 1000000; + } +#endif + + static void myStatusCb(WOLFSSH* sshIn, word32* bytes, char* name) { + double currentTime; char buf[80]; word64 longBytes = ((word64)bytes[1] << 32) | bytes[0]; + +#ifndef WOLFSSH_NO_TIMESTAMP + if (WSTRNCMP(currentFile, name, WSTRLEN(name)) != 0) { + startTime = current_time(1); + WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME); + WSTRNCPY(currentFile, name, WSTRLEN(name)); + } + currentTime = current_time(0) - startTime; + WSNPRINTF(buf, sizeof(buf), "Processed %8llu\t bytes in %.2f seconds\r", + (unsigned long long)longBytes, currentTime); + if (currentTime > TIMEOUT_VALUE) { + WSNPRINTF(buf, sizeof(buf), "\nProcess timed out at %.2fs, stopping\r", + currentTime); + WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME); + wolfSSH_SFTP_Interrupt(ssh); + } +#else WSNPRINTF(buf, sizeof(buf), "Processed %8llu\t bytes \r", (unsigned long long)longBytes); + (void)currentTime; +#endif WFPUTS(buf, fout); (void)name; (void)sshIn; From 69b91a3a2ea3ebaf5fee48ea28d70dffd71fa7cc Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Thu, 17 Sep 2020 11:16:22 -0600 Subject: [PATCH 2/3] adjust size of strncpy and time limit --- examples/sftpclient/sftpclient.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index c8c45f3..dbddab3 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -55,9 +55,9 @@ static void err_msg(const char* s) #ifndef WOLFSSH_NO_TIMESTAMP #include - static char currentFile[WOLFSSH_MAX_FILENAME] = ""; + static char currentFile[WOLFSSH_MAX_FILENAME+1] = ""; static double startTime; - #define TIMEOUT_VALUE 60 + #define TIMEOUT_VALUE 120 double current_time(int); double current_time(int reset) @@ -83,7 +83,7 @@ static void myStatusCb(WOLFSSH* sshIn, word32* bytes, char* name) if (WSTRNCMP(currentFile, name, WSTRLEN(name)) != 0) { startTime = current_time(1); WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME); - WSTRNCPY(currentFile, name, WSTRLEN(name)); + WSTRNCPY(currentFile, name, WOLFSSH_MAX_FILENAME); } currentTime = current_time(0) - startTime; WSNPRINTF(buf, sizeof(buf), "Processed %8llu\t bytes in %.2f seconds\r", From a4b2c693feb6c603b7601cabbf2c275e3711cc50 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Wed, 30 Sep 2020 11:45:33 -0600 Subject: [PATCH 3/3] use word32 type with timeout example --- examples/sftpclient/sftpclient.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index dbddab3..fad13f6 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -56,26 +56,27 @@ static void err_msg(const char* s) #include static char currentFile[WOLFSSH_MAX_FILENAME+1] = ""; - static double startTime; + static word32 startTime; #define TIMEOUT_VALUE 120 - double current_time(int); - double current_time(int reset) + word32 current_time(int); + + /* return number of seconds*/ + word32 current_time(int reset) { struct timeval tv; (void)reset; gettimeofday(&tv, 0); - - return (double)tv.tv_sec + (double)tv.tv_usec / 1000000; + return (word32)tv.tv_sec; } #endif static void myStatusCb(WOLFSSH* sshIn, word32* bytes, char* name) { - double currentTime; + word32 currentTime; char buf[80]; word64 longBytes = ((word64)bytes[1] << 32) | bytes[0]; @@ -86,11 +87,11 @@ static void myStatusCb(WOLFSSH* sshIn, word32* bytes, char* name) WSTRNCPY(currentFile, name, WOLFSSH_MAX_FILENAME); } currentTime = current_time(0) - startTime; - WSNPRINTF(buf, sizeof(buf), "Processed %8llu\t bytes in %.2f seconds\r", + WSNPRINTF(buf, sizeof(buf), "Processed %8llu\t bytes in %d seconds\r", (unsigned long long)longBytes, currentTime); if (currentTime > TIMEOUT_VALUE) { - WSNPRINTF(buf, sizeof(buf), "\nProcess timed out at %.2fs, stopping\r", - currentTime); + WSNPRINTF(buf, sizeof(buf), "\nProcess timed out at %d seconds, " + "stopping\r", currentTime); WMEMSET(currentFile, 0, WOLFSSH_MAX_FILENAME); wolfSSH_SFTP_Interrupt(ssh); }