Skip to content

Commit

Permalink
examples,tests: update users of deprecated nanocoap APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed Jan 23, 2025
1 parent 9192317 commit 7458bce
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 54 deletions.
6 changes: 3 additions & 3 deletions examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#include "event/thread.h"
#include "event/timeout.h"
#include "fmt.h"
#include "hashes/sha256.h"
#include "net/nanocoap.h"
#include "net/nanocoap_sock.h"
#include "hashes/sha256.h"

/* internal value that can be read/written via CoAP */
static uint8_t internal_value = 0;
Expand Down Expand Up @@ -158,7 +158,7 @@ ssize_t _sha256_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, coap_request_
return reply_len;
}

uint8_t *pkt_pos = (uint8_t*)pkt->hdr + reply_len;
uint8_t *pkt_pos = pkt->buf + reply_len;
if (blockwise) {
pkt_pos += coap_opt_put_block1_control(pkt_pos, 0, &block1);
}
Expand All @@ -167,7 +167,7 @@ ssize_t _sha256_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, coap_request_
pkt_pos += fmt_bytes_hex((char *)pkt_pos, digest, sizeof(digest));
}

return pkt_pos - (uint8_t*)pkt->hdr;
return pkt_pos - pkt->buf;
}

NANOCOAP_RESOURCE(echo) {
Expand Down
15 changes: 7 additions & 8 deletions tests/net/nanocoap_cli/nanocli_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ static int _cmd_client(int argc, char **argv)
{
/* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */
const char *method_codes[] = {"get", "post", "put"};
unsigned buflen = 128;
uint8_t buf[buflen];
uint8_t buf[128];
coap_pkt_t pkt;
size_t len;

Expand All @@ -113,14 +112,14 @@ static int _cmd_client(int argc, char **argv)
goto end;
}

pkt.hdr = (coap_hdr_t *)buf;
pkt.buf = buf;

/* parse options */
if (argc == 5 || argc == 6) {
ssize_t hdrlen = coap_build_hdr(pkt.hdr, COAP_TYPE_CON,
_client_token, _client_token_len,
code_pos+1, 1);
coap_pkt_init(&pkt, &buf[0], buflen, hdrlen);
ssize_t hdrlen = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_CON,
_client_token, _client_token_len,
code_pos + 1, 1);
coap_pkt_init(&pkt, &buf[0], sizeof(buf), hdrlen);
coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, argv[4], '/');
if (argc == 6) {
coap_opt_add_uint(&pkt, COAP_OPT_CONTENT_FORMAT, COAP_FORMAT_TEXT);
Expand All @@ -137,7 +136,7 @@ static int _cmd_client(int argc, char **argv)
printf("nanocli: sending msg ID %u, %" PRIuSIZE " bytes\n", coap_get_id(&pkt),
len);

ssize_t res = _send(&pkt, buflen, argv[2], argv[3]);
ssize_t res = _send(&pkt, sizeof(buf), argv[2], argv[3]);
if (res < 0) {
printf("nanocli: msg send failed: %" PRIdSIZE "\n", res);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/net/nanocoap_cli/nanocli_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static int _nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize,
.remote = &remote,
};

if (coap_parse(&pkt, (uint8_t *)buf, res) < 0) {
if (coap_parse_udp(&pkt, (uint8_t *)buf, res) < 0) {
DEBUG("nanocoap: error parsing packet\n");
continue;
}
Expand Down
11 changes: 2 additions & 9 deletions tests/pkg/edhoc_c/initiator.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
#include <stdio.h>

#include "net/gnrc/netif.h"
#include "net/ipv6.h"
#include "net/nanocoap_sock.h"
#include "shell.h"

#include "edhoc/edhoc.h"
#include "edhoc_keys.h"
Expand All @@ -33,9 +31,6 @@
#include "tinycrypt/sha256.h"
#endif

#define ENABLE_DEBUG 0
#include "debug.h"

#define COAP_BUF_SIZE (256U)

#if IS_ACTIVE(CONFIG_INITIATOR)
Expand Down Expand Up @@ -114,11 +109,9 @@ static ssize_t _build_coap_pkt(coap_pkt_t *pkt, uint8_t *buf, ssize_t buflen,
uint8_t token[2] = { 0xDA, 0xEC };
ssize_t len = 0;

/* set pkt buffer */
pkt->hdr = (coap_hdr_t *)buf;
/* build header, confirmed message always post */
ssize_t hdrlen = coap_build_hdr(pkt->hdr, COAP_TYPE_CON, token,
sizeof(token), COAP_METHOD_POST, 1);
ssize_t hdrlen = coap_build_udp_hdr(buf, buflen, COAP_TYPE_CON, token,
sizeof(token), COAP_METHOD_POST, 1);

coap_pkt_init(pkt, buf, buflen, hdrlen);
coap_opt_add_string(pkt, COAP_OPT_URI_PATH, "/.well-known/edhoc", '/');
Expand Down
5 changes: 2 additions & 3 deletions tests/riotboot_flashwrite/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "net/nanocoap.h"
#include "riotboot/flashwrite.h"
Expand Down Expand Up @@ -72,10 +71,10 @@ ssize_t _flashwrite_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, coap_requ
return reply_len;
}

uint8_t *pkt_pos = (uint8_t*)pkt->hdr + reply_len;
uint8_t *pkt_pos = pkt->buf + reply_len;
pkt_pos += coap_put_block1_ok(pkt_pos, &block1, 0);

return pkt_pos - (uint8_t*)pkt->hdr;
return pkt_pos - pkt->buf;
}

NANOCOAP_RESOURCE(flashwrite) {
Expand Down
10 changes: 5 additions & 5 deletions tests/unittests/tests-gcoap/tests-gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static void test_gcoap__client_get_resp(void)
};
memcpy(buf, pdu_data, sizeof(pdu_data));

res = coap_parse(&pdu, &buf[0], sizeof(pdu_data));
res = coap_parse_udp(&pdu, &buf[0], sizeof(pdu_data));

TEST_ASSERT_EQUAL_INT(sizeof(pdu_data), res);
TEST_ASSERT_EQUAL_INT(COAP_CLASS_SUCCESS, coap_get_code_class(&pdu));
Expand Down Expand Up @@ -155,7 +155,7 @@ static void test_gcoap__client_put_req(void)
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);
memcpy(pdu.payload, payload, 1);

coap_parse(&pdu, buf, len + 1);
coap_parse_udp(&pdu, buf, len + 1);

TEST_ASSERT_EQUAL_INT(COAP_METHOD_PUT, coap_get_code_decimal(&pdu));
TEST_ASSERT_EQUAL_INT(1, pdu.payload_len);
Expand Down Expand Up @@ -203,7 +203,7 @@ static void test_gcoap__client_get_path_defer(void)
TEST_ASSERT_EQUAL_INT(len,
sizeof(coap_udp_hdr_t) + CONFIG_GCOAP_TOKENLEN + ETAG_SLACK + optlen);

coap_parse(&pdu, buf, len);
coap_parse_udp(&pdu, buf, len);

char uri[CONFIG_NANOCOAP_URI_MAX] = {0};
coap_get_uri_path(&pdu, (uint8_t *)&uri[0]);
Expand Down Expand Up @@ -249,7 +249,7 @@ static ssize_t _read_cli_stats_req(coap_pkt_t *pdu, uint8_t *buf)
};
memcpy(buf, pdu_data, sizeof(pdu_data));

return coap_parse(pdu, buf, sizeof(pdu_data));
return coap_parse_udp(pdu, buf, sizeof(pdu_data));
}

/* Server GET request success case. Validate request example. */
Expand Down Expand Up @@ -321,7 +321,7 @@ static ssize_t _read_cli_stats_req_con(coap_pkt_t *pdu, uint8_t *buf)
};
memcpy(buf, pdu_data, sizeof(pdu_data));

return coap_parse(pdu, buf, sizeof(pdu_data));
return coap_parse_udp(pdu, buf, sizeof(pdu_data));
}

/* Server CON GET request success case. Validate request is confirmable. */
Expand Down
50 changes: 25 additions & 25 deletions tests/unittests/tests-nanocoap/tests-nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void test_nanocoap__hdr(void)
pktpos += coap_opt_put_uri_path(pktpos, COAP_OPT_LOCATION_PATH, path);

coap_pkt_t pkt;
coap_parse(&pkt, &buf[0], pktpos - &buf[0]);
TEST_ASSERT_EQUAL_INT(pktpos - &buf[0], coap_parse_udp(&pkt, &buf[0], pktpos - &buf[0]));

TEST_ASSERT_EQUAL_INT(msgid, coap_get_id(&pkt));

Expand Down Expand Up @@ -74,7 +74,7 @@ static void test_nanocoap__hdr_2(void)
pktpos += coap_opt_put_uri_pathquery(pktpos, &lastonum, path);

coap_pkt_t pkt;
coap_parse(&pkt, &buf[0], pktpos - &buf[0]);
TEST_ASSERT_EQUAL_INT(pktpos - &buf[0], coap_parse_udp(&pkt, &buf[0], pktpos - &buf[0]));

TEST_ASSERT_EQUAL_INT(msgid, coap_get_id(&pkt));

Expand Down Expand Up @@ -692,7 +692,7 @@ static ssize_t _read_riot_value_req(coap_pkt_t *pkt, uint8_t *buf)
};
memcpy(buf, pkt_data, sizeof(pkt_data));

return coap_parse(pkt, buf, sizeof(pkt_data));
return coap_parse_udp(pkt, buf, sizeof(pkt_data));
}

/* Server GET request success case. */
Expand Down Expand Up @@ -750,7 +750,7 @@ static ssize_t _read_riot_value_req_con(coap_pkt_t *pkt, uint8_t *buf)
};
memcpy(buf, pkt_data, sizeof(pkt_data));

return coap_parse(pkt, buf, sizeof(pkt_data));
return coap_parse_udp(pkt, buf, sizeof(pkt_data));
}

/* Builds on test_nanocoap__server_get_req to test confirmable request. */
Expand Down Expand Up @@ -785,9 +785,9 @@ static void test_nanocoap__server_option_count_overflow_check(void)
{
/* this test passes a forged CoAP packet containing 42 options (provided by
* @nmeum in #10753, 42 is a random number which just needs to be higher
* than CONFIG_NANOCOAP_NOPTS_MAX) to coap_parse(). The used coap_pkt_t is part
* than CONFIG_NANOCOAP_NOPTS_MAX) to coap_parse_udp(). The used coap_pkt_t is part
* of a struct, followed by an array of 42 coap_option_t. The array is
* cleared before the call to coap_parse(). If the overflow protection is
* cleared before the call to coap_parse_udp(). If the overflow protection is
* working, the array must still be clear after parsing the packet, and the
* proper error code (-ENOMEM) is returned. Otherwise, the parsing wrote
* past scratch.pkt, thus the array is not zeroed anymore.
Expand All @@ -813,7 +813,7 @@ static void test_nanocoap__server_option_count_overflow_check(void)

memset(&scratch, 0, sizeof(scratch));

ssize_t res = coap_parse(&scratch.pkt, pkt_data, sizeof(pkt_data));
ssize_t res = coap_parse_udp(&scratch.pkt, pkt_data, sizeof(pkt_data));

/* check if any byte of the guard_data array is non-zero */
int dirty = 0;
Expand All @@ -830,7 +830,7 @@ static void test_nanocoap__server_option_count_overflow_check(void)
}

/*
* Verifies that coap_parse() recognizes inclusion of too many options.
* Verifies that coap_parse_udp() recognizes inclusion of too many options.
*/
static void test_nanocoap__server_option_count_overflow(void)
{
Expand All @@ -855,13 +855,13 @@ static void test_nanocoap__server_option_count_overflow(void)
}

/* don't read final two bytes, where overflow option will be added later */
ssize_t res = coap_parse(&pkt, buf, sizeof(buf) - 2);
ssize_t res = coap_parse_udp(&pkt, buf, sizeof(buf) - 2);
TEST_ASSERT_EQUAL_INT(sizeof(buf) - 2, res);

/* add option to overflow */
memcpy(&buf[base_len+i], fill_opt, 2);

res = coap_parse(&pkt, buf, sizeof(buf));
res = coap_parse_udp(&pkt, buf, sizeof(buf));
TEST_ASSERT(res < 0);
}

Expand Down Expand Up @@ -891,7 +891,7 @@ static ssize_t _read_rd_post_req(coap_pkt_t *pkt, bool omit_payload)
};

size_t len = omit_payload ? 59 : sizeof(pkt_data);
return coap_parse(pkt, pkt_data, len);
return coap_parse_udp(pkt, pkt_data, len);
}

/*
Expand Down Expand Up @@ -976,7 +976,7 @@ static void test_nanocoap__empty(void)
uint16_t msgid = 0xABCD;

coap_pkt_t pkt;
ssize_t res = coap_parse(&pkt, pkt_data, 4);
ssize_t res = coap_parse_udp(&pkt, pkt_data, 4);

TEST_ASSERT(res > 0);
TEST_ASSERT_EQUAL_INT(0, coap_get_code_raw(&pkt));
Expand All @@ -986,12 +986,12 @@ static void test_nanocoap__empty(void)

/* too short */
memset(&pkt, 0, sizeof(coap_pkt_t));
res = coap_parse(&pkt, pkt_data, 3);
res = coap_parse_udp(&pkt, pkt_data, 3);
TEST_ASSERT_EQUAL_INT(-EBADMSG, res);

/* too long */
memset(&pkt, 0, sizeof(coap_pkt_t));
res = coap_parse(&pkt, pkt_data, 5);
res = coap_parse_udp(&pkt, pkt_data, 5);
TEST_ASSERT_EQUAL_INT(-EBADMSG, res);
}

Expand Down Expand Up @@ -1055,7 +1055,7 @@ static void test_nanocoap__add_get_proxy_uri(void)
}

/*
* Verifies that coap_parse() recognizes token length bigger than allowed.
* Verifies that coap_parse_udp() recognizes token length bigger than allowed.
*/
static void test_nanocoap__token_length_over_limit(void)
{
Expand All @@ -1075,7 +1075,7 @@ static void test_nanocoap__token_length_over_limit(void)
coap_pkt_t pkt;

/* Valid packet (TKL = 8) */
ssize_t res = coap_parse(&pkt, buf_valid, sizeof(buf_valid));
ssize_t res = coap_parse_udp(&pkt, buf_valid, sizeof(buf_valid));

TEST_ASSERT_EQUAL_INT(sizeof(buf_valid), res);
TEST_ASSERT_EQUAL_INT(1, coap_get_code_raw(&pkt));
Expand All @@ -1084,12 +1084,12 @@ static void test_nanocoap__token_length_over_limit(void)
TEST_ASSERT_EQUAL_INT(0, pkt.payload_len);

/* Invalid packet (TKL = 15) */
res = coap_parse(&pkt, buf_invalid, sizeof(buf_invalid));
res = coap_parse_udp(&pkt, buf_invalid, sizeof(buf_invalid));
TEST_ASSERT_EQUAL_INT(-EBADMSG, res);
}

/*
* Verifies that coap_parse() recognizes 8 bit extended token length
* Verifies that coap_parse_udp() recognizes 8 bit extended token length
*/
static void test_nanocoap__token_length_ext_16(void)
{
Expand All @@ -1106,7 +1106,7 @@ static void test_nanocoap__token_length_ext_16(void)

/* parse the packet build, and verify it parses back as expected */
coap_pkt_t pkt;
ssize_t res = coap_parse(&pkt, buf, 21);
ssize_t res = coap_parse_udp(&pkt, buf, 21);

TEST_ASSERT_EQUAL_INT(21, res);
TEST_ASSERT_EQUAL_INT(21, coap_get_total_hdr_len(&pkt));
Expand All @@ -1123,7 +1123,7 @@ static void test_nanocoap__token_length_ext_16(void)
ssize_t len = coap_build_reply_header(&pkt, COAP_CODE_DELETED, rbuf,
sizeof(rbuf), 0, NULL, NULL);
TEST_ASSERT_EQUAL_INT(21, len);
res = coap_parse(&pkt, rbuf, 21);
res = coap_parse_udp(&pkt, rbuf, 21);
TEST_ASSERT_EQUAL_INT(21, res);
TEST_ASSERT_EQUAL_INT(21, coap_get_total_hdr_len(&pkt));
TEST_ASSERT_EQUAL_INT(COAP_CODE_DELETED, coap_get_code_raw(&pkt));
Expand All @@ -1135,7 +1135,7 @@ static void test_nanocoap__token_length_ext_16(void)
}

/*
* Verifies that coap_parse() recognizes 16 bit extended token length
* Verifies that coap_parse_udp() recognizes 16 bit extended token length
*/
static void test_nanocoap__token_length_ext_269(void)
{
Expand All @@ -1155,7 +1155,7 @@ static void test_nanocoap__token_length_ext_269(void)

/* parse the packet build, and verify it parses back as expected */
coap_pkt_t pkt;
ssize_t res = coap_parse(&pkt, buf, 275);
ssize_t res = coap_parse_udp(&pkt, buf, 275);

TEST_ASSERT_EQUAL_INT(275, res);
TEST_ASSERT_EQUAL_INT(275, coap_get_total_hdr_len(&pkt));
Expand All @@ -1173,7 +1173,7 @@ static void test_nanocoap__token_length_ext_269(void)
sizeof(rbuf), 0, NULL, NULL);

TEST_ASSERT_EQUAL_INT(275, len);
res = coap_parse(&pkt, rbuf, 275);
res = coap_parse_udp(&pkt, rbuf, 275);
TEST_ASSERT_EQUAL_INT(275, res);
TEST_ASSERT_EQUAL_INT(275, coap_get_total_hdr_len(&pkt));
TEST_ASSERT_EQUAL_INT(COAP_CODE_DELETED, coap_get_code_raw(&pkt));
Expand Down Expand Up @@ -1207,7 +1207,7 @@ static void test_nanocoap___rst_message(void)

/* now check that parsing it back works */
coap_pkt_t pkt;
TEST_ASSERT_EQUAL_INT(sizeof(rst_expected), coap_parse(&pkt, buf, sizeof(rst_expected)));
TEST_ASSERT_EQUAL_INT(sizeof(rst_expected), coap_parse_udp(&pkt, buf, sizeof(rst_expected)));
TEST_ASSERT_EQUAL_INT(COAP_TYPE_RST, coap_get_type(&pkt));
TEST_ASSERT_EQUAL_INT(0, coap_get_code_raw(&pkt));
TEST_ASSERT_EQUAL_INT(0, coap_get_token_len(&pkt));
Expand All @@ -1220,7 +1220,7 @@ static void test_nanocoap___rst_message(void)
0xde, 0xed, 0xbe, 0xef, /* Token = 0xdeadbeef */
};
memset(buf, 0x55, sizeof(buf));
TEST_ASSERT_EQUAL_INT(sizeof(con_request), coap_parse(&pkt, con_request, sizeof(con_request)));
TEST_ASSERT_EQUAL_INT(sizeof(con_request), coap_parse_udp(&pkt, con_request, sizeof(con_request)));
TEST_ASSERT_EQUAL_INT(sizeof(rst_expected), coap_build_reply(&pkt, 0, buf, sizeof(buf), 0));
TEST_ASSERT(0 == memcmp(rst_expected, buf, sizeof(rst_expected)));
TEST_ASSERT_EQUAL_INT(0x55, buf[sizeof(rst_expected)]);
Expand Down

0 comments on commit 7458bce

Please sign in to comment.