Skip to content

Commit 4a164c3

Browse files
authored
Merge pull request #2987 from cesanta/ifp
mgr::priv -> mgr::ifp, use explicit type
2 parents 19cbb27 + b0e4069 commit 4a164c3

File tree

6 files changed

+46
-66
lines changed

6 files changed

+46
-66
lines changed

mongoose.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,7 +4243,7 @@ static uint16_t ipcsum(const void *buf, size_t len) {
42434243
}
42444244

42454245
static void settmout(struct mg_connection *c, uint8_t type) {
4246-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
4246+
struct mg_tcpip_if *ifp = c->mgr->ifp;
42474247
struct connstate *s = (struct connstate *) (c + 1);
42484248
unsigned n = type == MIP_TTYPE_ACK ? MIP_TCP_ACK_MS
42494249
: type == MIP_TTYPE_ARP ? MIP_ARP_RESP_MS
@@ -4668,7 +4668,7 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
46684668
}
46694669

46704670
static size_t trim_len(struct mg_connection *c, size_t len) {
4671-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
4671+
struct mg_tcpip_if *ifp = c->mgr->ifp;
46724672
size_t eth_h_len = 14, ip_max_h_len = 24, tcp_max_h_len = 60, udp_h_len = 8;
46734673
size_t max_headers_len =
46744674
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) {
46954695
}
46964696

46974697
long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
4698-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
4698+
struct mg_tcpip_if *ifp = c->mgr->ifp;
46994699
struct connstate *s = (struct connstate *) (c + 1);
47004700
uint32_t dst_ip = *(uint32_t *) c->rem.ip;
47014701
len = trim_len(c, len);
@@ -4758,8 +4758,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
47584758
c->is_draining = 1;
47594759
settmout(c, MIP_TTYPE_FIN);
47604760
}
4761-
tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, flags,
4762-
c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), "", 0);
4761+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, flags, c->loc.port, c->rem.port,
4762+
mg_htonl(s->seq), mg_htonl(s->ack), "", 0);
47634763
} else if (pkt->pay.len == 0) {
47644764
// TODO(cpq): handle this peer's ACK
47654765
} else if (seq != s->ack) {
@@ -4768,9 +4768,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
47684768
MG_VERBOSE(("ignoring duplicate pkt"));
47694769
} else {
47704770
MG_VERBOSE(("SEQ != ACK: %x %x %x", seq, s->ack, ack));
4771-
tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, TH_ACK,
4772-
c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), "",
4773-
0);
4771+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_ACK, c->loc.port, c->rem.port,
4772+
mg_htonl(s->seq), mg_htonl(s->ack), "", 0);
47744773
}
47754774
} else if (io->size - io->len < pkt->pay.len &&
47764775
!mg_iobuf_resize(io, io->len + pkt->pay.len)) {
@@ -4792,9 +4791,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
47924791
if (s->unacked > MIP_TCP_WIN / 2 && s->acked != s->ack) {
47934792
// Send ACK immediately
47944793
MG_VERBOSE(("%lu imm ACK %lu", c->id, s->acked));
4795-
tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, TH_ACK,
4796-
c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL,
4797-
0);
4794+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_ACK, c->loc.port, c->rem.port,
4795+
mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
47984796
s->unacked = 0;
47994797
s->acked = s->ack;
48004798
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) {
51115109
ifp->recv_queue.size = ifp->driver->rx ? framesize : 8192;
51125110
ifp->recv_queue.buf = (char *) calloc(1, ifp->recv_queue.size);
51135111
ifp->timer_1000ms = mg_millis();
5114-
mgr->priv = ifp;
5112+
mgr->ifp = ifp;
51155113
ifp->mgr = mgr;
51165114
ifp->mtu = MG_TCPIP_MTU_DEFAULT;
51175115
mgr->extraconnsize = sizeof(struct connstate);
@@ -5132,11 +5130,10 @@ void mg_tcpip_free(struct mg_tcpip_if *ifp) {
51325130
static void send_syn(struct mg_connection *c) {
51335131
struct connstate *s = (struct connstate *) (c + 1);
51345132
uint32_t isn = mg_htonl((uint32_t) mg_ntohs(c->loc.port));
5135-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
51365133
uint32_t rem_ip;
51375134
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
5138-
tx_tcp(ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0, NULL,
5139-
0);
5135+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0,
5136+
NULL, 0);
51405137
}
51415138

51425139
static void mac_resolved(struct mg_connection *c) {
@@ -5150,7 +5147,7 @@ static void mac_resolved(struct mg_connection *c) {
51505147
}
51515148

51525149
void mg_connect_resolved(struct mg_connection *c) {
5153-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
5150+
struct mg_tcpip_if *ifp = c->mgr->ifp;
51545151
uint32_t rem_ip;
51555152
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
51565153
c->is_resolving = 0;
@@ -5206,12 +5203,10 @@ static void init_closure(struct mg_connection *c) {
52065203
struct connstate *s = (struct connstate *) (c + 1);
52075204
if (c->is_udp == false && c->is_listening == false &&
52085205
c->is_connecting == false) { // For TCP conns,
5209-
struct mg_tcpip_if *ifp =
5210-
(struct mg_tcpip_if *) c->mgr->priv; // send TCP FIN
52115206
uint32_t rem_ip;
52125207
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
5213-
tx_tcp(ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port, c->rem.port,
5214-
mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
5208+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port,
5209+
c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
52155210
settmout(c, MIP_TTYPE_FIN);
52165211
}
52175212
}
@@ -5228,12 +5223,11 @@ static bool can_write(struct mg_connection *c) {
52285223
}
52295224

52305225
void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
5231-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) mgr->priv;
52325226
struct mg_connection *c, *tmp;
52335227
uint64_t now = mg_millis();
52345228
mg_timer_poll(&mgr->timers, now);
5235-
if (ifp == NULL || ifp->driver == NULL) return;
5236-
mg_tcpip_poll(ifp, now);
5229+
if (mgr->ifp == NULL || mgr->ifp->driver == NULL) return;
5230+
mg_tcpip_poll(mgr->ifp, now);
52375231
for (c = mgr->conns; c != NULL; c = tmp) {
52385232
tmp = c->next;
52395233
struct connstate *s = (struct connstate *) (c + 1);
@@ -5251,7 +5245,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
52515245
}
52525246

52535247
bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
5254-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
5248+
struct mg_tcpip_if *ifp = c->mgr->ifp;
52555249
bool res = false;
52565250
uint32_t rem_ip;
52575251
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));

mongoose.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,7 @@ enum {
21502150

21512151

21522152

2153+
21532154
struct mg_dns {
21542155
const char *url; // DNS server URL
21552156
struct mg_connection *c; // DNS server connection
@@ -2176,8 +2177,8 @@ struct mg_mgr {
21762177
void *active_dns_requests; // DNS requests in progress
21772178
struct mg_timer *timers; // Active timers
21782179
int epoll_fd; // Used when MG_EPOLL_ENABLE=1
2179-
void *priv; // Used by the MIP stack
2180-
size_t extraconnsize; // Used by the MIP stack
2180+
struct mg_tcpip_if *ifp; // Builtin TCP/IP stack only. Interface pointer
2181+
size_t extraconnsize; // Builtin TCP/IP stack only. Extra space
21812182
MG_SOCKET_TYPE pipe; // Socketpair end for mg_wakeup()
21822183
#if MG_ENABLE_FREERTOS_TCP
21832184
SocketSet_t ss; // NOTE(lsm): referenced from socket struct
@@ -2711,9 +2712,7 @@ bool mg_ota_flash_end(struct mg_flash *flash);
27112712

27122713

27132714

2714-
#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP
27152715
struct mg_tcpip_if; // Mongoose TCP/IP network interface
2716-
#define MG_TCPIP_IFACE(mgr_) ((struct mg_tcpip_if *) (mgr_)->priv)
27172716

27182717
struct mg_tcpip_driver {
27192718
bool (*init)(struct mg_tcpip_if *); // Init driver
@@ -2799,8 +2798,6 @@ struct mg_tcpip_spi {
27992798
uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply
28002799
};
28012800

2802-
#endif
2803-
28042801

28052802

28062803
// Macros to record timestamped events that happens with a connection.

src/net.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "config.h"
55
#include "event.h"
66
#include "iobuf.h"
7+
#include "net_builtin.h"
78
#include "str.h"
89
#include "timer.h"
910

@@ -33,8 +34,8 @@ struct mg_mgr {
3334
void *active_dns_requests; // DNS requests in progress
3435
struct mg_timer *timers; // Active timers
3536
int epoll_fd; // Used when MG_EPOLL_ENABLE=1
36-
void *priv; // Used by the MIP stack
37-
size_t extraconnsize; // Used by the MIP stack
37+
struct mg_tcpip_if *ifp; // Builtin TCP/IP stack only. Interface pointer
38+
size_t extraconnsize; // Builtin TCP/IP stack only. Extra space
3839
MG_SOCKET_TYPE pipe; // Socketpair end for mg_wakeup()
3940
#if MG_ENABLE_FREERTOS_TCP
4041
SocketSet_t ss; // NOTE(lsm): referenced from socket struct

src/net_builtin.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static uint16_t ipcsum(const void *buf, size_t len) {
167167
}
168168

169169
static void settmout(struct mg_connection *c, uint8_t type) {
170-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
170+
struct mg_tcpip_if *ifp = c->mgr->ifp;
171171
struct connstate *s = (struct connstate *) (c + 1);
172172
unsigned n = type == MIP_TTYPE_ACK ? MIP_TCP_ACK_MS
173173
: type == MIP_TTYPE_ARP ? MIP_ARP_RESP_MS
@@ -592,7 +592,7 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
592592
}
593593

594594
static size_t trim_len(struct mg_connection *c, size_t len) {
595-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
595+
struct mg_tcpip_if *ifp = c->mgr->ifp;
596596
size_t eth_h_len = 14, ip_max_h_len = 24, tcp_max_h_len = 60, udp_h_len = 8;
597597
size_t max_headers_len =
598598
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) {
619619
}
620620

621621
long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
622-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
622+
struct mg_tcpip_if *ifp = c->mgr->ifp;
623623
struct connstate *s = (struct connstate *) (c + 1);
624624
uint32_t dst_ip = *(uint32_t *) c->rem.ip;
625625
len = trim_len(c, len);
@@ -682,8 +682,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
682682
c->is_draining = 1;
683683
settmout(c, MIP_TTYPE_FIN);
684684
}
685-
tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, flags,
686-
c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), "", 0);
685+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, flags, c->loc.port, c->rem.port,
686+
mg_htonl(s->seq), mg_htonl(s->ack), "", 0);
687687
} else if (pkt->pay.len == 0) {
688688
// TODO(cpq): handle this peer's ACK
689689
} else if (seq != s->ack) {
@@ -692,9 +692,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
692692
MG_VERBOSE(("ignoring duplicate pkt"));
693693
} else {
694694
MG_VERBOSE(("SEQ != ACK: %x %x %x", seq, s->ack, ack));
695-
tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, TH_ACK,
696-
c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), "",
697-
0);
695+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_ACK, c->loc.port, c->rem.port,
696+
mg_htonl(s->seq), mg_htonl(s->ack), "", 0);
698697
}
699698
} else if (io->size - io->len < pkt->pay.len &&
700699
!mg_iobuf_resize(io, io->len + pkt->pay.len)) {
@@ -716,9 +715,8 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
716715
if (s->unacked > MIP_TCP_WIN / 2 && s->acked != s->ack) {
717716
// Send ACK immediately
718717
MG_VERBOSE(("%lu imm ACK %lu", c->id, s->acked));
719-
tx_tcp((struct mg_tcpip_if *) c->mgr->priv, s->mac, rem_ip, TH_ACK,
720-
c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL,
721-
0);
718+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_ACK, c->loc.port, c->rem.port,
719+
mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
722720
s->unacked = 0;
723721
s->acked = s->ack;
724722
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) {
10351033
ifp->recv_queue.size = ifp->driver->rx ? framesize : 8192;
10361034
ifp->recv_queue.buf = (char *) calloc(1, ifp->recv_queue.size);
10371035
ifp->timer_1000ms = mg_millis();
1038-
mgr->priv = ifp;
1036+
mgr->ifp = ifp;
10391037
ifp->mgr = mgr;
10401038
ifp->mtu = MG_TCPIP_MTU_DEFAULT;
10411039
mgr->extraconnsize = sizeof(struct connstate);
@@ -1056,11 +1054,10 @@ void mg_tcpip_free(struct mg_tcpip_if *ifp) {
10561054
static void send_syn(struct mg_connection *c) {
10571055
struct connstate *s = (struct connstate *) (c + 1);
10581056
uint32_t isn = mg_htonl((uint32_t) mg_ntohs(c->loc.port));
1059-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
10601057
uint32_t rem_ip;
10611058
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
1062-
tx_tcp(ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0, NULL,
1063-
0);
1059+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0,
1060+
NULL, 0);
10641061
}
10651062

10661063
static void mac_resolved(struct mg_connection *c) {
@@ -1074,7 +1071,7 @@ static void mac_resolved(struct mg_connection *c) {
10741071
}
10751072

10761073
void mg_connect_resolved(struct mg_connection *c) {
1077-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
1074+
struct mg_tcpip_if *ifp = c->mgr->ifp;
10781075
uint32_t rem_ip;
10791076
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
10801077
c->is_resolving = 0;
@@ -1130,12 +1127,10 @@ static void init_closure(struct mg_connection *c) {
11301127
struct connstate *s = (struct connstate *) (c + 1);
11311128
if (c->is_udp == false && c->is_listening == false &&
11321129
c->is_connecting == false) { // For TCP conns,
1133-
struct mg_tcpip_if *ifp =
1134-
(struct mg_tcpip_if *) c->mgr->priv; // send TCP FIN
11351130
uint32_t rem_ip;
11361131
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
1137-
tx_tcp(ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port, c->rem.port,
1138-
mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
1132+
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port,
1133+
c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
11391134
settmout(c, MIP_TTYPE_FIN);
11401135
}
11411136
}
@@ -1152,12 +1147,11 @@ static bool can_write(struct mg_connection *c) {
11521147
}
11531148

11541149
void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
1155-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) mgr->priv;
11561150
struct mg_connection *c, *tmp;
11571151
uint64_t now = mg_millis();
11581152
mg_timer_poll(&mgr->timers, now);
1159-
if (ifp == NULL || ifp->driver == NULL) return;
1160-
mg_tcpip_poll(ifp, now);
1153+
if (mgr->ifp == NULL || mgr->ifp->driver == NULL) return;
1154+
mg_tcpip_poll(mgr->ifp, now);
11611155
for (c = mgr->conns; c != NULL; c = tmp) {
11621156
tmp = c->next;
11631157
struct connstate *s = (struct connstate *) (c + 1);
@@ -1175,7 +1169,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
11751169
}
11761170

11771171
bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
1178-
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
1172+
struct mg_tcpip_if *ifp = c->mgr->ifp;
11791173
bool res = false;
11801174
uint32_t rem_ip;
11811175
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));

src/net_builtin.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
#include "arch.h"
44
#include "net.h"
55
#include "queue.h"
6+
#include "str.h"
67

7-
#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP
88
struct mg_tcpip_if; // Mongoose TCP/IP network interface
9-
#define MG_TCPIP_IFACE(mgr_) ((struct mg_tcpip_if *) (mgr_)->priv)
109

1110
struct mg_tcpip_driver {
1211
bool (*init)(struct mg_tcpip_if *); // Init driver
@@ -91,5 +90,3 @@ struct mg_tcpip_spi {
9190
void (*end)(void *); // SPI end: slave select high
9291
uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply
9392
};
94-
95-
#endif

src/ota_stm32f.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,7 @@ MG_IRAM static bool mg_stm32f_swap(void) {
161161

162162
static bool s_flash_irq_disabled;
163163

164-
MG_IRAM static bool mg_stm32f_write(void *addr,
165-
const void *buf,
166-
size_t len) {
164+
MG_IRAM static bool mg_stm32f_write(void *addr, const void *buf, size_t len) {
167165
if ((len % s_mg_flash_stm32f.align) != 0) {
168166
MG_ERROR(("%lu is not aligned to %lu", len, s_mg_flash_stm32f.align));
169167
return false;
@@ -194,8 +192,7 @@ MG_IRAM static bool mg_stm32f_write(void *addr,
194192
}
195193

196194
// just overwrite instead of swap
197-
MG_IRAM void single_bank_swap(char *p1, char *p2,
198-
size_t size) {
195+
MG_IRAM void single_bank_swap(char *p1, char *p2, size_t size) {
199196
// no stdlib calls here
200197
mg_stm32f_write(p1, p2, size);
201198
*(volatile unsigned long *) 0xe000ed0c = 0x5fa0004;
@@ -226,4 +223,4 @@ bool mg_ota_end(void) {
226223
}
227224
return false;
228225
}
229-
#endif
226+
#endif

0 commit comments

Comments
 (0)