diff --git a/examples/networking/coap/gcoap/client.c b/examples/networking/coap/gcoap/client.c index ffccfa76f5..a296bb4842 100644 --- a/examples/networking/coap/gcoap/client.c +++ b/examples/networking/coap/gcoap/client.c @@ -131,16 +131,16 @@ static void _resp_handler(const gcoap_request_memo_t *memo, coap_pkt_t* pdu, uri_parser_result_t urip; uri_parser_process(&urip, _last_req_uri, strlen(_last_req_uri)); if (*_proxy_uri) { - gcoap_req_init(pdu, (uint8_t *)pdu->hdr, CONFIG_GCOAP_PDU_BUF_SIZE, + gcoap_req_init(pdu, pdu->buf, CONFIG_GCOAP_PDU_BUF_SIZE, COAP_METHOD_GET, NULL); } else { - gcoap_req_init(pdu, (uint8_t *)pdu->hdr, CONFIG_GCOAP_PDU_BUF_SIZE, + gcoap_req_init(pdu, pdu->buf, CONFIG_GCOAP_PDU_BUF_SIZE, COAP_METHOD_GET, urip.path); } if (msg_type == COAP_TYPE_ACK) { - coap_hdr_set_type(pdu->hdr, COAP_TYPE_CON); + coap_pkt_set_type(pdu, COAP_TYPE_CON); } block.blknum++; coap_opt_add_block2_control(pdu, &block); @@ -151,7 +151,7 @@ static void _resp_handler(const gcoap_request_memo_t *memo, coap_pkt_t* pdu, int len = coap_opt_finish(pdu, COAP_OPT_FINISH_NONE); gcoap_socket_type_t tl = _get_tl(*_proxy_uri ? _proxy_uri : _last_req_uri); - _send((uint8_t *)pdu->hdr, len, remote, memo->context, tl); + _send(pdu->buf, len, remote, memo->context, tl); } else { puts("--- blockwise complete ---"); @@ -338,7 +338,7 @@ static int _cli_cmd(int argc, char **argv) } } - coap_hdr_set_type(pdu.hdr, msg_type); + coap_pkt_set_type(&pdu, msg_type); size_t paylen = 0; if (apos < argc) { diff --git a/examples/networking/coap/gcoap/server.c b/examples/networking/coap/gcoap/server.c index e740ed8003..209597cde5 100644 --- a/examples/networking/coap/gcoap/server.c +++ b/examples/networking/coap/gcoap/server.c @@ -119,7 +119,7 @@ static void _rtc_notify_observers(void *arg) } size_t len; char str_time[20] = ""; - uint8_t buf[sizeof(coap_hdr_t) + COAP_TOKEN_LENGTH_MAX + 1 + sizeof(str_time)]; + uint8_t buf[sizeof(coap_udp_hdr_t) + COAP_TOKEN_LENGTH_MAX + 1 + sizeof(str_time)]; coap_pkt_t pdu; const coap_resource_t *rtc_resource = NULL; const gcoap_listener_t *listener = NULL; diff --git a/examples/networking/coap/gcoap_dtls/client.c b/examples/networking/coap/gcoap_dtls/client.c index 224dcd11a4..46d3dfe536 100644 --- a/examples/networking/coap/gcoap_dtls/client.c +++ b/examples/networking/coap/gcoap_dtls/client.c @@ -131,16 +131,16 @@ static void _resp_handler(const gcoap_request_memo_t *memo, coap_pkt_t* pdu, uri_parser_result_t urip; uri_parser_process(&urip, _last_req_uri, strlen(_last_req_uri)); if (*_proxy_uri) { - gcoap_req_init(pdu, (uint8_t *)pdu->hdr, CONFIG_GCOAP_PDU_BUF_SIZE, + gcoap_req_init(pdu, pdu->buf, CONFIG_GCOAP_PDU_BUF_SIZE, COAP_METHOD_GET, NULL); } else { - gcoap_req_init(pdu, (uint8_t *)pdu->hdr, CONFIG_GCOAP_PDU_BUF_SIZE, + gcoap_req_init(pdu, pdu->buf, CONFIG_GCOAP_PDU_BUF_SIZE, COAP_METHOD_GET, urip.path); } if (msg_type == COAP_TYPE_ACK) { - coap_hdr_set_type(pdu->hdr, COAP_TYPE_CON); + coap_pkt_set_type(pdu, COAP_TYPE_CON); } block.blknum++; coap_opt_add_block2_control(pdu, &block); @@ -151,7 +151,7 @@ static void _resp_handler(const gcoap_request_memo_t *memo, coap_pkt_t* pdu, int len = coap_opt_finish(pdu, COAP_OPT_FINISH_NONE); gcoap_socket_type_t tl = _get_tl(*_proxy_uri ? _proxy_uri : _last_req_uri); - _send((uint8_t *)pdu->hdr, len, remote, memo->context, tl); + _send(pdu->buf, len, remote, memo->context, tl); } else { puts("--- blockwise complete ---"); @@ -344,7 +344,7 @@ static int _cli_cmd(int argc, char **argv) } } - coap_hdr_set_type(pdu.hdr, msg_type); + coap_pkt_set_type(&pdu, msg_type); size_t paylen = 0; if (apos < argc) { diff --git a/examples/networking/coap/nanocoap_server/coap_handler.c b/examples/networking/coap/nanocoap_server/coap_handler.c index 645874ef9a..a415693a99 100644 --- a/examples/networking/coap/nanocoap_server/coap_handler.c +++ b/examples/networking/coap/nanocoap_server/coap_handler.c @@ -12,9 +12,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; @@ -155,7 +155,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); } @@ -164,7 +164,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) { diff --git a/examples/wasm/hello.wasm b/examples/wasm/hello.wasm new file mode 100755 index 0000000000..78a35e794e Binary files /dev/null and b/examples/wasm/hello.wasm differ diff --git a/sys/include/net/gcoap.h b/sys/include/net/gcoap.h index 01938ab45e..f40c88d68a 100644 --- a/sys/include/net/gcoap.h +++ b/sys/include/net/gcoap.h @@ -400,14 +400,15 @@ #include "event/callback.h" #include "event/timeout.h" -#include "net/ipv6/addr.h" #include "net/sock/udp.h" -#if IS_USED(MODULE_GCOAP_DTLS) -#include "net/sock/dtls.h" -#endif #include "net/nanocoap.h" -#include "net/nanocoap/cache.h" -#include "timex.h" + +#if IS_USED(MODULE_GCOAP_DTLS) +# include "net/sock/dtls.h" +#endif +#if IS_USED(MODULE_NANOCOAP_CACHE) +# include "net/nanocoap/cache.h" +#endif #ifdef __cplusplus extern "C" { @@ -479,7 +480,7 @@ extern "C" { /** * @brief Maximum length in bytes for a header, including the token */ -#define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX) +#define GCOAP_HEADER_MAXLEN (sizeof(coap_udp_hdr_t) + GCOAP_TOKENLEN_MAX) /** * @ingroup net_gcoap_conf @@ -1187,21 +1188,36 @@ ssize_t gcoap_encode_link(const coap_resource_t *resource, char *buf, sock_dtls_t *gcoap_get_sock_dtls(void); #endif +/** + * @brief Get the buffer from a @ref gcoap_request_memo_t + * + * @param[in] memo A request memo. Must not be NULL. + * + * @return The buffer storing the message + */ +static inline uint8_t *gcoap_request_memo_get_buf(gcoap_request_memo_t *memo) +{ + if (memo->send_limit == GCOAP_SEND_LIMIT_NON) { + return &memo->msg.hdr_buf[0]; + } + else { + return memo->msg.data.pdu_buf; + } +} + /** * @brief Get the header of a request from a @ref gcoap_request_memo_t * * @param[in] memo A request memo. Must not be NULL. * * @return The request header for the given request memo. + * + * @deprecated Use @ref gcoap_request_memo_get_buf instead */ -static inline coap_hdr_t *gcoap_request_memo_get_hdr(const gcoap_request_memo_t *memo) +static inline coap_udp_hdr_t *gcoap_request_memo_get_hdr(const gcoap_request_memo_t *memo) { - if (memo->send_limit == GCOAP_SEND_LIMIT_NON) { - return (coap_hdr_t *)&memo->msg.hdr_buf[0]; - } - else { - return (coap_hdr_t *)memo->msg.data.pdu_buf; - } + gcoap_request_memo_t *evil_cast_is_evil = (gcoap_request_memo_t *)memo; + return (coap_udp_hdr_t *)gcoap_request_memo_get_buf(evil_cast_is_evil); } #ifdef __cplusplus diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index c7812cca1f..4f4fb836ed 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -22,7 +22,7 @@ * * nanocoap includes the core structs to store message information. It also * provides helper functions for use before sending and after receiving a - * message, such as coap_parse() to read an incoming message. + * message, such as coap_parse_udp() to read an incoming message. * * ## Application APIs * @@ -77,7 +77,6 @@ */ #include -#include #include #include #include @@ -185,13 +184,21 @@ extern "C" { /** @} */ /** - * @brief Raw CoAP PDU header structure + * @brief Raw CoAP over UDP PDU header structure */ typedef struct __attribute__((packed)) { uint8_t ver_t_tkl; /**< version, token, token length */ uint8_t code; /**< CoAP code (e.g.m 205) */ uint16_t id; /**< Req/resp ID */ -} coap_hdr_t; +} coap_udp_hdr_t; + +/** + * @brief Alias for @ref coap_udp_hdr_t for backward compatibility + * + * @deprecated Avoid using low level types in your application, but rather use + * the zero overhead wrappers and work on @ref coap_pkt_t instead. + */ +typedef coap_udp_hdr_t coap_hdr_t; /** * @brief CoAP option array entry @@ -204,9 +211,10 @@ typedef struct { /** * @brief CoAP PDU parsing context structure * - * When this struct is used to assemble the header, @p payload is used as the - * write pointer and @p payload_len contains the number of free bytes left in - * then packet buffer pointed to by @ref coap_pkt_t::hdr + * When this struct is used to assemble the header, @ref coap_pkt_t::payload is + * used as the write pointer and @ref coap_pkt_t::payload_len contains the + * number of free bytes left in then packet buffer pointed to by @ref + * coap_pkt_t::buf * * When the header was written, @p payload must not be changed, it must remain * pointing to the end of the header. @@ -219,7 +227,26 @@ typedef struct { * (or header byte) in the original CoAP packet buffer. */ typedef struct { - coap_hdr_t *hdr; /**< pointer to raw packet */ + union { + /** + * @brief pointer to the beginning of the buffer holding the pkt + * + * In other words: Pointer to the first byte of the header. + */ + uint8_t *buf; + /** + * @brief Deprecated alias for @ref coap_pkt_t::buf + * + * @warning This alias for @ref coap_pkt_t::buf is not available if a + * non-UDP transport for nanocoap is used, as this has the + * assumption baked in that the beginning of the message + * buffer holds a UDP style CoAP header. + * @deprecated Use @ref coap_pkt_t::buf to access the underlying buffer. + * Use helpers such as @ref coap_get_code_raw to parse the + * contents in a transport agnostic way. + */ + coap_udp_hdr_t *hdr; + }; uint8_t *payload; /**< pointer to end of the header */ iolist_t *snips; /**< payload snips (optional)*/ uint16_t payload_len; /**< length of payload */ @@ -326,9 +353,15 @@ void coap_request_ctx_init(coap_request_ctx_t *ctx, sock_udp_ep_t *remote); */ struct _coap_request_ctx { const coap_resource_t *resource; /**< resource of the request */ - sock_udp_ep_t *remote; /**< remote endpoint of the request */ + union { + sock_udp_ep_t *remote_udp; /**< remote UDP endpoint of the request */ + sock_udp_ep_t *remote; /**< deprecated alias for request_udp */ + }; #if defined(MODULE_SOCK_AUX_LOCAL) || DOXYGEN - sock_udp_ep_t *local; /**< local endpoint of the request */ + union { + sock_udp_ep_t *local_udp; /**< local UDP endpoint of the request */ + sock_udp_ep_t *local; /**< deprecated alias for local_udp */ + }; #endif #if defined(MODULE_GCOAP) || DOXYGEN /** @@ -342,9 +375,9 @@ struct _coap_request_ctx { }; /* forward declarations */ -static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr); -static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr); -static inline const void * coap_hdr_get_token(const coap_hdr_t *hdr); +static inline uint8_t *coap_hdr_data_ptr(const coap_udp_hdr_t *hdr); +static inline size_t coap_hdr_get_token_len(const coap_udp_hdr_t *hdr); +static inline const void *coap_hdr_get_token(const coap_udp_hdr_t *hdr); /** * @brief Get resource path associated with a CoAP request @@ -445,6 +478,27 @@ extern const unsigned coap_resources_numof; * Includes message ID, code, type, token, CoAP version */ /**@{*/ +/** + * @brief Get the CoAP header of a CoAP over UDP packet + * @param[in] pkt The packet to get the header of + * @return A pointer to the header in the packet + * @retval NULL The packet is not using UDP as transport + */ +static inline coap_udp_hdr_t *coap_get_udp_hdr(coap_pkt_t *pkt) +{ + /* currently only UDP as transport supported */ + return (coap_udp_hdr_t *)pkt->buf; +} + +/** + * @brief Same as @ref coap_get_udp_hdr but for `const` memory + */ +static inline const coap_udp_hdr_t *coap_get_udp_hdr_const(const coap_pkt_t *pkt) +{ + /* currently only UDP as transport supported */ + return (const coap_udp_hdr_t *)pkt->buf; +} + /** * @brief Encode given code class and code detail to raw code * @@ -458,6 +512,18 @@ static inline uint8_t coap_code(unsigned cls, unsigned detail) return (cls << 5) | detail; } +/** + * @brief Get a message's raw code (class + detail) + * + * @param[in] pkt CoAP packet + * + * @returns raw message code + */ +static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt) +{ + return coap_get_udp_hdr_const(pkt)->code; +} + /** * @brief Get a message's code class (3 most significant bits of code) * @@ -467,7 +533,7 @@ static inline uint8_t coap_code(unsigned cls, unsigned detail) */ static inline unsigned coap_get_code_class(const coap_pkt_t *pkt) { - return pkt->hdr->code >> 5; + return coap_get_code_raw(pkt) >> 5; } /** @@ -479,7 +545,7 @@ static inline unsigned coap_get_code_class(const coap_pkt_t *pkt) */ static inline unsigned coap_get_code_detail(const coap_pkt_t *pkt) { - return pkt->hdr->code & 0x1f; + return coap_get_code_raw(pkt) & 0x1f; } /** @@ -494,18 +560,6 @@ static inline unsigned coap_get_code_decimal(const coap_pkt_t *pkt) return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt); } -/** - * @brief Get a message's raw code (class + detail) - * - * @param[in] pkt CoAP packet - * - * @returns raw message code - */ -static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt) -{ - return (unsigned)pkt->hdr->code; -} - /** * @brief Get a request's method type * @@ -515,19 +569,30 @@ static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt) */ static inline coap_method_t coap_get_method(const coap_pkt_t *pkt) { - return pkt->hdr->code; + return coap_get_code_raw(pkt); } /** * @brief Get the message ID of the given CoAP packet * - * @param[in] pkt CoAP packet + * @param[in] pkt CoAP packet to get the ID of * * @returns message ID */ static inline unsigned coap_get_id(const coap_pkt_t *pkt) { - return ntohs(pkt->hdr->id); + return ntohs(coap_get_udp_hdr_const(pkt)->id); +} + +/** + * @brief Set the message ID of the given CoAP packet + * + * @param[out] pkt CoAP packet to write the ID to + * @param[in] id Message ID to write (in host byte order) + */ +static inline void coap_set_id(coap_pkt_t *pkt, uint16_t id) +{ + coap_get_udp_hdr(pkt)->id = htons(id); } /** @@ -542,7 +607,7 @@ static inline unsigned coap_get_id(const coap_pkt_t *pkt) */ static inline unsigned coap_get_token_len(const coap_pkt_t *pkt) { - return coap_hdr_get_token_len(pkt->hdr); + return coap_hdr_get_token_len((const coap_udp_hdr_t *)pkt->buf); } /** @@ -554,7 +619,7 @@ static inline unsigned coap_get_token_len(const coap_pkt_t *pkt) */ static inline void *coap_get_token(const coap_pkt_t *pkt) { - return coap_hdr_data_ptr(pkt->hdr); + return coap_hdr_data_ptr(coap_get_udp_hdr_const(pkt)); } /** @@ -568,7 +633,7 @@ static inline void *coap_get_token(const coap_pkt_t *pkt) */ static inline unsigned coap_get_total_len(const coap_pkt_t *pkt) { - return (uintptr_t)pkt->payload - (uintptr_t)pkt->hdr + pkt->payload_len; + return (uintptr_t)pkt->payload - (uintptr_t)pkt->buf + pkt->payload_len; } /** @@ -583,7 +648,7 @@ static inline unsigned coap_get_total_len(const coap_pkt_t *pkt) */ static inline unsigned coap_get_type(const coap_pkt_t *pkt) { - return (pkt->hdr->ver_t_tkl & 0x30) >> 4; + return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x30) >> 4; } /** @@ -595,20 +660,22 @@ static inline unsigned coap_get_type(const coap_pkt_t *pkt) */ static inline unsigned coap_get_ver(const coap_pkt_t *pkt) { - return (pkt->hdr->ver_t_tkl & 0x60) >> 6; + return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x60) >> 6; } /** * @brief Get the size of the extended Token length field * (RFC 8974) * + * @deprecated Use @ref coap_pkt_tkl_ext_len instead. + * * @note This requires the `nanocoap_token_ext` module to be enabled * * @param[in] hdr CoAP header * * @returns number of bytes used for extended token length */ -static inline uint8_t coap_hdr_tkl_ext_len(const coap_hdr_t *hdr) +static inline uint8_t coap_hdr_tkl_ext_len(const coap_udp_hdr_t *hdr) { if (!IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) { return 0; @@ -627,16 +694,33 @@ static inline uint8_t coap_hdr_tkl_ext_len(const coap_hdr_t *hdr) } } +/** + * @brief Get the size of the extended Token length field + * (RFC 8974) + * + * @note This requires the `nanocoap_token_ext` module to be enabled + * + * @param[in] pkt CoAP packet + * + * @returns number of bytes used for extended token length + */ +static inline uint8_t coap_pkt_tkl_ext_len(const coap_pkt_t *pkt) +{ + return coap_hdr_tkl_ext_len(coap_get_udp_hdr_const(pkt)); +} + /** * @brief Get the start of data after the header * * @param[in] hdr Header of CoAP packet in contiguous memory * + * @deprecated Use coap_get_token() instead + * * @returns pointer to first byte after the header */ -static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr) +static inline uint8_t *coap_hdr_data_ptr(const coap_udp_hdr_t *hdr) { - return ((uint8_t *)hdr) + sizeof(coap_hdr_t) + coap_hdr_tkl_ext_len(hdr); + return ((uint8_t *)hdr) + sizeof(coap_udp_hdr_t) + coap_hdr_tkl_ext_len(hdr); } /** @@ -648,7 +732,7 @@ static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr) */ 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) + + return sizeof(coap_udp_hdr_t) + coap_hdr_tkl_ext_len(pkt->hdr) + coap_get_token_len(pkt); } @@ -673,8 +757,10 @@ static inline unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt) * * @param[out] hdr CoAP header to write to * @param[in] code raw message code + * + * @deprecated Use @ref coap_pkt_set_code instead */ -static inline void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code) +static inline void coap_hdr_set_code(coap_udp_hdr_t *hdr, uint8_t code) { hdr->code = code; } @@ -687,7 +773,7 @@ static inline void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code) */ static inline void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code) { - coap_hdr_set_code(pkt->hdr, code); + coap_hdr_set_code(coap_get_udp_hdr(pkt), code); } /** @@ -697,8 +783,10 @@ static inline void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code) * * @param[out] hdr CoAP header to write * @param[in] type message type as integer value [0-3] + * + * @deprecated Use @ref coap_pkt_set_type instead */ -static inline void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type) +static inline void coap_hdr_set_type(coap_udp_hdr_t *hdr, unsigned type) { /* assert correct range of type */ assert(!(type & ~0x3)); @@ -722,7 +810,7 @@ static inline void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type) * @ref coap_get_token instead. In the TX path the token was * added by us, so we really should know. */ -static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr) +static inline size_t coap_hdr_get_token_len(const coap_udp_hdr_t *hdr) { const uint8_t *buf = (const void *)hdr; /* Regarding use unnamed magic numbers 13 and 269: @@ -739,9 +827,9 @@ static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr) case 0: return hdr->ver_t_tkl & 0xf; case 1: - return buf[sizeof(coap_hdr_t)] + 13; + return buf[sizeof(coap_udp_hdr_t)] + 13; case 2: - return byteorder_bebuftohs(buf + sizeof(coap_hdr_t)) + 269; + return byteorder_bebuftohs(buf + sizeof(coap_udp_hdr_t)) + 269; } return 0; @@ -762,7 +850,7 @@ static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr) * @ref coap_get_token instead. In the TX path the token was * added by us, so we really should know. */ -static inline const void * coap_hdr_get_token(const coap_hdr_t *hdr) +static inline const void * coap_hdr_get_token(const coap_udp_hdr_t *hdr) { uint8_t *token = (void *)hdr; token += sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr); @@ -783,10 +871,44 @@ static inline const void * coap_hdr_get_token(const coap_hdr_t *hdr) * was created by us (e.g. using @ref coap_build_hdr which returns * the header size), so we really should know already. */ -static inline size_t coap_hdr_len(const coap_hdr_t *hdr) +static inline size_t coap_hdr_len(const coap_udp_hdr_t *hdr) { return sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr) + coap_hdr_get_token_len(hdr); } + +/** + * @brief Set the message type for the given CoAP packet + * + * @pre (type := [0-3]) + * + * @param[out] pkt CoAP packet to write to + * @param[in] type message type as integer value [0-3] + */ +static inline void coap_pkt_set_type(coap_pkt_t *pkt, unsigned type) +{ + coap_udp_hdr_t *hdr = coap_get_udp_hdr(pkt); + + if (hdr) { + coap_hdr_set_type(hdr, type); + } +} + +/** + * @brief Set the message token length for the given CoAP packet + * + * @pre `tkl <= 8` + * + * @param[out] pkt CoAP packet to write to + * @param[in] tkl Token length to write + * + * @warning This function is internal, no out of tree users please. + */ +static inline void coap_pkt_set_tkl(coap_pkt_t *pkt, uint8_t tkl) +{ + coap_udp_hdr_t * hdr = coap_get_udp_hdr(pkt); + hdr->ver_t_tkl &= 0xf0; + hdr->ver_t_tkl |= (tkl & 0x0f); +} /**@}*/ /** @@ -2062,6 +2184,21 @@ ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned payload_len, coap_block_slicer_t *slicer); +/** + * @brief Build a CoAP over UDP header + * + * @param[out] buf Destination buffer to write to + * @param[in] buf_len Length of @p buf in bytes + * @param[in] type CoAP packet type (e.g., COAP_TYPE_CON, ...) + * @param[in] token token + * @param[in] token_len length of @p token + * @param[in] code CoAP code (e.g., COAP_CODE_204, ...) + * @param[in] id CoAP request id + * + * @returns length of resulting header + */ +ssize_t coap_build_udp_hdr(void *buf, size_t buf_len, uint8_t type, const void *token, + size_t token_len, uint8_t code, uint16_t id); /** * @brief Builds a CoAP header * @@ -2080,9 +2217,22 @@ ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code, * the request). * * @returns length of resulting header + * + * @deprecated Use @ref coap_build_udp_hdr instead */ -ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, const void *token, - size_t token_len, unsigned code, uint16_t id); +static inline ssize_t coap_build_hdr(coap_udp_hdr_t *hdr, unsigned type, const void *token, + size_t token_len, unsigned code, uint16_t id) +{ + size_t fingers_crossed_size = sizeof(*hdr) + token_len; + + if (IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) { + /* With RFC 8974, we have an additional extended TKL + * field of 0-4 bytes in length */ + fingers_crossed_size += 4; + } + + return coap_build_udp_hdr(hdr, fingers_crossed_size, type, token, token_len, code, id); +} /** * @brief Build reply to CoAP request @@ -2160,7 +2310,7 @@ ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, * @returns size of reply packet on success * @returns -ENOSPC if @p rbuf too small */ -ssize_t coap_build_empty_ack(coap_pkt_t *pkt, coap_hdr_t *ack); +ssize_t coap_build_empty_ack(const coap_pkt_t *pkt, coap_udp_hdr_t *ack); /** * @brief Handle incoming CoAP request @@ -2233,7 +2383,7 @@ static inline coap_method_flags_t coap_method2flag(unsigned code) } /** - * @brief Parse a CoAP PDU + * @brief Parse a CoAP PDU in UDP / DTLS format * * This function parses a raw CoAP PDU from @p buf with size @p len and fills * the structure pointed to by @p pkt. @@ -2243,10 +2393,21 @@ static inline coap_method_flags_t coap_method2flag(unsigned code) * @param[in] buf pointer to raw packet data * @param[in] len length of packet at @p buf * - * @returns 0 on success - * @returns <0 on error + * @return Number of bytes parsed (may not match @p len on stream + * transports) + * @retval <0 error */ -int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len); +ssize_t coap_parse_udp(coap_pkt_t *pkt, uint8_t *buf, size_t len); + +/** + * @brief Alias for @ref coap_parse_udp + * + * @deprecated Use @ref coap_parse_udp instead + */ +static inline ssize_t coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len) +{ + return coap_parse_udp(pkt, buf, len); +} /** * @brief Initialize a packet struct, to build a message buffer diff --git a/sys/net/application_layer/cord/ep/cord_ep.c b/sys/net/application_layer/cord/ep/cord_ep.c index 42e47a77c9..58a6fdd76d 100644 --- a/sys/net/application_layer/cord/ep/cord_ep.c +++ b/sys/net/application_layer/cord/ep/cord_ep.c @@ -18,6 +18,7 @@ * @} */ +#include #include #include "mutex.h" @@ -28,15 +29,11 @@ #include "net/ipv6/addr.h" #include "net/cord/ep.h" #include "net/cord/common.h" -#include "net/cord/config.h" #ifdef MODULE_CORD_EP_STANDALONE #include "net/cord/ep_standalone.h" #endif -#define ENABLE_DEBUG 0 -#include "debug.h" - #define FLAG_SUCCESS (0x0001) #define FLAG_TIMEOUT (0x0002) #define FLAG_ERR (0x0004) @@ -87,7 +84,7 @@ static void _on_register(const gcoap_request_memo_t *memo, coap_pkt_t* pdu, thread_flags_t flag = FLAG_ERR; if ((memo->state == GCOAP_MEMO_RESP) && - (pdu->hdr->code == COAP_CODE_CREATED)) { + (coap_get_code_raw(pdu) == COAP_CODE_CREATED)) { /* read the location header and save the RD details on success */ if (coap_get_location_path(pdu, (uint8_t *)_rd_loc, sizeof(_rd_loc)) > 0) { @@ -110,7 +107,7 @@ static void _on_update_remove(unsigned req_state, coap_pkt_t *pdu, uint8_t code) { thread_flags_t flag = FLAG_ERR; - if ((req_state == GCOAP_MEMO_RESP) && (pdu->hdr->code == code)) { + if ((req_state == GCOAP_MEMO_RESP) && (coap_get_code_raw(pdu) == code)) { flag = FLAG_SUCCESS; } else if (req_state == GCOAP_MEMO_TIMEOUT) { @@ -147,7 +144,7 @@ static int _update_remove(unsigned code, gcoap_resp_handler_t handle) if (res < 0) { return CORD_EP_ERR; } - coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON); + coap_pkt_set_type(&pkt, COAP_TYPE_CON); ssize_t pkt_len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE); /* send request */ @@ -223,7 +220,7 @@ static int _discover_internal(const sock_udp_ep_t *remote, if (res < 0) { return CORD_EP_ERR; } - coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON); + coap_pkt_set_type(&pkt, COAP_TYPE_CON); coap_opt_add_uri_query(&pkt, "rt", "core.rd"); size_t pkt_len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE); res = gcoap_req_send(buf, pkt_len, remote, NULL, _on_discover, NULL, GCOAP_SOCKET_TYPE_UNDEF); @@ -277,7 +274,7 @@ int cord_ep_register(const sock_udp_ep_t *remote, const char *regif) goto end; } /* set some packet options and write query string */ - coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON); + coap_pkt_set_type(&pkt, COAP_TYPE_CON); coap_opt_add_uint(&pkt, COAP_OPT_CONTENT_FORMAT, COAP_FORMAT_LINK); res = cord_common_add_qstring(&pkt); if (res < 0) { diff --git a/sys/net/application_layer/cord/epsim/cord_epsim.c b/sys/net/application_layer/cord/epsim/cord_epsim.c index 0f24d4b91a..742d0f2471 100644 --- a/sys/net/application_layer/cord/epsim/cord_epsim.c +++ b/sys/net/application_layer/cord/epsim/cord_epsim.c @@ -23,9 +23,7 @@ #include "assert.h" #include "net/gcoap.h" #include "net/cord/epsim.h" -#include "net/cord/config.h" #include "net/cord/common.h" -#include "net/ipv6/addr.h" #define BUFSIZE (128U) @@ -59,7 +57,7 @@ int cord_epsim_register(const sock_udp_ep_t *rd_ep) return CORD_EPSIM_ERROR; } /* make packet confirmable */ - coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON); + coap_pkt_set_type(&pkt, COAP_TYPE_CON); /* add Uri-Query options */ if (cord_common_add_qstring(&pkt) < 0) { return CORD_EPSIM_ERROR; diff --git a/sys/net/application_layer/cord/lc/cord_lc.c b/sys/net/application_layer/cord/lc/cord_lc.c index 72d8fd9f0e..2a103a28de 100644 --- a/sys/net/application_layer/cord/lc/cord_lc.c +++ b/sys/net/application_layer/cord/lc/cord_lc.c @@ -183,7 +183,7 @@ static ssize_t _lookup_raw(const cord_lc_rd_t *rd, unsigned content_format, DEBUG("cord_lc: unsupported content format\n"); return CORD_LC_ERR; } - coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON); + coap_pkt_set_type(&pkt, COAP_TYPE_CON); coap_opt_add_uint(&pkt, COAP_OPT_ACCEPT, content_format); pkt_len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE); @@ -251,7 +251,7 @@ static int _send_rd_init_req(coap_pkt_t *pkt, const sock_udp_ep_t *remote, return CORD_LC_ERR; } - coap_hdr_set_type(pkt->hdr, COAP_TYPE_CON); + coap_pkt_set_type(pkt, COAP_TYPE_CON); coap_opt_add_uri_query(pkt, "rt", "core.rd-lookup-*"); ssize_t pkt_len = coap_opt_finish(pkt, COAP_OPT_FINISH_NONE); diff --git a/sys/net/application_layer/gcoap/dns.c b/sys/net/application_layer/gcoap/dns.c index 7273ac1193..886c4b4b0b 100644 --- a/sys/net/application_layer/gcoap/dns.c +++ b/sys/net/application_layer/gcoap/dns.c @@ -17,22 +17,17 @@ #include #include -#include "fmt.h" -#include "log.h" #include "mutex.h" #include "net/credman.h" #include "net/gcoap.h" -#include "net/af.h" #include "net/dns/cache.h" #include "net/ipv4/addr.h" #include "net/ipv6/addr.h" #include "net/sock/dns.h" #include "net/sock/udp.h" -#include "net/sock/util.h" #include "random.h" #include "string_utils.h" #include "uri_parser.h" -#include "ut_process.h" #include "net/gcoap/dns.h" @@ -493,7 +488,7 @@ static ssize_t _req_init(coap_pkt_t *pdu, uri_parser_result_t *uri_comp, bool co gcoap_req_init_path_buffer(pdu, pdu->payload, pdu->payload_len, COAP_METHOD_FETCH, uri_comp->path, uri_comp->path_len); if (con) { - coap_hdr_set_type(pdu->hdr, COAP_TYPE_CON); + coap_pkt_set_type(pdu, COAP_TYPE_CON); } if (coap_opt_add_format(pdu, COAP_FORMAT_DNS_MESSAGE) < 0) { @@ -545,7 +540,7 @@ static int _do_block(coap_pkt_t *pdu, const sock_udp_ep_t *remote, coap_block1_finish(&slicer); - if ((len = _send(pdu->hdr, len, remote, slicer.start == 0, context, tl_type)) <= 0) { + if ((len = _send(pdu->buf, len, remote, slicer.start == 0, context, tl_type)) <= 0) { DEBUG("gcoap_dns: msg send failed: %" PRIdSIZE "\n", len); return len; } @@ -583,7 +578,7 @@ static ssize_t _req(_req_ctx_t *context) } len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD); memcpy(pdu->payload, context->dns_buf, context->dns_buf_len); - return _send(pdu->hdr, len + context->dns_buf_len, &_remote, true, context, tl_type); + return _send(pdu->buf, len + context->dns_buf_len, &_remote, true, context, tl_type); } } @@ -698,7 +693,7 @@ static void _resp_handler(const gcoap_request_memo_t *memo, coap_pkt_t *pdu, unsigned msg_type = coap_get_type(pdu); int len; - pdu->payload = (uint8_t *)pdu->hdr; + pdu->payload = pdu->buf; pdu->payload_len = CONFIG_GCOAP_DNS_PDU_BUF_SIZE; tl_type = _req_init(pdu, &_uri_comp, msg_type == COAP_TYPE_ACK); block.blknum++; @@ -712,7 +707,7 @@ static void _resp_handler(const gcoap_request_memo_t *memo, coap_pkt_t *pdu, goto unlock; } len = coap_opt_finish(pdu, COAP_OPT_FINISH_NONE); - if ((len = _send((uint8_t *)pdu->hdr, len, remote, false, context, tl_type)) <= 0) { + if ((len = _send(pdu->buf, len, remote, false, context, tl_type)) <= 0) { DEBUG("gcoap_dns: Unable to request next block: %d\n", len); context->res = len; goto unlock; diff --git a/sys/net/application_layer/gcoap/forward_proxy.c b/sys/net/application_layer/gcoap/forward_proxy.c index b49dd56f9d..5182cfa135 100644 --- a/sys/net/application_layer/gcoap/forward_proxy.c +++ b/sys/net/application_layer/gcoap/forward_proxy.c @@ -18,15 +18,17 @@ #include #include "event.h" -#include "kernel_defines.h" #include "net/gcoap.h" #include "net/gcoap/forward_proxy.h" #include "uri_parser.h" -#include "net/nanocoap/cache.h" #include "ztimer.h" #include "forward_proxy_internal.h" +#if MODULE_NANOCOAP_CACHE +# include "net/nanocoap/cache.h" +#endif + #define ENABLE_DEBUG 0 #include "debug.h" @@ -242,7 +244,7 @@ static ssize_t _dispatch_msg(const void *buf, size_t len, sock_udp_ep_t *remote, static void _send_empty_ack(event_t *event) { - coap_hdr_t buf; + uint8_t buf[sizeof(coap_udp_hdr_t)]; client_ep_t *cep = container_of(event, client_ep_t, event); if (_cep_get_response_type(cep) != COAP_TYPE_ACK) { @@ -250,17 +252,15 @@ static void _send_empty_ack(event_t *event) } _cep_set_response_type(cep, COAP_TYPE_CON); - /* Flipping byte order as unlike in the other places where mid is - * used, coap_build_hdr would actually flip it back */ - coap_build_hdr(&buf, COAP_TYPE_ACK, NULL, 0, 0, ntohs(cep->mid)); + coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_ACK, NULL, 0, 0, cep->mid); _dispatch_msg(&buf, sizeof(buf), &cep->ep, &cep->proxy_ep); } static void _set_response_type(coap_pkt_t *pdu, uint8_t resp_type) { - coap_hdr_set_type(pdu->hdr, resp_type); + coap_pkt_set_type(pdu, resp_type); if (resp_type == COAP_TYPE_CON) { - pdu->hdr->id = htons(gcoap_next_msg_id()); + coap_set_id(pdu, gcoap_next_msg_id()); } } @@ -315,18 +315,18 @@ static void _forward_resp_handler(const gcoap_request_memo_t *memo, /* the response was truncated, so there should be enough space * to allocate an empty error message instead (with a potential Observe option) if not, * _listen_buf is _way_ too short ;-) */ - assert(buf_len >= (sizeof(*pdu->hdr) + 4U)); - gcoap_resp_init(pdu, (uint8_t *)pdu->hdr, buf_len, COAP_CODE_INTERNAL_SERVER_ERROR); + assert(buf_len >= (sizeof(coap_udp_hdr_t) + 4U)); + gcoap_resp_init(pdu, pdu->buf, buf_len, COAP_CODE_INTERNAL_SERVER_ERROR); coap_opt_finish(pdu, COAP_OPT_FINISH_NONE); _set_response_type(pdu, _cep_get_response_type(cep)); } else if (memo->state == GCOAP_MEMO_TIMEOUT) { /* send RST */ - gcoap_resp_init(pdu, (uint8_t *)pdu->hdr, buf_len, COAP_CODE_EMPTY); + gcoap_resp_init(pdu, pdu->buf, buf_len, COAP_CODE_EMPTY); coap_opt_finish(pdu, COAP_OPT_FINISH_NONE); } /* don't use buf_len here, in case the above `gcoap_resp_init`s changed `pdu` */ - _dispatch_msg(pdu->hdr, coap_get_total_len(pdu), &cep->ep, &cep->proxy_ep); + _dispatch_msg(pdu->buf, coap_get_total_len(pdu), &cep->ep, &cep->proxy_ep); _free_client_ep(cep); } @@ -417,7 +417,7 @@ static int _gcoap_forward_proxy_copy_options(coap_pkt_t *pkt, int gcoap_forward_proxy_req_send(client_ep_t *cep) { int len; - if ((len = gcoap_req_send((uint8_t *)cep->pdu.hdr, coap_get_total_len(&cep->pdu), + if ((len = gcoap_req_send(cep->pdu.buf, coap_get_total_len(&cep->pdu), &cep->server_ep, NULL, _forward_resp_handler, cep, GCOAP_SOCKET_TYPE_UNDEF)) <= 0) { DEBUG("gcoap_forward_proxy_req_send(): gcoap_req_send failed %d\n", len); @@ -456,15 +456,9 @@ static int _gcoap_forward_proxy_via_coap(coap_pkt_t *client_pkt, unsigned token_len = coap_get_token_len(client_pkt); coap_pkt_init(&client_ep->pdu, proxy_req_buf, CONFIG_GCOAP_PDU_BUF_SIZE, - sizeof(coap_hdr_t) + token_len); + sizeof(coap_udp_hdr_t) + token_len); - client_ep->pdu.hdr->ver_t_tkl = client_pkt->hdr->ver_t_tkl; - client_ep->pdu.hdr->code = client_pkt->hdr->code; - client_ep->pdu.hdr->id = client_pkt->hdr->id; - - if (token_len) { - memcpy(coap_get_token(&client_ep->pdu), coap_get_token(client_pkt), token_len); - } + memcpy(client_ep->pdu.buf, client_pkt->buf, coap_get_total_hdr_len(client_pkt)); /* copy all options from client_pkt to pkt */ len = _gcoap_forward_proxy_copy_options(&client_ep->pdu, client_pkt, client_ep, urip); @@ -501,7 +495,7 @@ int gcoap_forward_proxy_request_process(coap_pkt_t *pkt, return -ENOMEM; } - cep->mid = pkt->hdr->id; + cep->mid = coap_get_id(pkt); _cep_set_response_type( cep, (coap_get_type(pkt) == COAP_TYPE_CON) ? COAP_TYPE_ACK : COAP_TYPE_NON diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index ce6dd0e4c0..cb6187be3f 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -72,7 +72,7 @@ static size_t _handle_req(gcoap_socket_t *sock, coap_pkt_t *pdu, uint8_t *buf, size_t len, sock_udp_ep_t *remote, sock_udp_aux_tx_t *aux); static void _expire_request(gcoap_request_memo_t *memo); static gcoap_request_memo_t* _find_req_memo_by_mid(const sock_udp_ep_t *remote, - uint16_t mid); + const coap_pkt_t *pkt); static gcoap_request_memo_t* _find_req_memo_by_token(const sock_udp_ep_t *remote, const uint8_t *token, size_t tkl); static gcoap_request_memo_t* _find_req_memo_by_pdu_token(const coap_pkt_t *src_pdu, @@ -420,7 +420,7 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote, sock_ */ int8_t messagelayer_emptyresponse_type = NO_IMMEDIATE_REPLY; - ssize_t res = coap_parse(&pdu, buf, len); + ssize_t res = coap_parse_udp(&pdu, buf, len); if (res < 0) { DEBUG("gcoap: parse failure: %" PRIdSIZE "\n", res); /* If a response, can't clear memo, but it will timeout later. @@ -436,7 +436,7 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote, sock_ if (coap_get_type(&pdu) == COAP_TYPE_RST) { DEBUG("gcoap: received RST, expiring potentially existing memo\n"); - memo = _find_req_memo_by_mid(remote, pdu.hdr->id); + memo = _find_req_memo_by_mid(remote, &pdu); if (memo) { event_timeout_clear(&memo->resp_evt_tmout); _expire_request(memo); @@ -458,7 +458,7 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote, sock_ messagelayer_emptyresponse_type = COAP_TYPE_RST; DEBUG("gcoap: Answering empty CON request with RST\n"); } else if (coap_get_type(&pdu) == COAP_TYPE_ACK) { - memo = _find_req_memo_by_mid(remote, pdu.hdr->id); + memo = _find_req_memo_by_mid(remote, &pdu); if ((memo != NULL) && (memo->send_limit != GCOAP_SEND_LIMIT_NON)) { DEBUG("gcoap: empty ACK processed, stopping retransmissions\n"); _cease_retransmission(memo); @@ -515,7 +515,7 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote, sock_ if (IS_USED(MODULE_NANOCOAP_CACHE)) { nanocoap_cache_entry_t *ce = NULL; - if ((pdu.hdr->code == COAP_CODE_VALID) && + if ((coap_get_code_raw(&pdu) == COAP_CODE_VALID) && (ce = _cache_lookup_memo(memo))) { /* update max_age from response and send cached response */ uint32_t max_age = 60; @@ -524,7 +524,7 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote, sock_ ce->max_age = ztimer_now(ZTIMER_SEC) + max_age; /* copy all options and possible payload from the cached response * to the new response */ - assert((uint8_t *)pdu.hdr == &_listen_buf[0]); + assert((uint8_t *)pdu.buf == &_listen_buf[0]); if (_cache_build_response(ce, &pdu, _listen_buf, sizeof(_listen_buf)) < 0) { memo->state = GCOAP_MEMO_ERR; @@ -534,7 +534,7 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote, sock_ } } /* TODO: resend request if VALID but no cache entry? */ - else if ((pdu.hdr->code != COAP_CODE_VALID)) { + else if ((coap_get_code_raw(&pdu) != COAP_CODE_VALID)) { _cache_process(memo, &pdu); } } @@ -584,16 +584,11 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote, sock_ } if (messagelayer_emptyresponse_type != NO_IMMEDIATE_REPLY) { - coap_hdr_set_type(pdu.hdr, (uint8_t)messagelayer_emptyresponse_type); + coap_pkt_set_type(&pdu, messagelayer_emptyresponse_type); coap_pkt_set_code(&pdu, COAP_CODE_EMPTY); - /* Set the token length to 0, preserving the CoAP version as it was and - * the empty message type that was just set. - * - * FIXME: Introduce an internal function to set or truncate the token - * */ - pdu.hdr->ver_t_tkl &= 0xf0; + coap_pkt_set_tkl(&pdu, 0); - ssize_t bytes = _tl_send(sock, buf, sizeof(coap_hdr_t), remote, aux); + ssize_t bytes = _tl_send(sock, buf, sizeof(coap_udp_hdr_t), remote, aux); if (bytes <= 0) { DEBUG("gcoap: empty response failed: %" PRIdSIZE "\n", bytes); } @@ -801,8 +796,8 @@ static size_t _handle_req(gcoap_socket_t *sock, coap_pkt_t *pdu, uint8_t *buf, coap_request_ctx_t ctx = { .resource = resource, .tl_type = (uint32_t)sock->type, - .remote = remote, - .local = aux ? &aux->local : NULL, + .remote_udp = remote, + .local_udp = aux ? &aux->local : NULL, }; pdu_len = resource->handler(pdu, buf, len, &ctx); @@ -951,7 +946,7 @@ static gcoap_request_memo_t* _find_req_memo_by_token(const sock_udp_ep_t *remote } gcoap_request_memo_t *memo = &_coap_state.open_reqs[i]; - coap_hdr_t *hdr = gcoap_request_memo_get_hdr(memo); + coap_udp_hdr_t *hdr = gcoap_request_memo_get_hdr(memo); /* verbose debug to catch bugs with request/response matching */ #if SOCK_HAS_IPV4 @@ -1025,13 +1020,15 @@ static gcoap_request_memo_t* _find_req_memo_by_pdu_token( * Finds the memo for an outstanding request within the _coap_state.open_reqs * array. Matches on remote endpoint and message ID. * - * remote[in] Remote endpoint to match - * mid[in] Message ID to match + * @param[in] remote Remote endpoint to match + * @param[in] pkt Packet containing the message ID to search for * * return Registered request memo, or NULL if not found */ -static gcoap_request_memo_t* _find_req_memo_by_mid(const sock_udp_ep_t *remote, uint16_t mid) +static gcoap_request_memo_t* _find_req_memo_by_mid(const sock_udp_ep_t *remote, const coap_pkt_t *pkt) { + /* mid is in network byte order */ + uint16_t mid = coap_get_udp_hdr_const(pkt)->id; for (int i = 0; i < CONFIG_GCOAP_REQ_WAITING_MAX; i++) { if (_coap_state.open_reqs[i].state == GCOAP_MEMO_UNUSED) { continue; @@ -1059,7 +1056,7 @@ static void _expire_request(gcoap_request_memo_t *memo) memset(&req, 0, sizeof(req)); /* 0 means there is an observe option value */ coap_clear_observe(&req); - req.hdr = gcoap_request_memo_get_hdr(memo); + req.buf = gcoap_request_memo_get_buf(memo); memo->resp_handler(memo, &req, NULL); } _memo_clear_resend_buffer(memo); @@ -1406,9 +1403,8 @@ static void _cache_process(gcoap_request_memo_t *memo, } coap_pkt_t req; - req.hdr = gcoap_request_memo_get_hdr(memo); - size_t pdu_len = pdu->payload_len + - (pdu->payload - (uint8_t *)pdu->hdr); + req.buf = gcoap_request_memo_get_buf(memo); + size_t pdu_len = pdu->payload_len + (pdu->payload - pdu->buf); #if IS_USED(MODULE_NANOCOAP_CACHE) nanocoap_cache_entry_t *ce; /* cache_key in memo is pre-processor guarded so we need to as well */ @@ -1432,7 +1428,7 @@ static ssize_t _cache_build_response(nanocoap_cache_entry_t *ce, coap_pkt_t *pdu } /* Use the same code from the cached content. Use other header * fields from the incoming request */ - gcoap_resp_init(pdu, buf, len, ce->response_pkt.hdr->code); + gcoap_resp_init(pdu, buf, len, coap_get_code_raw(&ce->response_pkt)); /* copy all options and possible payload from the cached response * to the new response */ unsigned header_len_req = coap_get_total_hdr_len(pdu); @@ -1445,13 +1441,13 @@ static ssize_t _cache_build_response(nanocoap_cache_entry_t *ce, coap_pkt_t *pdu (ce->response_buf + header_len_cached), opt_payload_len); /* parse into pdu including all options and payload pointers etc */ - coap_parse(pdu, buf, header_len_req + opt_payload_len); + coap_parse_udp(pdu, buf, header_len_req + opt_payload_len); return ce->response_len; } static void _copy_hdr_from_req_memo(coap_pkt_t *pdu, gcoap_request_memo_t *memo) { - const coap_hdr_t *hdr = gcoap_request_memo_get_hdr(memo); + const coap_udp_hdr_t *hdr = gcoap_request_memo_get_hdr(memo); size_t hdr_len = coap_hdr_len(hdr); memcpy(pdu->hdr, hdr, hdr_len); } @@ -1469,7 +1465,7 @@ static void _receive_from_cache_cb(void *ctx) if (memo->resp_handler) { /* copy header from request so gcoap_resp_init in _cache_build_response works correctly */ - coap_pkt_t pdu = { .hdr = (coap_hdr_t *)_listen_buf }; + coap_pkt_t pdu = { .buf = _listen_buf }; _copy_hdr_from_req_memo(&pdu, memo); if (_cache_build_response(ce, &pdu, _listen_buf, sizeof(_listen_buf)) >= 0) { memo->state = (ce->truncated) ? GCOAP_MEMO_RESP_TRUNC : GCOAP_MEMO_RESP; @@ -1536,7 +1532,7 @@ static ssize_t _cache_check(const uint8_t *buf, size_t len, coap_pkt_t req; nanocoap_cache_entry_t *ce = NULL; /* XXX cast to const might cause problems here :-/ */ - ssize_t res = coap_parse(&req, (uint8_t *)buf, len); + ssize_t res = coap_parse_udp(&req, (uint8_t *)buf, len); if (res < 0) { DEBUG("gcoap: parse failure for cache lookup: %" PRIdSIZE "\n", res); @@ -1691,30 +1687,29 @@ int gcoap_req_init_path_buffer(coap_pkt_t *pdu, uint8_t *buf, size_t len, { assert((path == NULL) || (path[0] == '/')); - pdu->hdr = (coap_hdr_t *)buf; - /* generate token */ uint16_t msgid = gcoap_next_msg_id(); ssize_t res; if (code) { -#if CONFIG_GCOAP_TOKENLEN - uint8_t token[CONFIG_GCOAP_TOKENLEN]; - for (size_t i = 0; i < CONFIG_GCOAP_TOKENLEN; i += 4) { - uint32_t rand = random_uint32(); - memcpy(&token[i], - &rand, - (CONFIG_GCOAP_TOKENLEN - i >= 4) ? 4 : CONFIG_GCOAP_TOKENLEN - i); + if (CONFIG_GCOAP_TOKENLEN > 0) { + uint8_t token[CONFIG_GCOAP_TOKENLEN]; + for (size_t i = 0; i < CONFIG_GCOAP_TOKENLEN; i += 4) { + uint32_t rand = random_uint32(); + memcpy(&token[i], + &rand, + (CONFIG_GCOAP_TOKENLEN - i >= 4) ? 4 : CONFIG_GCOAP_TOKENLEN - i); + } + res = coap_build_udp_hdr(buf, len, COAP_TYPE_NON, &token[0], + CONFIG_GCOAP_TOKENLEN, code, msgid); + } + else { + res = coap_build_udp_hdr(buf, len, COAP_TYPE_NON, NULL, + CONFIG_GCOAP_TOKENLEN, code, msgid); } - res = coap_build_hdr(pdu->hdr, COAP_TYPE_NON, &token[0], - CONFIG_GCOAP_TOKENLEN, code, msgid); -#else - res = coap_build_hdr(pdu->hdr, COAP_TYPE_NON, NULL, - CONFIG_GCOAP_TOKENLEN, code, msgid); -#endif } else { /* ping request */ - res = coap_build_hdr(pdu->hdr, COAP_TYPE_CON, NULL, 0, code, msgid); + res = coap_build_udp_hdr(buf, len, COAP_TYPE_CON, NULL, 0, code, msgid); } coap_pkt_init(pdu, buf, len, res); @@ -1950,10 +1945,10 @@ int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, return GCOAP_OBS_INIT_UNUSED; } - pdu->hdr = (coap_hdr_t *)buf; + pdu->buf = buf; uint16_t msgid = gcoap_next_msg_id(); - ssize_t hdrlen = coap_build_hdr(pdu->hdr, COAP_TYPE_NON, &memo->token[0], - memo->token_len, COAP_CODE_CONTENT, msgid); + ssize_t hdrlen = coap_build_udp_hdr(buf, len, COAP_TYPE_NON, &memo->token[0], + memo->token_len, COAP_CODE_CONTENT, msgid); if (hdrlen <= 0) { /* reason for negative hdrlen is not defined, so we also are vague */ diff --git a/sys/net/application_layer/nanocoap/fileserver.c b/sys/net/application_layer/nanocoap/fileserver.c index 6a4e61ae07..98dc6e763f 100644 --- a/sys/net/application_layer/nanocoap/fileserver.c +++ b/sys/net/application_layer/nanocoap/fileserver.c @@ -21,11 +21,13 @@ #include #include -#include "kernel_defines.h" #include "checksum/fletcher32.h" #include "net/nanocoap/fileserver.h" #include "vfs.h" -#include "vfs_util.h" + +#if MODULE_VFS_UTIL +# include "vfs_util.h" +#endif #define ENABLE_DEBUG 0 #include "debug.h" @@ -518,16 +520,15 @@ static ssize_t _get_directory(coap_pkt_t *pdu, uint8_t *buf, size_t len, vfs_closedir(&dir); coap_block2_finish(&slicer); - return (uintptr_t)buf - (uintptr_t)pdu->hdr; + return (uintptr_t)buf - (uintptr_t)pdu->buf; } #if IS_USED(MODULE_NANOCOAP_FILESERVER_PUT) static ssize_t _put_directory(coap_pkt_t *pdu, uint8_t *buf, size_t len, struct requestdata *request) { - int err; vfs_DIR dir; - if ((err = vfs_opendir(&dir, request->namebuf)) == 0) { + if (vfs_opendir(&dir, request->namebuf) == 0) { vfs_closedir(&dir); if (request->options.exists.if_match && request->options.if_match_len) { return _error_handler(pdu, buf, len, COAP_CODE_PRECONDITION_FAILED); @@ -538,6 +539,7 @@ static ssize_t _put_directory(coap_pkt_t *pdu, uint8_t *buf, size_t len, if (request->options.exists.if_match) { return _error_handler(pdu, buf, len, COAP_CODE_PRECONDITION_FAILED); } + int err; if ((err = vfs_mkdir(request->namebuf, 0777)) < 0) { return _error_handler(pdu, buf, len, err); } diff --git a/sys/net/application_layer/nanocoap/fs.c b/sys/net/application_layer/nanocoap/fs.c index 3018d929e2..2c8f68f0fb 100644 --- a/sys/net/application_layer/nanocoap/fs.c +++ b/sys/net/application_layer/nanocoap/fs.c @@ -177,18 +177,20 @@ static int _query_server(nanocoap_sock_t *sock, const char *path, uint8_t *buf = _buf; coap_pkt_t pkt = { - .hdr = (void *)buf, + .buf = buf, }; uint16_t lastonum = 0; - buf += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, - nanocoap_sock_next_msg_id(sock)); + ssize_t hdr_len = coap_build_udp_hdr(_buf, sizeof(_buf), COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, + nanocoap_sock_next_msg_id(sock)); + assume(hdr_len > 0); + buf += hdr_len; buf += coap_opt_put_uri_pathquery(buf, &lastonum, path); buf += coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2, 0); buf += coap_opt_put_uint(buf, COAP_OPT_BLOCK2, COAP_OPT_SIZE2, 0); - assert((uintptr_t)buf - (uintptr_t)pkt.hdr < sizeof(_buf)); + assert((uintptr_t)buf - (uintptr_t)_buf < sizeof(_buf)); pkt.payload = buf; pkt.payload_len = 0; diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index ff64fa81ea..b536dc4768 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -77,10 +77,10 @@ static size_t _encode_uint(uint32_t *val); * |1 1 1 1 1 1 1 1| Payload (if any) ... * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ -int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len) +ssize_t coap_parse_udp(coap_pkt_t *pkt, uint8_t *buf, size_t len) { - coap_hdr_t *hdr = (coap_hdr_t *)buf; - pkt->hdr = hdr; + pkt->buf = buf; + coap_udp_hdr_t *hdr = (coap_udp_hdr_t *)buf; uint8_t *pkt_pos = coap_hdr_data_ptr(hdr); uint8_t *pkt_end = buf + len; @@ -90,17 +90,17 @@ int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len) memset(pkt->opt_crit, 0, sizeof(pkt->opt_crit)); pkt->snips = NULL; - if (len < sizeof(coap_hdr_t)) { + if (len < sizeof(coap_udp_hdr_t)) { DEBUG("msg too short\n"); return -EBADMSG; } - else if ((coap_get_code_raw(pkt) == 0) && (len > sizeof(coap_hdr_t))) { + else if ((coap_get_code_raw(pkt) == 0) && (len > sizeof(coap_udp_hdr_t))) { DEBUG("empty msg too long\n"); return -EBADMSG; } if (IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) { - if ((pkt->hdr->ver_t_tkl & 0xf) == 15) { + if ((coap_get_udp_hdr(pkt)->ver_t_tkl & 0xf) == 15) { DEBUG("nanocoap: token length is reserved value 15," "invalid for extended token length field.\n"); return -EBADMSG; @@ -182,7 +182,7 @@ int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len) coap_get_code_detail(pkt), pkt->payload_len, option_count, hdr->code); - return 0; + return len; } int coap_match_path(const coap_resource_t *resource, const uint8_t *uri) @@ -209,7 +209,7 @@ uint8_t *coap_find_option(coap_pkt_t *pkt, unsigned opt_num) if (optpos->opt_num == opt_num) { unsigned idx = index_of(pkt->options, optpos); bf_unset(pkt->opt_crit, idx); - return (uint8_t*)pkt->hdr + optpos->offset; + return pkt->buf + optpos->offset; } optpos++; } @@ -352,7 +352,7 @@ ssize_t coap_opt_get_next(const coap_pkt_t *pkt, coap_optpos_t *opt, opt->opt_num = 0; opt->offset = coap_get_total_hdr_len(pkt); } - uint8_t *start = (uint8_t*)pkt->hdr + opt->offset; + uint8_t *start = pkt->buf + opt->offset; /* Find start of option value and value length. */ uint16_t delta; @@ -365,7 +365,7 @@ ssize_t coap_opt_get_next(const coap_pkt_t *pkt, coap_optpos_t *opt, *value = start; opt->opt_num += delta; - opt->offset = start + len - (uint8_t*)pkt->hdr; + opt->offset = start + len - pkt->buf; return len; } @@ -642,7 +642,7 @@ ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code, uint8_t *bufpos = buf; uint32_t no_response; unsigned tkl = coap_get_token_len(pkt); - size_t hdr_len = sizeof(coap_hdr_t) + tkl; + size_t hdr_len = sizeof(coap_udp_hdr_t) + tkl; uint8_t type = coap_get_type(pkt) == COAP_TYPE_CON ? COAP_TYPE_ACK : COAP_TYPE_NON; @@ -658,8 +658,8 @@ ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code, return -ENOBUFS; } - bufpos += coap_build_hdr(buf, type, coap_get_token(pkt), tkl, - code, ntohs(pkt->hdr->id)); + bufpos += coap_build_udp_hdr(buf, len, type, coap_get_token(pkt), tkl, + code, coap_get_id(pkt)); if (coap_opt_get_uint(pkt, COAP_OPT_NO_RESPONSE, &no_response) == 0) { const uint8_t no_response_index = (code >> 5) - 1; @@ -726,16 +726,14 @@ ssize_t coap_reply_simple(coap_pkt_t *pkt, return header_len + payload_len; } -ssize_t coap_build_empty_ack(coap_pkt_t *pkt, coap_hdr_t *ack) +ssize_t coap_build_empty_ack(const coap_pkt_t *pkt, coap_udp_hdr_t *ack) { if (coap_get_type(pkt) != COAP_TYPE_CON) { return 0; } - coap_build_hdr(ack, COAP_TYPE_ACK, NULL, 0, - COAP_CODE_EMPTY, ntohs(pkt->hdr->id)); - - return sizeof(*ack); + return coap_build_udp_hdr(ack, sizeof(*ack), COAP_TYPE_ACK, NULL, 0, + COAP_CODE_EMPTY, coap_get_id(pkt)); } ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, @@ -753,7 +751,7 @@ ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, else if (coap_get_type(pkt) == COAP_TYPE_CON) { type = COAP_TYPE_ACK; } - unsigned len = sizeof(coap_hdr_t) + tkl; + unsigned len = sizeof(coap_udp_hdr_t) + tkl; if ((len + payload_len) > rlen) { return -ENOSPC; @@ -774,8 +772,7 @@ ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, } } - coap_build_hdr((coap_hdr_t *)rbuf, type, coap_get_token(pkt), tkl, code, - ntohs(pkt->hdr->id)); + coap_build_udp_hdr(rbuf, rlen, type, coap_get_token(pkt), tkl, code, coap_get_id(pkt)); len += payload_len; /* HACK: many CoAP handlers assume that the pkt buffer is also used for the response */ @@ -784,8 +781,9 @@ ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, return len; } -ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, const void *token, - size_t token_len, unsigned code, uint16_t id) +ssize_t coap_build_udp_hdr(void *buf, size_t buf_len, uint8_t type, + const void *token, size_t token_len, + uint8_t code, uint16_t id) { assert(!(type & ~0x3)); @@ -807,13 +805,21 @@ ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, const void *token, tkl_ext_len = 0; } - memset(hdr, 0, sizeof(coap_hdr_t)); + size_t hdr_len = sizeof(coap_udp_hdr_t) + token_len + tkl_ext_len; + if (buf_len < hdr_len) { + return -EOVERFLOW; + } + + memset(buf, 0, sizeof(coap_udp_hdr_t)); + coap_udp_hdr_t *hdr = buf; hdr->ver_t_tkl = (COAP_V1 << 6) | (type << 4) | tkl; hdr->code = code; hdr->id = htons(id); + buf += sizeof(coap_udp_hdr_t); if (tkl_ext_len) { - memcpy(hdr + 1, &tkl_ext, tkl_ext_len); + memcpy(buf, &tkl_ext, tkl_ext_len); + buf += tkl_ext_len; } /* Some users build a response packet in the same buffer that contained @@ -828,13 +834,13 @@ ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, const void *token, memcpy(token_dest, token, token_len); } - return sizeof(coap_hdr_t) + token_len + tkl_ext_len; + return hdr_len; } void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len) { memset(pkt, 0, sizeof(coap_pkt_t)); - pkt->hdr = (coap_hdr_t *)buf; + pkt->buf = buf; pkt->payload = buf + header_len; pkt->payload_len = len - header_len; } @@ -1167,7 +1173,7 @@ static ssize_t _add_opt_pkt(coap_pkt_t *pkt, uint16_t optnum, const void *val, coap_put_option(pkt->payload, lastonum, optnum, val, val_len); pkt->options[pkt->options_len].opt_num = optnum; - pkt->options[pkt->options_len].offset = pkt->payload - (uint8_t *)pkt->hdr; + pkt->options[pkt->options_len].offset = pkt->payload - pkt->buf; pkt->options_len++; pkt->payload += optlen; pkt->payload_len -= optlen; @@ -1289,7 +1295,7 @@ ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags) pkt->payload_len = 0; } - return pkt->payload - (uint8_t *)pkt->hdr; + return pkt->payload - pkt->buf; } ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t opt_num) @@ -1313,7 +1319,7 @@ ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t opt_num) if (opt_count == 0) { /* this is the last option => use payload / end pointer as old start */ start_old = (pkt->payload_len) ? pkt->payload - 1 : pkt->payload; - start_new = (uint8_t *)pkt->hdr + optpos->offset; + start_new = pkt->buf + optpos->offset; break; } @@ -1327,12 +1333,12 @@ ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t opt_num) optpos++; opt_count--; /* select start of next option */ - opt_start = (uint8_t *)pkt->hdr + optpos->offset; + opt_start = pkt->buf + optpos->offset; start_old = _parse_option(pkt, opt_start, &old_delta, &option_len); old_hdr_len = start_old - opt_start; /* select start of to be deleted option and set delta/length of next option */ - start_new = (uint8_t *)pkt->hdr + prev_opt->offset; + start_new = pkt->buf + prev_opt->offset; *start_new = 0; /* write new_delta value to option header: 4 upper bits of header (shift 4) + * 1 or 2 optional bytes depending on delta value) */ @@ -1364,7 +1370,7 @@ ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t opt_num) } pkt->payload -= (start_old - start_new); } - return (pkt->payload - ((uint8_t *)pkt->hdr)) + pkt->payload_len; + return pkt->payload - pkt->buf + pkt->payload_len; } ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len) @@ -1532,7 +1538,7 @@ ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, \ unsigned coap_get_len(coap_pkt_t *pkt) { - unsigned pktlen = sizeof(coap_hdr_t) + coap_get_token_len(pkt); + unsigned pktlen = sizeof(coap_udp_hdr_t) + coap_get_token_len(pkt); if (pkt->payload) { pktlen += pkt->payload_len + 1; } @@ -1542,7 +1548,7 @@ unsigned coap_get_len(coap_pkt_t *pkt) void coap_request_ctx_init(coap_request_ctx_t *ctx, sock_udp_ep_t *remote) { memset(ctx, 0, sizeof(*ctx)); - ctx->remote = remote; + ctx->remote_udp = remote; } const char *coap_request_ctx_get_path(const coap_request_ctx_t *ctx) @@ -1567,13 +1573,13 @@ uint32_t coap_request_ctx_get_tl_type(const coap_request_ctx_t *ctx) const sock_udp_ep_t *coap_request_ctx_get_remote_udp(const coap_request_ctx_t *ctx) { - return ctx->remote; + return ctx->remote_udp; } const sock_udp_ep_t *coap_request_ctx_get_local_udp(const coap_request_ctx_t *ctx) { #if defined(MODULE_SOCK_AUX_LOCAL) - return ctx->local; + return ctx->local_udp; #else (void)ctx; return NULL; diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index 19cee5ecd9..e80c5cd109 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -182,8 +182,7 @@ static int _send_ack(nanocoap_sock_t *sock, coap_pkt_t *pkt) .iol_len = sizeof(ack), }; - coap_build_hdr(&ack, COAP_TYPE_ACK, NULL, 0, - COAP_CODE_EMPTY, ntohs(pkt->hdr->id)); + coap_build_empty_ack(pkt, &ack); return _sock_sendv(sock, &snip); } @@ -201,7 +200,7 @@ static bool _id_or_token_missmatch(const coap_pkt_t *pkt, unsigned id, /* falls through */ default: /* token has to match if message is not empty */ - if (pkt->hdr->code != 0) { + if (coap_get_code_raw(pkt) != 0) { if (coap_get_token_len(pkt) != token_len) { return true; } @@ -260,7 +259,7 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, /* Create the first payload snip from the request buffer */ iolist_t head = { .iol_next = pkt->snips, - .iol_base = pkt->hdr, + .iol_base = pkt->buf, .iol_len = coap_get_total_len(pkt), }; @@ -334,7 +333,7 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, } /* parse response */ - if (coap_parse(pkt, payload, res) < 0) { + if (coap_parse_udp(pkt, payload, res) < 0) { DEBUG("nanocoap: error parsing packet\n"); continue; } @@ -400,10 +399,10 @@ static int _request_cb(void *arg, coap_pkt_t *pkt) return -ENOBUFS; } - memcpy(buf->iov_base, pkt->hdr, pkt_len); + memcpy(buf->iov_base, pkt->buf, pkt_len); - pkt->hdr = buf->iov_base; - pkt->payload = (uint8_t*)pkt->hdr + (pkt_len - pkt->payload_len); + pkt->buf = buf->iov_base; + pkt->payload = pkt->buf + (pkt_len - pkt->payload_len); return pkt_len; } @@ -411,7 +410,7 @@ static int _request_cb(void *arg, coap_pkt_t *pkt) ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len) { struct iovec buf = { - .iov_base = pkt->hdr, + .iov_base = pkt->buf, .iov_len = len, }; return nanocoap_sock_request_cb(sock, pkt, _request_cb, &buf); @@ -442,7 +441,7 @@ static ssize_t _sock_get(nanocoap_sock_t *sock, const char *path, uint8_t *pktpos = sock->hdr_buf; coap_pkt_t pkt = { - .hdr = (void *)pktpos, + .buf = pktpos, }; struct iovec ctx = { @@ -450,8 +449,10 @@ static ssize_t _sock_get(nanocoap_sock_t *sock, const char *path, .iov_len = max_len, }; - pktpos += coap_build_hdr(pkt.hdr, type, NULL, 0, COAP_METHOD_GET, - nanocoap_sock_next_msg_id(sock)); + ssize_t hdr_len = coap_build_udp_hdr(sock->hdr_buf, sizeof(sock->hdr_buf), type, NULL, 0, COAP_METHOD_GET, + nanocoap_sock_next_msg_id(sock)); + assume(hdr_len > 0); + pktpos += hdr_len; pktpos += coap_opt_put_uri_pathquery(pktpos, NULL, path); assert(pktpos < (uint8_t *)sock->hdr_buf + sizeof(sock->hdr_buf)); @@ -522,13 +523,13 @@ static ssize_t _get_observe(coap_observe_client_t *ctx, const char *path, uint8_t *pktpos = buffer; coap_pkt_t pkt = { - .hdr = (void *)pktpos, + .buf = pktpos, }; uint16_t lastonum = 0; - pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, - nanocoap_sock_next_msg_id(&ctx->sock)); + pktpos += coap_build_udp_hdr(buffer, sizeof(buffer), COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, + nanocoap_sock_next_msg_id(&ctx->sock)); pktpos += coap_opt_put_observe(pktpos, lastonum, unregister); lastonum = COAP_OPT_OBSERVE; pktpos += coap_opt_put_uri_pathquery(pktpos, &lastonum, path); @@ -593,7 +594,7 @@ ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code, }; coap_pkt_t pkt = { - .hdr = (void *)pktpos, + .buf = pktpos, .snips = &payload, }; @@ -603,7 +604,9 @@ ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code, }; uint16_t lastonum = 0; - pktpos += coap_build_hdr(pkt.hdr, type, NULL, 0, code, nanocoap_sock_next_msg_id(sock)); + ssize_t hdr_len = coap_build_udp_hdr(sock->hdr_buf, sizeof(sock->hdr_buf), type, NULL, 0, code, nanocoap_sock_next_msg_id(sock)); + assume(hdr_len > 0); + pktpos += hdr_len; pktpos += coap_opt_put_uri_pathquery(pktpos, &lastonum, path); if (response == NULL && type == COAP_TYPE_NON) { @@ -721,11 +724,13 @@ ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path) uint8_t *pktpos = sock->hdr_buf; coap_pkt_t pkt = { - .hdr = (void *)pktpos, + .buf = pktpos, }; - pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_DELETE, - nanocoap_sock_next_msg_id(sock)); + ssize_t hdr_len = coap_build_udp_hdr(sock->hdr_buf, sizeof(sock->hdr_buf), COAP_TYPE_CON, NULL, 0, + COAP_METHOD_DELETE, nanocoap_sock_next_msg_id(sock)); + assume(hdr_len > 0); + pktpos += hdr_len; pktpos += coap_opt_put_uri_pathquery(pktpos, NULL, path); assert(pktpos < (uint8_t *)sock->hdr_buf + sizeof(sock->hdr_buf)); @@ -797,7 +802,7 @@ static int _fetch_block(nanocoap_sock_t *sock, uint8_t *buf, size_t len, _block_ctx_t *ctx) { coap_pkt_t pkt = { - .hdr = (void *)buf, + .buf = buf, }; uint16_t lastonum = 0; @@ -811,13 +816,14 @@ static int _fetch_block(nanocoap_sock_t *sock, uint8_t *buf, size_t len, 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)); + ssize_t hdr_len = coap_build_udp_hdr(pkt.buf, len, COAP_TYPE_CON, token, token_len, + COAP_METHOD_GET, nanocoap_sock_next_msg_id(sock)); + assume(hdr_len > 0); + buf += hdr_len; buf += coap_opt_put_uri_pathquery(buf, &lastonum, path); buf += coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2, (ctx->blknum << 4) | blksize); - (void)len; - assert((uintptr_t)buf - (uintptr_t)pkt.hdr < len); + assume((uintptr_t)buf - (uintptr_t)pkt.buf < len); pkt.payload = buf; pkt.payload_len = 0; @@ -842,15 +848,17 @@ int nanocoap_sock_block_request(coap_block_request_t *req, }; coap_pkt_t pkt = { - .hdr = (void *)req->sock->hdr_buf, + .buf = req->sock->hdr_buf, .snips = &snip, }; - uint8_t *pktpos = (void *)pkt.hdr; + uint8_t *pktpos = pkt.buf; uint16_t lastonum = 0; - pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, req->method, - nanocoap_sock_next_msg_id(req->sock)); + ssize_t hdr_size = coap_build_udp_hdr(req->sock->hdr_buf, sizeof(req->sock->hdr_buf), COAP_TYPE_CON, NULL, 0, req->method, + nanocoap_sock_next_msg_id(req->sock)); + assume(hdr_size > 0); + pktpos += hdr_size; pktpos += coap_opt_put_uri_pathquery(pktpos, &lastonum, req->path); pktpos += coap_opt_put_uint(pktpos, lastonum, COAP_OPT_BLOCK1, (req->blknum << 4) | req->blksize | (more ? 0x8 : 0)); @@ -1174,7 +1182,7 @@ int nanocoap_server(sock_udp_ep_t *local, void *rsp_buf, size_t rsp_buf_len) continue; } coap_pkt_t pkt; - if (coap_parse(&pkt, buf, res) < 0) { + if (coap_parse_udp(&pkt, buf, res) < 0) { DEBUG("nanocoap: error parsing packet\n"); continue; } diff --git a/tests/net/nanocoap_cli/nanocli_client.c b/tests/net/nanocoap_cli/nanocli_client.c index 6887ec02eb..75a25df0d4 100644 --- a/tests/net/nanocoap_cli/nanocli_client.c +++ b/tests/net/nanocoap_cli/nanocli_client.c @@ -94,8 +94,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; @@ -114,14 +113,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); @@ -138,7 +137,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); } diff --git a/tests/net/nanocoap_cli/nanocli_server.c b/tests/net/nanocoap_cli/nanocli_server.c index af063d959c..e13f3e6769 100644 --- a/tests/net/nanocoap_cli/nanocli_server.c +++ b/tests/net/nanocoap_cli/nanocli_server.c @@ -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; } diff --git a/tests/pkg/edhoc_c/initiator.c b/tests/pkg/edhoc_c/initiator.c index f95cf1b3f5..0bcd3a9ae7 100644 --- a/tests/pkg/edhoc_c/initiator.c +++ b/tests/pkg/edhoc_c/initiator.c @@ -17,9 +17,7 @@ #include #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" @@ -30,9 +28,6 @@ #include "tinycrypt/sha256.h" #endif -#define ENABLE_DEBUG 0 -#include "debug.h" - #define COAP_BUF_SIZE (256U) #if IS_ACTIVE(CONFIG_INITIATOR) @@ -111,11 +106,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", '/'); diff --git a/tests/riotboot_flashwrite/coap_handler.c b/tests/riotboot_flashwrite/coap_handler.c index 2e18f24187..d7ea8d01ee 100644 --- a/tests/riotboot_flashwrite/coap_handler.c +++ b/tests/riotboot_flashwrite/coap_handler.c @@ -5,7 +5,6 @@ #include #include -#include #include "net/nanocoap.h" #include "riotboot/flashwrite.h" @@ -69,10 +68,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) { diff --git a/tests/unittests/tests-gcoap/tests-gcoap.c b/tests/unittests/tests-gcoap/tests-gcoap.c index dec724cd32..513de57e4e 100644 --- a/tests/unittests/tests-gcoap/tests-gcoap.c +++ b/tests/unittests/tests-gcoap/tests-gcoap.c @@ -108,7 +108,7 @@ static void test_gcoap__client_get_resp(void) { uint8_t buf[CONFIG_GCOAP_PDU_BUF_SIZE]; coap_pkt_t pdu; - int res; + ssize_t res; size_t hdr_fixed_len = 4; char exp_payload[] = "Oct 22 10:46:48"; size_t exp_tokenlen = 2; @@ -121,9 +121,9 @@ 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(0, res); + TEST_ASSERT_EQUAL_INT(sizeof(pdu_data), res); TEST_ASSERT_EQUAL_INT(COAP_CLASS_SUCCESS, coap_get_code_class(&pdu)); TEST_ASSERT_EQUAL_INT(exp_tokenlen, coap_get_token_len(&pdu)); TEST_ASSERT_EQUAL_INT(hdr_fixed_len + exp_tokenlen, coap_get_total_hdr_len(&pdu)); @@ -152,7 +152,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); @@ -198,9 +198,9 @@ static void test_gcoap__client_get_path_defer(void) len = coap_opt_finish(&pdu, COAP_OPT_FINISH_NONE); TEST_ASSERT_EQUAL_INT(len, - sizeof(coap_hdr_t) + CONFIG_GCOAP_TOKENLEN + ETAG_SLACK + optlen); + 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]); @@ -234,7 +234,7 @@ static void test_gcoap__client_ping(void) * Request from libcoap example for gcoap_cli /cli/stats resource * Include 2-byte token and Uri-Host option. */ -static int _read_cli_stats_req(coap_pkt_t *pdu, uint8_t *buf) +static ssize_t _read_cli_stats_req(coap_pkt_t *pdu, uint8_t *buf) { uint8_t pdu_data[] = { 0x52, 0x01, 0x20, 0xb6, 0x35, 0x61, 0x3d, 0x10, @@ -246,7 +246,7 @@ static int _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. */ @@ -255,9 +255,9 @@ static void test_gcoap__server_get_req(void) uint8_t buf[CONFIG_GCOAP_PDU_BUF_SIZE]; coap_pkt_t pdu; - int res = _read_cli_stats_req(&pdu, &buf[0]); + ssize_t res = _read_cli_stats_req(&pdu, &buf[0]); - TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT(res > 0); TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code_decimal(&pdu)); TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pdu)); TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pdu)); @@ -310,7 +310,7 @@ static void test_gcoap__server_get_resp(void) * Confirmable request from libcoap example for gcoap_cli /cli/stats resource. * Include 2-byte token. */ -static int _read_cli_stats_req_con(coap_pkt_t *pdu, uint8_t *buf) +static ssize_t _read_cli_stats_req_con(coap_pkt_t *pdu, uint8_t *buf) { uint8_t pdu_data[] = { 0x42, 0x01, 0x8e, 0x03, 0x35, 0x61, 0xb3, 0x63, @@ -318,7 +318,7 @@ static int _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. */ @@ -327,9 +327,9 @@ static void test_gcoap__server_con_req(void) uint8_t buf[CONFIG_GCOAP_PDU_BUF_SIZE]; coap_pkt_t pdu; - int res = _read_cli_stats_req_con(&pdu, &buf[0]); + ssize_t res = _read_cli_stats_req_con(&pdu, &buf[0]); - TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT(res > 0); TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code_decimal(&pdu)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_CON, coap_get_type(&pdu)); } diff --git a/tests/unittests/tests-nanocoap/tests-nanocoap.c b/tests/unittests/tests-nanocoap/tests-nanocoap.c index 4b102d8af9..8cb1a0efc1 100644 --- a/tests/unittests/tests-nanocoap/tests-nanocoap.c +++ b/tests/unittests/tests-nanocoap/tests-nanocoap.c @@ -18,7 +18,6 @@ #include "net/nanocoap.h" -#include "unittests-constants.h" #include "tests-nanocoap.h" #define _BUF_SIZE (128U) @@ -35,13 +34,13 @@ static void test_nanocoap__hdr(void) unsigned char path_tmp[64] = {0}; uint8_t *pktpos = &buf[0]; - pktpos += coap_build_hdr((coap_hdr_t *)pktpos, COAP_TYPE_CON, NULL, 0, + pktpos += coap_build_udp_hdr(pktpos, sizeof(buf), COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, msgid); pktpos += coap_opt_put_location_path(pktpos, 0, loc_path); 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)); @@ -69,12 +68,12 @@ static void test_nanocoap__hdr_2(void) uint8_t *pktpos = &buf[0]; uint16_t lastonum = 0; - pktpos += coap_build_hdr((coap_hdr_t *)pktpos, COAP_TYPE_CON, NULL, 0, - COAP_METHOD_GET, msgid); + pktpos += coap_build_udp_hdr(pktpos, sizeof(buf), COAP_TYPE_CON, NULL, 0, + COAP_METHOD_GET, msgid); 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)); @@ -120,8 +119,8 @@ static void test_nanocoap__get_req(void) size_t total_hdr_len = 6; size_t total_opt_len = 5; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); TEST_ASSERT_EQUAL_INT(total_hdr_len, len); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); @@ -158,8 +157,8 @@ static void test_nanocoap__put_req(void) size_t fmt_opt_len = 1; size_t accept_opt_len = 2; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_PUT, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_PUT, msgid); TEST_ASSERT_EQUAL_INT(total_hdr_len, len); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); @@ -196,8 +195,8 @@ static void test_nanocoap__get_multi_path(void) char path[] = "/ab/cde"; size_t uri_opt_len = 7; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -222,8 +221,8 @@ static void test_nanocoap__get_path_trailing_slash(void) char path[] = "/time/"; size_t uri_opt_len = 6; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -247,8 +246,8 @@ static void test_nanocoap__get_root_path(void) uint8_t token[2] = {0xDA, 0xEC}; char path[] = "/"; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -271,8 +270,8 @@ static void test_nanocoap__get_max_path(void) /* includes extra byte for option length > 12 */ size_t uri_opt_len = 64; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -299,8 +298,8 @@ static void test_nanocoap__get_path_too_long(void) /* includes extra byte for option length > 12 */ size_t uri_opt_len = 65; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -327,8 +326,8 @@ static void test_nanocoap__get_query(void) char qs[] = "ab=cde"; size_t query_opt_len = 7; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -372,8 +371,8 @@ static void test_nanocoap__get_multi_query(void) char key[8]; char value[8]; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -441,8 +440,8 @@ static void test_nanocoap__add_uri_query2(void) char qs3[] = "a=do&bcd&bcd"; size_t query3_opt_len = 4; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -499,8 +498,8 @@ static void test_nanocoap__option_add_buffer_max(void) size_t uri_opt_len = 64; /* option hdr 2, option value 62 */ - ssize_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + ssize_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -524,8 +523,8 @@ static void __test_option_remove(uint16_t stride) uint16_t msgid = 0xABCD; uint8_t token[2] = {0xDA, 0xEC}; - ssize_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + ssize_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -685,8 +684,8 @@ static void test_nanocoap__option_remove_no_payload(void) uint16_t msgid = 0xABCD; uint8_t token[2] = {0xDA, 0xEC}; - ssize_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + ssize_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -716,7 +715,7 @@ static void test_nanocoap__option_remove_no_payload(void) * Includes 2-byte token; non-confirmable. * Generated with libcoap. */ -static int _read_riot_value_req(coap_pkt_t *pkt, uint8_t *buf) +static ssize_t _read_riot_value_req(coap_pkt_t *pkt, uint8_t *buf) { uint8_t pkt_data[] = { 0x52, 0x01, 0x9e, 0x6b, 0x35, 0x61, 0xb4, 0x72, @@ -725,7 +724,7 @@ static int _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. */ @@ -735,9 +734,9 @@ static void test_nanocoap__server_get_req(void) coap_pkt_t pkt; char path[] = "/riot/value"; - int res = _read_riot_value_req(&pkt, &buf[0]); + ssize_t res = _read_riot_value_req(&pkt, &buf[0]); - TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT(res > 0); TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code_decimal(&pkt)); TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pkt)); TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pkt)); @@ -756,12 +755,12 @@ static void test_nanocoap__server_reply_simple(void) coap_pkt_t pkt; char *payload = "0"; - int res = _read_riot_value_req(&pkt, &buf[0]); + ssize_t res = _read_riot_value_req(&pkt, &buf[0]); coap_reply_simple(&pkt, COAP_CODE_CONTENT, buf, _BUF_SIZE, COAP_FORMAT_TEXT, (uint8_t *)payload, 1); - TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT(res > 0); TEST_ASSERT_EQUAL_INT(COAP_CODE_CONTENT, coap_get_code_raw(&pkt)); TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pkt)); TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pkt)); @@ -774,7 +773,7 @@ static void test_nanocoap__server_reply_simple(void) * Includes 2-byte token; confirmable. * Generated with libcoap. */ -static int _read_riot_value_req_con(coap_pkt_t *pkt, uint8_t *buf) +static ssize_t _read_riot_value_req_con(coap_pkt_t *pkt, uint8_t *buf) { uint8_t pkt_data[] = { 0x42, 0x01, 0xbe, 0x16, 0x35, 0x61, 0xb4, 0x72, @@ -783,7 +782,7 @@ static int _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. */ @@ -792,9 +791,9 @@ static void test_nanocoap__server_get_req_con(void) uint8_t buf[_BUF_SIZE]; coap_pkt_t pkt; - int res = _read_riot_value_req_con(&pkt, &buf[0]); + ssize_t res = _read_riot_value_req_con(&pkt, &buf[0]); - TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT(res > 0); TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code_decimal(&pkt)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_CON, coap_get_type(&pkt)); } @@ -818,9 +817,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. @@ -846,11 +845,11 @@ static void test_nanocoap__server_option_count_overflow_check(void) memset(&scratch, 0, sizeof(scratch)); - int 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; - volatile uint8_t *pos = scratch.guard_data; + uint8_t *pos = scratch.guard_data; for (size_t i = 0; i < sizeof(scratch.guard_data); i++) { if (*pos) { dirty = 1; @@ -863,7 +862,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) { @@ -888,13 +887,13 @@ static void test_nanocoap__server_option_count_overflow(void) } /* don't read final two bytes, where overflow option will be added later */ - int res = coap_parse(&pkt, buf, sizeof(buf) - 2); - TEST_ASSERT_EQUAL_INT(0, res); + 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); } @@ -909,7 +908,7 @@ static void test_nanocoap__server_option_count_overflow(void) * Uri-Query: lt=60 * Payload: (absent if omit_payload) */ -static int _read_rd_post_req(coap_pkt_t *pkt, bool omit_payload) +static ssize_t _read_rd_post_req(coap_pkt_t *pkt, bool omit_payload) { static uint8_t pkt_data[] = { 0x42, 0x02, 0x20, 0x92, 0xb9, 0x27, 0xbd, 0x04, @@ -924,7 +923,7 @@ static int _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); } /* @@ -933,8 +932,8 @@ static int _read_rd_post_req(coap_pkt_t *pkt, bool omit_payload) static void test_nanocoap__options_iterate(void) { coap_pkt_t pkt; - int res = _read_rd_post_req(&pkt, true); - TEST_ASSERT_EQUAL_INT(0, res); + ssize_t res = _read_rd_post_req(&pkt, true); + TEST_ASSERT(res > 0); /* read all options */ coap_optpos_t opt = {0, 0}; @@ -957,7 +956,7 @@ static void test_nanocoap__options_iterate(void) /* test with no payload to verify end of options handling */ memset(&pkt, 0, sizeof(pkt)); res = _read_rd_post_req(&pkt, false); - TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT(res > 0); for (int i = 0; i < 5; i++) { ssize_t optlen = coap_opt_get_next(&pkt, &opt, &value, !i); @@ -978,15 +977,15 @@ static void test_nanocoap__options_iterate(void) static void test_nanocoap__options_get_opaque(void) { coap_pkt_t pkt; - int res = _read_rd_post_req(&pkt, true); - TEST_ASSERT_EQUAL_INT(0, res); + ssize_t res = _read_rd_post_req(&pkt, true); + TEST_ASSERT(res > 0); /* read Uri-Query options */ uint8_t *value; ssize_t optlen = coap_opt_get_opaque(&pkt, COAP_OPT_URI_QUERY, &value); TEST_ASSERT_EQUAL_INT(24, optlen); - coap_optpos_t opt = {0, value + optlen - (uint8_t *)pkt.hdr}; + coap_optpos_t opt = {0, value + optlen - pkt.buf}; optlen = coap_opt_get_next(&pkt, &opt, &value, false); TEST_ASSERT_EQUAL_INT(0, opt.opt_num); @@ -1009,9 +1008,9 @@ static void test_nanocoap__empty(void) uint16_t msgid = 0xABCD; coap_pkt_t pkt; - int res = coap_parse(&pkt, pkt_data, 4); + ssize_t res = coap_parse_udp(&pkt, pkt_data, 4); - TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT(res > 0); TEST_ASSERT_EQUAL_INT(0, coap_get_code_raw(&pkt)); TEST_ASSERT_EQUAL_INT(msgid, coap_get_id(&pkt)); TEST_ASSERT_EQUAL_INT(0, coap_get_token_len(&pkt)); @@ -1019,12 +1018,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); } @@ -1043,8 +1042,8 @@ static void test_nanocoap__add_path_unterminated_string(void) /* some random non-zero character at the end of /time */ path[path_len] = 'Z'; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -1069,8 +1068,8 @@ static void test_nanocoap__add_get_proxy_uri(void) uint8_t token[2] = {0xDA, 0xEC}; char proxy_uri[60] = "coap://[2001:db8::1]:5683/.well-known/core"; - size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + size_t len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &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)); @@ -1088,7 +1087,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) { @@ -1108,28 +1107,28 @@ static void test_nanocoap__token_length_over_limit(void) coap_pkt_t pkt; /* Valid packet (TKL = 8) */ - int 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(0, res); + TEST_ASSERT_EQUAL_INT(sizeof(buf_valid), res); TEST_ASSERT_EQUAL_INT(1, coap_get_code_raw(&pkt)); TEST_ASSERT_EQUAL_INT(msgid, coap_get_id(&pkt)); TEST_ASSERT_EQUAL_INT(8, coap_get_token_len(&pkt)); 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) { const char *token = "0123456789ABCDEF"; uint8_t buf[32]; - coap_hdr_t *hdr = (void *)buf; + coap_udp_hdr_t *hdr = (void *)buf; /* build a request with an overlong token (that mandates the use of * an 8 bit extended token length field) */ @@ -1139,9 +1138,9 @@ static void test_nanocoap__token_length_ext_16(void) /* parse the packet build, and verify it parses back as expected */ coap_pkt_t pkt; - int res = coap_parse(&pkt, buf, 21); + ssize_t res = coap_parse_udp(&pkt, buf, 21); - TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT_EQUAL_INT(21, res); TEST_ASSERT_EQUAL_INT(21, coap_get_total_hdr_len(&pkt)); TEST_ASSERT_EQUAL_INT(COAP_METHOD_DELETE, coap_get_code_raw(&pkt)); TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt)); @@ -1156,8 +1155,8 @@ 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); - TEST_ASSERT_EQUAL_INT(0, res); + 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)); TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt)); @@ -1168,7 +1167,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) { @@ -1178,7 +1177,7 @@ static void test_nanocoap__token_length_ext_269(void) "et justo duo dolores et ea rebum. Stet clita kasd gubergren," "no sea takimata sanctus est Lore."; uint8_t buf[280]; - coap_hdr_t *hdr = (void *)buf; + coap_udp_hdr_t *hdr = (void *)buf; /* build a request with an overlong token (that mandates the use of * an 16 bit extended token length field) */ @@ -1188,9 +1187,9 @@ static void test_nanocoap__token_length_ext_269(void) /* parse the packet build, and verify it parses back as expected */ coap_pkt_t pkt; - int res = coap_parse(&pkt, buf, 275); + ssize_t res = coap_parse_udp(&pkt, buf, 275); - TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT_EQUAL_INT(275, res); TEST_ASSERT_EQUAL_INT(275, coap_get_total_hdr_len(&pkt)); TEST_ASSERT_EQUAL_INT(COAP_METHOD_DELETE, coap_get_code_raw(&pkt)); TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt)); @@ -1206,8 +1205,8 @@ 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); - TEST_ASSERT_EQUAL_INT(0, res); + 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)); TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt)); @@ -1240,7 +1239,7 @@ static void test_nanocoap___rst_message(void) /* now check that parsing it back works */ coap_pkt_t pkt; - TEST_ASSERT_EQUAL_INT(0, 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)); @@ -1253,7 +1252,7 @@ static void test_nanocoap___rst_message(void) 0xde, 0xed, 0xbe, 0xef, /* Token = 0xdeadbeef */ }; memset(buf, 0x55, sizeof(buf)); - TEST_ASSERT_EQUAL_INT(0, 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)]); diff --git a/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c b/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c index 0d0192b81d..736df735c3 100644 --- a/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c +++ b/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c @@ -8,7 +8,6 @@ * * @file */ -#include #include #include #include @@ -19,7 +18,6 @@ #include "ztimer.h" #include "hashes/sha256.h" -#include "unittests-constants.h" #include "tests-nanocoap_cache.h" #define _BUF_SIZE (128U) @@ -38,15 +36,15 @@ static void test_nanocoap_cache__cachekey(void) size_t len; /* 1. packet */ - len = coap_build_hdr((coap_hdr_t *)&buf1[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + len = coap_build_udp_hdr(buf1, sizeof(buf1), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt1, &buf1[0], sizeof(buf1), len); coap_opt_add_string(&pkt1, COAP_OPT_URI_PATH, &path[0], '/'); coap_opt_finish(&pkt1, COAP_OPT_FINISH_NONE); /* 2. packet */ - len = coap_build_hdr((coap_hdr_t *)&buf2[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + len = coap_build_udp_hdr(buf2, sizeof(buf2), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt2, &buf2[0], sizeof(buf2), len); coap_opt_add_string(&pkt2, COAP_OPT_URI_PATH, &path[0], '/'); coap_opt_finish(&pkt2, COAP_OPT_FINISH_NONE); @@ -58,8 +56,8 @@ static void test_nanocoap_cache__cachekey(void) TEST_ASSERT_EQUAL_INT(0, nanocoap_cache_key_compare(digest1, digest2)); /* 3. packet */ - len = coap_build_hdr((coap_hdr_t *)&buf2[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + len = coap_build_udp_hdr(buf2, sizeof(buf2), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt2, &buf2[0], sizeof(buf2), len); coap_opt_add_string(&pkt2, COAP_OPT_URI_PATH, &path2[0], '/'); coap_opt_finish(&pkt2, COAP_OPT_FINISH_NONE); @@ -92,8 +90,8 @@ static void test_nanocoap_cache__cachekey_blockwise(void) }; /* 1. packet */ - len = coap_build_hdr((coap_hdr_t *)&buf1[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + len = coap_build_udp_hdr(buf1, sizeof(buf1), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt1, &buf1[0], sizeof(buf1), len); coap_opt_add_string(&pkt1, COAP_OPT_URI_PATH, &path[0], '/'); coap_opt_add_block1_control(&pkt1, &blockopt); @@ -103,8 +101,8 @@ static void test_nanocoap_cache__cachekey_blockwise(void) blockopt.blknum = 2; /* 2. packet */ - len = coap_build_hdr((coap_hdr_t *)&buf2[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + len = coap_build_udp_hdr(buf2, sizeof(buf2), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt2, &buf2[0], sizeof(buf2), len); coap_opt_add_string(&pkt2, COAP_OPT_URI_PATH, &path[0], '/'); coap_opt_add_block1_control(&pkt1, &blockopt); @@ -156,15 +154,15 @@ static void test_nanocoap_cache__add(void) snprintf(path, sizeof(path), "/path_%u", i); /* request */ - len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&req, &buf[0], sizeof(buf), len); coap_opt_add_string(&req, COAP_OPT_URI_PATH, &path[0], '/'); coap_opt_finish(&req, COAP_OPT_FINISH_NONE); /* response */ - len = coap_build_hdr((coap_hdr_t *)&rbuf[0], COAP_TYPE_NON, - &token[0], 2, COAP_CODE_205, msgid); + len = coap_build_udp_hdr(rbuf, sizeof(rbuf), COAP_TYPE_NON, + &token[0], 2, COAP_CODE_205, msgid); coap_pkt_init(&resp, &rbuf[0], sizeof(rbuf), len); coap_opt_finish(&resp, COAP_OPT_FINISH_NONE); @@ -215,15 +213,15 @@ static void test_nanocoap_cache__del(void) TEST_ASSERT_EQUAL_INT(0, nanocoap_cache_used_count()); /* request */ - len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&req, &buf[0], sizeof(buf), len); coap_opt_add_string(&req, COAP_OPT_URI_PATH, &path[0], '/'); coap_opt_finish(&req, COAP_OPT_FINISH_NONE); /* response */ - len = coap_build_hdr((coap_hdr_t *)&rbuf[0], COAP_TYPE_NON, - &token[0], 2, COAP_CODE_205, msgid); + len = coap_build_udp_hdr(rbuf, sizeof(rbuf), COAP_TYPE_NON, + &token[0], 2, COAP_CODE_205, msgid); coap_pkt_init(&resp, &rbuf[0], sizeof(rbuf), len); coap_opt_finish(&resp, COAP_OPT_FINISH_NONE); @@ -261,15 +259,15 @@ static void test_nanocoap_cache__max_age(void) nanocoap_cache_init(); /* request */ - len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, - &token[0], 2, COAP_METHOD_GET, msgid); + len = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&req, &buf[0], sizeof(buf), len); coap_opt_add_string(&req, COAP_OPT_URI_PATH, &path[0], '/'); coap_opt_finish(&req, COAP_OPT_FINISH_NONE); /* response with max-age 30 sec */ - len = coap_build_hdr((coap_hdr_t *)&rbuf[0], COAP_TYPE_NON, - &token[0], 2, COAP_CODE_205, msgid); + len = coap_build_udp_hdr(rbuf, sizeof(rbuf), COAP_TYPE_NON, + &token[0], 2, COAP_CODE_205, msgid); coap_pkt_init(&resp, &rbuf[0], sizeof(rbuf), len); coap_opt_add_uint(&resp, COAP_OPT_MAX_AGE, 30); coap_opt_finish(&resp, COAP_OPT_FINISH_NONE); @@ -287,8 +285,8 @@ static void test_nanocoap_cache__max_age(void) nanocoap_cache_del(c); /* response with max-age 60 sec (default, if option is missing) */ - len = coap_build_hdr((coap_hdr_t *)&rbuf[0], COAP_TYPE_NON, - &token[0], 2, COAP_CODE_205, msgid); + len = coap_build_udp_hdr(rbuf, sizeof(rbuf), COAP_TYPE_NON, + &token[0], 2, COAP_CODE_205, msgid); coap_pkt_init(&resp, &rbuf[0], sizeof(rbuf), len); coap_opt_finish(&resp, COAP_OPT_FINISH_NONE);