diff --git a/include/coap3/coap_block_internal.h b/include/coap3/coap_block_internal.h index f38d5f1d..901eb246 100644 --- a/include/coap3/coap_block_internal.h +++ b/include/coap3/coap_block_internal.h @@ -220,6 +220,7 @@ struct coap_lg_crcv_t { size_t obs_token_cnt; /**< number of tokens used to set up Observe */ uint16_t o_block_option; /**< Block CoAP option used when initiating Observe */ uint8_t o_blk_size; /**< Block size used when initiating Observe */ + uint8_t total_len_set; /**< Set if total_len is where the last block ends */ uint32_t ref; /**< Reference count */ uint64_t state_token; /**< state token */ coap_pdu_t *sent_pdu; /**< The sent pdu with all the data */ @@ -248,6 +249,7 @@ struct coap_lg_srcv_t { uint8_t szx; /**< size of individual blocks */ uint16_t block_option; /**< Block option in use */ uint8_t dont_timeout; /**< Set if app handler in use */ + uint8_t total_len_set; /**< Set if total_len is where the last block ends */ coap_tick_t last_used; /**< Last time data sent or 0 */ size_t total_len; /**< Length as indicated by SIZE1 option */ coap_binary_t *body_data; /**< Used for re-assembling entire body */ diff --git a/src/coap_block.c b/src/coap_block.c index 321953cc..3f54a0c4 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -3730,7 +3730,9 @@ coap_handle_request_put_block(coap_context_t *context, #if COAP_Q_BLOCK_SUPPORT if (block_option == COAP_OPTION_Q_BLOCK1) { - if (total != lg_srcv->total_len) { + /* The body is passed on as total_len bytes, so the last block must end there */ + if (total != lg_srcv->total_len || + (!block.m && offset + length != total)) { coap_add_data(response, sizeof("Size1 mismatch")-1, (const uint8_t *)"Size1 mismatch"); response->code = COAP_RESPONSE_CODE(408); @@ -3742,6 +3744,16 @@ coap_handle_request_put_block(coap_context_t *context, } #endif /* COAP_Q_BLOCK_SUPPORT */ + /* Once the last block has set the body length, no block can go past it */ + if (lg_srcv->total_len_set && + (offset + length > lg_srcv->total_len || + (!block.m && offset + length != lg_srcv->total_len))) { + coap_add_data(response, sizeof("Block after last block")-1, + (const uint8_t *)"Block after last block"); + response->code = COAP_RESPONSE_CODE(408); + goto free_lg_srcv; + } + lg_srcv->last_type = pdu->type; if (context->max_bodies_ram) { @@ -3812,7 +3824,11 @@ coap_handle_request_put_block(coap_context_t *context, lg_srcv->rec_blocks.processing_payload_set = block.num / COAP_MAX_PAYLOADS(session); #endif /* COAP_Q_BLOCK_SUPPORT */ - if (lg_srcv->total_len < saved_offset + length) { + /* The last block gives the body length, Size1 is only an estimate */ + if (!block.m) { + lg_srcv->total_len = saved_offset + length; + lg_srcv->total_len_set = 1; + } else if (lg_srcv->total_len < saved_offset + length) { lg_srcv->total_len = saved_offset + length; } @@ -4752,11 +4768,14 @@ reinit: lg_crcv->last_type = rcvd->type; lg_crcv->rec_blocks.used = 0; lg_crcv->rec_blocks.total_blocks = 0; + lg_crcv->total_len_set = 0; #if COAP_Q_BLOCK_SUPPORT lg_crcv->rec_blocks.processing_payload_set = 0; #endif /* COAP_Q_BLOCK_SUPPORT */ } - if (lg_crcv->total_len < size2) + /* Q-Block2 Size2 is the body length and is checked below */ + if (block_opt != COAP_OPTION_Q_BLOCK2 && !lg_crcv->total_len_set && + lg_crcv->total_len < size2) lg_crcv->total_len = size2; /* Check whether we can handle this size */ @@ -4843,7 +4862,22 @@ reinit: size2, lg_crcv->total_len); goto fail_resp; } + /* The body is passed on as total_len bytes, so the last block must end there */ + if (block_opt == COAP_OPTION_Q_BLOCK2 && !block.m && + saved_offset + length != lg_crcv->total_len) { + coap_log_warn("Q-Block2: last block ends at %" PRIuS ", Size2 is %" PRIuS "\n", + saved_offset + length, lg_crcv->total_len); + goto fail_resp; + } #endif /* COAP_Q_BLOCK_SUPPORT */ + /* Once the last block has set the body length, no block can go past it */ + if (lg_crcv->total_len_set && + (saved_offset + length > lg_crcv->total_len || + (!block.m && saved_offset + length != lg_crcv->total_len))) { + coap_log_warn("block: block %u ends at %" PRIuS ", last block ends at %" PRIuS "\n", + block.num, saved_offset + length, lg_crcv->total_len); + goto fail_resp; + } if (block.num == 0) { coap_opt_t *obs_opt = coap_check_option(rcvd, COAP_OPTION_OBSERVE, @@ -4909,6 +4943,10 @@ reinit: /* Update last_used to prevent premature timeout during long transfers */ coap_ticks(&lg_crcv->last_used); + if (!block.m) { + lg_crcv->total_len = saved_offset + length; + lg_crcv->total_len_set = 1; + } if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) { if (size2 < saved_offset + length) { diff --git a/tests/test_block.c b/tests/test_block.c index f742c365..daf5e6c7 100644 --- a/tests/test_block.c +++ b/tests/test_block.c @@ -194,6 +194,256 @@ done: } #endif /* COAP_SERVER_SUPPORT */ +#if COAP_Q_BLOCK_SUPPORT +static size_t qblock2_len; +static unsigned int qblock2_calls; + +static coap_response_t +qblock2_handler(coap_session_t *s COAP_UNUSED, + const coap_pdu_t *sent COAP_UNUSED, + const coap_pdu_t *received, + const coap_mid_t mid COAP_UNUSED) { + size_t len, offset, total; + const uint8_t *data; + + qblock2_calls++; + if (coap_get_data_large(received, &len, &data, &offset, &total)) + qblock2_len = len; + return COAP_RESPONSE_OK; +} + +static coap_pdu_t * +qblock2_response(coap_session_t *s, uint32_t blk, size_t size2) { + static const uint8_t token[] = { 0x51, 0x42 }; + static const uint8_t etag[] = { 0x01 }; + uint8_t payload[16]; + uint8_t buf[4]; + coap_pdu_t *pdu; + + pdu = coap_pdu_init(COAP_MESSAGE_ACK, COAP_RESPONSE_CODE(205), + coap_new_message_id_lkd(s), 256); + if (!pdu) + return NULL; + memset(payload, 'a', sizeof(payload)); + coap_add_token(pdu, sizeof(token), token); + coap_add_option(pdu, COAP_OPTION_ETAG, sizeof(etag), etag); + coap_add_option(pdu, COAP_OPTION_Q_BLOCK2, + coap_encode_var_safe(buf, sizeof(buf), blk), buf); + coap_add_option(pdu, COAP_OPTION_SIZE2, + coap_encode_var_safe(buf, sizeof(buf), size2), buf); + coap_add_data(pdu, sizeof(payload), payload); + return pdu; +} + +/* Two 16 byte Q-Block2 responses, with the Size2 of each */ +static void +qblock2_get(size_t size2_first, size_t size2_last) { + static const uint8_t token[] = { 0x51, 0x42 }; + coap_context_t *qctx; + coap_session_t *qsession; + coap_address_t addr; + coap_pdu_t *sent; + coap_pdu_t *rcvd; + + qblock2_len = 0; + qblock2_calls = 0; + + coap_address_init(&addr); + addr.size = sizeof(struct sockaddr_in6); + addr.addr.sin6.sin6_family = AF_INET6; + addr.addr.sin6.sin6_addr = in6addr_loopback; + addr.addr.sin6.sin6_port = htons(20001); + + qctx = coap_new_context(NULL); + ReturnIf_CU_ASSERT_PTR_NOT_NULL(qctx); + coap_context_set_block_mode(qctx, COAP_BLOCK_USE_LIBCOAP | + COAP_BLOCK_SINGLE_BODY); + coap_register_response_handler(qctx, qblock2_handler); + qsession = coap_new_client_session(qctx, NULL, &addr, COAP_PROTO_UDP); + if (!qsession) { + CU_FAIL("no session"); + coap_free_context(qctx); + return; + } + + coap_lock_lock(return); + sent = coap_pdu_init(COAP_MESSAGE_CON, COAP_REQUEST_CODE_GET, + coap_new_message_id_lkd(qsession), 64); + CU_ASSERT_PTR_NOT_NULL(sent); + if (sent) { + coap_add_token(sent, sizeof(token), token); + coap_add_option(sent, COAP_OPTION_URI_PATH, 1, (const uint8_t *)"x"); + + rcvd = qblock2_response(qsession, (0 << 4) | (1 << 3) | 0, size2_first); + CU_ASSERT_PTR_NOT_NULL(rcvd); + if (rcvd) { + coap_handle_response_get_block(qctx, qsession, sent, rcvd, + COAP_RECURSE_OK); + coap_delete_pdu_lkd(rcvd); + } + rcvd = qblock2_response(qsession, (1 << 4) | (0 << 3) | 0, size2_last); + CU_ASSERT_PTR_NOT_NULL(rcvd); + if (rcvd) { + coap_handle_response_get_block(qctx, qsession, sent, rcvd, + COAP_RECURSE_OK); + coap_delete_pdu_lkd(rcvd); + } + coap_delete_pdu_lkd(sent); + } + coap_lock_unlock(); + + coap_session_release(qsession); + coap_free_context(qctx); +} + +/* A Q-Block2 body is only presented with the 32 bytes that were received */ +static void +t_block_qblock2_size2(void) { + /* Size2 changes on the last block */ + qblock2_get(32, 1000); + CU_ASSERT(qblock2_len <= 32); + /* Size2 larger than the body the last block ends */ + qblock2_get(1000, 1000); + CU_ASSERT(qblock2_len <= 32); + /* Consistent Size2 */ + qblock2_get(32, 32); + CU_ASSERT(qblock2_calls == 1); + CU_ASSERT(qblock2_len == 32); +} +#endif /* COAP_Q_BLOCK_SUPPORT */ + +#if COAP_SERVER_SUPPORT +static coap_pdu_t * +block1_request(coap_session_t *s, coap_option_num_t opt, uint32_t blk, + size_t size1) { + static const uint8_t token[] = { 0x42, 0x31 }; + static const uint8_t rtag[] = { 0x02 }; + uint8_t payload[16]; + uint8_t buf[4]; + coap_pdu_t *pdu; + + pdu = coap_pdu_init(COAP_MESSAGE_CON, COAP_REQUEST_CODE_PUT, + coap_new_message_id_lkd(s), 256); + if (!pdu) + return NULL; + memset(payload, 'b', sizeof(payload)); + coap_add_token(pdu, sizeof(token), token); + coap_add_option(pdu, COAP_OPTION_URI_PATH, 1, (const uint8_t *)"y"); + coap_add_option(pdu, opt, coap_encode_var_safe(buf, sizeof(buf), blk), buf); + coap_add_option(pdu, COAP_OPTION_SIZE1, + coap_encode_var_safe(buf, sizeof(buf), size1), buf); + coap_add_option(pdu, COAP_OPTION_RTAG, sizeof(rtag), rtag); + coap_add_data(pdu, sizeof(payload), payload); + return pdu; +} + +/* + * Sends 16 byte blocks with the given Block option values, all with the + * given Size1. Returns what coap_handle_request_put_block() returns for + * the last one, with the body length handed on and the response code. + */ +static int +block1_put(coap_option_num_t opt, const uint32_t *blks, size_t count, + size_t size1, size_t *len, coap_pdu_code_t *code) { + coap_context_t *sctx; + coap_session_t *ssession; + coap_resource_t *resource; + coap_address_t addr; + coap_lg_srcv_t *free_lg_srcv; + coap_pdu_t *request; + coap_pdu_t *response; + size_t i; + int added_block; + int ret = -1; + + *len = 0; + *code = 0; + coap_address_init(&addr); + addr.size = sizeof(struct sockaddr_in6); + addr.addr.sin6.sin6_family = AF_INET6; + addr.addr.sin6.sin6_addr = in6addr_loopback; + addr.addr.sin6.sin6_port = htons(20002); + + sctx = coap_new_context(NULL); + if (!sctx) + return -1; + coap_context_set_block_mode(sctx, COAP_BLOCK_USE_LIBCOAP | + COAP_BLOCK_SINGLE_BODY); + ssession = coap_new_client_session(sctx, NULL, &addr, COAP_PROTO_UDP); + if (!ssession) { + coap_free_context(sctx); + return -1; + } + + coap_lock_lock(return -1); + resource = coap_resource_init(coap_make_str_const("y"), 0); + if (resource) { + coap_add_resource_lkd(sctx, resource); + + for (i = 0; i < count; i++) { + request = block1_request(ssession, opt, blks[i], size1); + response = coap_pdu_init(COAP_MESSAGE_ACK, 0, 0, 256); + ret = -1; + *len = 0; + if (request && response) { + size_t offset, total; + const uint8_t *data; + + ret = coap_handle_request_put_block(sctx, ssession, request, response, + resource, NULL, &added_block, + &free_lg_srcv); + if (ret == 0) + coap_get_data_large(request, len, &data, &offset, &total); + *code = response->code; + } + coap_delete_pdu_lkd(request); + coap_delete_pdu_lkd(response); + } + } + coap_lock_unlock(); + + coap_session_release(ssession); + coap_free_context(sctx); + return ret; +} + +/* A Block1 body is presented with the length the last block ends at, + * not the larger Size1 estimate */ +static void +t_block_block1_size1(void) { + static const uint32_t blks[] = { (0 << 4) | (1 << 3), (1 << 4) | (0 << 3) }; + coap_pdu_code_t code; + size_t len; + + CU_ASSERT(block1_put(COAP_OPTION_BLOCK1, blks, 2, 1000, &len, &code) == 0); + CU_ASSERT(len == 32); +#if COAP_Q_BLOCK_SUPPORT + /* Q-Block1 Size1 is the body length, so must match the last block */ + CU_ASSERT(block1_put(COAP_OPTION_Q_BLOCK1, blks, 2, 1000, &len, &code) == 1); + CU_ASSERT(code == COAP_RESPONSE_CODE(408)); +#endif /* COAP_Q_BLOCK_SUPPORT */ +} + +/* Once the last Block1 block is in, no block can go past where it ends */ +static void +t_block_block1_after_last(void) { + static const uint32_t later[] = { (1 << 4) | (0 << 3), (2 << 4) | (1 << 3) }; + static const uint32_t shorter[] = { (2 << 4) | (0 << 3), (1 << 4) | (0 << 3) }; + static const uint32_t repeated[] = { (1 << 4) | (0 << 3), (1 << 4) | (0 << 3), + (0 << 4) | (1 << 3) + }; + coap_pdu_code_t code; + size_t len; + + CU_ASSERT(block1_put(COAP_OPTION_BLOCK1, later, 2, 32, &len, &code) == 1); + CU_ASSERT(code == COAP_RESPONSE_CODE(408)); + CU_ASSERT(block1_put(COAP_OPTION_BLOCK1, shorter, 2, 48, &len, &code) == 1); + CU_ASSERT(code == COAP_RESPONSE_CODE(408)); + CU_ASSERT(block1_put(COAP_OPTION_BLOCK1, repeated, 3, 32, &len, &code) == 0); + CU_ASSERT(len == 32); +} +#endif /* COAP_SERVER_SUPPORT */ + CU_pSuite t_init_block_tests(void) { CU_pSuite suite; @@ -212,10 +462,15 @@ t_init_block_tests(void) { CU_get_error_msg()); \ } +#if COAP_Q_BLOCK_SUPPORT + BLOCK_TEST(suite, t_block_qblock2_size2); +#endif /* COAP_Q_BLOCK_SUPPORT */ #if COAP_SERVER_SUPPORT BLOCK_TEST(suite, t_block_echo_get_func); BLOCK_TEST(suite, t_block_cancel_observe_get_func); BLOCK_TEST(suite, t_block_large_app_block_num); + BLOCK_TEST(suite, t_block_block1_size1); + BLOCK_TEST(suite, t_block_block1_after_last); #endif /* COAP_SERVER_SUPPORT */ return suite;