From 2594e7b37accb9c97ad374000fcadfcbe3af459e Mon Sep 17 00:00:00 2001 From: cpq Date: Sat, 13 Mar 2021 12:34:26 +0000 Subject: [PATCH] Stricter MG_ARCH_CUSTOM and type conversions in sock.c --- mongoose.c | 36 +++++++--- mongoose.h | 157 ++++++++++++++++++++++++++++++-------------- src/arch.h | 28 ++------ src/arch_esp32.h | 12 ++++ src/arch_esp8266.h | 16 ++++- src/arch_freertos.h | 14 ++++ src/arch_unix.h | 13 ++++ src/arch_win32.h | 13 ++++ src/config.h | 9 ++- src/net.c | 2 +- src/sock.c | 34 +++++++--- 11 files changed, 238 insertions(+), 96 deletions(-) diff --git a/mongoose.c b/mongoose.c index 0f48089a..e61450c9 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2236,7 +2236,7 @@ void mg_mgr_free(struct mg_mgr *mgr) { } void mg_mgr_init(struct mg_mgr *mgr) { -#ifdef _WIN32 +#if defined(_WIN32) && MG_ENABLE_WINSOCK WSADATA data; WSAStartup(MAKEWORD(2, 2), &data); #elif MG_ARCH == MG_ARCH_FREERTOS @@ -2601,25 +2601,41 @@ struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url, #if MG_ENABLE_SOCKET -#if defined(_WIN32) +#if defined(_WIN32) && MG_ENABLE_WINSOCK #define MG_SOCK_ERRNO WSAGetLastError() -#define FD(C_) ((SOCKET)(C_)->fd) #ifndef SO_EXCLUSIVEADDRUSE #define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR)) #endif #elif MG_ARCH == MG_ARCH_FREERTOS #define MG_SOCK_ERRNO errno typedef Socket_t SOCKET; -#define FD(C_) ((long) (C_)->fd) #define INVALID_SOCKET FREERTOS_INVALID_SOCKET #else #define MG_SOCK_ERRNO errno +#ifndef closesocket #define closesocket(x) close(x) +#endif #define INVALID_SOCKET (-1) typedef int SOCKET; -#define FD(C_) ((SOCKET)(long) (C_)->fd) #endif +union _su { + SOCKET s; + void *ptr; +}; +#define FD(c_) ptr2sock((c_)->fd) + +static SOCKET ptr2sock(void *ptr) { + union _su u = {0}; + u.ptr = ptr; + return u.s; +} + +static void *sock2ptr(SOCKET s) { + union _su u = {s}; + return u.ptr; +} + #ifndef MSG_NONBLOCKING #define MSG_NONBLOCKING 0 #endif @@ -2654,7 +2670,7 @@ static int mg_sock_failed(void) { #ifndef WINCE && err != EAGAIN && err != EINTR #endif -#ifdef _WIN32 +#if defined(_WIN32) && MG_ENABLE_WINSOCK && err != WSAEINTR && err != WSAEWOULDBLOCK #endif ; @@ -2665,7 +2681,7 @@ static struct mg_connection *alloc_conn(struct mg_mgr *mgr, int is_client, struct mg_connection *c = (struct mg_connection *) calloc(1, sizeof(*c)); if (c != NULL) { c->is_client = is_client; - c->fd = (void *) (long) fd; + c->fd = sock2ptr(fd); c->mgr = mgr; c->id = ++mgr->nextid; } @@ -2757,7 +2773,7 @@ int mg_send(struct mg_connection *c, const void *buf, size_t len) { } static void mg_set_non_blocking_mode(SOCKET fd) { -#ifdef _WIN32 +#if defined(_WIN32) && MG_ENABLE_WINSOCK unsigned long on = 1; ioctlsocket(fd, FIONBIO, &on); #elif MG_ARCH == MG_ARCH_FREERTOS @@ -2912,7 +2928,7 @@ void mg_connect_resolved(struct mg_connection *c) { if (c->peer.is_ip6) af = AF_INET6; #endif mg_straddr(c, buf, sizeof(buf)); - c->fd = (void *) (long) socket(af, type, 0); + c->fd = sock2ptr(socket(af, type, 0)); if (FD(c) == INVALID_SOCKET) { mg_error(c, "socket(): %d", MG_SOCK_ERRNO); return; @@ -3049,7 +3065,7 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, LOG(LL_ERROR, ("OOM %s", url)); closesocket(fd); } else { - c->fd = (void *) (long) fd; + c->fd = sock2ptr(fd); c->is_listening = 1; c->is_udp = is_udp; setsockopts(c); diff --git a/mongoose.h b/mongoose.h index 864eb7cc..95f3937f 100644 --- a/mongoose.h +++ b/mongoose.h @@ -82,7 +82,80 @@ #define _CRT_SECURE_NO_WARNINGS #define _WINSOCK_DEPRECATED_NO_WARNINGS -// Standard C headers +#if !defined(PRINTF_LIKE) +#if defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__) +#define PRINTF_LIKE(f, a) __attribute__((format(printf, f, a))) +#else +#define PRINTF_LIKE(f, a) +#endif +#endif + +#if MG_ARCH == MG_ARCH_CUSTOM +#include +#endif + + + + + + + + +#if MG_ARCH == MG_ARCH_ESP32 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MG_DIRSEP '/' +#define MG_INT64_FMT "%lld" +#ifndef MG_PATH_MAX +#define MG_PATH_MAX 128 +#endif + +#endif + + +#if MG_ARCH == MG_ARCH_ESP8266 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define MG_DIRSEP '/' +#define MG_INT64_FMT "%lld" + +#endif + + +#if MG_ARCH == MG_ARCH_FREERTOS + #include #include #include @@ -96,53 +169,6 @@ #include #include -#if MG_ARCH == MG_ARCH_CUSTOM -#include -#endif - - - - - - - -#if !defined(PRINTF_LIKE) -#if defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__) -#define PRINTF_LIKE(f, a) __attribute__((format(printf, f, a))) -#else -#define PRINTF_LIKE(f, a) -#endif -#endif - - -#if MG_ARCH == MG_ARCH_ESP32 - -#include -#include -#include -#define MG_DIRSEP '/' -#define MG_INT64_FMT "%lld" -#ifndef MG_PATH_MAX -#define MG_PATH_MAX 128 -#endif - -#endif - - -#if MG_ARCH == MG_ARCH_ESP8266 - -#include -#include -#include -#include -#include -#define MG_DIRSEP '/' -#define MG_INT64_FMT "%lld" - -#endif - - -#if MG_ARCH == MG_ARCH_FREERTOS #include #include @@ -211,17 +237,30 @@ static inline int ff_vfprintf(FF_FILE *fp, const char *fmt, va_list ap) { #define _DARWIN_UNLIMITED_SELECT 1 #include +#include #include +#include +#include #include +#include #include #include #include +#include #include +#include #include +#include +#include +#include #include #include +#include #include +#include +#include #include + #define MG_DIRSEP '/' #define MG_ENABLE_POSIX 1 #define MG_INT64_FMT "%" PRId64 @@ -231,6 +270,19 @@ static inline int ff_vfprintf(FF_FILE *fp, const char *fmt, va_list ap) { #if MG_ARCH == MG_ARCH_WIN32 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #if defined(_MSC_VER) && _MSC_VER < 1700 #define __func__ "" typedef __int64 int64_t; @@ -290,9 +342,7 @@ typedef int socklen_t; #define MG_ENABLE_LWIP 0 #endif -#if MG_ENABLE_LWIP -#define MG_ENABLE_SOCKET 0 -#elif !defined(MG_ENABLE_SOCKET) +#ifndef MG_ENABLE_SOCKET #define MG_ENABLE_SOCKET 1 #endif @@ -328,6 +378,11 @@ typedef int socklen_t; #define MG_ENABLE_MD5 0 #endif +// Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP) +#ifndef MG_ENABLE_WINSOCK +#define MG_ENABLE_WINSOCK 1 +#endif + #ifndef MG_ENABLE_DIRECTORY_LISTING #define MG_ENABLE_DIRECTORY_LISTING 0 #endif diff --git a/src/arch.h b/src/arch.h index 18221626..f023894b 100644 --- a/src/arch.h +++ b/src/arch.h @@ -64,19 +64,13 @@ #define _CRT_SECURE_NO_WARNINGS #define _WINSOCK_DEPRECATED_NO_WARNINGS -// Standard C headers -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#if !defined(PRINTF_LIKE) +#if defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__) +#define PRINTF_LIKE(f, a) __attribute__((format(printf, f, a))) +#else +#define PRINTF_LIKE(f, a) +#endif +#endif #if MG_ARCH == MG_ARCH_CUSTOM #include @@ -87,11 +81,3 @@ #include "arch_freertos.h" #include "arch_unix.h" #include "arch_win32.h" - -#if !defined(PRINTF_LIKE) -#if defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__) -#define PRINTF_LIKE(f, a) __attribute__((format(printf, f, a))) -#else -#define PRINTF_LIKE(f, a) -#endif -#endif diff --git a/src/arch_esp32.h b/src/arch_esp32.h index 3ad98e8f..c85c5b78 100644 --- a/src/arch_esp32.h +++ b/src/arch_esp32.h @@ -2,9 +2,21 @@ #if MG_ARCH == MG_ARCH_ESP32 +#include #include +#include +#include +#include #include +#include +#include +#include +#include +#include #include +#include +#include + #define MG_DIRSEP '/' #define MG_INT64_FMT "%lld" #ifndef MG_PATH_MAX diff --git a/src/arch_esp8266.h b/src/arch_esp8266.h index 18834717..294a353d 100644 --- a/src/arch_esp8266.h +++ b/src/arch_esp8266.h @@ -2,11 +2,25 @@ #if MG_ARCH == MG_ARCH_ESP8266 +#include #include -#include +#include +#include +#include #include +#include #include +#include +#include +#include +#include +#include #include +#include +#include + +#include + #define MG_DIRSEP '/' #define MG_INT64_FMT "%lld" diff --git a/src/arch_freertos.h b/src/arch_freertos.h index 64fbb9ac..1048cd19 100644 --- a/src/arch_freertos.h +++ b/src/arch_freertos.h @@ -1,6 +1,20 @@ #pragma once #if MG_ARCH == MG_ARCH_FREERTOS + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include #include diff --git a/src/arch_unix.h b/src/arch_unix.h index b3dbb352..773bddaa 100644 --- a/src/arch_unix.h +++ b/src/arch_unix.h @@ -5,17 +5,30 @@ #define _DARWIN_UNLIMITED_SELECT 1 #include +#include #include +#include +#include #include +#include #include #include #include +#include #include +#include #include +#include +#include +#include #include #include +#include #include +#include +#include #include + #define MG_DIRSEP '/' #define MG_ENABLE_POSIX 1 #define MG_INT64_FMT "%" PRId64 diff --git a/src/arch_win32.h b/src/arch_win32.h index 45d341e3..45d50fcc 100644 --- a/src/arch_win32.h +++ b/src/arch_win32.h @@ -2,6 +2,19 @@ #if MG_ARCH == MG_ARCH_WIN32 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #if defined(_MSC_VER) && _MSC_VER < 1700 #define __func__ "" typedef __int64 int64_t; diff --git a/src/config.h b/src/config.h index 891bc333..4cbe49a7 100644 --- a/src/config.h +++ b/src/config.h @@ -4,9 +4,7 @@ #define MG_ENABLE_LWIP 0 #endif -#if MG_ENABLE_LWIP -#define MG_ENABLE_SOCKET 0 -#elif !defined(MG_ENABLE_SOCKET) +#ifndef MG_ENABLE_SOCKET #define MG_ENABLE_SOCKET 1 #endif @@ -42,6 +40,11 @@ #define MG_ENABLE_MD5 0 #endif +// Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP) +#ifndef MG_ENABLE_WINSOCK +#define MG_ENABLE_WINSOCK 1 +#endif + #ifndef MG_ENABLE_DIRECTORY_LISTING #define MG_ENABLE_DIRECTORY_LISTING 0 #endif diff --git a/src/net.c b/src/net.c index 538f5c62..e8b33a7d 100644 --- a/src/net.c +++ b/src/net.c @@ -122,7 +122,7 @@ void mg_mgr_free(struct mg_mgr *mgr) { } void mg_mgr_init(struct mg_mgr *mgr) { -#ifdef _WIN32 +#if defined(_WIN32) && MG_ENABLE_WINSOCK WSADATA data; WSAStartup(MAKEWORD(2, 2), &data); #elif MG_ARCH == MG_ARCH_FREERTOS diff --git a/src/sock.c b/src/sock.c index 031de008..d02c8a76 100644 --- a/src/sock.c +++ b/src/sock.c @@ -10,25 +10,41 @@ #include "util.h" #if MG_ENABLE_SOCKET -#if defined(_WIN32) +#if defined(_WIN32) && MG_ENABLE_WINSOCK #define MG_SOCK_ERRNO WSAGetLastError() -#define FD(C_) ((SOCKET)(C_)->fd) #ifndef SO_EXCLUSIVEADDRUSE #define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR)) #endif #elif MG_ARCH == MG_ARCH_FREERTOS #define MG_SOCK_ERRNO errno typedef Socket_t SOCKET; -#define FD(C_) ((long) (C_)->fd) #define INVALID_SOCKET FREERTOS_INVALID_SOCKET #else #define MG_SOCK_ERRNO errno +#ifndef closesocket #define closesocket(x) close(x) +#endif #define INVALID_SOCKET (-1) typedef int SOCKET; -#define FD(C_) ((SOCKET)(long) (C_)->fd) #endif +union _su { + SOCKET s; + void *ptr; +}; +#define FD(c_) ptr2sock((c_)->fd) + +static SOCKET ptr2sock(void *ptr) { + union _su u = {0}; + u.ptr = ptr; + return u.s; +} + +static void *sock2ptr(SOCKET s) { + union _su u = {s}; + return u.ptr; +} + #ifndef MSG_NONBLOCKING #define MSG_NONBLOCKING 0 #endif @@ -63,7 +79,7 @@ static int mg_sock_failed(void) { #ifndef WINCE && err != EAGAIN && err != EINTR #endif -#ifdef _WIN32 +#if defined(_WIN32) && MG_ENABLE_WINSOCK && err != WSAEINTR && err != WSAEWOULDBLOCK #endif ; @@ -74,7 +90,7 @@ static struct mg_connection *alloc_conn(struct mg_mgr *mgr, int is_client, struct mg_connection *c = (struct mg_connection *) calloc(1, sizeof(*c)); if (c != NULL) { c->is_client = is_client; - c->fd = (void *) (long) fd; + c->fd = sock2ptr(fd); c->mgr = mgr; c->id = ++mgr->nextid; } @@ -166,7 +182,7 @@ int mg_send(struct mg_connection *c, const void *buf, size_t len) { } static void mg_set_non_blocking_mode(SOCKET fd) { -#ifdef _WIN32 +#if defined(_WIN32) && MG_ENABLE_WINSOCK unsigned long on = 1; ioctlsocket(fd, FIONBIO, &on); #elif MG_ARCH == MG_ARCH_FREERTOS @@ -321,7 +337,7 @@ void mg_connect_resolved(struct mg_connection *c) { if (c->peer.is_ip6) af = AF_INET6; #endif mg_straddr(c, buf, sizeof(buf)); - c->fd = (void *) (long) socket(af, type, 0); + c->fd = sock2ptr(socket(af, type, 0)); if (FD(c) == INVALID_SOCKET) { mg_error(c, "socket(): %d", MG_SOCK_ERRNO); return; @@ -458,7 +474,7 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, LOG(LL_ERROR, ("OOM %s", url)); closesocket(fd); } else { - c->fd = (void *) (long) fd; + c->fd = sock2ptr(fd); c->is_listening = 1; c->is_udp = is_udp; setsockopts(c);