Fix some neats, add IPv6 basic testing in LAN

This commit is contained in:
Sergio R. Caprile committed 2026-06-12 16:20:39 -03:00
1 parent 6b589b6900
commit 2006f8302a
11 files changed
+157 -89

No files matched your search

+2 -2
View File
@@ -77,7 +77,7 @@ uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
qtag->tpid = mg_htons(0x8100);
qtag->tci = mg_htons(d->vlan_id); // PCP = default (best-effort)
hlp += sizeof(*qtag);
*(uint16_t *) (qtag + 1) = mg_htons(eth_types[(unsigned int) proto]);
MG_STORE_BE16(qtag + 1, eth_types[(unsigned int) proto]);
}
return hlp;
}
@@ -111,7 +111,7 @@ bool mg_l2_eth_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
if (qtag->tpid != mg_htons(0x8100)) return false; // Untagged frame
if (mg_ntohs(VLAN_ID(qtag->tci)) != VLAN_ID(d->vlan_id))
return false; // Not our VLAN
type = mg_ntohs(*(uint16_t *) (qtag + 1));
type = MG_LOAD_BE16(qtag + 1);
}
if (ifp->enable_mac_check &&
memcmp(eth->dst, ifp->mac, sizeof(eth->dst)) != 0 &&
+9 -5
View File
@@ -848,7 +848,7 @@ static void rx_ndp_na(struct mg_tcpip_if *ifp, struct pkt *pkt) {
uint8_t len = *opts++; // check valid hwaddr and get it
if (!mg_l2_ip6get(ifp->l2type, s->mac, opts, len)) return;
MG_DEBUG(("%lu NDP resolved %M -> %M", c->id, mg_print_ip6,
&c->rem.addr.ip6, mg_print_mac, ifp->l2type, s->mac));
&c->rem.addr.ip6, mg_print_l2addr, ifp->l2type, s->mac));
c->is_arplooking = 0;
l2addr_resolved(c);
}
@@ -983,12 +983,12 @@ static void rx_ndp_ra(struct mg_tcpip_if *ifp, struct pkt *pkt) {
gotl2addr = true;
} else if (type == 5 && length >= 8) {
// process MTU if available
mtu = mg_ntohl(*(uint32_t *) (opts + 4));
mtu = MG_LOAD_BE32(opts + 4);
} else if (type == 3 && length >= 32) {
// process prefix, 4.6.2
uint8_t pfx_flags = opts[3]; // L=0x80, A=0x40
uint32_t valid = mg_ntohl(*(uint32_t *) (opts + 4));
uint32_t pref_lifetime = mg_ntohl(*(uint32_t *) (opts + 8));
uint32_t valid = MG_LOAD_BE32(opts + 4);
uint32_t pref_lifetime = MG_LOAD_BE32(opts + 8);
prefix_len = opts[2];
prefix = opts + 16;
@@ -2119,7 +2119,11 @@ void mg_connect_resolved(struct mg_connection *c) {
c->loc.port = mg_htons(ifp->eport++);
#if MG_ENABLE_IPV6
if (c->rem.is_ip6) {
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1],
if(c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
} else { // use global address
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
}
c->loc.is_ip6 = true;
} else
#endif
+1 -1
View File
@@ -28,7 +28,7 @@ size_t mg_print_ip4(mg_pfn_t, void *arg, va_list *ap); // expects: uint32_t
size_t mg_print_ip6(mg_pfn_t, void *arg, va_list *ap); // expects: uint8_t[16] ipv6
size_t mg_print_mac(mg_pfn_t, void *arg, va_list *ap); // expects: uint8_t[6] mac
size_t mg_print_ieee64(mg_pfn_t, void *arg, va_list *ap); // expects: uint64_t
size_t mg_print_l2addr(mg_pfn_t, void *arg, va_list *ap); // expects: const struct mg_addr *
size_t mg_print_l2addr(mg_pfn_t, void *arg, va_list *ap); // expects: uint8_t l2, uint8_t[n] n-byte l2-dependent address
// Output functions for use as the fn argument to mg_xprintf/mg_vxprintf.
void mg_pfn_iobuf(char ch, void *param); // param: struct mg_iobuf * (resizes as needed)
+25 -25
View File
@@ -96,34 +96,34 @@ void mg_delayms(unsigned int ms);
// Write a native integer to byte pointer p in big-endian byte order.
// Safe on architectures that forbid unaligned access.
#define MG_STORE_BE16(p, n) \
do { \
MG_U8P(p)[0] = ((n) >> 8U) & 255; \
MG_U8P(p)[1] = (n) &255; \
#define MG_STORE_BE16(p, n) \
do { \
MG_U8P(p)[0] = (uint8_t) ((n) >> 8U); \
MG_U8P(p)[1] = (uint8_t) (n); \
} while (0)
#define MG_STORE_BE24(p, n) \
do { \
MG_U8P(p)[0] = ((n) >> 16U) & 255; \
MG_U8P(p)[1] = ((n) >> 8U) & 255; \
MG_U8P(p)[2] = (n) &255; \
#define MG_STORE_BE24(p, n) \
do { \
MG_U8P(p)[0] = (uint8_t) ((n) >> 16U); \
MG_U8P(p)[1] = (uint8_t) ((n) >> 8U); \
MG_U8P(p)[2] = (uint8_t) (n); \
} while (0)
#define MG_STORE_BE32(p, n) \
do { \
MG_U8P(p)[0] = ((n) >> 24U) & 255; \
MG_U8P(p)[1] = ((n) >> 16U) & 255; \
MG_U8P(p)[2] = ((n) >> 8U) & 255; \
MG_U8P(p)[3] = (n) &255; \
#define MG_STORE_BE32(p, n) \
do { \
MG_U8P(p)[0] = (uint8_t) ((n) >> 24U); \
MG_U8P(p)[1] = (uint8_t) ((n) >> 16U); \
MG_U8P(p)[2] = (uint8_t) ((n) >> 8U); \
MG_U8P(p)[3] = (uint8_t) (n); \
} while (0)
#define MG_STORE_BE64(p, n) \
do { \
MG_U8P(p)[0] = ((n) >> 56U) & 255; \
MG_U8P(p)[1] = ((n) >> 48U) & 255; \
MG_U8P(p)[2] = ((n) >> 40U) & 255; \
MG_U8P(p)[3] = ((n) >> 32U) & 255; \
MG_U8P(p)[4] = ((n) >> 24U) & 255; \
MG_U8P(p)[5] = ((n) >> 16U) & 255; \
MG_U8P(p)[6] = ((n) >> 8U) & 255; \
MG_U8P(p)[7] = (n) &255; \
#define MG_STORE_BE64(p, n) \
do { \
MG_U8P(p)[0] = (uint8_t) ((n) >> 56U); \
MG_U8P(p)[1] = (uint8_t) ((n) >> 48U); \
MG_U8P(p)[2] = (uint8_t) ((n) >> 40U); \
MG_U8P(p)[3] = (uint8_t) ((n) >> 32U); \
MG_U8P(p)[4] = (uint8_t) ((n) >> 24U); \
MG_U8P(p)[5] = (uint8_t) ((n) >> 16U); \
MG_U8P(p)[6] = (uint8_t) ((n) >> 8U); \
MG_U8P(p)[7] = (uint8_t) (n); \
} while (0)
// Network / host byte-order conversion (big-endian <-> native).