Skip to content

Commit

Permalink
coap_block.c: Handle incorrect SZX changes for Block2
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed Nov 30, 2023
1 parent 0f3ae50 commit a141e44
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions src/coap_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -2366,24 +2366,30 @@ coap_handle_request_send_block(coap_session_t *session,
1 << (block.szx + 4), block.num, block.m);
}
if (block.bert == 0 && block.szx != p->blk_size) {
if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
/*
* Recompute the block number of the previous packet given
* the new block size
*/
block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1);
p->blk_size = block.szx;
chunk = (size_t)1 << (p->blk_size + 4);
p->offset = block.num * chunk;
coap_log_debug("new Block size is %u, block number %u completed\n",
1 << (block.szx + 4), block.num);
if (block.num == 0) {
if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
/*
* Recompute the block number of the previous packet given
* the new block size
*/
block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1);
p->blk_size = block.szx;
chunk = (size_t)1 << (p->blk_size + 4);
p->offset = block.num * chunk;
coap_log_debug("new Block size is %u, block number %u completed\n",
1 << (block.szx + 4), block.num);
} else {
coap_log_debug("ignoring request to increase Block size, "
"next block is not aligned on requested block size "
"boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
p->offset/chunk + 1, (1 << (p->blk_size + 4)),
(1 << (block.szx + 4)),
(p->offset + chunk) % ((size_t)1 << (block.szx + 4)));
}
} else {
coap_log_debug("ignoring request to increase Block size, "
"next block is not aligned on requested block size "
"boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
p->offset/chunk + 1, (1 << (p->blk_size + 4)),
(1 << (block.szx + 4)),
(p->offset + chunk) % ((size_t)1 << (block.szx + 4)));
coap_log_debug("ignoring request to change Block size from %u to %u\n",
(1 << (p->blk_size + 4)), (1 << (block.szx + 4)));
block.szx = block.aszx = p->blk_size;
}
}
}
Expand Down Expand Up @@ -3546,6 +3552,13 @@ coap_handle_response_get_block(coap_context_t *context,
block.chunk_size, length);
length = block.chunk_size;
}
if (block.m && length != block.chunk_size) {
coap_log_warn("block: Undersized packet - expected %"PRIu32", got %zu\n",
block.chunk_size, length);
/* Unclear how to properly handle this */
rcvd->code = COAP_RESPONSE_CODE(402);
goto expire_lg_crcv;
}
/* Possibility that Size2 not sent, or is too small */
chunk = (size_t)1 << (block.szx + 4);
offset = block.num * chunk;
Expand Down

0 comments on commit a141e44

Please sign in to comment.