Skip to content

Commit ad8dd8d

Browse files
committed
nanocoap_sock: ensure response address is the same as request address
1 parent a7866f7 commit ad8dd8d

1 file changed

Lines changed: 35 additions & 14 deletions

File tree

  • sys/net/application_layer/nanocoap

sys/net/application_layer/nanocoap/sock.c

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -796,23 +796,44 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
796796
}
797797

798798
while (1) {
799-
res = sock_udp_recv(&sock.udp, buf, bufsize, -1, &remote);
800-
if (res < 0) {
799+
800+
sock_udp_aux_rx_t *aux_in_ptr = NULL;
801+
#ifdef MODULE_SOCK_AUX_LOCAL
802+
sock_udp_aux_rx_t aux_in = {
803+
.flags = SOCK_AUX_GET_LOCAL,
804+
};
805+
aux_in_ptr = &aux_in;
806+
#endif
807+
808+
res = sock_udp_recv_aux(&sock.udp, buf, bufsize, SOCK_NO_TIMEOUT,
809+
&remote, aux_in_ptr);
810+
if (res <= 0) {
801811
DEBUG("error receiving UDP packet %d\n", (int)res);
812+
continue;
802813
}
803-
else if (res > 0) {
804-
coap_pkt_t pkt;
805-
if (coap_parse(&pkt, (uint8_t *)buf, res) < 0) {
806-
DEBUG("error parsing packet\n");
807-
continue;
808-
}
809-
if ((res = coap_handle_req(&pkt, buf, bufsize, &ctx)) > 0) {
810-
sock_udp_send(&sock.udp, buf, res, &remote);
811-
}
812-
else {
813-
DEBUG("error handling request %d\n", (int)res);
814-
}
814+
coap_pkt_t pkt;
815+
if (coap_parse(&pkt, (uint8_t *)buf, res) < 0) {
816+
DEBUG("error parsing packet\n");
817+
continue;
818+
}
819+
if ((res = coap_handle_req(&pkt, buf, bufsize, &ctx)) <= 0) {
820+
DEBUG("error handling request %d\n", (int)res);
821+
continue;
815822
}
823+
824+
sock_udp_aux_tx_t *aux_out_ptr = NULL;
825+
#ifdef MODULE_SOCK_AUX_LOCAL
826+
/* make sure we reply with the same address that the request was
827+
* destined for -- except in the multicast case */
828+
sock_udp_aux_tx_t aux_out = {
829+
.flags = SOCK_AUX_SET_LOCAL,
830+
.local = aux_in.local,
831+
};
832+
if (!sock_udp_ep_is_multicast(&aux_in.local)) {
833+
aux_out_ptr = &aux_out;
834+
}
835+
#endif
836+
sock_udp_send_aux(&sock.udp, buf, res, &remote, aux_out_ptr);
816837
}
817838

818839
return 0;

0 commit comments

Comments
 (0)