Compare commits

...
5 Commits
Author SHA1 Message Date
Sergio R. Caprile d597b89bd4 use link-local and global accordingly 2026-12-04 09:57:21 -03:00
Sergey Lyubka f3189519d8 Merge pull request #3816 from cesanta/flash
SPI flash support, with xiao nrf52 and portenta-h7
2026-12-03 12:46:50 +00:00
Sergio R. Caprile f03c00a516 Merge pull request #3813 from cesanta/tight
tighten MQTT and HTTP handlers
2026-12-03 09:38:58 -03:00
Sergey Lyubka d7a756a24a Merge pull request #3815 from cesanta/flash1
Introduce struct mg_flash::{read_fn,init_fn}
2026-12-03 11:39:41 +00:00
Sergio R. Caprile 24f5519463 tighten MQTT and HTTP handlers 2026-12-02 14:31:43 -03:00
6 changed files with 134 additions and 80 deletions

No files matched your search

+47 -40
View File
@@ -5397,7 +5397,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
while (c->pfn_data == NULL && ofs < c->recv.len) {
const char *buf = (char *) c->recv.buf + ofs;
int n = mg_http_parse(buf, c->recv.len - ofs, &hm);
struct mg_str *te; // Transfer - encoding header
struct mg_str *hcl, *te; // Content-Length and Transfer-encoding headers
bool is_chunked = false, is_http_1_0 = false;
size_t old_len = c->recv.len;
if (n < 0) {
@@ -5417,8 +5417,11 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
c->pfn = NULL;
return;
}
if (ev == MG_EV_CLOSE) { // If client did not set Content-Length
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG
hcl = mg_http_get_header(&hm, "Content-Length");
if (ev == MG_EV_CLOSE && (hcl == NULL || c->is_client)) {
// - a server responded to HEAD with optional Content-Length (no body)
// - a client sent no Content-Length and closed --> deliver MSG
hm.message.len = c->recv.len - ofs;
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
}
is_http_1_0 =
@@ -5432,7 +5435,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
mg_error(c, "Invalid Transfer-Encoding"); // See #2460
return;
}
} else if (mg_http_get_header(&hm, "Content-length") == NULL) {
} else if (hcl == NULL) {
// #2593: HTTP packets must contain either Transfer-Encoding or
// Content-length
bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0;
@@ -8591,9 +8594,8 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
m->ack = p[1];
p += 2;
if (version == 5) {
if (p >= end) return MQTT_MALFORMED;
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
@@ -8624,9 +8626,9 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
p += 2;
}
if (p > end) return MQTT_MALFORMED;
if (version == 5 && p + 2 < end) {
if (version == 5) {
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
@@ -9525,6 +9527,7 @@ static const struct mg_addr ip6_allnodes = {
static const uint8_t locpx[8] = {0xfe, 0x80, 0, 0, 0, 0, 0, 0};
#define MG_IP6MATCH(a, b) (a[0] == b[0] && a[1] == b[1])
#define MG_IP6UNSPEC(a) (a[0] == 0 && a[1] == 0)
#endif
static void settmout(struct mg_connection *c, uint8_t type) {
@@ -10079,9 +10082,9 @@ static void rx_ndp_ns(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// - use solicited node multicast to resolve a l2 address (l2_addr = NULL)
// - use unicast to verify presence (l2_addr = neighbor l2 address)
static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
uint8_t *l2_addr) {
uint8_t *l2_addr, bool ll) {
uint64_t *ip_src = ll ? ifp->ip6ll : ifp->ip6;
uint8_t payload[4 + 16 + 16]; // NOTE(): 16 --> optional len upto 2 hw addr
uint64_t ip_unspec[2] = {0, 0};
size_t payload_len = 20;
bool mcast = (l2_addr == NULL);
uint64_t ip_mcast[2] = {0, 0};
@@ -10101,14 +10104,12 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
l2 = mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6, &ipd);
}
payload_len = 20;
// TODO(robertc2000): using only link-local IP addr for now
// We might consider to add an option to use either link-local or global IP
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
if (!MG_IP6UNSPEC(ip_src)) {
payload[20] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
payload[21] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 22);
payload_len += 8 * payload[21];
}
tx_icmp6(ifp, l2, ifp->ip6ll, mcast ? ip_mcast : ip_dst, 135, 0, payload,
tx_icmp6(ifp, l2, ip_src, mcast ? ip_mcast : ip_dst, 135, 0, payload,
payload_len);
}
@@ -10116,11 +10117,10 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
static void tx_ndp_rs(struct mg_tcpip_if *ifp) {
uint8_t payload[4 + 16]; // reserved + optional len upto 2 hw addr NOTE()
size_t payload_len = 4;
uint64_t ip_unspec[2] = {0, 0};
memset(payload, 0, sizeof(payload));
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
if (!MG_IP6UNSPEC(ifp->ip6ll)) {
payload[4] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
payload[5] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 6);
payload_len += 8 * payload[5];
@@ -10289,7 +10289,7 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
(struct mg_addr *) &ip6_allnodes),
ifp->ip6ll, (uint64_t *) ip6_allnodes.addr.ip6, false,
ifp->mac);
if (ifp->ip6[0] != 0 && ifp->ip6[1] != 0)
if (!MG_IP6UNSPEC(ifp->ip6))
tx_ndp_na(ifp, // RFC-9131 4.1
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6,
(struct mg_addr *) &ip6_allrouters),
@@ -10304,9 +10304,9 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
ifp->is_ip6_changed = false;
}
} else if (ifp->state6 == MG_TCPIP_STATE_IP) {
if ((ifp->gw6[0] != 0 || ifp->gw6[1] != 0) &&
if (!MG_IP6UNSPEC(ifp->gw6) &&
(ifp->l2type != MG_TCPIP_L2_PPP && ifp->l2type != MG_TCPIP_L2_PPPoE))
tx_ndp_ns(ifp, ifp->gw6, NULL); // unsolicited GW hwaddr resolution
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // unsolicited GW hwaddr resolution
} else if (ifp->state6 == MG_TCPIP_STATE_UP) {
MG_INFO(("IP: %M", mg_print_ip6, &ifp->ip6ll));
}
@@ -10519,13 +10519,14 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
struct pkt *pkt, uint16_t mss) {
struct connstate *s;
uint8_t *l2addr;
struct mg_tcpip_if *ifp = lsn->mgr->ifp;
struct mg_connection *c = mg_alloc_conn(lsn->mgr);
if (c == NULL) {
MG_ERROR(("OOM"));
return NULL;
}
s = (struct connstate *) (c + 1);
s->retransmit = lsn->mgr->ifp->enable_tcp_retransmit;
s->retransmit = ifp->enable_tcp_retransmit;
s->txq.align = MG_TCPIP_TXQ_ALIGN;
s->dmss = mss; // from options in client SYN
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
@@ -10534,19 +10535,21 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
if (lsn->loc.is_ip6) {
c->rem.addr.ip6[0] = pkt->ip6->src[0],
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
c->loc.addr.ip6[0] = c->mgr->ifp->ip6[0],
c->loc.addr.ip6[1] = c->mgr->ifp->ip6[1], c->loc.is_ip6 = true;
// TODO(): compare lsn to link-local, or rem as link-local: use ll instead
if (pkt->ip6->dst[0] == ifp->ip6ll[0]) { // link-local prefix
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
} else {
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
}
c->loc.is_ip6 = true;
} else
#endif
{
c->rem.addr.ip4 = pkt->ip->src;
c->loc.addr.ip4 = c->mgr->ifp->ip;
c->loc.addr.ip4 = ifp->ip;
}
c->rem.port = pkt->tcp->sport;
c->loc.port = lsn->loc.port;
if ((l2addr = get_return_l2addr(lsn->mgr->ifp, &c->rem, false, pkt)) ==
NULL) {
if ((l2addr = get_return_l2addr(ifp, &c->rem, false, pkt)) == NULL) {
mg_free(c); // safety net for lousy networks, not actually needed
return NULL; // as path has already been checked at SYN (sending SYN+ACK)
}
@@ -10604,10 +10607,14 @@ static bool udp_send(struct mg_connection *c, const void *buf, size_t len) {
struct mg_addr ips;
memset(&ips, 0, sizeof(ips));
#if MG_ENABLE_IPV6
if (c->loc.is_ip6) {
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1],
if (c->loc.is_ip6) { // At [::] infer link-local from peer
if (MG_IP6UNSPEC(c->loc.addr.ip6) ? c->rem.addr.ip6[0] == ifp->ip6ll[0]
: c->loc.addr.ip6[0] == ifp->ip6ll[0]) {
ips.addr.ip6[0] = ifp->ip6ll[0], ips.addr.ip6[1] = ifp->ip6ll[1];
} else {
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1];
}
ips.is_ip6 = true;
// TODO(): detect link-local (c->rem) and use it
} else
#endif
{
@@ -11268,14 +11275,14 @@ static void mg_ip6_link(struct mg_tcpip_if *ifp, bool drv_up, bool l2_up) {
bool cur_drv = (ifp->state6 != MG_TCPIP_STATE_DOWN);
bool cur_l2 = (ifp->state6 >= MG_TCPIP_STATE_UP);
if (drv_up != cur_drv || l2_up != cur_l2) { // link/L2 state has changed
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up && ifp->ip6ll[0] == 0 &&
ifp->ip6ll[1] == 0) { // gen ll address
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up &&
MG_IP6UNSPEC(ifp->ip6ll)) { // gen ll address
mg_l2_genip6(ifp->l2type, ifp->ip6ll, 64, ifp->mac);
memcpy(ifp->ip6ll, locpx, 8); // RFC-4291 2.5.6; RFC-2464 5
} // just got our link local address if we didn't have one.
// If static configuration is used, global addresses,
// prefix length, and gw are already filled at this point.
if (ifp->ip6[0] == 0 && ifp->ip6[1] == 0) ifp->enable_slaac = true;
if (MG_IP6UNSPEC(ifp->ip6)) ifp->enable_slaac = true;
if (!l2_up && ifp->enable_slaac) ifp->ip6[0] = ifp->ip6[1] = 0;
ifp->state6 = !drv_up ? MG_TCPIP_STATE_DOWN
: !l2_up ? MG_TCPIP_STATE_LINK_UP
@@ -11336,8 +11343,8 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
onstate6change(ifp);
}
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
(ifp->gw6[0] != 0 || ifp->gw6[1] != 0))
tx_ndp_ns(ifp, ifp->gw6, NULL); // retry GW hwaddr resolution
!MG_IP6UNSPEC(ifp->gw6))
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // retry GW hwaddr resolution
#endif
{ // poll driver and let L2 do its work, if any
@@ -11516,13 +11523,12 @@ void mg_connect_resolved(struct mg_connection *c) {
} // otherwise retain an existing bound port
#if MG_ENABLE_IPV6
if (c->rem.is_ip6) {
if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0 &&
c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
bool any = MG_IP6UNSPEC(c->loc.addr.ip6);
if (any && 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 if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0) {
// Use global address
} else if (any) { // Use global address
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
} // otherwise retain an existing bound address
} // otherwise retain an existing bound address
c->loc.is_ip6 = true;
} else
#endif
@@ -11544,7 +11550,8 @@ void mg_connect_resolved(struct mg_connection *c) {
&& !MG_IP6MATCH(c->rem.addr.ip6, ifp->gw6))) { // and not gw
// If we're in the same LAN, fire a Neighbor Solicitation
MG_DEBUG(("%lu NS lookup...", c->id));
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL); // RFC-4861 4.3, requesting
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL,
c->loc.addr.ip6[0] == ifp->ip6ll[0]); // RFC-4861 4.3, request
settmout(c, MIP_TTYPE_ARP);
c->is_arplooking = 1;
} else if (ifp->gw6_ready) {
+7 -4
View File
@@ -1152,7 +1152,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
while (c->pfn_data == NULL && ofs < c->recv.len) {
const char *buf = (char *) c->recv.buf + ofs;
int n = mg_http_parse(buf, c->recv.len - ofs, &hm);
struct mg_str *te; // Transfer - encoding header
struct mg_str *hcl, *te; // Content-Length and Transfer-encoding headers
bool is_chunked = false, is_http_1_0 = false;
size_t old_len = c->recv.len;
if (n < 0) {
@@ -1172,8 +1172,11 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
c->pfn = NULL;
return;
}
if (ev == MG_EV_CLOSE) { // If client did not set Content-Length
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG
hcl = mg_http_get_header(&hm, "Content-Length");
if (ev == MG_EV_CLOSE && (hcl == NULL || c->is_client)) {
// - a server responded to HEAD with optional Content-Length (no body)
// - a client sent no Content-Length and closed --> deliver MSG
hm.message.len = c->recv.len - ofs;
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
}
is_http_1_0 =
@@ -1187,7 +1190,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
mg_error(c, "Invalid Transfer-Encoding"); // See #2460
return;
}
} else if (mg_http_get_header(&hm, "Content-length") == NULL) {
} else if (hcl == NULL) {
// #2593: HTTP packets must contain either Transfer-Encoding or
// Content-length
bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0;
+3 -4
View File
@@ -440,9 +440,8 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
m->ack = p[1];
p += 2;
if (version == 5) {
if (p >= end) return MQTT_MALFORMED;
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
@@ -473,9 +472,9 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
p += 2;
}
if (p > end) return MQTT_MALFORMED;
if (version == 5 && p + 2 < end) {
if (version == 5) {
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
+37 -32
View File
@@ -380,6 +380,7 @@ static const struct mg_addr ip6_allnodes = {
static const uint8_t locpx[8] = {0xfe, 0x80, 0, 0, 0, 0, 0, 0};
#define MG_IP6MATCH(a, b) (a[0] == b[0] && a[1] == b[1])
#define MG_IP6UNSPEC(a) (a[0] == 0 && a[1] == 0)
#endif
static void settmout(struct mg_connection *c, uint8_t type) {
@@ -934,9 +935,9 @@ static void rx_ndp_ns(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// - use solicited node multicast to resolve a l2 address (l2_addr = NULL)
// - use unicast to verify presence (l2_addr = neighbor l2 address)
static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
uint8_t *l2_addr) {
uint8_t *l2_addr, bool ll) {
uint64_t *ip_src = ll ? ifp->ip6ll : ifp->ip6;
uint8_t payload[4 + 16 + 16]; // NOTE(): 16 --> optional len upto 2 hw addr
uint64_t ip_unspec[2] = {0, 0};
size_t payload_len = 20;
bool mcast = (l2_addr == NULL);
uint64_t ip_mcast[2] = {0, 0};
@@ -956,14 +957,12 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
l2 = mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6, &ipd);
}
payload_len = 20;
// TODO(robertc2000): using only link-local IP addr for now
// We might consider to add an option to use either link-local or global IP
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
if (!MG_IP6UNSPEC(ip_src)) {
payload[20] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
payload[21] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 22);
payload_len += 8 * payload[21];
}
tx_icmp6(ifp, l2, ifp->ip6ll, mcast ? ip_mcast : ip_dst, 135, 0, payload,
tx_icmp6(ifp, l2, ip_src, mcast ? ip_mcast : ip_dst, 135, 0, payload,
payload_len);
}
@@ -971,11 +970,10 @@ static void tx_ndp_ns(struct mg_tcpip_if *ifp, uint64_t *ip_dst,
static void tx_ndp_rs(struct mg_tcpip_if *ifp) {
uint8_t payload[4 + 16]; // reserved + optional len upto 2 hw addr NOTE()
size_t payload_len = 4;
uint64_t ip_unspec[2] = {0, 0};
memset(payload, 0, sizeof(payload));
if (!MG_IP6MATCH(ifp->ip6ll, ip_unspec)) {
if (!MG_IP6UNSPEC(ifp->ip6ll)) {
payload[4] = 1; // 4.6.1, source hwaddr; option length in 8-byte units
payload[5] = mg_l2_ip6put(ifp->l2type, ifp->mac, payload + 6);
payload_len += 8 * payload[5];
@@ -1144,7 +1142,7 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
(struct mg_addr *) &ip6_allnodes),
ifp->ip6ll, (uint64_t *) ip6_allnodes.addr.ip6, false,
ifp->mac);
if (ifp->ip6[0] != 0 && ifp->ip6[1] != 0)
if (!MG_IP6UNSPEC(ifp->ip6))
tx_ndp_na(ifp, // RFC-9131 4.1
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_MCAST6,
(struct mg_addr *) &ip6_allrouters),
@@ -1159,9 +1157,9 @@ static void onstate6change(struct mg_tcpip_if *ifp) {
ifp->is_ip6_changed = false;
}
} else if (ifp->state6 == MG_TCPIP_STATE_IP) {
if ((ifp->gw6[0] != 0 || ifp->gw6[1] != 0) &&
if (!MG_IP6UNSPEC(ifp->gw6) &&
(ifp->l2type != MG_TCPIP_L2_PPP && ifp->l2type != MG_TCPIP_L2_PPPoE))
tx_ndp_ns(ifp, ifp->gw6, NULL); // unsolicited GW hwaddr resolution
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // unsolicited GW hwaddr resolution
} else if (ifp->state6 == MG_TCPIP_STATE_UP) {
MG_INFO(("IP: %M", mg_print_ip6, &ifp->ip6ll));
}
@@ -1374,13 +1372,14 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
struct pkt *pkt, uint16_t mss) {
struct connstate *s;
uint8_t *l2addr;
struct mg_tcpip_if *ifp = lsn->mgr->ifp;
struct mg_connection *c = mg_alloc_conn(lsn->mgr);
if (c == NULL) {
MG_ERROR(("OOM"));
return NULL;
}
s = (struct connstate *) (c + 1);
s->retransmit = lsn->mgr->ifp->enable_tcp_retransmit;
s->retransmit = ifp->enable_tcp_retransmit;
s->txq.align = MG_TCPIP_TXQ_ALIGN;
s->dmss = mss; // from options in client SYN
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
@@ -1389,19 +1388,21 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn,
if (lsn->loc.is_ip6) {
c->rem.addr.ip6[0] = pkt->ip6->src[0],
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
c->loc.addr.ip6[0] = c->mgr->ifp->ip6[0],
c->loc.addr.ip6[1] = c->mgr->ifp->ip6[1], c->loc.is_ip6 = true;
// TODO(): compare lsn to link-local, or rem as link-local: use ll instead
if (pkt->ip6->dst[0] == ifp->ip6ll[0]) { // link-local prefix
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
} else {
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
}
c->loc.is_ip6 = true;
} else
#endif
{
c->rem.addr.ip4 = pkt->ip->src;
c->loc.addr.ip4 = c->mgr->ifp->ip;
c->loc.addr.ip4 = ifp->ip;
}
c->rem.port = pkt->tcp->sport;
c->loc.port = lsn->loc.port;
if ((l2addr = get_return_l2addr(lsn->mgr->ifp, &c->rem, false, pkt)) ==
NULL) {
if ((l2addr = get_return_l2addr(ifp, &c->rem, false, pkt)) == NULL) {
mg_free(c); // safety net for lousy networks, not actually needed
return NULL; // as path has already been checked at SYN (sending SYN+ACK)
}
@@ -1459,10 +1460,14 @@ static bool udp_send(struct mg_connection *c, const void *buf, size_t len) {
struct mg_addr ips;
memset(&ips, 0, sizeof(ips));
#if MG_ENABLE_IPV6
if (c->loc.is_ip6) {
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1],
if (c->loc.is_ip6) { // At [::] infer link-local from peer
if (MG_IP6UNSPEC(c->loc.addr.ip6) ? c->rem.addr.ip6[0] == ifp->ip6ll[0]
: c->loc.addr.ip6[0] == ifp->ip6ll[0]) {
ips.addr.ip6[0] = ifp->ip6ll[0], ips.addr.ip6[1] = ifp->ip6ll[1];
} else {
ips.addr.ip6[0] = ifp->ip6[0], ips.addr.ip6[1] = ifp->ip6[1];
}
ips.is_ip6 = true;
// TODO(): detect link-local (c->rem) and use it
} else
#endif
{
@@ -2123,14 +2128,14 @@ static void mg_ip6_link(struct mg_tcpip_if *ifp, bool drv_up, bool l2_up) {
bool cur_drv = (ifp->state6 != MG_TCPIP_STATE_DOWN);
bool cur_l2 = (ifp->state6 >= MG_TCPIP_STATE_UP);
if (drv_up != cur_drv || l2_up != cur_l2) { // link/L2 state has changed
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up && ifp->ip6ll[0] == 0 &&
ifp->ip6ll[1] == 0) { // gen ll address
if (ifp->l2type == MG_TCPIP_L2_ETH && l2_up &&
MG_IP6UNSPEC(ifp->ip6ll)) { // gen ll address
mg_l2_genip6(ifp->l2type, ifp->ip6ll, 64, ifp->mac);
memcpy(ifp->ip6ll, locpx, 8); // RFC-4291 2.5.6; RFC-2464 5
} // just got our link local address if we didn't have one.
// If static configuration is used, global addresses,
// prefix length, and gw are already filled at this point.
if (ifp->ip6[0] == 0 && ifp->ip6[1] == 0) ifp->enable_slaac = true;
if (MG_IP6UNSPEC(ifp->ip6)) ifp->enable_slaac = true;
if (!l2_up && ifp->enable_slaac) ifp->ip6[0] = ifp->ip6[1] = 0;
ifp->state6 = !drv_up ? MG_TCPIP_STATE_DOWN
: !l2_up ? MG_TCPIP_STATE_LINK_UP
@@ -2191,8 +2196,8 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
onstate6change(ifp);
}
if (expired_1000ms && ifp->state6 == MG_TCPIP_STATE_READY && !ifp->gw6_ready &&
(ifp->gw6[0] != 0 || ifp->gw6[1] != 0))
tx_ndp_ns(ifp, ifp->gw6, NULL); // retry GW hwaddr resolution
!MG_IP6UNSPEC(ifp->gw6))
tx_ndp_ns(ifp, ifp->gw6, NULL, true); // retry GW hwaddr resolution
#endif
{ // poll driver and let L2 do its work, if any
@@ -2371,13 +2376,12 @@ void mg_connect_resolved(struct mg_connection *c) {
} // otherwise retain an existing bound port
#if MG_ENABLE_IPV6
if (c->rem.is_ip6) {
if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0 &&
c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
bool any = MG_IP6UNSPEC(c->loc.addr.ip6);
if (any && 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 if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0) {
// Use global address
} else if (any) { // Use global address
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
} // otherwise retain an existing bound address
} // otherwise retain an existing bound address
c->loc.is_ip6 = true;
} else
#endif
@@ -2399,7 +2403,8 @@ void mg_connect_resolved(struct mg_connection *c) {
&& !MG_IP6MATCH(c->rem.addr.ip6, ifp->gw6))) { // and not gw
// If we're in the same LAN, fire a Neighbor Solicitation
MG_DEBUG(("%lu NS lookup...", c->id));
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL); // RFC-4861 4.3, requesting
tx_ndp_ns(ifp, c->rem.addr.ip6, NULL,
c->loc.addr.ip6[0] == ifp->ip6ll[0]); // RFC-4861 4.3, request
settmout(c, MIP_TTYPE_ARP);
c->is_arplooking = 1;
} else if (ifp->gw6_ready) {
+24
View File
@@ -328,6 +328,8 @@ static void init_tests(struct mg_mgr *mgr, struct eth *e, struct ipp *ipp,
mif->driver_data = &s_driver_data;
#if MG_ENABLE_IPV6
if (ipp->ip6 != NULL) {
mif->ip6ll[0] = mg_htonll(0xfe80000000000000);
mif->ip6ll[1] = mg_htonll(1);
mif->ip6[0] = mg_htonll(0x100000000000000);
mif->prefix[0] = 1;
mif->prefix_len = 64;
@@ -664,6 +666,16 @@ static void test_tcp_basics(bool ipv6) {
ASSERT((t->ack == mg_htonl(1002)));
// make sure it is closed
ASSERT(mgr.conns->next == NULL); // only one connection: the listener
#if MG_ENABLE_IPV6
// Test we use link-local address for link-local peers
if (ipv6) {
ip6.src[0] = mif.ip6ll[0], ip6.src[1] = mg_htonll(2);
ip6.dst[0] = mif.ip6ll[0], ip6.dst[1] = mif.ip6ll[1];
init_tcp_handshake(&e, &ipp, &mgr);
ASSERT(mgr.conns->loc.addr.ip6[0] == mif.ip6ll[0] &&
mgr.conns->loc.addr.ip6[1] == mif.ip6ll[1]);
}
#endif
s_driver_data.len = 0;
mg_mgr_free(&mgr);
@@ -1513,6 +1525,18 @@ static void test_udp(bool ipv6) {
ASSERT((uint8_t) (((i6->ver & 15) << 2) | (i6->label[0] >> 6)) ==
MG_DSCP_AF(4, 1));
ASSERT(udp6csum_ok(i6, u));
#if MG_ENABLE_IPV6
// Test we use link-local address for link-local peers
mgr.conns->recv.len = 0;
ip6.src[0] = mif.ip6ll[0], ip6.src[1] = mg_htonll(2);
ip6.dst[0] = mif.ip6ll[0], ip6.dst[1] = mif.ip6ll[1];
s_driver_data.len = 0;
create_udp_dat(&e, &ipp, 1, 888, 2);
mg_mgr_poll(&mgr, 0);
ASSERT(received_response(&s_driver_data));
ASSERT(i6->src[0] == mif.ip6ll[0] && i6->src[1] == mif.ip6ll[1] &&
i6->dst[0] == ip6.src[0] && i6->dst[1] == ip6.src[1]);
#endif
} else {
ASSERT(i->src == mg_htonl(0x1000000) && i->dst == mg_htonl(0x2000000));
ASSERT(i->tos == (MG_DSCP_AF(4, 1) << 2));
+16
View File
@@ -817,6 +817,12 @@ static void test_mqtt_parse(void) {
struct mg_mqtt_prop prop;
// MQTT5 PUBLISH with Subscription Identifier = 5
const uint8_t sub_id_pkt[] = {0x30, 0x06, 0x00, 0x01, 'x', 0x02, 0x0B, 0x05};
// MQTT5 PUBLISH with no properties and no payload
const uint8_t pub_empty[] = {0x30, 0x04, 0x00, 0x01, 'x', 0x00};
// MQTT5 PUBLISH with no properties and a one-byte payload
const uint8_t pub_byte[] = {0x30, 0x05, 0x00, 0x01, 'x', 0x00, 'y'};
// MQTT5 PUBLISH without its mandatory Property Length
const uint8_t pub_no_props[] = {0x30, 0x03, 0x00, 0x01, 'x'};
// MQTT5 CONNACK with Server Keep Alive = 60
const uint8_t connack_pkt[] = {0x20, 0x06, 0x00, 0x00, 0x03, 0x13, 0x00, 0x3C};
@@ -825,6 +831,13 @@ static void test_mqtt_parse(void) {
ASSERT(mg_mqtt_parse(sub_id_pkt, sizeof(sub_id_pkt), 5, &mm) == MQTT_OK);
mg_mqtt_next_prop(&mm, &prop, 0);
ASSERT(mg_mqtt_parse(pub_empty, sizeof(pub_empty), 5, &mm) == MQTT_OK);
ASSERT(mm.props_size == 0 && mm.data.len == 0);
ASSERT(mg_mqtt_parse(pub_byte, sizeof(pub_byte), 5, &mm) == MQTT_OK);
ASSERT(mm.props_size == 0 && mm.data.len == 1 && mm.data.buf[0] == 'y');
ASSERT(mg_mqtt_parse(pub_no_props, sizeof(pub_no_props), 5, &mm) ==
MQTT_MALFORMED);
ASSERT(mg_mqtt_parse(connack_pkt, sizeof(connack_pkt), 5, &mm) == MQTT_OK);
ASSERT(mm.ack == 0);
ASSERT(mg_mqtt_next_prop(&mm, &prop, 0) > 0);
@@ -1277,6 +1290,9 @@ static void test_http_server(void) {
"POST /body HTTP/1.1\r\n"
"Content-Length: 4\r\n\r\nkuku") == 200);
ASSERT(cmpbody(buf, "kuku") == 0);
ASSERT(fetch(&mgr, buf, url, "%s",
"POST /body HTTP/1.1\r\n"
"Content-Length: 4\r\n\r\nku") == 0);
ASSERT(fetch(&mgr, buf, url, "GET /ssi HTTP/1.1\r\n\r\n") == 301);
ASSERT(fetch(&mgr, buf, url, "GET /ssi/ HTTP/1.1\r\n\r\n") == 200);