Skip to content

Commit cc069e7

Browse files
committed
align mg_addr to 32- and 64-bits
1 parent df994df commit cc069e7

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

mongoose.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4817,7 +4817,7 @@ static size_t trim_len(struct mg_connection *c, size_t len) {
48174817
long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
48184818
struct mg_tcpip_if *ifp = c->mgr->ifp;
48194819
struct connstate *s = (struct connstate *) (c + 1);
4820-
uint32_t dst_ip = *(uint32_t *) c->rem.ip;
4820+
uint32_t dst_ip = c->rem.ip4;
48214821
len = trim_len(c, len);
48224822
if (c->is_udp) {
48234823
if (!tx_udp(ifp, s->mac, ifp->ip, c->loc.port, dst_ip, c->rem.port, buf,
@@ -4864,8 +4864,7 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
48644864
struct connstate *s = (struct connstate *) (c + 1);
48654865
struct mg_iobuf *io = c->is_tls ? &c->rtls : &c->recv;
48664866
uint32_t seq = mg_ntohl(pkt->tcp->seq);
4867-
uint32_t rem_ip;
4868-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
4867+
uint32_t rem_ip = c->rem.ip4;
48694868
if (pkt->tcp->flags & TH_FIN) {
48704869
uint8_t flags = TH_ACK;
48714870
if (mg_ntohl(pkt->tcp->seq) != s->ack) {
@@ -5353,7 +5352,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
53535352
uint32_t rem_ip;
53545353
if ((c->is_udp && !c->is_arplooking) || c->is_listening || c->is_resolving)
53555354
continue;
5356-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
5355+
rem_ip = c->rem.ip4;
53575356
if (ifp->now > s->timer) {
53585357
if (s->ttype == MIP_TTYPE_ARP) {
53595358
mg_error(c, "ARP timeout");
@@ -5444,8 +5443,7 @@ void mg_tcpip_free(struct mg_tcpip_if *ifp) {
54445443
static void send_syn(struct mg_connection *c) {
54455444
struct connstate *s = (struct connstate *) (c + 1);
54465445
uint32_t isn = mg_htonl((uint32_t) mg_ntohs(c->loc.port));
5447-
uint32_t rem_ip;
5448-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
5446+
uint32_t rem_ip = c->rem.ip4;
54495447
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0,
54505448
NULL, 0);
54515449
}
@@ -5469,11 +5467,10 @@ static void ip4_mcastmac(uint8_t *mac, uint32_t *ip) {
54695467

54705468
void mg_connect_resolved(struct mg_connection *c) {
54715469
struct mg_tcpip_if *ifp = c->mgr->ifp;
5472-
uint32_t rem_ip;
5473-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
5470+
uint32_t rem_ip = c->rem.ip4;
54745471
c->is_resolving = 0;
54755472
if (ifp->eport < MG_EPHEMERAL_PORT_BASE) ifp->eport = MG_EPHEMERAL_PORT_BASE;
5476-
memcpy(c->loc.ip, &ifp->ip, sizeof(uint32_t));
5473+
c->loc.ip4 = ifp->ip;
54775474
c->loc.port = mg_htons(ifp->eport++);
54785475
MG_DEBUG(("%lu %M -> %M", c->id, mg_print_ip_port, &c->loc, mg_print_ip_port,
54795476
&c->rem));
@@ -5525,8 +5522,7 @@ static void init_closure(struct mg_connection *c) {
55255522
struct connstate *s = (struct connstate *) (c + 1);
55265523
if (c->is_udp == false && c->is_listening == false &&
55275524
c->is_connecting == false) { // For TCP conns,
5528-
uint32_t rem_ip;
5529-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
5525+
uint32_t rem_ip = c->rem.ip4;
55305526
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port,
55315527
c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
55325528
settmout(c, MIP_TTYPE_FIN);
@@ -5580,8 +5576,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
55805576
bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
55815577
struct mg_tcpip_if *ifp = c->mgr->ifp;
55825578
bool res = false;
5583-
uint32_t rem_ip;
5584-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
5579+
uint32_t rem_ip = c->rem.ip4;
55855580
if (ifp->ip == 0 || ifp->state != MG_TCPIP_STATE_READY) {
55865581
mg_error(c, "net down");
55875582
} else if (c->is_udp && (c->is_arplooking || c->is_resolving)) {

mongoose.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,11 @@ struct mg_dns {
16051605
};
16061606

16071607
struct mg_addr {
1608-
uint8_t ip[16]; // Holds IPv4 or IPv6 address, in network byte order
1608+
union { // Holds IPv4 or IPv6 address, in network byte order
1609+
uint8_t ip[16];
1610+
uint32_t ip4;
1611+
uint64_t ip6[2];
1612+
};
16091613
uint16_t port; // TCP or UDP port in network byte order
16101614
uint8_t scope_id; // IPv6 scope ID
16111615
bool is_ip6; // True when address is IPv6 address

src/net.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ struct mg_dns {
1414
};
1515

1616
struct mg_addr {
17-
uint8_t ip[16]; // Holds IPv4 or IPv6 address, in network byte order
17+
union { // Holds IPv4 or IPv6 address, in network byte order
18+
uint8_t ip[16];
19+
uint32_t ip4;
20+
uint64_t ip6[2];
21+
};
1822
uint16_t port; // TCP or UDP port in network byte order
1923
uint8_t scope_id; // IPv6 scope ID
2024
bool is_ip6; // True when address is IPv6 address

src/net_builtin.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ static size_t trim_len(struct mg_connection *c, size_t len) {
660660
long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
661661
struct mg_tcpip_if *ifp = c->mgr->ifp;
662662
struct connstate *s = (struct connstate *) (c + 1);
663-
uint32_t dst_ip = *(uint32_t *) c->rem.ip;
663+
uint32_t dst_ip = c->rem.ip4;
664664
len = trim_len(c, len);
665665
if (c->is_udp) {
666666
if (!tx_udp(ifp, s->mac, ifp->ip, c->loc.port, dst_ip, c->rem.port, buf,
@@ -707,8 +707,7 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
707707
struct connstate *s = (struct connstate *) (c + 1);
708708
struct mg_iobuf *io = c->is_tls ? &c->rtls : &c->recv;
709709
uint32_t seq = mg_ntohl(pkt->tcp->seq);
710-
uint32_t rem_ip;
711-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
710+
uint32_t rem_ip = c->rem.ip4;
712711
if (pkt->tcp->flags & TH_FIN) {
713712
uint8_t flags = TH_ACK;
714713
if (mg_ntohl(pkt->tcp->seq) != s->ack) {
@@ -1196,7 +1195,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
11961195
uint32_t rem_ip;
11971196
if ((c->is_udp && !c->is_arplooking) || c->is_listening || c->is_resolving)
11981197
continue;
1199-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
1198+
rem_ip = c->rem.ip4;
12001199
if (ifp->now > s->timer) {
12011200
if (s->ttype == MIP_TTYPE_ARP) {
12021201
mg_error(c, "ARP timeout");
@@ -1287,8 +1286,7 @@ void mg_tcpip_free(struct mg_tcpip_if *ifp) {
12871286
static void send_syn(struct mg_connection *c) {
12881287
struct connstate *s = (struct connstate *) (c + 1);
12891288
uint32_t isn = mg_htonl((uint32_t) mg_ntohs(c->loc.port));
1290-
uint32_t rem_ip;
1291-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
1289+
uint32_t rem_ip = c->rem.ip4;
12921290
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0,
12931291
NULL, 0);
12941292
}
@@ -1312,11 +1310,10 @@ static void ip4_mcastmac(uint8_t *mac, uint32_t *ip) {
13121310

13131311
void mg_connect_resolved(struct mg_connection *c) {
13141312
struct mg_tcpip_if *ifp = c->mgr->ifp;
1315-
uint32_t rem_ip;
1316-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
1313+
uint32_t rem_ip = c->rem.ip4;
13171314
c->is_resolving = 0;
13181315
if (ifp->eport < MG_EPHEMERAL_PORT_BASE) ifp->eport = MG_EPHEMERAL_PORT_BASE;
1319-
memcpy(c->loc.ip, &ifp->ip, sizeof(uint32_t));
1316+
c->loc.ip4 = ifp->ip;
13201317
c->loc.port = mg_htons(ifp->eport++);
13211318
MG_DEBUG(("%lu %M -> %M", c->id, mg_print_ip_port, &c->loc, mg_print_ip_port,
13221319
&c->rem));
@@ -1368,8 +1365,7 @@ static void init_closure(struct mg_connection *c) {
13681365
struct connstate *s = (struct connstate *) (c + 1);
13691366
if (c->is_udp == false && c->is_listening == false &&
13701367
c->is_connecting == false) { // For TCP conns,
1371-
uint32_t rem_ip;
1372-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
1368+
uint32_t rem_ip = c->rem.ip4;
13731369
tx_tcp(c->mgr->ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port,
13741370
c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0);
13751371
settmout(c, MIP_TTYPE_FIN);
@@ -1423,8 +1419,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
14231419
bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
14241420
struct mg_tcpip_if *ifp = c->mgr->ifp;
14251421
bool res = false;
1426-
uint32_t rem_ip;
1427-
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
1422+
uint32_t rem_ip = c->rem.ip4;
14281423
if (ifp->ip == 0 || ifp->state != MG_TCPIP_STATE_READY) {
14291424
mg_error(c, "net down");
14301425
} else if (c->is_udp && (c->is_arplooking || c->is_resolving)) {

test/unit_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3182,7 +3182,7 @@ static void test_udp(void) {
31823182
}
31833183

31843184
static void test_check_ip_acl(void) {
3185-
struct mg_addr ip = {{1, 2, 3, 4}, 0, 0, false}; // 1.2.3.4
3185+
struct mg_addr ip = {{{1, 2, 3, 4}}, 0, 0, false}; // 1.2.3.4
31863186
ASSERT(mg_check_ip_acl(mg_str(NULL), &ip) == 1);
31873187
ASSERT(mg_check_ip_acl(mg_str(""), &ip) == 1);
31883188
ASSERT(mg_check_ip_acl(mg_str("invalid"), &ip) == -1);

0 commit comments

Comments
 (0)