Skip to content

Commit

Permalink
Merge pull request #20946 from maribu/sys/nanocoap/fix-header-size-co…
Browse files Browse the repository at this point in the history
…mputation

sys/net/nanocoap: fix coap_get_total_hdr_len()
  • Loading branch information
maribu authored Nov 1, 2024
2 parents 00e25ad + 2b3da39 commit 5ae4c96
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
25 changes: 13 additions & 12 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,18 +582,6 @@ static inline void *coap_get_token(const coap_pkt_t *pkt)
return coap_hdr_data_ptr(pkt->hdr);
}

/**
* @brief Get the total header length (4-byte header + token length)
*
* @param[in] pkt CoAP packet
*
* @returns total header length
*/
static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
{
return sizeof(coap_hdr_t) + coap_get_token_len(pkt);
}

/**
* @brief Get the total length of a CoAP packet in the packet buffer
*
Expand Down Expand Up @@ -676,6 +664,19 @@ static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr)
return ((uint8_t *)hdr) + sizeof(coap_hdr_t) + coap_hdr_tkl_ext_len(hdr);
}

/**
* @brief Get the total header length (4-byte header + token length)
*
* @param[in] pkt CoAP packet
*
* @returns total header length
*/
static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
{
return sizeof(coap_hdr_t) + coap_hdr_tkl_ext_len(pkt->hdr) +
coap_get_token_len(pkt);
}

/**
* @brief Write the given raw message code to given CoAP header
*
Expand Down
15 changes: 15 additions & 0 deletions tests/unittests/tests-nanocoap/tests-nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ static void test_nanocoap__get_multi_path(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
Expand All @@ -207,6 +208,7 @@ static void test_nanocoap__get_path_trailing_slash(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
Expand All @@ -231,6 +233,7 @@ static void test_nanocoap__get_root_path(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

char uri[10] = {0};
coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
Expand All @@ -254,6 +257,7 @@ static void test_nanocoap__get_max_path(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
Expand Down Expand Up @@ -281,6 +285,7 @@ static void test_nanocoap__get_path_too_long(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
Expand Down Expand Up @@ -308,6 +313,7 @@ static void test_nanocoap__get_query(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
TEST_ASSERT_EQUAL_INT(path_opt_len, len);
Expand Down Expand Up @@ -350,6 +356,7 @@ static void test_nanocoap__get_multi_query(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

uint8_t *query_pos = &pkt.payload[0];
/* first opt header is 2 bytes long */
Expand Down Expand Up @@ -406,6 +413,7 @@ static void test_nanocoap__add_uri_query2(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

/* includes key and value */
char query[20] = {0};
Expand Down Expand Up @@ -463,6 +471,7 @@ static void test_nanocoap__option_add_buffer_max(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
Expand All @@ -487,6 +496,7 @@ static void __test_option_remove(uint16_t stride)
&token[0], 2, COAP_METHOD_GET, msgid);
/* shrink buffer to attempt overfill */
coap_pkt_init(&pkt, &buf[0], sizeof(buf) - 1, len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

/* add seven options of options 1 to 7 */
for (uint16_t count = 1; count < 8; count++) {
Expand Down Expand Up @@ -647,6 +657,7 @@ static void test_nanocoap__option_remove_no_payload(void)
&token[0], 2, COAP_METHOD_GET, msgid);
/* shrink buffer to attempt overfill */
coap_pkt_init(&pkt, &buf[0], sizeof(buf) - 1, len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

len = coap_opt_add_uint(&pkt, 1U, 500U);
TEST_ASSERT_EQUAL_INT(3U, len);
Expand Down Expand Up @@ -1004,6 +1015,7 @@ static void test_nanocoap__add_path_unterminated_string(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
coap_opt_add_chars(&pkt, COAP_OPT_URI_PATH, &path[0], path_len, '/');

char uri[10] = {0};
Expand All @@ -1029,6 +1041,7 @@ static void test_nanocoap__add_get_proxy_uri(void)
&token[0], 2, COAP_METHOD_GET, msgid);

coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));

len = coap_opt_add_proxy_uri(&pkt, proxy_uri);

Expand Down Expand Up @@ -1093,6 +1106,7 @@ static void test_nanocoap__token_length_ext_16(void)
int res = coap_parse(&pkt, buf, 21);

TEST_ASSERT_EQUAL_INT(0, res);
TEST_ASSERT_EQUAL_INT(21, coap_get_total_hdr_len(&pkt));
TEST_ASSERT_EQUAL_INT(204, coap_get_code_decimal(&pkt));
TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt));
TEST_ASSERT_EQUAL_INT(strlen(token), coap_get_token_len(&pkt));
Expand Down Expand Up @@ -1121,6 +1135,7 @@ static void test_nanocoap__token_length_ext_269(void)
int res = coap_parse(&pkt, buf, 275);

TEST_ASSERT_EQUAL_INT(0, res);
TEST_ASSERT_EQUAL_INT(275, coap_get_total_hdr_len(&pkt));
TEST_ASSERT_EQUAL_INT(204, coap_get_code_decimal(&pkt));
TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt));
TEST_ASSERT_EQUAL_INT(strlen(token), coap_get_token_len(&pkt));
Expand Down

0 comments on commit 5ae4c96

Please sign in to comment.