Skip to content

Commit

Permalink
nanocoap_sock: use random message IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Apr 20, 2022
1 parent 25c9b5a commit 75787a7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string.h>
#include <stdio.h>

#include "atomic_utils.h"
#include "net/nanocoap_sock.h"
#include "net/sock/util.h"
#include "net/sock/udp.h"
Expand All @@ -46,6 +47,14 @@ 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) {
Expand Down Expand Up @@ -231,7 +240,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;
Expand Down Expand Up @@ -310,8 +319,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);
Expand Down

0 comments on commit 75787a7

Please sign in to comment.