Compare commits

...
5 Commits
Author SHA1 Message Date
Sergio R. Caprile 63ba221588 map DSCP into capable L2s 2026-12-04 12:49:46 -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
10 changed files with 98 additions and 66 deletions

No files matched your search

+41 -33
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;
@@ -6131,7 +6134,8 @@ size_t mg_jwt_verify_es256(struct mg_str jwt, const struct mg_jwt_opts *opts,
void mg_l2_init(struct mg_tcpip_if *ifp);
bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms);
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *src, uint8_t *dst, uint8_t *frame);
uint8_t *src, uint8_t *dst, uint8_t *frame,
uint8_t dscp);
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw);
@@ -6150,7 +6154,7 @@ size_t mg_l2_driver_output(struct mg_tcpip_if *ifp, size_t len);
// clang-format off
extern void mg_l2_eth_init(struct mg_tcpip_if *);
extern bool mg_l2_eth_poll(struct mg_tcpip_if *, bool);
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
extern size_t mg_l2_eth_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
extern bool mg_l2_eth_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
extern struct mg_l2addr *mg_l2_eth_getaddr(struct mg_tcpip_if *, uint8_t *);
@@ -6163,7 +6167,7 @@ extern uint8_t mg_l2_eth_ip6put(struct mg_l2addr *, uint8_t *);
extern void mg_l2_ppp_init(struct mg_tcpip_if *);
extern bool mg_l2_ppp_poll(struct mg_tcpip_if *, bool);
extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
extern size_t mg_l2_ppp_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
extern bool mg_l2_ppp_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
extern struct mg_l2addr *mg_l2_ppp_getaddr(struct mg_tcpip_if *, uint8_t *);
@@ -6175,13 +6179,13 @@ extern uint8_t mg_l2_ppp_ip6put(struct mg_l2addr *, uint8_t *);
#endif
extern void mg_l2_pppoe_init(struct mg_tcpip_if *);
extern bool mg_l2_pppoe_poll(struct mg_tcpip_if *, bool);
extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
extern size_t mg_l2_pppoe_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
extern bool mg_l2_pppoe_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
typedef void (*l2_init_fn)(struct mg_tcpip_if *);
typedef bool (*l2_poll_fn)(struct mg_tcpip_if *, bool);
typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *));
typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t));
typedef size_t (*l2_trailer_fn)(struct mg_tcpip_if *, size_t, uint8_t *);
typedef bool (*l2_rx_fn)(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
typedef struct mg_l2addr (*(*l2_getaddr_fn)(struct mg_tcpip_if *, uint8_t *));
@@ -6224,9 +6228,10 @@ bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
}
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *src, uint8_t *dst, uint8_t *frame) {
uint8_t *src, uint8_t *dst, uint8_t *frame,
uint8_t dscp) {
return l2_header[ifp->l2type](ifp, proto, (struct mg_l2addr *) src,
(struct mg_l2addr *) dst, frame);
(struct mg_l2addr *) dst, frame, dscp);
}
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *frame) {
@@ -6340,7 +6345,7 @@ bool mg_l2_eth_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame) {
uint8_t *frame, uint8_t dscp) {
struct eth_data *d = &ifp->l2data.eth;
struct eth *eth = (struct eth *) frame;
uint8_t *hlp;
@@ -6352,7 +6357,8 @@ uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
} else { // Add 802.1Q tag
struct qtag *qtag = (struct qtag *) &eth->type;
qtag->tpid = mg_htons(0x8100);
qtag->tci = mg_htons(d->vlan_id); // PCP = default (best-effort)
qtag->tci = mg_htons((uint16_t) ((d->vlan_id & 0x1fff) | // VID + DEI
(dscp >> 3) << 13)); // RFC-9435 5.1.1
hlp += sizeof(*qtag);
MG_STORE_BE16(qtag + 1, eth_types[(unsigned int) proto]);
}
@@ -6631,22 +6637,24 @@ static uint8_t *l2_ppp_header(enum mg_l2proto proto, uint8_t *p) {
uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame) {
uint8_t *frame, uint8_t dscp) {
(void) ifp;
(void) src;
(void) dst;
(void) dscp;
return l2_ppp_header(proto, hdlc_header(frame));
}
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame);
uint8_t *frame, uint8_t dscp);
static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t code, uint16_t id, struct mg_l2addr *src,
struct mg_l2addr *dst, uint8_t *frame) {
struct mg_l2addr *dst, uint8_t *frame,
uint8_t dscp) {
struct pppoe *pppoe =
(struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame);
(struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame, dscp);
pppoe->vertype = 0x11;
pppoe->code = code;
pppoe->id = id;
@@ -6655,10 +6663,10 @@ static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame) {
uint8_t *frame, uint8_t dscp) {
(void) dst;
return l2_ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
s_id, src, dst, frame));
s_id, src, dst, frame, dscp));
}
size_t mg_l2_ppp_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) {
@@ -6694,7 +6702,8 @@ static uint8_t *ppp_tx_frame_header(struct mg_tcpip_if *ifp, uint16_t proto) {
return ppp_header(proto, hdlc_header(l2p));
return ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
s_id, (struct mg_l2addr *) ifp->mac,
(struct mg_l2addr *) ifp->gwmac, l2p));
(struct mg_l2addr *) ifp->gwmac, l2p,
0));
}
static size_t ppp_tx_frame_trailer(struct mg_tcpip_if *ifp, size_t len,
uint8_t *cur) {
@@ -7009,7 +7018,7 @@ static size_t pppoe_tx_frame(struct mg_tcpip_if *ifp, uint8_t code, uint16_t id,
struct mg_l2addr *dst) {
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
uint8_t *p = pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_DISC, code, id,
(struct mg_l2addr *) ifp->mac, dst, l2p);
(struct mg_l2addr *) ifp->mac, dst, l2p, 0);
memmove(p, data, datasz);
return mg_l2_driver_output(ifp, pppoe_trailer(ifp, datasz, p + datasz));
}
@@ -8591,9 +8600,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 +8632,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;
@@ -9372,7 +9380,7 @@ struct pkt {
// L2 API
void mg_l2_init(struct mg_tcpip_if *ifp);
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *src, uint8_t *dst, uint8_t *frame);
uint8_t *src, uint8_t *dst, uint8_t *frame, uint8_t dscp);
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw);
@@ -9552,7 +9560,7 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac) {
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct arp *arp = (struct arp *) mg_l2_header(
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p);
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p, 0);
memset(arp, 0, sizeof(*arp));
arp->fmt = mg_htons(1), arp->pro = mg_htons(0x800), arp->hlen = 6,
arp->plen = 4;
@@ -9599,7 +9607,7 @@ static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *l2_dst, uint8_t proto,
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct ip *ip = (struct ip *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV4,
ifp->mac, l2_dst, l2p);
ifp->mac, l2_dst, l2p, dscp);
memset(ip, 0, sizeof(*ip));
ip->ver = 0x45; // Version 4, header length 5 words
ip->tos = (uint8_t) (dscp << 2);
@@ -9775,8 +9783,9 @@ static void rx_arp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// ARP request. Make a response, then send
// MG_VERBOSE(("ARP req from %M", mg_print_ip4, &pkt->arp->spa));
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct arp *arp = (struct arp *) mg_l2_header(
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac, mg_l2_getaddr(ifp, pkt->l2), l2p);
struct arp *arp =
(struct arp *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
mg_l2_getaddr(ifp, pkt->l2), l2p, 0);
*arp = *pkt->arp;
arp->op = mg_htons(2);
memcpy(arp->tha, pkt->arp->sha, sizeof(pkt->arp->tha));
@@ -9974,7 +9983,7 @@ static struct ip6 *tx_ip6(struct mg_tcpip_if *ifp, uint8_t *l2_dst,
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct ip6 *ip6 = (struct ip6 *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV6,
ifp->mac, l2_dst, l2p);
ifp->mac, l2_dst, l2p, dscp);
memset(ip6, 0, sizeof(*ip6));
ip6->ver = (uint8_t) (0x60 | (dscp >> 2)); // Version 6, traffic class
ip6->label[0] = (uint8_t) (dscp << 6);
@@ -18508,7 +18517,6 @@ static void mg_tls_derive_secret(const char *label, uint8_t *key, size_t keysz,
size_t labelsz = strlen(label);
uint8_t secret[32];
uint8_t packed[256] = {0, (uint8_t) hashsz, (uint8_t) labelsz};
// TODO: assert lengths of label, key, data and hash
if (labelsz > 0) memmove(packed + 3, label, labelsz);
packed[3 + labelsz] = (uint8_t) datasz;
if (datasz > 0) memmove(packed + labelsz + 4, data, datasz);
@@ -31082,7 +31090,7 @@ static size_t mg_cyw_tx(unsigned int ifc, void *data, size_t len) {
uint16_t txlen = (uint16_t) (len + sizeof(*hdr));
memset(txdata, 0, sizeof(*hdr));
memcpy((uint8_t *) txdata + sizeof(*hdr), data, len);
// TODO(): hdr->bdc.priority = map IP to TOS if supporting QoS/ToS
// TODO(): hdr->bdc.priority = dscp >> 3; // RFC-9435 5.1.1
hdr->bdc.flags = 2 << 4; // BDC version 2
hdr->bdc.flags2 = (uint8_t) ifc; // 0 -> STA, 1 -> AP
// hdr->bdc.data_offset = 0; // actually zeroed above
+1 -1
View File
@@ -225,7 +225,7 @@ static size_t mg_cyw_tx(unsigned int ifc, void *data, size_t len) {
uint16_t txlen = (uint16_t) (len + sizeof(*hdr));
memset(txdata, 0, sizeof(*hdr));
memcpy((uint8_t *) txdata + sizeof(*hdr), data, len);
// TODO(): hdr->bdc.priority = map IP to TOS if supporting QoS/ToS
// TODO(): hdr->bdc.priority = dscp >> 3; // RFC-9435 5.1.1
hdr->bdc.flags = 2 << 4; // BDC version 2
hdr->bdc.flags2 = (uint8_t) ifc; // 0 -> STA, 1 -> AP
// hdr->bdc.data_offset = 0; // actually zeroed above
+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;
+9 -7
View File
@@ -8,7 +8,8 @@
void mg_l2_init(struct mg_tcpip_if *ifp);
bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms);
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *src, uint8_t *dst, uint8_t *frame);
uint8_t *src, uint8_t *dst, uint8_t *frame,
uint8_t dscp);
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw);
@@ -27,7 +28,7 @@ size_t mg_l2_driver_output(struct mg_tcpip_if *ifp, size_t len);
// clang-format off
extern void mg_l2_eth_init(struct mg_tcpip_if *);
extern bool mg_l2_eth_poll(struct mg_tcpip_if *, bool);
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
extern size_t mg_l2_eth_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
extern bool mg_l2_eth_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
extern struct mg_l2addr *mg_l2_eth_getaddr(struct mg_tcpip_if *, uint8_t *);
@@ -40,7 +41,7 @@ extern uint8_t mg_l2_eth_ip6put(struct mg_l2addr *, uint8_t *);
extern void mg_l2_ppp_init(struct mg_tcpip_if *);
extern bool mg_l2_ppp_poll(struct mg_tcpip_if *, bool);
extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
extern uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
extern size_t mg_l2_ppp_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
extern bool mg_l2_ppp_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
extern struct mg_l2addr *mg_l2_ppp_getaddr(struct mg_tcpip_if *, uint8_t *);
@@ -52,13 +53,13 @@ extern uint8_t mg_l2_ppp_ip6put(struct mg_l2addr *, uint8_t *);
#endif
extern void mg_l2_pppoe_init(struct mg_tcpip_if *);
extern bool mg_l2_pppoe_poll(struct mg_tcpip_if *, bool);
extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *);
extern uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t);
extern size_t mg_l2_pppoe_trailer(struct mg_tcpip_if *, size_t, uint8_t *);
extern bool mg_l2_pppoe_rx(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
typedef void (*l2_init_fn)(struct mg_tcpip_if *);
typedef bool (*l2_poll_fn)(struct mg_tcpip_if *, bool);
typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *));
typedef uint8_t *((*l2_header_fn)(struct mg_tcpip_if *, enum mg_l2proto, struct mg_l2addr *, struct mg_l2addr *, uint8_t *, uint8_t));
typedef size_t (*l2_trailer_fn)(struct mg_tcpip_if *, size_t, uint8_t *);
typedef bool (*l2_rx_fn)(struct mg_tcpip_if *, enum mg_l2proto *, struct mg_str *, struct mg_str *);
typedef struct mg_l2addr (*(*l2_getaddr_fn)(struct mg_tcpip_if *, uint8_t *));
@@ -101,9 +102,10 @@ bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
}
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *src, uint8_t *dst, uint8_t *frame) {
uint8_t *src, uint8_t *dst, uint8_t *frame,
uint8_t dscp) {
return l2_header[ifp->l2type](ifp, proto, (struct mg_l2addr *) src,
(struct mg_l2addr *) dst, frame);
(struct mg_l2addr *) dst, frame, dscp);
}
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *frame) {
+3 -2
View File
@@ -63,7 +63,7 @@ bool mg_l2_eth_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame) {
uint8_t *frame, uint8_t dscp) {
struct eth_data *d = &ifp->l2data.eth;
struct eth *eth = (struct eth *) frame;
uint8_t *hlp;
@@ -75,7 +75,8 @@ uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
} else { // Add 802.1Q tag
struct qtag *qtag = (struct qtag *) &eth->type;
qtag->tpid = mg_htons(0x8100);
qtag->tci = mg_htons(d->vlan_id); // PCP = default (best-effort)
qtag->tci = mg_htons((uint16_t) ((d->vlan_id & 0x1fff) | // VID + DEI
(dscp >> 3) << 13)); // RFC-9435 5.1.1
hlp += sizeof(*qtag);
MG_STORE_BE16(qtag + 1, eth_types[(unsigned int) proto]);
}
+11 -8
View File
@@ -139,22 +139,24 @@ static uint8_t *l2_ppp_header(enum mg_l2proto proto, uint8_t *p) {
uint8_t *mg_l2_ppp_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame) {
uint8_t *frame, uint8_t dscp) {
(void) ifp;
(void) src;
(void) dst;
(void) dscp;
return l2_ppp_header(proto, hdlc_header(frame));
}
extern uint8_t *mg_l2_eth_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame);
uint8_t *frame, uint8_t dscp);
static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t code, uint16_t id, struct mg_l2addr *src,
struct mg_l2addr *dst, uint8_t *frame) {
struct mg_l2addr *dst, uint8_t *frame,
uint8_t dscp) {
struct pppoe *pppoe =
(struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame);
(struct pppoe *) mg_l2_eth_header(ifp, proto, src, dst, frame, dscp);
pppoe->vertype = 0x11;
pppoe->code = code;
pppoe->id = id;
@@ -163,10 +165,10 @@ static uint8_t *pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *mg_l2_pppoe_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
struct mg_l2addr *src, struct mg_l2addr *dst,
uint8_t *frame) {
uint8_t *frame, uint8_t dscp) {
(void) dst;
return l2_ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
s_id, src, dst, frame));
s_id, src, dst, frame, dscp));
}
size_t mg_l2_ppp_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) {
@@ -202,7 +204,8 @@ static uint8_t *ppp_tx_frame_header(struct mg_tcpip_if *ifp, uint16_t proto) {
return ppp_header(proto, hdlc_header(l2p));
return ppp_header(proto, pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_SESS, 0,
s_id, (struct mg_l2addr *) ifp->mac,
(struct mg_l2addr *) ifp->gwmac, l2p));
(struct mg_l2addr *) ifp->gwmac, l2p,
0));
}
static size_t ppp_tx_frame_trailer(struct mg_tcpip_if *ifp, size_t len,
uint8_t *cur) {
@@ -517,7 +520,7 @@ static size_t pppoe_tx_frame(struct mg_tcpip_if *ifp, uint8_t code, uint16_t id,
struct mg_l2addr *dst) {
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
uint8_t *p = pppoe_header(ifp, MG_TCPIP_L2PROTO_PPPoE_DISC, code, id,
(struct mg_l2addr *) ifp->mac, dst, l2p);
(struct mg_l2addr *) ifp->mac, dst, l2p, 0);
memmove(p, data, datasz);
return mg_l2_driver_output(ifp, pppoe_trailer(ifp, datasz, p + datasz));
}
+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;
+7 -6
View File
@@ -227,7 +227,7 @@ struct pkt {
// L2 API
void mg_l2_init(struct mg_tcpip_if *ifp);
uint8_t *mg_l2_header(struct mg_tcpip_if *ifp, enum mg_l2proto proto,
uint8_t *src, uint8_t *dst, uint8_t *frame);
uint8_t *src, uint8_t *dst, uint8_t *frame, uint8_t dscp);
size_t mg_l2_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur);
bool mg_l2_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
struct mg_str *pay, struct mg_str *raw);
@@ -407,7 +407,7 @@ void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac) {
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct arp *arp = (struct arp *) mg_l2_header(
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p);
mg_l2_mapip(ifp->l2type, MG_TCPIP_L2ADDR_BCAST, NULL), l2p, 0);
memset(arp, 0, sizeof(*arp));
arp->fmt = mg_htons(1), arp->pro = mg_htons(0x800), arp->hlen = 6,
arp->plen = 4;
@@ -454,7 +454,7 @@ static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *l2_dst, uint8_t proto,
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct ip *ip = (struct ip *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV4,
ifp->mac, l2_dst, l2p);
ifp->mac, l2_dst, l2p, dscp);
memset(ip, 0, sizeof(*ip));
ip->ver = 0x45; // Version 4, header length 5 words
ip->tos = (uint8_t) (dscp << 2);
@@ -630,8 +630,9 @@ static void rx_arp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// ARP request. Make a response, then send
// MG_VERBOSE(("ARP req from %M", mg_print_ip4, &pkt->arp->spa));
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct arp *arp = (struct arp *) mg_l2_header(
ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac, mg_l2_getaddr(ifp, pkt->l2), l2p);
struct arp *arp =
(struct arp *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_ARP, ifp->mac,
mg_l2_getaddr(ifp, pkt->l2), l2p, 0);
*arp = *pkt->arp;
arp->op = mg_htons(2);
memcpy(arp->tha, pkt->arp->sha, sizeof(pkt->arp->tha));
@@ -829,7 +830,7 @@ static struct ip6 *tx_ip6(struct mg_tcpip_if *ifp, uint8_t *l2_dst,
// ifp->tx.buf is 8-bit aligned, keep other headers as pointers, see pkt
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
struct ip6 *ip6 = (struct ip6 *) mg_l2_header(ifp, MG_TCPIP_L2PROTO_IPV6,
ifp->mac, l2_dst, l2p);
ifp->mac, l2_dst, l2p, dscp);
memset(ip6, 0, sizeof(*ip6));
ip6->ver = (uint8_t) (0x60 | (dscp >> 2)); // Version 6, traffic class
ip6->label[0] = (uint8_t) (dscp << 6);
-1
View File
@@ -317,7 +317,6 @@ static void mg_tls_derive_secret(const char *label, uint8_t *key, size_t keysz,
size_t labelsz = strlen(label);
uint8_t secret[32];
uint8_t packed[256] = {0, (uint8_t) hashsz, (uint8_t) labelsz};
// TODO: assert lengths of label, key, data and hash
if (labelsz > 0) memmove(packed + 3, label, labelsz);
packed[3 + labelsz] = (uint8_t) datasz;
if (datasz > 0) memmove(packed + labelsz + 4, data, datasz);
+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);