ssh
wolfssh/test.h@0:c4152c628df5, 2019-11-25 (annotated)
- Committer:
- sPymbed
- Date:
- Mon Nov 25 14:24:05 2019 +0000
- Revision:
- 0:c4152c628df5
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sPymbed | 0:c4152c628df5 | 1 | /* test.h */ |
sPymbed | 0:c4152c628df5 | 2 | |
sPymbed | 0:c4152c628df5 | 3 | #pragma once |
sPymbed | 0:c4152c628df5 | 4 | |
sPymbed | 0:c4152c628df5 | 5 | #ifndef _WOLFSSH_TEST_H_ |
sPymbed | 0:c4152c628df5 | 6 | #define _WOLFSSH_TEST_H_ |
sPymbed | 0:c4152c628df5 | 7 | |
sPymbed | 0:c4152c628df5 | 8 | |
sPymbed | 0:c4152c628df5 | 9 | #include <stdio.h> |
sPymbed | 0:c4152c628df5 | 10 | /*#include <stdlib.h>*/ |
sPymbed | 0:c4152c628df5 | 11 | #include <ctype.h> |
sPymbed | 0:c4152c628df5 | 12 | /*#include <wolfssh/error.h>*/ |
sPymbed | 0:c4152c628df5 | 13 | |
sPymbed | 0:c4152c628df5 | 14 | #ifdef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 15 | #include <winsock2.h> |
sPymbed | 0:c4152c628df5 | 16 | #include <process.h> |
sPymbed | 0:c4152c628df5 | 17 | #include <assert.h> |
sPymbed | 0:c4152c628df5 | 18 | #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */ |
sPymbed | 0:c4152c628df5 | 19 | #include <ws2tcpip.h> |
sPymbed | 0:c4152c628df5 | 20 | #include <wspiapi.h> |
sPymbed | 0:c4152c628df5 | 21 | #endif |
sPymbed | 0:c4152c628df5 | 22 | #define SOCKET_T SOCKET |
sPymbed | 0:c4152c628df5 | 23 | #else /* USE_WINDOWS_API */ |
sPymbed | 0:c4152c628df5 | 24 | #include <unistd.h> |
sPymbed | 0:c4152c628df5 | 25 | #include <netdb.h> |
sPymbed | 0:c4152c628df5 | 26 | #include <netinet/in.h> |
sPymbed | 0:c4152c628df5 | 27 | //#include <netinet/tcp.h> |
sPymbed | 0:c4152c628df5 | 28 | #include <arpa/inet.h> |
sPymbed | 0:c4152c628df5 | 29 | #include <sys/ioctl.h> |
sPymbed | 0:c4152c628df5 | 30 | #include <sys/socket.h> |
sPymbed | 0:c4152c628df5 | 31 | #include <pthread.h> |
sPymbed | 0:c4152c628df5 | 32 | #include <fcntl.h> |
sPymbed | 0:c4152c628df5 | 33 | #ifndef SO_NOSIGPIPE // FIX: ESP32 |
sPymbed | 0:c4152c628df5 | 34 | #include <signal.h> /* ignore SIGPIPE */ |
sPymbed | 0:c4152c628df5 | 35 | #endif |
sPymbed | 0:c4152c628df5 | 36 | #define SOCKET_T int |
sPymbed | 0:c4152c628df5 | 37 | #endif /* USE_WINDOWS_API */ |
sPymbed | 0:c4152c628df5 | 38 | |
sPymbed | 0:c4152c628df5 | 39 | |
sPymbed | 0:c4152c628df5 | 40 | /* Socket Handling */ |
sPymbed | 0:c4152c628df5 | 41 | #ifndef WOLFSSH_SOCKET_INVALID |
sPymbed | 0:c4152c628df5 | 42 | #ifdef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 43 | #define WOLFSSH_SOCKET_INVALID ((SOCKET_T)INVALID_SOCKET) |
sPymbed | 0:c4152c628df5 | 44 | #elif defined(WOLFSSH_TIRTOS) |
sPymbed | 0:c4152c628df5 | 45 | #define WOLFSSH_SOCKET_INVALID ((SOCKET_T)-1) |
sPymbed | 0:c4152c628df5 | 46 | #else |
sPymbed | 0:c4152c628df5 | 47 | #define WOLFSSH_SOCKET_INVALID (SOCKET_T)(0) |
sPymbed | 0:c4152c628df5 | 48 | #endif |
sPymbed | 0:c4152c628df5 | 49 | #endif /* WOLFSSH_SOCKET_INVALID */ |
sPymbed | 0:c4152c628df5 | 50 | |
sPymbed | 0:c4152c628df5 | 51 | #ifndef WOLFSSL_SOCKET_IS_INVALID |
sPymbed | 0:c4152c628df5 | 52 | #if defined(USE_WINDOWS_API) || defined(WOLFSSL_TIRTOS) |
sPymbed | 0:c4152c628df5 | 53 | #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) == WOLFSSL_SOCKET_INVALID) |
sPymbed | 0:c4152c628df5 | 54 | #else |
sPymbed | 0:c4152c628df5 | 55 | #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) < WOLFSSL_SOCKET_INVALID) |
sPymbed | 0:c4152c628df5 | 56 | #endif |
sPymbed | 0:c4152c628df5 | 57 | #endif /* WOLFSSL_SOCKET_IS_INVALID */ |
sPymbed | 0:c4152c628df5 | 58 | |
sPymbed | 0:c4152c628df5 | 59 | |
sPymbed | 0:c4152c628df5 | 60 | #if defined(__MACH__) || defined(USE_WINDOWS_API) |
sPymbed | 0:c4152c628df5 | 61 | #ifndef _SOCKLEN_T |
sPymbed | 0:c4152c628df5 | 62 | typedef int socklen_t; |
sPymbed | 0:c4152c628df5 | 63 | #endif |
sPymbed | 0:c4152c628df5 | 64 | #endif |
sPymbed | 0:c4152c628df5 | 65 | |
sPymbed | 0:c4152c628df5 | 66 | |
sPymbed | 0:c4152c628df5 | 67 | #ifdef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 68 | #define WCLOSESOCKET(s) closesocket(s) |
sPymbed | 0:c4152c628df5 | 69 | #define WSTARTTCP() do { WSADATA wsd; WSAStartup(0x0002, &wsd); } while(0) |
sPymbed | 0:c4152c628df5 | 70 | #else |
sPymbed | 0:c4152c628df5 | 71 | #define WCLOSESOCKET(s) close(s) |
sPymbed | 0:c4152c628df5 | 72 | #define WSTARTTCP() |
sPymbed | 0:c4152c628df5 | 73 | #endif |
sPymbed | 0:c4152c628df5 | 74 | |
sPymbed | 0:c4152c628df5 | 75 | |
sPymbed | 0:c4152c628df5 | 76 | #ifdef SINGLE_THREADED |
sPymbed | 0:c4152c628df5 | 77 | typedef unsigned int THREAD_RETURN; |
sPymbed | 0:c4152c628df5 | 78 | typedef void* THREAD_TYPE; |
sPymbed | 0:c4152c628df5 | 79 | #define WOLFSSH_THREAD |
sPymbed | 0:c4152c628df5 | 80 | #else |
sPymbed | 0:c4152c628df5 | 81 | #if defined(_POSIX_THREADS) && !defined(__MINGW32__) |
sPymbed | 0:c4152c628df5 | 82 | typedef void* THREAD_RETURN; |
sPymbed | 0:c4152c628df5 | 83 | typedef pthread_t THREAD_TYPE; |
sPymbed | 0:c4152c628df5 | 84 | #define WOLFSSH_THREAD |
sPymbed | 0:c4152c628df5 | 85 | #define INFINITE -1 |
sPymbed | 0:c4152c628df5 | 86 | #define WAIT_OBJECT_0 0L |
sPymbed | 0:c4152c628df5 | 87 | #else |
sPymbed | 0:c4152c628df5 | 88 | typedef unsigned int THREAD_RETURN; |
sPymbed | 0:c4152c628df5 | 89 | typedef intptr_t THREAD_TYPE; |
sPymbed | 0:c4152c628df5 | 90 | #define WOLFSSH_THREAD __stdcall |
sPymbed | 0:c4152c628df5 | 91 | #endif |
sPymbed | 0:c4152c628df5 | 92 | #endif |
sPymbed | 0:c4152c628df5 | 93 | |
sPymbed | 0:c4152c628df5 | 94 | #ifdef TEST_IPV6 |
sPymbed | 0:c4152c628df5 | 95 | typedef struct sockaddr_in6 SOCKADDR_IN_T; |
sPymbed | 0:c4152c628df5 | 96 | #define AF_INET_V AF_INET6 |
sPymbed | 0:c4152c628df5 | 97 | #else |
sPymbed | 0:c4152c628df5 | 98 | typedef struct sockaddr_in SOCKADDR_IN_T; |
sPymbed | 0:c4152c628df5 | 99 | #define AF_INET_V AF_INET |
sPymbed | 0:c4152c628df5 | 100 | #endif |
sPymbed | 0:c4152c628df5 | 101 | |
sPymbed | 0:c4152c628df5 | 102 | |
sPymbed | 0:c4152c628df5 | 103 | #define serverKeyRsaPemFile "./keys/server-key-rsa.pem" |
sPymbed | 0:c4152c628df5 | 104 | |
sPymbed | 0:c4152c628df5 | 105 | |
sPymbed | 0:c4152c628df5 | 106 | typedef struct tcp_ready { |
sPymbed | 0:c4152c628df5 | 107 | word16 ready; /* predicate */ |
sPymbed | 0:c4152c628df5 | 108 | word16 port; |
sPymbed | 0:c4152c628df5 | 109 | char* srfName; /* server ready file name */ |
sPymbed | 0:c4152c628df5 | 110 | #if defined(_POSIX_THREADS) && !defined(__MINGW32__) |
sPymbed | 0:c4152c628df5 | 111 | pthread_mutex_t mutex; |
sPymbed | 0:c4152c628df5 | 112 | pthread_cond_t cond; |
sPymbed | 0:c4152c628df5 | 113 | #endif |
sPymbed | 0:c4152c628df5 | 114 | } tcp_ready; |
sPymbed | 0:c4152c628df5 | 115 | |
sPymbed | 0:c4152c628df5 | 116 | |
sPymbed | 0:c4152c628df5 | 117 | static INLINE void InitTcpReady(tcp_ready* ready) |
sPymbed | 0:c4152c628df5 | 118 | { |
sPymbed | 0:c4152c628df5 | 119 | ready->ready = 0; |
sPymbed | 0:c4152c628df5 | 120 | ready->port = 0; |
sPymbed | 0:c4152c628df5 | 121 | ready->srfName = NULL; |
sPymbed | 0:c4152c628df5 | 122 | #ifdef SINGLE_THREADED |
sPymbed | 0:c4152c628df5 | 123 | #elif defined(_POSIX_THREADS) && !defined(__MINGW32__) |
sPymbed | 0:c4152c628df5 | 124 | pthread_mutex_init(&ready->mutex, 0); |
sPymbed | 0:c4152c628df5 | 125 | pthread_cond_init(&ready->cond, 0); |
sPymbed | 0:c4152c628df5 | 126 | #endif |
sPymbed | 0:c4152c628df5 | 127 | } |
sPymbed | 0:c4152c628df5 | 128 | |
sPymbed | 0:c4152c628df5 | 129 | |
sPymbed | 0:c4152c628df5 | 130 | static INLINE void FreeTcpReady(tcp_ready* ready) |
sPymbed | 0:c4152c628df5 | 131 | { |
sPymbed | 0:c4152c628df5 | 132 | #ifdef SINGLE_THREADED |
sPymbed | 0:c4152c628df5 | 133 | (void)ready; |
sPymbed | 0:c4152c628df5 | 134 | #elif defined(_POSIX_THREADS) && !defined(__MINGW32__) |
sPymbed | 0:c4152c628df5 | 135 | pthread_mutex_destroy(&ready->mutex); |
sPymbed | 0:c4152c628df5 | 136 | pthread_cond_destroy(&ready->cond); |
sPymbed | 0:c4152c628df5 | 137 | #else |
sPymbed | 0:c4152c628df5 | 138 | (void)ready; |
sPymbed | 0:c4152c628df5 | 139 | #endif |
sPymbed | 0:c4152c628df5 | 140 | } |
sPymbed | 0:c4152c628df5 | 141 | |
sPymbed | 0:c4152c628df5 | 142 | |
sPymbed | 0:c4152c628df5 | 143 | typedef struct func_args { |
sPymbed | 0:c4152c628df5 | 144 | int argc; |
sPymbed | 0:c4152c628df5 | 145 | char** argv; |
sPymbed | 0:c4152c628df5 | 146 | int return_code; |
sPymbed | 0:c4152c628df5 | 147 | tcp_ready* signal; |
sPymbed | 0:c4152c628df5 | 148 | WS_CallbackUserAuth user_auth; |
sPymbed | 0:c4152c628df5 | 149 | } func_args; |
sPymbed | 0:c4152c628df5 | 150 | |
sPymbed | 0:c4152c628df5 | 151 | |
sPymbed | 0:c4152c628df5 | 152 | typedef THREAD_RETURN WOLFSSH_THREAD THREAD_FUNC(void*); |
sPymbed | 0:c4152c628df5 | 153 | void ThreadStart(THREAD_FUNC, void*, THREAD_TYPE*); |
sPymbed | 0:c4152c628df5 | 154 | void ThreadJoin(THREAD_TYPE); |
sPymbed | 0:c4152c628df5 | 155 | void ThreadDetach(THREAD_TYPE); |
sPymbed | 0:c4152c628df5 | 156 | void WaitTcpReady(func_args*); |
sPymbed | 0:c4152c628df5 | 157 | |
sPymbed | 0:c4152c628df5 | 158 | |
sPymbed | 0:c4152c628df5 | 159 | #ifndef TEST_IPV6 |
sPymbed | 0:c4152c628df5 | 160 | static const char* const wolfSshIp = "127.0.0.1"; |
sPymbed | 0:c4152c628df5 | 161 | #else /* TEST_IPV6 */ |
sPymbed | 0:c4152c628df5 | 162 | static const char* const wolfSshIp = "::1"; |
sPymbed | 0:c4152c628df5 | 163 | #endif /* TEST_IPV6 */ |
sPymbed | 0:c4152c628df5 | 164 | static const word16 wolfSshPort = 22222; |
sPymbed | 0:c4152c628df5 | 165 | |
sPymbed | 0:c4152c628df5 | 166 | |
sPymbed | 0:c4152c628df5 | 167 | #ifdef __GNUC__ |
sPymbed | 0:c4152c628df5 | 168 | #define WS_NORETURN __attribute__((noreturn)) |
sPymbed | 0:c4152c628df5 | 169 | #else |
sPymbed | 0:c4152c628df5 | 170 | #define WS_NORETURN |
sPymbed | 0:c4152c628df5 | 171 | #endif |
sPymbed | 0:c4152c628df5 | 172 | |
sPymbed | 0:c4152c628df5 | 173 | static INLINE WS_NORETURN void err_sys(const char* msg) |
sPymbed | 0:c4152c628df5 | 174 | { |
sPymbed | 0:c4152c628df5 | 175 | printf("wolfSSH error: %s\n", msg); |
sPymbed | 0:c4152c628df5 | 176 | |
sPymbed | 0:c4152c628df5 | 177 | #ifndef __GNUC__ |
sPymbed | 0:c4152c628df5 | 178 | /* scan-build (which pretends to be gnuc) can get confused and think the |
sPymbed | 0:c4152c628df5 | 179 | * msg pointer can be null even when hardcoded and then it won't exit, |
sPymbed | 0:c4152c628df5 | 180 | * making null pointer checks above the err_sys() call useless. |
sPymbed | 0:c4152c628df5 | 181 | * We could just always exit() but some compilers will complain about no |
sPymbed | 0:c4152c628df5 | 182 | * possible return, with gcc we know the attribute to handle that with |
sPymbed | 0:c4152c628df5 | 183 | * WS_NORETURN. */ |
sPymbed | 0:c4152c628df5 | 184 | if (msg) |
sPymbed | 0:c4152c628df5 | 185 | #endif |
sPymbed | 0:c4152c628df5 | 186 | { |
sPymbed | 0:c4152c628df5 | 187 | exit(EXIT_FAILURE); |
sPymbed | 0:c4152c628df5 | 188 | } |
sPymbed | 0:c4152c628df5 | 189 | } |
sPymbed | 0:c4152c628df5 | 190 | |
sPymbed | 0:c4152c628df5 | 191 | |
sPymbed | 0:c4152c628df5 | 192 | #define MY_EX_USAGE 2 |
sPymbed | 0:c4152c628df5 | 193 | |
sPymbed | 0:c4152c628df5 | 194 | extern int myoptind; |
sPymbed | 0:c4152c628df5 | 195 | extern char* myoptarg; |
sPymbed | 0:c4152c628df5 | 196 | |
sPymbed | 0:c4152c628df5 | 197 | static INLINE int mygetopt(int argc, char** argv, const char* optstring) |
sPymbed | 0:c4152c628df5 | 198 | { |
sPymbed | 0:c4152c628df5 | 199 | static char* next = NULL; |
sPymbed | 0:c4152c628df5 | 200 | |
sPymbed | 0:c4152c628df5 | 201 | char c; |
sPymbed | 0:c4152c628df5 | 202 | char* cp; |
sPymbed | 0:c4152c628df5 | 203 | |
sPymbed | 0:c4152c628df5 | 204 | if (myoptind == 0) |
sPymbed | 0:c4152c628df5 | 205 | next = NULL; /* we're starting new/over */ |
sPymbed | 0:c4152c628df5 | 206 | |
sPymbed | 0:c4152c628df5 | 207 | if (next == NULL || *next == '\0') { |
sPymbed | 0:c4152c628df5 | 208 | if (myoptind == 0) |
sPymbed | 0:c4152c628df5 | 209 | myoptind++; |
sPymbed | 0:c4152c628df5 | 210 | |
sPymbed | 0:c4152c628df5 | 211 | if (myoptind >= argc || argv[myoptind][0] != '-' || |
sPymbed | 0:c4152c628df5 | 212 | argv[myoptind][1] == '\0') { |
sPymbed | 0:c4152c628df5 | 213 | myoptarg = NULL; |
sPymbed | 0:c4152c628df5 | 214 | if (myoptind < argc) |
sPymbed | 0:c4152c628df5 | 215 | myoptarg = argv[myoptind]; |
sPymbed | 0:c4152c628df5 | 216 | |
sPymbed | 0:c4152c628df5 | 217 | return -1; |
sPymbed | 0:c4152c628df5 | 218 | } |
sPymbed | 0:c4152c628df5 | 219 | |
sPymbed | 0:c4152c628df5 | 220 | if (strcmp(argv[myoptind], "--") == 0) { |
sPymbed | 0:c4152c628df5 | 221 | myoptind++; |
sPymbed | 0:c4152c628df5 | 222 | myoptarg = NULL; |
sPymbed | 0:c4152c628df5 | 223 | |
sPymbed | 0:c4152c628df5 | 224 | if (myoptind < argc) |
sPymbed | 0:c4152c628df5 | 225 | myoptarg = argv[myoptind]; |
sPymbed | 0:c4152c628df5 | 226 | |
sPymbed | 0:c4152c628df5 | 227 | return -1; |
sPymbed | 0:c4152c628df5 | 228 | } |
sPymbed | 0:c4152c628df5 | 229 | |
sPymbed | 0:c4152c628df5 | 230 | next = argv[myoptind]; |
sPymbed | 0:c4152c628df5 | 231 | next++; /* skip - */ |
sPymbed | 0:c4152c628df5 | 232 | myoptind++; |
sPymbed | 0:c4152c628df5 | 233 | } |
sPymbed | 0:c4152c628df5 | 234 | |
sPymbed | 0:c4152c628df5 | 235 | c = *next++; |
sPymbed | 0:c4152c628df5 | 236 | /* The C++ strchr can return a different value */ |
sPymbed | 0:c4152c628df5 | 237 | cp = (char*)strchr(optstring, c); |
sPymbed | 0:c4152c628df5 | 238 | |
sPymbed | 0:c4152c628df5 | 239 | if (cp == NULL || c == ':') |
sPymbed | 0:c4152c628df5 | 240 | return '?'; |
sPymbed | 0:c4152c628df5 | 241 | |
sPymbed | 0:c4152c628df5 | 242 | cp++; |
sPymbed | 0:c4152c628df5 | 243 | |
sPymbed | 0:c4152c628df5 | 244 | if (*cp == ':') { |
sPymbed | 0:c4152c628df5 | 245 | if (*next != '\0') { |
sPymbed | 0:c4152c628df5 | 246 | myoptarg = next; |
sPymbed | 0:c4152c628df5 | 247 | next = NULL; |
sPymbed | 0:c4152c628df5 | 248 | } |
sPymbed | 0:c4152c628df5 | 249 | else if (myoptind < argc) { |
sPymbed | 0:c4152c628df5 | 250 | myoptarg = argv[myoptind]; |
sPymbed | 0:c4152c628df5 | 251 | myoptind++; |
sPymbed | 0:c4152c628df5 | 252 | } |
sPymbed | 0:c4152c628df5 | 253 | else |
sPymbed | 0:c4152c628df5 | 254 | return '?'; |
sPymbed | 0:c4152c628df5 | 255 | } |
sPymbed | 0:c4152c628df5 | 256 | |
sPymbed | 0:c4152c628df5 | 257 | return c; |
sPymbed | 0:c4152c628df5 | 258 | } |
sPymbed | 0:c4152c628df5 | 259 | |
sPymbed | 0:c4152c628df5 | 260 | |
sPymbed | 0:c4152c628df5 | 261 | #ifdef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 262 | #pragma warning(push) |
sPymbed | 0:c4152c628df5 | 263 | #pragma warning(disable:4996) |
sPymbed | 0:c4152c628df5 | 264 | /* For Windows builds, disable compiler warnings for: |
sPymbed | 0:c4152c628df5 | 265 | * - 4996: deprecated function */ |
sPymbed | 0:c4152c628df5 | 266 | #endif |
sPymbed | 0:c4152c628df5 | 267 | |
sPymbed | 0:c4152c628df5 | 268 | static INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer, |
sPymbed | 0:c4152c628df5 | 269 | word16 port) |
sPymbed | 0:c4152c628df5 | 270 | { |
sPymbed | 0:c4152c628df5 | 271 | int useLookup = 0; |
sPymbed | 0:c4152c628df5 | 272 | (void)useLookup; |
sPymbed | 0:c4152c628df5 | 273 | |
sPymbed | 0:c4152c628df5 | 274 | memset(addr, 0, sizeof(SOCKADDR_IN_T)); |
sPymbed | 0:c4152c628df5 | 275 | |
sPymbed | 0:c4152c628df5 | 276 | #ifndef TEST_IPV6 |
sPymbed | 0:c4152c628df5 | 277 | /* peer could be in human readable form */ |
sPymbed | 0:c4152c628df5 | 278 | if ( ((size_t)peer != INADDR_ANY) && isalpha((int)peer[0])) { |
sPymbed | 0:c4152c628df5 | 279 | #ifdef CYASSL_MDK_ARM |
sPymbed | 0:c4152c628df5 | 280 | int err; |
sPymbed | 0:c4152c628df5 | 281 | struct hostent* entry = gethostbyname(peer, &err); |
sPymbed | 0:c4152c628df5 | 282 | #else |
sPymbed | 0:c4152c628df5 | 283 | struct hostent* entry = gethostbyname(peer); |
sPymbed | 0:c4152c628df5 | 284 | #endif |
sPymbed | 0:c4152c628df5 | 285 | |
sPymbed | 0:c4152c628df5 | 286 | if (entry) { |
sPymbed | 0:c4152c628df5 | 287 | memcpy(&addr->sin_addr.s_addr, entry->h_addr_list[0], |
sPymbed | 0:c4152c628df5 | 288 | entry->h_length); |
sPymbed | 0:c4152c628df5 | 289 | useLookup = 1; |
sPymbed | 0:c4152c628df5 | 290 | } |
sPymbed | 0:c4152c628df5 | 291 | else |
sPymbed | 0:c4152c628df5 | 292 | err_sys("no entry for host"); |
sPymbed | 0:c4152c628df5 | 293 | } |
sPymbed | 0:c4152c628df5 | 294 | #endif |
sPymbed | 0:c4152c628df5 | 295 | |
sPymbed | 0:c4152c628df5 | 296 | #ifndef TEST_IPV6 |
sPymbed | 0:c4152c628df5 | 297 | #if defined(CYASSL_MDK_ARM) |
sPymbed | 0:c4152c628df5 | 298 | addr->sin_family = PF_INET; |
sPymbed | 0:c4152c628df5 | 299 | #else |
sPymbed | 0:c4152c628df5 | 300 | addr->sin_family = AF_INET_V; |
sPymbed | 0:c4152c628df5 | 301 | #endif |
sPymbed | 0:c4152c628df5 | 302 | addr->sin_port = htons(port); |
sPymbed | 0:c4152c628df5 | 303 | if ((size_t)peer == INADDR_ANY) |
sPymbed | 0:c4152c628df5 | 304 | addr->sin_addr.s_addr = INADDR_ANY; |
sPymbed | 0:c4152c628df5 | 305 | else { |
sPymbed | 0:c4152c628df5 | 306 | if (!useLookup) |
sPymbed | 0:c4152c628df5 | 307 | addr->sin_addr.s_addr = inet_addr(peer); |
sPymbed | 0:c4152c628df5 | 308 | } |
sPymbed | 0:c4152c628df5 | 309 | #else |
sPymbed | 0:c4152c628df5 | 310 | addr->sin6_family = AF_INET_V; |
sPymbed | 0:c4152c628df5 | 311 | addr->sin6_port = htons(port); |
sPymbed | 0:c4152c628df5 | 312 | if ((size_t)peer == INADDR_ANY) |
sPymbed | 0:c4152c628df5 | 313 | addr->sin6_addr = in6addr_any; |
sPymbed | 0:c4152c628df5 | 314 | else { |
sPymbed | 0:c4152c628df5 | 315 | #ifdef HAVE_GETADDRINFO |
sPymbed | 0:c4152c628df5 | 316 | struct addrinfo hints; |
sPymbed | 0:c4152c628df5 | 317 | struct addrinfo* answer = NULL; |
sPymbed | 0:c4152c628df5 | 318 | int ret; |
sPymbed | 0:c4152c628df5 | 319 | char strPort[80]; |
sPymbed | 0:c4152c628df5 | 320 | |
sPymbed | 0:c4152c628df5 | 321 | memset(&hints, 0, sizeof(hints)); |
sPymbed | 0:c4152c628df5 | 322 | |
sPymbed | 0:c4152c628df5 | 323 | hints.ai_family = AF_INET_V; |
sPymbed | 0:c4152c628df5 | 324 | hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM; |
sPymbed | 0:c4152c628df5 | 325 | hints.ai_protocol = udp ? IPPROTO_UDP : IPPROTO_TCP; |
sPymbed | 0:c4152c628df5 | 326 | |
sPymbed | 0:c4152c628df5 | 327 | WSNPRINTF(strPort, sizeof(strPort), "%d", port); |
sPymbed | 0:c4152c628df5 | 328 | strPort[79] = '\0'; |
sPymbed | 0:c4152c628df5 | 329 | |
sPymbed | 0:c4152c628df5 | 330 | ret = getaddrinfo(peer, strPort, &hints, &answer); |
sPymbed | 0:c4152c628df5 | 331 | if (ret < 0 || answer == NULL) |
sPymbed | 0:c4152c628df5 | 332 | err_sys("getaddrinfo failed"); |
sPymbed | 0:c4152c628df5 | 333 | |
sPymbed | 0:c4152c628df5 | 334 | memcpy(addr, answer->ai_addr, answer->ai_addrlen); |
sPymbed | 0:c4152c628df5 | 335 | freeaddrinfo(answer); |
sPymbed | 0:c4152c628df5 | 336 | #else |
sPymbed | 0:c4152c628df5 | 337 | printf("no ipv6 getaddrinfo, loopback only tests/examples\n"); |
sPymbed | 0:c4152c628df5 | 338 | addr->sin6_addr = in6addr_loopback; |
sPymbed | 0:c4152c628df5 | 339 | #endif |
sPymbed | 0:c4152c628df5 | 340 | } |
sPymbed | 0:c4152c628df5 | 341 | #endif |
sPymbed | 0:c4152c628df5 | 342 | } |
sPymbed | 0:c4152c628df5 | 343 | |
sPymbed | 0:c4152c628df5 | 344 | #ifdef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 345 | #pragma warning(pop) |
sPymbed | 0:c4152c628df5 | 346 | #endif |
sPymbed | 0:c4152c628df5 | 347 | |
sPymbed | 0:c4152c628df5 | 348 | |
sPymbed | 0:c4152c628df5 | 349 | static INLINE void tcp_socket(SOCKET_T* sockFd) |
sPymbed | 0:c4152c628df5 | 350 | { |
sPymbed | 0:c4152c628df5 | 351 | *sockFd = socket(AF_INET_V, SOCK_STREAM, 0); |
sPymbed | 0:c4152c628df5 | 352 | |
sPymbed | 0:c4152c628df5 | 353 | #ifdef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 354 | if (*sockFd == INVALID_SOCKET) |
sPymbed | 0:c4152c628df5 | 355 | err_sys("socket failed\n"); |
sPymbed | 0:c4152c628df5 | 356 | #else |
sPymbed | 0:c4152c628df5 | 357 | if (*sockFd < 0) |
sPymbed | 0:c4152c628df5 | 358 | err_sys("socket failed\n"); |
sPymbed | 0:c4152c628df5 | 359 | #endif |
sPymbed | 0:c4152c628df5 | 360 | |
sPymbed | 0:c4152c628df5 | 361 | #ifndef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 362 | #ifdef SO_NOSIGPIPE |
sPymbed | 0:c4152c628df5 | 363 | { |
sPymbed | 0:c4152c628df5 | 364 | int on = 1; |
sPymbed | 0:c4152c628df5 | 365 | socklen_t len = sizeof(on); |
sPymbed | 0:c4152c628df5 | 366 | int res = setsockopt(*sockFd, SOL_SOCKET, SO_NOSIGPIPE, &on, len); |
sPymbed | 0:c4152c628df5 | 367 | if (res < 0) |
sPymbed | 0:c4152c628df5 | 368 | err_sys("setsockopt SO_NOSIGPIPE failed\n"); |
sPymbed | 0:c4152c628df5 | 369 | } |
sPymbed | 0:c4152c628df5 | 370 | #elif defined(CYASSL_MDK_ARM) |
sPymbed | 0:c4152c628df5 | 371 | /* nothing to define */ |
sPymbed | 0:c4152c628df5 | 372 | #else /* no S_NOSIGPIPE */ |
sPymbed | 0:c4152c628df5 | 373 | // signal(SIGPIPE, SIG_IGN); // FIX: ESP32 |
sPymbed | 0:c4152c628df5 | 374 | #endif /* S_NOSIGPIPE */ |
sPymbed | 0:c4152c628df5 | 375 | |
sPymbed | 0:c4152c628df5 | 376 | #if defined(TCP_NODELAY) |
sPymbed | 0:c4152c628df5 | 377 | { |
sPymbed | 0:c4152c628df5 | 378 | int on = 1; |
sPymbed | 0:c4152c628df5 | 379 | socklen_t len = sizeof(on); |
sPymbed | 0:c4152c628df5 | 380 | int res = setsockopt(*sockFd, IPPROTO_TCP, TCP_NODELAY, &on, len); |
sPymbed | 0:c4152c628df5 | 381 | if (res < 0) |
sPymbed | 0:c4152c628df5 | 382 | err_sys("setsockopt TCP_NODELAY failed\n"); |
sPymbed | 0:c4152c628df5 | 383 | } |
sPymbed | 0:c4152c628df5 | 384 | #endif |
sPymbed | 0:c4152c628df5 | 385 | #endif /* USE_WINDOWS_API */ |
sPymbed | 0:c4152c628df5 | 386 | } |
sPymbed | 0:c4152c628df5 | 387 | |
sPymbed | 0:c4152c628df5 | 388 | |
sPymbed | 0:c4152c628df5 | 389 | #ifndef XNTOHS |
sPymbed | 0:c4152c628df5 | 390 | #define XNTOHS(a) ntohs((a)) |
sPymbed | 0:c4152c628df5 | 391 | #endif |
sPymbed | 0:c4152c628df5 | 392 | |
sPymbed | 0:c4152c628df5 | 393 | |
sPymbed | 0:c4152c628df5 | 394 | static INLINE void tcp_listen(SOCKET_T* sockfd, word16* port, int useAnyAddr) |
sPymbed | 0:c4152c628df5 | 395 | { |
sPymbed | 0:c4152c628df5 | 396 | SOCKADDR_IN_T addr; |
sPymbed | 0:c4152c628df5 | 397 | |
sPymbed | 0:c4152c628df5 | 398 | /* don't use INADDR_ANY by default, firewall may block, make user switch |
sPymbed | 0:c4152c628df5 | 399 | on */ |
sPymbed | 0:c4152c628df5 | 400 | build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSshIp), *port); |
sPymbed | 0:c4152c628df5 | 401 | tcp_socket(sockfd); |
sPymbed | 0:c4152c628df5 | 402 | |
sPymbed | 0:c4152c628df5 | 403 | #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM)\ |
sPymbed | 0:c4152c628df5 | 404 | && !defined(WOLFSSL_KEIL_TCP_NET) |
sPymbed | 0:c4152c628df5 | 405 | { |
sPymbed | 0:c4152c628df5 | 406 | int res, on = 1; |
sPymbed | 0:c4152c628df5 | 407 | socklen_t len = sizeof(on); |
sPymbed | 0:c4152c628df5 | 408 | res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len); |
sPymbed | 0:c4152c628df5 | 409 | if (res < 0) |
sPymbed | 0:c4152c628df5 | 410 | err_sys("setsockopt SO_REUSEADDR failed\n"); |
sPymbed | 0:c4152c628df5 | 411 | } |
sPymbed | 0:c4152c628df5 | 412 | #endif |
sPymbed | 0:c4152c628df5 | 413 | |
sPymbed | 0:c4152c628df5 | 414 | |
sPymbed | 0:c4152c628df5 | 415 | if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) |
sPymbed | 0:c4152c628df5 | 416 | err_sys("tcp bind failed"); |
sPymbed | 0:c4152c628df5 | 417 | if (listen(*sockfd, 5) != 0) |
sPymbed | 0:c4152c628df5 | 418 | err_sys("tcp listen failed"); |
sPymbed | 0:c4152c628df5 | 419 | #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS) |
sPymbed | 0:c4152c628df5 | 420 | if (*port == 0) { |
sPymbed | 0:c4152c628df5 | 421 | socklen_t len = sizeof(addr); |
sPymbed | 0:c4152c628df5 | 422 | if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) { |
sPymbed | 0:c4152c628df5 | 423 | #ifndef TEST_IPV6 |
sPymbed | 0:c4152c628df5 | 424 | *port = XNTOHS(addr.sin_port); |
sPymbed | 0:c4152c628df5 | 425 | #else |
sPymbed | 0:c4152c628df5 | 426 | *port = XNTOHS(addr.sin6_port); |
sPymbed | 0:c4152c628df5 | 427 | #endif |
sPymbed | 0:c4152c628df5 | 428 | } |
sPymbed | 0:c4152c628df5 | 429 | } |
sPymbed | 0:c4152c628df5 | 430 | #endif |
sPymbed | 0:c4152c628df5 | 431 | } |
sPymbed | 0:c4152c628df5 | 432 | |
sPymbed | 0:c4152c628df5 | 433 | |
sPymbed | 0:c4152c628df5 | 434 | /* Wolf Root Directory Helper */ |
sPymbed | 0:c4152c628df5 | 435 | /* KEIL-RL File System does not support relative directory */ |
sPymbed | 0:c4152c628df5 | 436 | #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) |
sPymbed | 0:c4152c628df5 | 437 | /* Maximum depth to search for WolfSSL root */ |
sPymbed | 0:c4152c628df5 | 438 | #define MAX_WOLF_ROOT_DEPTH 5 |
sPymbed | 0:c4152c628df5 | 439 | |
sPymbed | 0:c4152c628df5 | 440 | static INLINE int ChangeToWolfSshRoot(void) |
sPymbed | 0:c4152c628df5 | 441 | { |
sPymbed | 0:c4152c628df5 | 442 | #if !defined(NO_FILESYSTEM) |
sPymbed | 0:c4152c628df5 | 443 | int depth, res; |
sPymbed | 0:c4152c628df5 | 444 | WFILE* file; |
sPymbed | 0:c4152c628df5 | 445 | for(depth = 0; depth <= MAX_WOLF_ROOT_DEPTH; depth++) { |
sPymbed | 0:c4152c628df5 | 446 | if (WFOPEN(&file, serverKeyRsaPemFile, "rb") == 0) { |
sPymbed | 0:c4152c628df5 | 447 | WFCLOSE(file); |
sPymbed | 0:c4152c628df5 | 448 | return depth; |
sPymbed | 0:c4152c628df5 | 449 | } |
sPymbed | 0:c4152c628df5 | 450 | #ifdef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 451 | res = SetCurrentDirectoryA("..\\"); |
sPymbed | 0:c4152c628df5 | 452 | #else |
sPymbed | 0:c4152c628df5 | 453 | res = chdir("../"); |
sPymbed | 0:c4152c628df5 | 454 | #endif |
sPymbed | 0:c4152c628df5 | 455 | if (res < 0) { |
sPymbed | 0:c4152c628df5 | 456 | printf("chdir to ../ failed!\n"); |
sPymbed | 0:c4152c628df5 | 457 | break; |
sPymbed | 0:c4152c628df5 | 458 | } |
sPymbed | 0:c4152c628df5 | 459 | } |
sPymbed | 0:c4152c628df5 | 460 | |
sPymbed | 0:c4152c628df5 | 461 | err_sys("wolfSSH root not found"); |
sPymbed | 0:c4152c628df5 | 462 | return -1; |
sPymbed | 0:c4152c628df5 | 463 | #else |
sPymbed | 0:c4152c628df5 | 464 | return 0; |
sPymbed | 0:c4152c628df5 | 465 | #endif |
sPymbed | 0:c4152c628df5 | 466 | } |
sPymbed | 0:c4152c628df5 | 467 | #endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOL |
sPymbed | 0:c4152c628df5 | 468 | FSSL_TIRTOS) */ |
sPymbed | 0:c4152c628df5 | 469 | |
sPymbed | 0:c4152c628df5 | 470 | |
sPymbed | 0:c4152c628df5 | 471 | #endif /* _WOLFSSH_TEST_H_ */ |