From 682e74fb40e6e15e7855c3f129ab8ff5f424b096 Mon Sep 17 00:00:00 2001 From: Lealem Amedie Date: Fri, 31 Mar 2023 15:08:24 -0600 Subject: [PATCH 1/2] CubePack: Better USER_IO support & cleanup warnings --- ide/STM32CUBE/README.md | 4 +- ide/STM32CUBE/default_conf.ftl | 10 ++- ide/STM32CUBE/myFilesystem.h | 42 ++++++------ ide/STM32CUBE/userio_template.h | 110 ++++++++++++++++++++++++++++++++ src/misc.c | 5 +- wolfssh/test.h | 10 +++ 6 files changed, 153 insertions(+), 28 deletions(-) create mode 100644 ide/STM32CUBE/userio_template.h diff --git a/ide/STM32CUBE/README.md b/ide/STM32CUBE/README.md index d4e287131..05a2fe86e 100644 --- a/ide/STM32CUBE/README.md +++ b/ide/STM32CUBE/README.md @@ -10,7 +10,7 @@ The wolfSSH Cube Pack can be found [here](https://www.wolfssl.com/files/ide/I-CU 4. In the `Software Packs` configuration category of the `.ioc` file, click on the wolfSSH pack and enable the library by checking the box. -5. Since LwIP is a dependency for the Cube Pack, enable LwIP in the `Middleware` configuration category of the project. Also enable the `LWIP_DNS` option in the LwIP configuration settings. +5. The Pack defaults to using custom IO provided by the user. Modify `ide/STM32CUBE/userio_template.h` to supply the custom IO. If you'd like to use LwIP instead, configure the wolfSSH IO settings in the `.ioc` to enable LwIP compatibilty. You'll also have to enable LwIP in the `Middleware` configuration category of the project. Enable the `LWIP_DNS` option in the LwIP configuration settings. 6. Save your changes and select yes to the prompt asking about generating code. @@ -19,4 +19,4 @@ The wolfSSH Cube Pack can be found [here](https://www.wolfssl.com/files/ide/I-CU ## Notes - Make sure to make [these changes](https://github.com/wolfSSL/wolfssl/tree/master/IDE/STM32Cube#stm32-printf) to redirect the printf's to the UART. -- If looking to enable filesystem support, the pack assumes the user has defined their own filesystem in `wolfssh/myFilesystem.h`. That file will originally contain a dummy filesystem. If going the FATFS route, make sure to replace `#define WOLFSSH_USER_FILESYSTEM` with `#define WOLFSSH_FATFS` in the `wolfSSL.I-CUBE-wolfSSH_conf.h` header file. The wolfSSL Cube Pack also defaults to disabling filesystem support so make sure to remove `#define NO_FILESYSTEM` from `wolfSSL.I-CUBE-wolfSSL_conf.h`. +- If looking to enable filesystem support (required for SFTP), the pack assumes the user has defined their own filesystem in `wolfssh/myFilesystem.h`. That file will originally contain a dummy filesystem. If going the FATFS route, make sure to replace `#define WOLFSSH_USER_FILESYSTEM` with `#define WOLFSSH_FATFS` in the `wolfSSL.I-CUBE-wolfSSH_conf.h` header file. The wolfSSL Cube Pack also defaults to disabling filesystem support so make sure to remove `#define NO_FILESYSTEM` from `wolfSSL.I-CUBE-wolfSSL_conf.h`. diff --git a/ide/STM32CUBE/default_conf.ftl b/ide/STM32CUBE/default_conf.ftl index d1ff44f34..0de5bf134 100644 --- a/ide/STM32CUBE/default_conf.ftl +++ b/ide/STM32CUBE/default_conf.ftl @@ -94,9 +94,11 @@ extern ${variable.value} ${variable.name}; /* ------------------------------------------------------------------------- */ /* wolfSSH IO */ /* ------------------------------------------------------------------------- */ -#define WOLFSSH_LWIP -/* Remove the LWIP define and uncomment the line below to use user defined IO */ -/* #define WOLFSSL_USER_IO */ +#if defined(WOLFSSH_CONF_IO) && WOLFSSH_CONF_IO == 2 + #define WOLFSSH_LWIP +#else + #define WOLFSSH_USER_IO +#endif /* To be defined for the target Socket API */ #define WSTARTTCP() @@ -140,6 +142,8 @@ extern ${variable.value} ${variable.name}; #define CURVE25519_SMALL #define HAVE_ED25519 +#define WOLFSSH_IGNORE_FILE_WARN + typedef unsigned int size_t; /* defines for unit tests */ diff --git a/ide/STM32CUBE/myFilesystem.h b/ide/STM32CUBE/myFilesystem.h index 13d0d8138..31eefd454 100644 --- a/ide/STM32CUBE/myFilesystem.h +++ b/ide/STM32CUBE/myFilesystem.h @@ -40,107 +40,107 @@ typedef struct { int i; } stat_t; #define WFD int enum { O_RDWR, O_RDONLY, O_WRONLY, O_APPEND, O_CREAT, O_TRUNC, O_EXCL } ; -static int WFOPEN(FILE **f, const char *n, const char *m){ +static inline int WFOPEN(FILE **f, const char *n, const char *m){ (void) n; (void) m; (void)f; return NULL; } -static int WFCLOSE(FILE *f) { +static inline int WFCLOSE(FILE *f) { (void) f; return 0; } -static size_t WFREAD(void *b, size_t s, size_t n, FILE *f) { +static inline size_t WFREAD(void *b, size_t s, size_t n, FILE *f) { (void) b; (void) s; (void) n; (void) f; return 0; } -static size_t WFWRITE(const void *b, size_t s, size_t n, FILE *f) { +static inline size_t WFWRITE(const void *b, size_t s, size_t n, FILE *f) { (void) b; (void) s; (void) n; (void) f; return 0; } -static int WFSEEK(FILE *f, long int p, int m) { +static inline int WFSEEK(FILE *f, long int p, int m) { (void) f; (void) p; (void) m; return 0; } -static long int WFTELL(FILE *f) { +static inline long int WFTELL(FILE *f) { (void) f; return 0; } -static void WREWIND(FILE *f) { +static inline void WREWIND(FILE *f) { (void) f; } -static int WOPEN (const char* n, int f, int m) { +static inline int WOPEN (const char* n, int f, int m) { (void) f; (void) n; (void) m; return 0; } -static int WCLOSE(int f) { +static inline int WCLOSE(int f) { (void) f; return 0; } -static size_t WPREAD(int f, void* b, size_t c, off_t *o) { +static inline size_t WPREAD(int f, void* b, size_t c, off_t *o) { (void) f; (void) b; (void) c; (void)o; return 0; } -static size_t WPWRITE(int f, void* b, size_t c, off_t *o) { +static inline size_t WPWRITE(int f, void* b, size_t c, off_t *o) { (void) f; (void) b; (void) c; (void)o; return 0; } -static char *WGETCWD(void *fs, char *f, size_t l){ +static inline char *WGETCWD(void *fs, char *f, size_t l){ (void) fs; (void) f; (void) l; return 0; } -static int WRMDIR(void *fs, const char *p){ +static inline int WRMDIR(void *fs, const char *p){ (void) p; return 0; } -static int WMKDIR(void *fs, const char *p, mode_t m) { +static inline int WMKDIR(void *fs, const char *p, mode_t m) { (void) p; (void) m; return 0; } -static int WREMOVE(void *fs, const char *p){ +static inline int WREMOVE(void *fs, const char *p){ (void) fs; (void) p; return 0; } -static int WRENAME(void *fs, const char *p, const char *np){ +static inline int WRENAME(void *fs, const char *p, const char *np){ (void) fs; (void) p; (void)np; return 0; } -static int WSTAT(const char *p, stat_t *b) { +static inline int WSTAT(const char *p, stat_t *b) { (void) p; (void)b; return 0; } -static int WLSTAT(const char *p, stat_t *b) { +static inline int WLSTAT(const char *p, stat_t *b) { (void) p; (void)b; return 0; } -static int WCHMOD(void *fs, const char *p, mode_t m) { +static inline int WCHMOD(void *fs, const char *p, mode_t m) { (void) fs; (void) p; (void)m; return 0; } -static int SFTP_GetAttributes(void* fs, const char* fileName, +static inline int SFTP_GetAttributes(void* fs, const char* fileName, void* atr, byte link, void* heap) { (void)fs; (void)fileName; (void)atr; (void)link; (void)heap; return 0; } -static int SFTP_GetAttributes_Handle(void* ssh, byte* handle, int handleSz, +static inline int SFTP_GetAttributes_Handle(void* ssh, byte* handle, int handleSz, void* atr) { (void)ssh; (void)handle; (void)handleSz; diff --git a/ide/STM32CUBE/userio_template.h b/ide/STM32CUBE/userio_template.h new file mode 100644 index 000000000..4b8144288 --- /dev/null +++ b/ide/STM32CUBE/userio_template.h @@ -0,0 +1,110 @@ +/* userio_template.h + * + * Copyright (C) 2014-2023 wolfSSL Inc. + * + * This file is part of wolfSSH. + * + * wolfSSH is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfSSH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with wolfSSH. If not, see . + */ + + +#ifndef USERIO_TEMPLATE_H +#define USERIO_TEMPLATE_H + +#ifdef WOLFSSH_USER_IO + +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 +#define SOCK_RAW 3 + +#define SOL_SOCKET 0xfff +#define SO_REUSEADDR 0x0004 + +#define AF_INET 2 +#define INADDR_ANY ((uint32_t)0x00000000UL) + +#define socklen_t uint32_t + +typedef struct { int s_addr; } in_addr; + +struct sockaddr { int i; }; + +typedef struct sockaddr sockaddr; + +struct sockaddr_in{ + int sin_len; + int sin_family; + int sin_port; + in_addr sin_addr; +}; + +typedef struct sockaddr_in sockaddr_in; + +struct hostent{ + char *h_name; + int h_length; + char **h_addr_list; +}; + +typedef struct hostent hostent; + +static inline int inet_addr(const char* n){ + (void) n; + return 0; +} + +static inline int htons(unsigned int n){ + (void) n; + return 0; +} + +static inline int ntohs(unsigned int n){ + (void) n; + return 0; +} + +static inline int socket(int d, int t, int p) { + (void) d; (void) t; (void) p; + return 0; +} + +static inline int setsockopt(int s, int l, int n, const void *o, + socklen_t len) { + (void) s; (void) l; (void) n; (void) o; (void) len; + return 0; +} + +static inline int getsockname(int s, struct sockaddr *n, socklen_t* len) { + (void) s; (void) n; (void) len; + return 0; +} + +static inline int bind(int s, const struct sockaddr *n, socklen_t l) { + (void) s; (void) n; (void) l; + return 0; +} + +static inline int listen(int s, int b) { + (void) s; (void) b; + return 0; +} + +static inline struct hostent* gethostbyname(const char* n) { + (void) n; + return NULL; +} + +#endif /* WOLFSSH_USER_IO */ + +#endif diff --git a/src/misc.c b/src/misc.c index 6ff0b2c8c..0567f1f5b 100644 --- a/src/misc.c +++ b/src/misc.c @@ -51,7 +51,8 @@ /* Check for if compiling misc.c when not needed. */ -#if !defined(WOLFSSH_MISC_INCLUDED) && !defined(NO_INLINE) +#if !defined(WOLFSSH_MISC_INCLUDED) && !defined(NO_INLINE) && \ + !defined(WOLFSSH_IGNORE_FILE_WARN) #define MISC_WARNING "misc.c does not need to be compiled when using inline (NO_INLINE not defined))" #ifndef _MSC_VER @@ -60,7 +61,7 @@ #pragma message("warning: " MISC_WARNING) #endif -#else /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */ +#else /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE && !WOLFSSH_IGNORE_FILE_WARN */ #ifndef min diff --git a/wolfssh/test.h b/wolfssh/test.h index 91717e783..92170aaab 100644 --- a/wolfssh/test.h +++ b/wolfssh/test.h @@ -126,6 +126,16 @@ #endif #define SOCKET_T int #define NUM_SOCKETS 5 +#elif defined(WOLFSSH_USER_IO) + #include + #include + #include + #include "userio_template.h" + #ifndef SO_NOSIGPIPE + #include /* ignore SIGPIPE */ + #endif + #define SOCKET_T int + #define NUM_SOCKETS 5 #else /* USE_WINDOWS_API */ #include #include From 29ea1af2fb1fb6974920c243268048ffa2334652 Mon Sep 17 00:00:00 2001 From: Lealem Amedie Date: Mon, 3 Apr 2023 09:37:28 -0600 Subject: [PATCH 2/2] Note how to resolve possible error --- ide/STM32CUBE/README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ide/STM32CUBE/README.md b/ide/STM32CUBE/README.md index 05a2fe86e..9cfcb1e71 100644 --- a/ide/STM32CUBE/README.md +++ b/ide/STM32CUBE/README.md @@ -1,4 +1,4 @@ -# wolfSSH for STM32 Cube IDE +# wolfSSH for STM32 Cube IDE The wolfSSH Cube Pack can be found [here](https://www.wolfssl.com/files/ide/I-CUBE-wolfSSH.pack) and is dependent on the `wolfCrypt` library. @@ -6,7 +6,7 @@ The wolfSSH Cube Pack can be found [here](https://www.wolfssl.com/files/ide/I-CU 2. Then install the wolfSSH Cube Pack in the same manner as the wolfSSL pack with CUBEMX. -3. Open the project `.ioc` file and click the `Software Packs` drop down menu and then `Select Components`. Expand the `wolfSSH` pack and check all the components. +3. Open the project `.ioc` file and click the `Software Packs` drop down menu and then `Select Components`. Expand the `wolfSSH` pack and check all the components. 4. In the `Software Packs` configuration category of the `.ioc` file, click on the wolfSSH pack and enable the library by checking the box. @@ -20,3 +20,11 @@ The wolfSSH Cube Pack can be found [here](https://www.wolfssl.com/files/ide/I-CU - Make sure to make [these changes](https://github.com/wolfSSL/wolfssl/tree/master/IDE/STM32Cube#stm32-printf) to redirect the printf's to the UART. - If looking to enable filesystem support (required for SFTP), the pack assumes the user has defined their own filesystem in `wolfssh/myFilesystem.h`. That file will originally contain a dummy filesystem. If going the FATFS route, make sure to replace `#define WOLFSSH_USER_FILESYSTEM` with `#define WOLFSSH_FATFS` in the `wolfSSL.I-CUBE-wolfSSH_conf.h` header file. The wolfSSL Cube Pack also defaults to disabling filesystem support so make sure to remove `#define NO_FILESYSTEM` from `wolfSSL.I-CUBE-wolfSSL_conf.h`. + +- If building with LwIP and you encounter the error `multiple definition of 'errno'` in `Middlewares/Third_Party/LwIP/system/OS/sys_arch.c`, modify the file as shown below. +``` +#if defined(LWIP_SOCKET_SET_ERRNO) && defined(LWIP_PROVIDE_ERRNO) +- int errno; ++ extern int errno; +#endif +```