From 76e2500b7d560ff75b1d58f7bfe2568d2dc7c6f8 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 14 Apr 2022 15:40:29 +0200 Subject: [PATCH] nanocoap_sock: use random message IDs --- sys/net/application_layer/nanocoap/sock.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index fba50b8f5d..04fa9a9e6c 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -25,6 +25,7 @@ #include #include +#include "atomic_utils.h" #include "net/nanocoap_sock.h" #include "net/sock/util.h" #include "net/sock/udp.h" @@ -46,6 +47,13 @@ typedef struct { bool more; } _block_ctx_t; +static uint16_t _get_id(void) +{ + __attribute__((section(".noinit"))) + static uint16_t id; + return atomic_fetch_add_u16(&id, 1); +} + int nanocoap_sock_connect(nanocoap_sock_t *sock, sock_udp_ep_t *local, sock_udp_ep_t *remote) { if (!remote->port) { @@ -231,7 +239,7 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si }; pkt.hdr = buf; - pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, 1); + pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, _get_id()); pktpos += coap_opt_put_uri_path(pktpos, 0, path); pkt.payload = pktpos; pkt.payload_len = 0; @@ -309,8 +317,7 @@ static int _fetch_block(coap_pkt_t *pkt, sock_udp_t *sock, uint8_t *pktpos = (void *)pkt->hdr; uint16_t lastonum = 0; - pktpos += coap_build_hdr(pkt->hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, - num); + pktpos += coap_build_hdr(pkt->hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, _get_id()); pktpos += coap_opt_put_uri_pathquery(pktpos, &lastonum, path); pktpos += coap_opt_put_uint(pktpos, lastonum, COAP_OPT_BLOCK2, (num << 4) | blksize);