From 4f891836757df87be04aef2f55ac04da3ac3f844 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 11 Mar 2024 12:30:04 -0300 Subject: [PATCH] refactor --- mongoose.c | 9 +++++---- mongoose.h | 13 +++++++------ src/config.h | 12 ++++++------ src/net.c | 6 ++---- src/net_builtin.c | 3 +++ src/net_builtin.h | 1 + 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/mongoose.c b/mongoose.c index 40642934154..e905db76e20 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4869,16 +4869,14 @@ void mg_mgr_init(struct mg_mgr *mgr) { // Ignore SIGPIPE signal, so if client cancels the request, it // won't kill the whole process. signal(SIGPIPE, SIG_IGN); +#elif MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT + mg_tcpip_auto_init(); #endif mgr->pipe = MG_INVALID_SOCKET; mgr->dnstimeout = 3000; mgr->dns4.url = "udp://8.8.8.8:53"; mgr->dns6.url = "udp://[2001:4860:4860::8888]:53"; mg_tls_ctx_init(mgr); - -#if (MG_ENABLE_DRIVER_INIT == 1) && defined(MG_TCPIP_DRIVER_INIT) - MG_TCPIP_DRIVER_INIT(mgr); -#endif } #ifdef MG_ENABLE_LINES @@ -6011,6 +6009,9 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) { } return res; } + +void mg_tcpip_auto_init(void) {} + #endif // MG_ENABLE_TCPIP #ifdef MG_ENABLE_LINES diff --git a/mongoose.h b/mongoose.h index b7ef4d91621..555cc1ca958 100644 --- a/mongoose.h +++ b/mongoose.h @@ -833,20 +833,20 @@ struct timeval { #define MG_ENABLE_PROFILE 0 #endif -#ifndef MG_ENABLE_DRIVER_INIT -#define MG_ENABLE_DRIVER_INIT 1 -#endif +#ifndef MG_ENABLE_TCPIP_DRIVER_INIT // mg_mgr_init() will also initialize +#define MG_ENABLE_TCPIP_DRIVER_INIT 0 // enabled built-in driver for +#endif // Mongoose built-in network stack #ifndef MG_TCPIP_IP -#define MG_TCPIP_IP 0 +#define MG_TCPIP_IP mg_htonl(MG_U32(0, 0, 0, 0)) #endif #ifndef MG_TCPIP_MASK -#define MG_TCPIP_MASK 0 +#define MG_TCPIP_MASK mg_htonl(MG_U32(255, 255, 255, 0)) #endif #ifndef MG_TCPIP_GW -#define MG_TCPIP_GW 0 +#define MG_TCPIP_GW mg_htonl(MG_U32(0, 0, 0, 1)) #endif #define MG_MAC_ADDRESS_RANDOM \ @@ -2786,6 +2786,7 @@ struct mg_tcpip_if { #define MG_TCPIP_STATE_READY 3 // Interface is up and has an IP assigned }; +void mg_tcpip_auto_init(void); void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *); void mg_tcpip_free(struct mg_tcpip_if *); void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp); diff --git a/src/config.h b/src/config.h index 21de0d365e9..0349d4f31a2 100644 --- a/src/config.h +++ b/src/config.h @@ -158,20 +158,20 @@ #define MG_ENABLE_PROFILE 0 #endif -#ifndef MG_ENABLE_DRIVER_INIT -#define MG_ENABLE_DRIVER_INIT 1 -#endif +#ifndef MG_ENABLE_TCPIP_DRIVER_INIT // mg_mgr_init() will also initialize +#define MG_ENABLE_TCPIP_DRIVER_INIT 0 // enabled built-in driver for +#endif // Mongoose built-in network stack #ifndef MG_TCPIP_IP -#define MG_TCPIP_IP 0 +#define MG_TCPIP_IP mg_htonl(MG_U32(0, 0, 0, 0)) #endif #ifndef MG_TCPIP_MASK -#define MG_TCPIP_MASK 0 +#define MG_TCPIP_MASK mg_htonl(MG_U32(255, 255, 255, 0)) #endif #ifndef MG_TCPIP_GW -#define MG_TCPIP_GW 0 +#define MG_TCPIP_GW mg_htonl(MG_U32(0, 0, 0, 1)) #endif #define MG_MAC_ADDRESS_RANDOM \ diff --git a/src/net.c b/src/net.c index 078fa28d44b..b5a714753ca 100644 --- a/src/net.c +++ b/src/net.c @@ -270,14 +270,12 @@ void mg_mgr_init(struct mg_mgr *mgr) { // Ignore SIGPIPE signal, so if client cancels the request, it // won't kill the whole process. signal(SIGPIPE, SIG_IGN); +#elif MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT + mg_tcpip_auto_init(); #endif mgr->pipe = MG_INVALID_SOCKET; mgr->dnstimeout = 3000; mgr->dns4.url = "udp://8.8.8.8:53"; mgr->dns6.url = "udp://[2001:4860:4860::8888]:53"; mg_tls_ctx_init(mgr); - -#if (MG_ENABLE_DRIVER_INIT == 1) && defined(MG_TCPIP_DRIVER_INIT) - MG_TCPIP_DRIVER_INIT(mgr); -#endif } diff --git a/src/net_builtin.c b/src/net_builtin.c index f5379df688f..d6c56b624f4 100644 --- a/src/net_builtin.c +++ b/src/net_builtin.c @@ -1125,4 +1125,7 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) { } return res; } + +void mg_tcpip_auto_init(void) {} + #endif // MG_ENABLE_TCPIP diff --git a/src/net_builtin.h b/src/net_builtin.h index b5ef0c938f5..8900e63f27c 100644 --- a/src/net_builtin.h +++ b/src/net_builtin.h @@ -48,6 +48,7 @@ struct mg_tcpip_if { #define MG_TCPIP_STATE_READY 3 // Interface is up and has an IP assigned }; +void mg_tcpip_auto_init(void); void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *); void mg_tcpip_free(struct mg_tcpip_if *); void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp);