Skip to content

Commit 81b8799

Browse files
committed
wip
1 parent 857dbec commit 81b8799

File tree

2 files changed

+46
-58
lines changed

2 files changed

+46
-58
lines changed

mongoose.c

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4324,18 +4324,11 @@ struct pkt {
43244324

43254325
static void mg_tcpip_call(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
43264326
#if MG_ENABLE_PROFILE
4327-
const char *names[] = {
4328-
"TCPIP_EV_ST_CHG",
4329-
"TCPIP_EV_DHCP_DNS",
4330-
"TCPIP_EV_DHCP_SNTP",
4331-
"TCPIP_EV_ARP",
4332-
"TCPIP_EV_TIMER_1S",
4333-
"TCPIP_EV_WIFI_SCAN_RESULT",
4334-
"TCPIP_EV_WIFI_SCAN_END",
4335-
"TCPIP_EV_WIFI_CONNECT_ERR",
4336-
"TCPIP_EV_DRIVER",
4337-
"TCPIP_EV_USER"
4338-
};
4327+
const char *names[] = {"TCPIP_EV_ST_CHG", "TCPIP_EV_DHCP_DNS",
4328+
"TCPIP_EV_DHCP_SNTP", "TCPIP_EV_ARP",
4329+
"TCPIP_EV_TIMER_1S", "TCPIP_EV_WIFI_SCAN_RESULT",
4330+
"TCPIP_EV_WIFI_SCAN_END", "TCPIP_EV_WIFI_CONNECT_ERR",
4331+
"TCPIP_EV_DRIVER", "TCPIP_EV_USER"};
43394332
if (ev != MG_TCPIP_EV_POLL && ev < (int) (sizeof(names) / sizeof(names[0]))) {
43404333
MG_PROF_ADD(c, names[ev]);
43414334
}
@@ -5126,13 +5119,13 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
51265119
if (pkt->pay.len < sizeof(*pkt->ip)) return; // Truncated
51275120
if ((pkt->ip->ver >> 4) != 4) return; // Not IP
51285121
ihl = pkt->ip->ver & 0x0F;
5129-
if (ihl < 5) return; // bad IHL
5130-
if (pkt->pay.len < (uint16_t)(ihl * 4)) return; // Truncated / malformed
5122+
if (ihl < 5) return; // bad IHL
5123+
if (pkt->pay.len < (uint16_t) (ihl * 4)) return; // Truncated / malformed
51315124
// There can be link padding, take length from IP header
5132-
len = mg_ntohs(pkt->ip->len); // IP datagram length
5133-
if (len < (ihl * 4) || len > pkt->pay.len) return; // malformed
5134-
pkt->pay.len = len; // strip padding
5135-
mkpay(pkt, (uint32_t *) pkt->ip + ihl); // account for opts
5125+
len = mg_ntohs(pkt->ip->len); // IP datagram length
5126+
if (len < (ihl * 4) || len > pkt->pay.len) return; // malformed
5127+
pkt->pay.len = len; // strip padding
5128+
mkpay(pkt, (uint32_t *) pkt->ip + ihl); // account for opts
51365129
frag = mg_ntohs(pkt->ip->frag);
51375130
if (frag & IP_MORE_FRAGS_MSK || frag & IP_FRAG_OFFSET_MSK) {
51385131
struct mg_connection *c;
@@ -5147,11 +5140,11 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
51475140
rx_icmp(ifp, pkt);
51485141
} else if (pkt->ip->proto == 17) {
51495142
pkt->udp = (struct udp *) (pkt->pay.buf);
5150-
if (pkt->pay.len < sizeof(*pkt->udp)) return; // truncated
5143+
if (pkt->pay.len < sizeof(*pkt->udp)) return; // truncated
51515144
// Take length from UDP header
5152-
len = mg_ntohs(pkt->udp->len); // UDP datagram length
5153-
if (len < sizeof(*pkt->udp) || len > pkt->pay.len) return; // malformed
5154-
pkt->pay.len = len; // strip excess data
5145+
len = mg_ntohs(pkt->udp->len); // UDP datagram length
5146+
if (len < sizeof(*pkt->udp) || len > pkt->pay.len) return; // malformed
5147+
pkt->pay.len = len; // strip excess data
51555148
mkpay(pkt, pkt->udp + 1);
51565149
MG_VERBOSE(("UDP %M:%hu -> %M:%hu len %u", mg_print_ip4, &pkt->ip->src,
51575150
mg_ntohs(pkt->udp->sport), mg_print_ip4, &pkt->ip->dst,
@@ -5172,7 +5165,7 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
51725165
pkt->tcp = (struct tcp *) (pkt->pay.buf);
51735166
if (pkt->pay.len < sizeof(*pkt->tcp)) return;
51745167
off = pkt->tcp->off >> 4; // account for opts
5175-
if (pkt->pay.len < (uint16_t)(4 * off)) return;
5168+
if (pkt->pay.len < (uint16_t) (4 * off)) return;
51765169
mkpay(pkt, (uint32_t *) pkt->tcp + off);
51775170
MG_VERBOSE(("TCP %M:%hu -> %M:%hu len %u", mg_print_ip4, &pkt->ip->src,
51785171
mg_ntohs(pkt->tcp->sport), mg_print_ip4, &pkt->ip->dst,
@@ -5201,7 +5194,7 @@ static void rx_ip6(struct mg_tcpip_if *ifp, struct pkt *pkt) {
52015194
case 51: // Authentication RFC-4302
52025195
MG_INFO(("IPv6 extension header %d", (int) next));
52035196
next = nhdr[0];
5204-
len += (uint16_t)(8 * (nhdr[1] + 1));
5197+
len += (uint16_t) (8 * (nhdr[1] + 1));
52055198
nhdr += 8 * (nhdr[1] + 1);
52065199
break;
52075200
case 44: // Fragment 4.5
@@ -5271,8 +5264,8 @@ static void mg_tcpip_rx(struct mg_tcpip_if *ifp, void *buf, size_t len) {
52715264
struct pkt pkt;
52725265
memset(&pkt, 0, sizeof(pkt));
52735266
pkt.pay.buf = pkt.raw.buf = (char *) buf;
5274-
pkt.pay.len = pkt.raw.len = len; // payload = raw
5275-
pkt.eth = (struct eth *) buf; // Ethernet = raw
5267+
pkt.pay.len = pkt.raw.len = len; // payload = raw
5268+
pkt.eth = (struct eth *) buf; // Ethernet = raw
52765269
if (pkt.raw.len < sizeof(*pkt.eth)) return; // Truncated - runt?
52775270
if (ifp->enable_mac_check &&
52785271
memcmp(pkt.eth->dst, ifp->mac, sizeof(pkt.eth->dst)) != 0 &&
@@ -5288,7 +5281,7 @@ static void mg_tcpip_rx(struct mg_tcpip_if *ifp, void *buf, size_t len) {
52885281
mkpay(&pkt, pkt.eth + 1);
52895282
if (pkt.eth->type == mg_htons(0x806)) {
52905283
pkt.arp = (struct arp *) (pkt.pay.buf);
5291-
if (pkt.pay.len < sizeof(*pkt.arp)) return; // Truncated
5284+
if (pkt.pay.len < sizeof(*pkt.arp)) return; // Truncated
52925285
mg_tcpip_call(ifp, MG_TCPIP_EV_ARP, &pkt.raw);
52935286
rx_arp(ifp, &pkt);
52945287
} else if (pkt.eth->type == mg_htons(0x86dd)) {
@@ -5556,7 +5549,7 @@ static void init_closure(struct mg_connection *c) {
55565549
struct connstate *s = (struct connstate *) (c + 1);
55575550
if (c->is_udp == false && c->is_listening == false &&
55585551
c->is_connecting == false) { // For TCP conns,
5559-
uint32_t rem_ip = c->rem.ip4;
5552+
uint32_t rem_ip = c->rem.ip4;
55605553
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port,
55615554
c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
55625555
settmout(c, MIP_TTYPE_FIN);
@@ -5623,7 +5616,8 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
56235616
len);
56245617
} else {
56255618
res = mg_iobuf_add(&c->send, c->send.len, buf, len);
5626-
// res == 0 means an OOM condition (iobuf couldn't resize), yet this is so far recoverable, let the caller decide
5619+
// res == 0 means an OOM condition (iobuf couldn't resize), yet this is so
5620+
// far recoverable, let the caller decide
56275621
}
56285622
return res;
56295623
}

src/net_builtin.c

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,11 @@ struct pkt {
160160

161161
static void mg_tcpip_call(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
162162
#if MG_ENABLE_PROFILE
163-
const char *names[] = {
164-
"TCPIP_EV_ST_CHG",
165-
"TCPIP_EV_DHCP_DNS",
166-
"TCPIP_EV_DHCP_SNTP",
167-
"TCPIP_EV_ARP",
168-
"TCPIP_EV_TIMER_1S",
169-
"TCPIP_EV_WIFI_SCAN_RESULT",
170-
"TCPIP_EV_WIFI_SCAN_END",
171-
"TCPIP_EV_WIFI_CONNECT_ERR",
172-
"TCPIP_EV_DRIVER",
173-
"TCPIP_EV_USER"
174-
};
163+
const char *names[] = {"TCPIP_EV_ST_CHG", "TCPIP_EV_DHCP_DNS",
164+
"TCPIP_EV_DHCP_SNTP", "TCPIP_EV_ARP",
165+
"TCPIP_EV_TIMER_1S", "TCPIP_EV_WIFI_SCAN_RESULT",
166+
"TCPIP_EV_WIFI_SCAN_END", "TCPIP_EV_WIFI_CONNECT_ERR",
167+
"TCPIP_EV_DRIVER", "TCPIP_EV_USER"};
175168
if (ev != MG_TCPIP_EV_POLL && ev < (int) (sizeof(names) / sizeof(names[0]))) {
176169
MG_PROF_ADD(c, names[ev]);
177170
}
@@ -962,13 +955,13 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
962955
if (pkt->pay.len < sizeof(*pkt->ip)) return; // Truncated
963956
if ((pkt->ip->ver >> 4) != 4) return; // Not IP
964957
ihl = pkt->ip->ver & 0x0F;
965-
if (ihl < 5) return; // bad IHL
966-
if (pkt->pay.len < (uint16_t)(ihl * 4)) return; // Truncated / malformed
958+
if (ihl < 5) return; // bad IHL
959+
if (pkt->pay.len < (uint16_t) (ihl * 4)) return; // Truncated / malformed
967960
// There can be link padding, take length from IP header
968-
len = mg_ntohs(pkt->ip->len); // IP datagram length
969-
if (len < (ihl * 4) || len > pkt->pay.len) return; // malformed
970-
pkt->pay.len = len; // strip padding
971-
mkpay(pkt, (uint32_t *) pkt->ip + ihl); // account for opts
961+
len = mg_ntohs(pkt->ip->len); // IP datagram length
962+
if (len < (ihl * 4) || len > pkt->pay.len) return; // malformed
963+
pkt->pay.len = len; // strip padding
964+
mkpay(pkt, (uint32_t *) pkt->ip + ihl); // account for opts
972965
frag = mg_ntohs(pkt->ip->frag);
973966
if (frag & IP_MORE_FRAGS_MSK || frag & IP_FRAG_OFFSET_MSK) {
974967
struct mg_connection *c;
@@ -983,11 +976,11 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
983976
rx_icmp(ifp, pkt);
984977
} else if (pkt->ip->proto == 17) {
985978
pkt->udp = (struct udp *) (pkt->pay.buf);
986-
if (pkt->pay.len < sizeof(*pkt->udp)) return; // truncated
979+
if (pkt->pay.len < sizeof(*pkt->udp)) return; // truncated
987980
// Take length from UDP header
988-
len = mg_ntohs(pkt->udp->len); // UDP datagram length
989-
if (len < sizeof(*pkt->udp) || len > pkt->pay.len) return; // malformed
990-
pkt->pay.len = len; // strip excess data
981+
len = mg_ntohs(pkt->udp->len); // UDP datagram length
982+
if (len < sizeof(*pkt->udp) || len > pkt->pay.len) return; // malformed
983+
pkt->pay.len = len; // strip excess data
991984
mkpay(pkt, pkt->udp + 1);
992985
MG_VERBOSE(("UDP %M:%hu -> %M:%hu len %u", mg_print_ip4, &pkt->ip->src,
993986
mg_ntohs(pkt->udp->sport), mg_print_ip4, &pkt->ip->dst,
@@ -1008,7 +1001,7 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
10081001
pkt->tcp = (struct tcp *) (pkt->pay.buf);
10091002
if (pkt->pay.len < sizeof(*pkt->tcp)) return;
10101003
off = pkt->tcp->off >> 4; // account for opts
1011-
if (pkt->pay.len < (uint16_t)(4 * off)) return;
1004+
if (pkt->pay.len < (uint16_t) (4 * off)) return;
10121005
mkpay(pkt, (uint32_t *) pkt->tcp + off);
10131006
MG_VERBOSE(("TCP %M:%hu -> %M:%hu len %u", mg_print_ip4, &pkt->ip->src,
10141007
mg_ntohs(pkt->tcp->sport), mg_print_ip4, &pkt->ip->dst,
@@ -1037,7 +1030,7 @@ static void rx_ip6(struct mg_tcpip_if *ifp, struct pkt *pkt) {
10371030
case 51: // Authentication RFC-4302
10381031
MG_INFO(("IPv6 extension header %d", (int) next));
10391032
next = nhdr[0];
1040-
len += (uint16_t)(8 * (nhdr[1] + 1));
1033+
len += (uint16_t) (8 * (nhdr[1] + 1));
10411034
nhdr += 8 * (nhdr[1] + 1);
10421035
break;
10431036
case 44: // Fragment 4.5
@@ -1107,8 +1100,8 @@ static void mg_tcpip_rx(struct mg_tcpip_if *ifp, void *buf, size_t len) {
11071100
struct pkt pkt;
11081101
memset(&pkt, 0, sizeof(pkt));
11091102
pkt.pay.buf = pkt.raw.buf = (char *) buf;
1110-
pkt.pay.len = pkt.raw.len = len; // payload = raw
1111-
pkt.eth = (struct eth *) buf; // Ethernet = raw
1103+
pkt.pay.len = pkt.raw.len = len; // payload = raw
1104+
pkt.eth = (struct eth *) buf; // Ethernet = raw
11121105
if (pkt.raw.len < sizeof(*pkt.eth)) return; // Truncated - runt?
11131106
if (ifp->enable_mac_check &&
11141107
memcmp(pkt.eth->dst, ifp->mac, sizeof(pkt.eth->dst)) != 0 &&
@@ -1124,7 +1117,7 @@ static void mg_tcpip_rx(struct mg_tcpip_if *ifp, void *buf, size_t len) {
11241117
mkpay(&pkt, pkt.eth + 1);
11251118
if (pkt.eth->type == mg_htons(0x806)) {
11261119
pkt.arp = (struct arp *) (pkt.pay.buf);
1127-
if (pkt.pay.len < sizeof(*pkt.arp)) return; // Truncated
1120+
if (pkt.pay.len < sizeof(*pkt.arp)) return; // Truncated
11281121
mg_tcpip_call(ifp, MG_TCPIP_EV_ARP, &pkt.raw);
11291122
rx_arp(ifp, &pkt);
11301123
} else if (pkt.eth->type == mg_htons(0x86dd)) {
@@ -1392,7 +1385,7 @@ static void init_closure(struct mg_connection *c) {
13921385
struct connstate *s = (struct connstate *) (c + 1);
13931386
if (c->is_udp == false && c->is_listening == false &&
13941387
c->is_connecting == false) { // For TCP conns,
1395-
uint32_t rem_ip = c->rem.ip4;
1388+
uint32_t rem_ip = c->rem.ip4;
13961389
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port,
13971390
c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
13981391
settmout(c, MIP_TTYPE_FIN);
@@ -1459,7 +1452,8 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
14591452
len);
14601453
} else {
14611454
res = mg_iobuf_add(&c->send, c->send.len, buf, len);
1462-
// res == 0 means an OOM condition (iobuf couldn't resize), yet this is so far recoverable, let the caller decide
1455+
// res == 0 means an OOM condition (iobuf couldn't resize), yet this is so
1456+
// far recoverable, let the caller decide
14631457
}
14641458
return res;
14651459
}

0 commit comments

Comments
 (0)