Skip to content

Commit 13fd806

Browse files
committed
nanocoap_sock: add option to include token for block-wise
1 parent 0fb153d commit 13fd806

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

sys/include/net/nanocoap_sock.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ extern "C" {
175175
#define CONFIG_NANOCOAP_SERVER_STACK_SIZE THREAD_STACKSIZE_DEFAULT
176176
#endif
177177

178+
/**
179+
* @brief Include a random token with block-wise transfers.
180+
*
181+
* This is a workaround for buggy CoPA implementations (e.g. go-coap) that expect
182+
* to identify block-wise transfers based on the token.
183+
*
184+
* See https://github.com/plgd-dev/go-coap/issues/512
185+
*/
186+
#ifndef CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
187+
#define CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN (0)
188+
#endif
189+
178190
/**
179191
* @brief NanoCoAP socket types
180192
*/

sys/net/application_layer/nanocoap/sock.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ typedef struct {
6060
coap_blockwise_cb_t callback;
6161
void *arg;
6262
bool more;
63+
#if CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
64+
uint8_t token[4];
65+
#endif
6366
} _block_ctx_t;
6467

6568
int nanocoap_sock_dtls_connect(nanocoap_sock_t *sock, sock_udp_ep_t *local,
@@ -601,7 +604,17 @@ static int _fetch_block(nanocoap_sock_t *sock, uint8_t *buf, size_t len,
601604
};
602605
uint16_t lastonum = 0;
603606

604-
buf += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET,
607+
void *token = NULL;
608+
size_t token_len = 0;
609+
610+
#if CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
611+
/* HACK: go-coap always expects a token */
612+
/* see https://github.com/plgd-dev/go-coap/issues/512 */
613+
token = ctx->token;
614+
token_len = sizeof(ctx->token);
615+
#endif
616+
617+
buf += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, token, token_len, COAP_METHOD_GET,
605618
nanocoap_sock_next_msg_id(sock));
606619
buf += coap_opt_put_uri_pathquery(buf, &lastonum, path);
607620
buf += coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2, (num << 4) | blksize);
@@ -674,6 +687,10 @@ int nanocoap_sock_get_blockwise(nanocoap_sock_t *sock, const char *path,
674687
.more = true,
675688
};
676689

690+
#if CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
691+
random_bytes(ctx.token, sizeof(ctx.token));
692+
#endif
693+
677694
unsigned num = 0;
678695
while (ctx.more) {
679696
DEBUG("nanocoap: fetching block %u\n", num);

0 commit comments

Comments
 (0)