From b0e4069bb7d1702f2bf8736fb24f1b3d1f4d2429 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Mon, 16 Dec 2024 14:22:13 +0000 Subject: [PATCH] mgr::priv -> mgr::ifp, use explicit type --- mongoose.c | 42 ++++++++++++++++++------------------------ mongoose.h | 9 +++------ src/net.h | 5 +++-- src/net_builtin.c | 42 ++++++++++++++++++------------------------ src/net_builtin.h | 5 +---- src/ota_stm32f.c | 9 +++------ 6 files changed, 46 insertions(+), 66 deletions(-) diff --git a/mongoose.c b/mongoose.c index fd1be671..d15df8c4 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4243,7 +4243,7 @@ static uint16_t ipcsum(const void *buf, size_t len) { } static void settmout(struct mg_connection *c, uint8_t type) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; struct connstate *s = (struct connstate *) (c + 1); unsigned n = type == MIP_TTYPE_ACK ? MIP_TCP_ACK_MS : type == MIP_TTYPE_ARP ? MIP_ARP_RESP_MS @@ -4668,7 +4668,7 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn, } static size_t trim_len(struct mg_connection *c, size_t len) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; size_t eth_h_len = 14, ip_max_h_len = 24, tcp_max_h_len = 60, udp_h_len = 8; size_t max_headers_len = eth_h_len + ip_max_h_len + (c->is_udp ? udp_h_len : tcp_max_h_len); @@ -4695,7 +4695,7 @@ static size_t trim_len(struct mg_connection *c, size_t len) { } long mg_io_send(struct mg_connection *c, const void *buf, size_t len) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; struct connstate *s = (struct connstate *) (c + 1); uint32_t dst_ip = *(uint32_t *) c->rem.ip; len = trim_len(c, len); @@ -4758,8 +4758,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { c->is_draining = 1; settmout(c, MIP_TTYPE_FIN); } - tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, flags, - c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), "", 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, flags, c->loc.port, c->rem.port, + mg_htonl(s->seq), mg_htonl(s->ack), "", 0); } else if (pkt->pay.len == 0) { // TODO(cpq): handle this peer's ACK } else if (seq != s->ack) { @@ -4768,9 +4768,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { MG_VERBOSE(("ignoring duplicate pkt")); } else { MG_VERBOSE(("SEQ != ACK: %x %x %x", seq, s->ack, ack)); - tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, TH_ACK, - c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), "", - 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_ACK, c->loc.port, c->rem.port, + mg_htonl(s->seq), mg_htonl(s->ack), "", 0); } } else if (io->size - io->len < pkt->pay.len && !mg_iobuf_resize(io, io->len + pkt->pay.len)) { @@ -4792,9 +4791,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { if (s->unacked > MIP_TCP_WIN / 2 && s->acked != s->ack) { // Send ACK immediately MG_VERBOSE(("%lu imm ACK %lu", c->id, s->acked)); - tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, TH_ACK, - c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, - 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_ACK, c->loc.port, c->rem.port, + mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); s->unacked = 0; s->acked = s->ack; if (s->ttype != MIP_TTYPE_KEEPALIVE) settmout(c, MIP_TTYPE_KEEPALIVE); @@ -5111,7 +5109,7 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) { ifp->recv_queue.size = ifp->driver->rx ? framesize : 8192; ifp->recv_queue.buf = (char *) calloc(1, ifp->recv_queue.size); ifp->timer_1000ms = mg_millis(); - mgr->priv = ifp; + mgr->ifp = ifp; ifp->mgr = mgr; ifp->mtu = MG_TCPIP_MTU_DEFAULT; mgr->extraconnsize = sizeof(struct connstate); @@ -5132,11 +5130,10 @@ void mg_tcpip_free(struct mg_tcpip_if *ifp) { static void send_syn(struct mg_connection *c) { struct connstate *s = (struct connstate *) (c + 1); uint32_t isn = mg_htonl((uint32_t) mg_ntohs(c->loc.port)); - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; uint32_t rem_ip; memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); - tx_tcp(ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0, NULL, - 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0, + NULL, 0); } static void mac_resolved(struct mg_connection *c) { @@ -5150,7 +5147,7 @@ static void mac_resolved(struct mg_connection *c) { } void mg_connect_resolved(struct mg_connection *c) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; uint32_t rem_ip; memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); c->is_resolving = 0; @@ -5206,12 +5203,10 @@ static void init_closure(struct mg_connection *c) { struct connstate *s = (struct connstate *) (c + 1); if (c->is_udp == false && c->is_listening == false && c->is_connecting == false) { // For TCP conns, - struct mg_tcpip_if *ifp = - (struct mg_tcpip_if *) c->mgr->priv; // send TCP FIN uint32_t rem_ip; memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); - tx_tcp(ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port, c->rem.port, - mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port, + c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); settmout(c, MIP_TTYPE_FIN); } } @@ -5228,12 +5223,11 @@ static bool can_write(struct mg_connection *c) { } void mg_mgr_poll(struct mg_mgr *mgr, int ms) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) mgr->priv; struct mg_connection *c, *tmp; uint64_t now = mg_millis(); mg_timer_poll(&mgr->timers, now); - if (ifp == NULL || ifp->driver == NULL) return; - mg_tcpip_poll(ifp, now); + if (mgr->ifp == NULL || mgr->ifp->driver == NULL) return; + mg_tcpip_poll(mgr->ifp, now); for (c = mgr->conns; c != NULL; c = tmp) { tmp = c->next; struct connstate *s = (struct connstate *) (c + 1); @@ -5251,7 +5245,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { } bool mg_send(struct mg_connection *c, const void *buf, size_t len) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; bool res = false; uint32_t rem_ip; memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); diff --git a/mongoose.h b/mongoose.h index 769975f8..38aba8c9 100644 --- a/mongoose.h +++ b/mongoose.h @@ -2150,6 +2150,7 @@ enum { + struct mg_dns { const char *url; // DNS server URL struct mg_connection *c; // DNS server connection @@ -2176,8 +2177,8 @@ struct mg_mgr { void *active_dns_requests; // DNS requests in progress struct mg_timer *timers; // Active timers int epoll_fd; // Used when MG_EPOLL_ENABLE=1 - void *priv; // Used by the MIP stack - size_t extraconnsize; // Used by the MIP stack + struct mg_tcpip_if *ifp; // Builtin TCP/IP stack only. Interface pointer + size_t extraconnsize; // Builtin TCP/IP stack only. Extra space MG_SOCKET_TYPE pipe; // Socketpair end for mg_wakeup() #if MG_ENABLE_FREERTOS_TCP SocketSet_t ss; // NOTE(lsm): referenced from socket struct @@ -2711,9 +2712,7 @@ bool mg_ota_flash_end(struct mg_flash *flash); -#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP struct mg_tcpip_if; // Mongoose TCP/IP network interface -#define MG_TCPIP_IFACE(mgr_) ((struct mg_tcpip_if *) (mgr_)->priv) struct mg_tcpip_driver { bool (*init)(struct mg_tcpip_if *); // Init driver @@ -2799,8 +2798,6 @@ struct mg_tcpip_spi { uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply }; -#endif - // Macros to record timestamped events that happens with a connection. diff --git a/src/net.h b/src/net.h index 679dc5b2..cca92c1e 100644 --- a/src/net.h +++ b/src/net.h @@ -4,6 +4,7 @@ #include "config.h" #include "event.h" #include "iobuf.h" +#include "net_builtin.h" #include "str.h" #include "timer.h" @@ -33,8 +34,8 @@ struct mg_mgr { void *active_dns_requests; // DNS requests in progress struct mg_timer *timers; // Active timers int epoll_fd; // Used when MG_EPOLL_ENABLE=1 - void *priv; // Used by the MIP stack - size_t extraconnsize; // Used by the MIP stack + struct mg_tcpip_if *ifp; // Builtin TCP/IP stack only. Interface pointer + size_t extraconnsize; // Builtin TCP/IP stack only. Extra space MG_SOCKET_TYPE pipe; // Socketpair end for mg_wakeup() #if MG_ENABLE_FREERTOS_TCP SocketSet_t ss; // NOTE(lsm): referenced from socket struct diff --git a/src/net_builtin.c b/src/net_builtin.c index 6ea7a2bd..31723c2a 100644 --- a/src/net_builtin.c +++ b/src/net_builtin.c @@ -167,7 +167,7 @@ static uint16_t ipcsum(const void *buf, size_t len) { } static void settmout(struct mg_connection *c, uint8_t type) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; struct connstate *s = (struct connstate *) (c + 1); unsigned n = type == MIP_TTYPE_ACK ? MIP_TCP_ACK_MS : type == MIP_TTYPE_ARP ? MIP_ARP_RESP_MS @@ -592,7 +592,7 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn, } static size_t trim_len(struct mg_connection *c, size_t len) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; size_t eth_h_len = 14, ip_max_h_len = 24, tcp_max_h_len = 60, udp_h_len = 8; size_t max_headers_len = eth_h_len + ip_max_h_len + (c->is_udp ? udp_h_len : tcp_max_h_len); @@ -619,7 +619,7 @@ static size_t trim_len(struct mg_connection *c, size_t len) { } long mg_io_send(struct mg_connection *c, const void *buf, size_t len) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; struct connstate *s = (struct connstate *) (c + 1); uint32_t dst_ip = *(uint32_t *) c->rem.ip; len = trim_len(c, len); @@ -682,8 +682,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { c->is_draining = 1; settmout(c, MIP_TTYPE_FIN); } - tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, flags, - c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), "", 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, flags, c->loc.port, c->rem.port, + mg_htonl(s->seq), mg_htonl(s->ack), "", 0); } else if (pkt->pay.len == 0) { // TODO(cpq): handle this peer's ACK } else if (seq != s->ack) { @@ -692,9 +692,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { MG_VERBOSE(("ignoring duplicate pkt")); } else { MG_VERBOSE(("SEQ != ACK: %x %x %x", seq, s->ack, ack)); - tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, TH_ACK, - c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), "", - 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_ACK, c->loc.port, c->rem.port, + mg_htonl(s->seq), mg_htonl(s->ack), "", 0); } } else if (io->size - io->len < pkt->pay.len && !mg_iobuf_resize(io, io->len + pkt->pay.len)) { @@ -716,9 +715,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { if (s->unacked > MIP_TCP_WIN / 2 && s->acked != s->ack) { // Send ACK immediately MG_VERBOSE(("%lu imm ACK %lu", c->id, s->acked)); - tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, TH_ACK, - c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, - 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_ACK, c->loc.port, c->rem.port, + mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); s->unacked = 0; s->acked = s->ack; if (s->ttype != MIP_TTYPE_KEEPALIVE) settmout(c, MIP_TTYPE_KEEPALIVE); @@ -1035,7 +1033,7 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) { ifp->recv_queue.size = ifp->driver->rx ? framesize : 8192; ifp->recv_queue.buf = (char *) calloc(1, ifp->recv_queue.size); ifp->timer_1000ms = mg_millis(); - mgr->priv = ifp; + mgr->ifp = ifp; ifp->mgr = mgr; ifp->mtu = MG_TCPIP_MTU_DEFAULT; mgr->extraconnsize = sizeof(struct connstate); @@ -1056,11 +1054,10 @@ void mg_tcpip_free(struct mg_tcpip_if *ifp) { static void send_syn(struct mg_connection *c) { struct connstate *s = (struct connstate *) (c + 1); uint32_t isn = mg_htonl((uint32_t) mg_ntohs(c->loc.port)); - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; uint32_t rem_ip; memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); - tx_tcp(ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0, NULL, - 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0, + NULL, 0); } static void mac_resolved(struct mg_connection *c) { @@ -1074,7 +1071,7 @@ static void mac_resolved(struct mg_connection *c) { } void mg_connect_resolved(struct mg_connection *c) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; uint32_t rem_ip; memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); c->is_resolving = 0; @@ -1130,12 +1127,10 @@ static void init_closure(struct mg_connection *c) { struct connstate *s = (struct connstate *) (c + 1); if (c->is_udp == false && c->is_listening == false && c->is_connecting == false) { // For TCP conns, - struct mg_tcpip_if *ifp = - (struct mg_tcpip_if *) c->mgr->priv; // send TCP FIN uint32_t rem_ip; memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); - tx_tcp(ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port, c->rem.port, - mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); + tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port, + c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); settmout(c, MIP_TTYPE_FIN); } } @@ -1152,12 +1147,11 @@ static bool can_write(struct mg_connection *c) { } void mg_mgr_poll(struct mg_mgr *mgr, int ms) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) mgr->priv; struct mg_connection *c, *tmp; uint64_t now = mg_millis(); mg_timer_poll(&mgr->timers, now); - if (ifp == NULL || ifp->driver == NULL) return; - mg_tcpip_poll(ifp, now); + if (mgr->ifp == NULL || mgr->ifp->driver == NULL) return; + mg_tcpip_poll(mgr->ifp, now); for (c = mgr->conns; c != NULL; c = tmp) { tmp = c->next; struct connstate *s = (struct connstate *) (c + 1); @@ -1175,7 +1169,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { } bool mg_send(struct mg_connection *c, const void *buf, size_t len) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + struct mg_tcpip_if *ifp = c->mgr->ifp; bool res = false; uint32_t rem_ip; memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); diff --git a/src/net_builtin.h b/src/net_builtin.h index 15536536..31c21298 100644 --- a/src/net_builtin.h +++ b/src/net_builtin.h @@ -3,10 +3,9 @@ #include "arch.h" #include "net.h" #include "queue.h" +#include "str.h" -#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP struct mg_tcpip_if; // Mongoose TCP/IP network interface -#define MG_TCPIP_IFACE(mgr_) ((struct mg_tcpip_if *) (mgr_)->priv) struct mg_tcpip_driver { bool (*init)(struct mg_tcpip_if *); // Init driver @@ -91,5 +90,3 @@ struct mg_tcpip_spi { void (*end)(void *); // SPI end: slave select high uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply }; - -#endif diff --git a/src/ota_stm32f.c b/src/ota_stm32f.c index edb40c42..bd91e43a 100644 --- a/src/ota_stm32f.c +++ b/src/ota_stm32f.c @@ -161,9 +161,7 @@ MG_IRAM static bool mg_stm32f_swap(void) { static bool s_flash_irq_disabled; -MG_IRAM static bool mg_stm32f_write(void *addr, - const void *buf, - size_t len) { +MG_IRAM static bool mg_stm32f_write(void *addr, const void *buf, size_t len) { if ((len % s_mg_flash_stm32f.align) != 0) { MG_ERROR(("%lu is not aligned to %lu", len, s_mg_flash_stm32f.align)); return false; @@ -194,8 +192,7 @@ MG_IRAM static bool mg_stm32f_write(void *addr, } // just overwrite instead of swap -MG_IRAM void single_bank_swap(char *p1, char *p2, - size_t size) { +MG_IRAM void single_bank_swap(char *p1, char *p2, size_t size) { // no stdlib calls here mg_stm32f_write(p1, p2, size); *(volatile unsigned long *) 0xe000ed0c = 0x5fa0004; @@ -226,4 +223,4 @@ bool mg_ota_end(void) { } return false; } -#endif \ No newline at end of file +#endif