1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

nanocoap_sock: add option to include token for block-wise

This commit is contained in:
Benjamin Valentin 2024-04-10 12:36:53 +02:00
parent 0fb153daca
commit 13fd806f4f
2 changed files with 30 additions and 1 deletions

View File

@ -175,6 +175,18 @@ extern "C" {
#define CONFIG_NANOCOAP_SERVER_STACK_SIZE THREAD_STACKSIZE_DEFAULT
#endif
/**
* @brief Include a random token with block-wise transfers.
*
* This is a workaround for buggy CoPA implementations (e.g. go-coap) that expect
* to identify block-wise transfers based on the token.
*
* See https://github.com/plgd-dev/go-coap/issues/512
*/
#ifndef CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
#define CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN (0)
#endif
/**
* @brief NanoCoAP socket types
*/

View File

@ -60,6 +60,9 @@ typedef struct {
coap_blockwise_cb_t callback;
void *arg;
bool more;
#if CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
uint8_t token[4];
#endif
} _block_ctx_t;
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,
};
uint16_t lastonum = 0;
buf += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET,
void *token = NULL;
size_t token_len = 0;
#if CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
/* HACK: go-coap always expects a token */
/* see https://github.com/plgd-dev/go-coap/issues/512 */
token = ctx->token;
token_len = sizeof(ctx->token);
#endif
buf += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, token, token_len, COAP_METHOD_GET,
nanocoap_sock_next_msg_id(sock));
buf += coap_opt_put_uri_pathquery(buf, &lastonum, path);
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,
.more = true,
};
#if CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
random_bytes(ctx.token, sizeof(ctx.token));
#endif
unsigned num = 0;
while (ctx.more) {
DEBUG("nanocoap: fetching block %u\n", num);