Stricter MG_ARCH_CUSTOM and type conversions in sock.c

This commit is contained in:
cpq 2021-03-13 12:34:26 +00:00
parent 4356b953ca
commit 2594e7b37a
11 changed files with 238 additions and 96 deletions

View File

@ -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);

View File

@ -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 <mongoose_custom.h>
#endif
#if MG_ARCH == MG_ARCH_ESP32
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#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 <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <esp_system.h>
#define MG_DIRSEP '/'
#define MG_INT64_FMT "%lld"
#endif
#if MG_ARCH == MG_ARCH_FREERTOS
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
@ -96,53 +169,6 @@
#include <sys/types.h>
#include <time.h>
#if MG_ARCH == MG_ARCH_CUSTOM
#include <mongoose_custom.h>
#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 <dirent.h>
#include <netdb.h>
#include <sys/stat.h>
#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 <dirent.h>
#include <esp_system.h>
#include <netdb.h>
#include <stdbool.h>
#include <sys/time.h>
#define MG_DIRSEP '/'
#define MG_INT64_FMT "%lld"
#endif
#if MG_ARCH == MG_ARCH_FREERTOS
#include <FreeRTOS.h>
#include <task.h>
@ -211,17 +237,30 @@ static inline int ff_vfprintf(FF_FILE *fp, const char *fmt, va_list ap) {
#define _DARWIN_UNLIMITED_SELECT 1
#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#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 <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#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

View File

@ -64,19 +64,13 @@
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
// Standard C headers
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.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
#if MG_ARCH == MG_ARCH_CUSTOM
#include <mongoose_custom.h>
@ -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

View File

@ -2,9 +2,21 @@
#if MG_ARCH == MG_ARCH_ESP32
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#define MG_DIRSEP '/'
#define MG_INT64_FMT "%lld"
#ifndef MG_PATH_MAX

View File

@ -2,11 +2,25 @@
#if MG_ARCH == MG_ARCH_ESP8266
#include <ctype.h>
#include <dirent.h>
#include <esp_system.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <esp_system.h>
#define MG_DIRSEP '/'
#define MG_INT64_FMT "%lld"

View File

@ -1,6 +1,20 @@
#pragma once
#if MG_ARCH == MG_ARCH_FREERTOS
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <FreeRTOS.h>
#include <task.h>

View File

@ -5,17 +5,30 @@
#define _DARWIN_UNLIMITED_SELECT 1
#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define MG_DIRSEP '/'
#define MG_ENABLE_POSIX 1
#define MG_INT64_FMT "%" PRId64

View File

@ -2,6 +2,19 @@
#if MG_ARCH == MG_ARCH_WIN32
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#if defined(_MSC_VER) && _MSC_VER < 1700
#define __func__ ""
typedef __int64 int64_t;

View File

@ -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

View File

@ -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

View File

@ -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);