From 395b375e24eb2093030aeafd6038477f3c4f0de6 Mon Sep 17 00:00:00 2001 From: ivan Chubb Date: Sat, 30 Sep 2023 15:41:33 -0400 Subject: [PATCH 1/9] adds some error handling, cleans up some magic values --- src/main.c | 73 +++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/src/main.c b/src/main.c index 7d9aeac6..d107aaf4 100644 --- a/src/main.c +++ b/src/main.c @@ -7,11 +7,6 @@ #include #include -#ifdef WIN32 -#else -#include -#endif - #include "error.h" #include "debug.h" #include "cJSON.h" @@ -28,11 +23,23 @@ #include "cert.h" #include "toniefile.h" +#ifdef _WIN32 +#include +#define PATH_LEN MAX_PATH +#else +#include +#include +#define PATH_LEN PATH_MAX +#endif + +#define DEFAULT_HTTP_PORT 80 +#define DEFAULT_HTTPS_PORT 443 +#define PORT_MAX 65535 + void platform_init(void); void platform_deinit(void); void server_init(void); -#define DEFAULT_HTTP_PORT 80 -#define DEFAULT_HTTPS_PORT 443 +static char *get_cwd(char *buffer, size_t size); typedef enum { @@ -40,29 +47,6 @@ typedef enum PROT_HTTPS } Protocol; -void get_directory_path(const char *filepath, char *dirpath, int maxLen) -{ - // Find the last occurrence of '/' or '\' in the file path - int lastSlash = -1; - for (int i = 0; filepath[i] != '\0'; i++) - { - if (filepath[i] == '/' || filepath[i] == '\\') - { - lastSlash = i; - } - } - - if (lastSlash == -1) - { - // No directory part found, use an empty string for the directory path - dirpath[0] = '\0'; - } - else - { - // Copy the characters before the last slash to the directory path buffer - snprintf(dirpath, maxLen, "%.*s", lastSlash, filepath); - } -} bool parse_url(const char *url, char **hostname, uint16_t *port, char **uri, Protocol *protocol) { @@ -98,7 +82,16 @@ bool parse_url(const char *url, char **hostname, uint16_t *port, char **uri, Pro strncpy(*hostname, url, hostname_length); (*hostname)[hostname_length] = '\0'; - *port = (uint16_t)atoi(port_start + 1); + // ensures port is in a valid range before casting + long temp = strtol(port_start + 1, NULL, 10); + if ((temp >= 0) && (temp <= PORT_MAX)) + { + *port = (uint16_t)temp; + } + else + { + *port = (*protocol == PROT_HTTP) ? DEFAULT_HTTP_PORT : DEFAULT_HTTPS_PORT; + } } else { @@ -122,14 +115,17 @@ int_t main(int argc, char *argv[]) error_t error = 0; - char cwd[256]; - if (getcwd(cwd, sizeof(cwd)) == NULL) + char *cwd = calloc(PATH_LEN, sizeof(char)); + + if ((cwd == NULL) || (get_cwd(cwd, PATH_LEN) == NULL)) { - get_directory_path(argv[0], cwd, sizeof(cwd)); + free(cwd); + return -1; } /* platform specific init */ error = settings_init(cwd); + free(cwd); if (error != NO_ERROR) { TRACE_ERROR("settings_init() failed with error code %d\r\n", error); @@ -351,3 +347,12 @@ int_t main(int argc, char *argv[]) return error; } + +static char *get_cwd(char *buffer, size_t size) +{ +#ifdef _WIN32 + return _getcwd(buffer, size); +#else + return getcwd(buffer, size); +#endif +} From 868b117141a1f29778ce69f0abb3c79cd2541021 Mon Sep 17 00:00:00 2001 From: ivan Chubb Date: Sat, 30 Sep 2023 16:17:44 -0400 Subject: [PATCH 2/9] removes double include of esp32.h --- src/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.c b/src/main.c index d107aaf4..3079b5fb 100644 --- a/src/main.c +++ b/src/main.c @@ -18,7 +18,6 @@ #include "cloud_request.h" #include "settings.h" -#include "esp32.h" #include "mqtt.h" #include "cert.h" #include "toniefile.h" @@ -47,7 +46,6 @@ typedef enum PROT_HTTPS } Protocol; - bool parse_url(const char *url, char **hostname, uint16_t *port, char **uri, Protocol *protocol) { if (strstr(url, "http://") == url) From 5e7142c2008f172c905209aee6c28cab8aa851c3 Mon Sep 17 00:00:00 2001 From: ivan Chubb Date: Sun, 1 Oct 2023 14:52:42 -0400 Subject: [PATCH 3/9] handles an uncaught case of bad argument with a -1 return value, adds a print_usage() function --- src/main.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main.c b/src/main.c index 3079b5fb..10ae666d 100644 --- a/src/main.c +++ b/src/main.c @@ -39,6 +39,7 @@ void platform_init(void); void platform_deinit(void); void server_init(void); static char *get_cwd(char *buffer, size_t size); +static void print_usage(char *argv[]); typedef enum { @@ -332,6 +333,12 @@ int_t main(int argc, char *argv[]) return 1; } #endif + else + { + TRACE_ERROR("Bad argument provided: %s\r\n", argv[0]); + print_usage(argv); + return -1; + } } else { @@ -354,3 +361,18 @@ static char *get_cwd(char *buffer, size_t size) return getcwd(buffer, size); #endif } + +static void print_usage(char *argv[]) +{ + printf( + "Usage: %s [options]\n\n" + "Options:\n" + " GENERIC [hash] Generic URL test.\r\n" + " SERVER_CERTS Generates Server Certs.\r\n" + " CLOUD [hash] Cloud API test.\r\n" + " CERTGEN Generate client certs\r\n" + " ESP32CERT (extract/inject) \r\n" + " ESP32FIXUP \r\n" + " ENCODE_TEST Runs an encoding test\r\n", + argv[0]); +} \ No newline at end of file From a199cb7036633ccd12fe4af6b21c783fbac61e0e Mon Sep 17 00:00:00 2001 From: ivan Chubb Date: Sun, 1 Oct 2023 14:55:43 -0400 Subject: [PATCH 4/9] fixes off-by-one bug of the argument print on the TRACE_ERROR call when an unknown argv[1] is provided --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 10ae666d..9bf959a8 100644 --- a/src/main.c +++ b/src/main.c @@ -335,7 +335,7 @@ int_t main(int argc, char *argv[]) #endif else { - TRACE_ERROR("Bad argument provided: %s\r\n", argv[0]); + TRACE_ERROR("Bad argument provided: %s\r\n", argv[1]); print_usage(argv); return -1; } From 928cdf1933b18a448bcca5ed8cb13bb934e4eca4 Mon Sep 17 00:00:00 2001 From: ivan Chubb Date: Sun, 1 Oct 2023 14:56:57 -0400 Subject: [PATCH 5/9] adds new line to the bottom of the file to be consistent with others --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 9bf959a8..a80fe2a1 100644 --- a/src/main.c +++ b/src/main.c @@ -375,4 +375,4 @@ static void print_usage(char *argv[]) " ESP32FIXUP \r\n" " ENCODE_TEST Runs an encoding test\r\n", argv[0]); -} \ No newline at end of file +} From 8af724644e84d05e288c075eafde418b628484d6 Mon Sep 17 00:00:00 2001 From: ivan Chubb Date: Sun, 1 Oct 2023 15:34:31 -0400 Subject: [PATCH 6/9] revert to WIN32 instead of _WIN32 --- src/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index a80fe2a1..94c2bc2d 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,7 @@ #include "cert.h" #include "toniefile.h" -#ifdef _WIN32 +#ifdef WIN32 #include #define PATH_LEN MAX_PATH #else @@ -355,7 +355,7 @@ int_t main(int argc, char *argv[]) static char *get_cwd(char *buffer, size_t size) { -#ifdef _WIN32 +#ifdef WIN32 return _getcwd(buffer, size); #else return getcwd(buffer, size); From 5a07489819b70ff9c11c660ce5fbd0eafcc4a322 Mon Sep 17 00:00:00 2001 From: ivan Chubb Date: Sun, 1 Oct 2023 15:41:08 -0400 Subject: [PATCH 7/9] include 'direct.h' header for _getcwd() windows function call. --- src/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.c b/src/main.c index 94c2bc2d..6dcad490 100644 --- a/src/main.c +++ b/src/main.c @@ -24,6 +24,7 @@ #ifdef WIN32 #include +#include #define PATH_LEN MAX_PATH #else #include From 5d808a163edc9b63ab68169551c201e71b25081a Mon Sep 17 00:00:00 2001 From: ivan Chubb Date: Sun, 1 Oct 2023 15:41:59 -0400 Subject: [PATCH 8/9] add back _WIN32 --- src/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 6dcad490..cc349466 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,7 @@ #include "cert.h" #include "toniefile.h" -#ifdef WIN32 +#ifdef _WIN32 #include #include #define PATH_LEN MAX_PATH @@ -356,7 +356,7 @@ int_t main(int argc, char *argv[]) static char *get_cwd(char *buffer, size_t size) { -#ifdef WIN32 +#ifdef _WIN32 return _getcwd(buffer, size); #else return getcwd(buffer, size); From c9ef241694d774d9e874ede16f380257876797f9 Mon Sep 17 00:00:00 2001 From: ivan Chubb Date: Sun, 1 Oct 2023 15:45:31 -0400 Subject: [PATCH 9/9] remove windows.h header, since the implicit addition of winsock.h could be added erorrs --- src/main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index cc349466..d5be1015 100644 --- a/src/main.c +++ b/src/main.c @@ -23,9 +23,8 @@ #include "toniefile.h" #ifdef _WIN32 -#include #include -#define PATH_LEN MAX_PATH +#define PATH_LEN 260 #else #include #include