Skip to content

Commit 0fe64fb

Browse files
authored
Merge pull request cesanta#3194 from cesanta/udpret
mg_send and mg_io_send now honor UDP failures
2 parents 64e8066 + 0b5c0c9 commit 0fe64fb

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

mongoose.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4407,12 +4407,13 @@ static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *mac_dst,
44074407
return ip;
44084408
}
44094409

4410-
static void tx_udp(struct mg_tcpip_if *ifp, uint8_t *mac_dst, uint32_t ip_src,
4410+
static bool tx_udp(struct mg_tcpip_if *ifp, uint8_t *mac_dst, uint32_t ip_src,
44114411
uint16_t sport, uint32_t ip_dst, uint16_t dport,
44124412
const void *buf, size_t len) {
44134413
struct ip *ip =
44144414
tx_ip(ifp, mac_dst, 17, ip_src, ip_dst, len + sizeof(struct udp));
44154415
struct udp *udp = (struct udp *) (ip + 1);
4416+
size_t eth_len;
44164417
// MG_DEBUG(("UDP XX LEN %d %d", (int) len, (int) ifp->tx.len));
44174418
udp->sport = sport;
44184419
udp->dport = dport;
@@ -4426,7 +4427,8 @@ static void tx_udp(struct mg_tcpip_if *ifp, uint8_t *mac_dst, uint32_t ip_src,
44264427
udp->csum = csumfin(cs);
44274428
memmove(udp + 1, buf, len);
44284429
// MG_DEBUG(("UDP LEN %d %d", (int) len, (int) ifp->frame_len));
4429-
ether_output(ifp, sizeof(struct eth) + sizeof(*ip) + sizeof(*udp) + len);
4430+
eth_len = sizeof(struct eth) + sizeof(*ip) + sizeof(*udp) + len;
4431+
return (ether_output(ifp, eth_len) == eth_len);
44304432
}
44314433

44324434
static void tx_dhcp(struct mg_tcpip_if *ifp, uint8_t *mac_dst, uint32_t ip_src,
@@ -4801,7 +4803,9 @@ long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
48014803
uint32_t dst_ip = *(uint32_t *) c->rem.ip;
48024804
len = trim_len(c, len);
48034805
if (c->is_udp) {
4804-
tx_udp(ifp, s->mac, ifp->ip, c->loc.port, dst_ip, c->rem.port, buf, len);
4806+
if (!tx_udp(ifp, s->mac, ifp->ip, c->loc.port, dst_ip, c->rem.port, buf,
4807+
len))
4808+
return MG_IO_WAIT;
48054809
} else { // TCP, cap to peer's MSS
48064810
size_t sent;
48074811
if (len > s->dmss) len = s->dmss; // RFC-6691: reduce if sending opts
@@ -5421,8 +5425,8 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
54215425
} else if (c->is_udp) {
54225426
struct connstate *s = (struct connstate *) (c + 1);
54235427
len = trim_len(c, len); // Trimming length if necessary
5424-
tx_udp(ifp, s->mac, ifp->ip, c->loc.port, rem_ip, c->rem.port, buf, len);
5425-
res = true;
5428+
res = tx_udp(ifp, s->mac, ifp->ip, c->loc.port, rem_ip, c->rem.port, buf,
5429+
len);
54265430
} else {
54275431
res = mg_iobuf_add(&c->send, c->send.len, buf, len);
54285432
}

src/net_builtin.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,13 @@ static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *mac_dst,
241241
return ip;
242242
}
243243

244-
static void tx_udp(struct mg_tcpip_if *ifp, uint8_t *mac_dst, uint32_t ip_src,
244+
static bool tx_udp(struct mg_tcpip_if *ifp, uint8_t *mac_dst, uint32_t ip_src,
245245
uint16_t sport, uint32_t ip_dst, uint16_t dport,
246246
const void *buf, size_t len) {
247247
struct ip *ip =
248248
tx_ip(ifp, mac_dst, 17, ip_src, ip_dst, len + sizeof(struct udp));
249249
struct udp *udp = (struct udp *) (ip + 1);
250+
size_t eth_len;
250251
// MG_DEBUG(("UDP XX LEN %d %d", (int) len, (int) ifp->tx.len));
251252
udp->sport = sport;
252253
udp->dport = dport;
@@ -260,7 +261,8 @@ static void tx_udp(struct mg_tcpip_if *ifp, uint8_t *mac_dst, uint32_t ip_src,
260261
udp->csum = csumfin(cs);
261262
memmove(udp + 1, buf, len);
262263
// MG_DEBUG(("UDP LEN %d %d", (int) len, (int) ifp->frame_len));
263-
ether_output(ifp, sizeof(struct eth) + sizeof(*ip) + sizeof(*udp) + len);
264+
eth_len = sizeof(struct eth) + sizeof(*ip) + sizeof(*udp) + len;
265+
return (ether_output(ifp, eth_len) == eth_len);
264266
}
265267

266268
static void tx_dhcp(struct mg_tcpip_if *ifp, uint8_t *mac_dst, uint32_t ip_src,
@@ -635,7 +637,9 @@ long mg_io_send(struct mg_connection *c, const void *buf, size_t len) {
635637
uint32_t dst_ip = *(uint32_t *) c->rem.ip;
636638
len = trim_len(c, len);
637639
if (c->is_udp) {
638-
tx_udp(ifp, s->mac, ifp->ip, c->loc.port, dst_ip, c->rem.port, buf, len);
640+
if (!tx_udp(ifp, s->mac, ifp->ip, c->loc.port, dst_ip, c->rem.port, buf,
641+
len))
642+
return MG_IO_WAIT;
639643
} else { // TCP, cap to peer's MSS
640644
size_t sent;
641645
if (len > s->dmss) len = s->dmss; // RFC-6691: reduce if sending opts
@@ -1255,8 +1259,8 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
12551259
} else if (c->is_udp) {
12561260
struct connstate *s = (struct connstate *) (c + 1);
12571261
len = trim_len(c, len); // Trimming length if necessary
1258-
tx_udp(ifp, s->mac, ifp->ip, c->loc.port, rem_ip, c->rem.port, buf, len);
1259-
res = true;
1262+
res = tx_udp(ifp, s->mac, ifp->ip, c->loc.port, rem_ip, c->rem.port, buf,
1263+
len);
12601264
} else {
12611265
res = mg_iobuf_add(&c->send, c->send.len, buf, len);
12621266
}

0 commit comments

Comments
 (0)