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/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/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/unittests/tests-gcoap/tests-gcoap.c b/tests/unittests/tests-gcoap/tests-gcoap.c index a043847ae6..8dcc757727 100644 --- a/tests/unittests/tests-gcoap/tests-gcoap.c +++ b/tests/unittests/tests-gcoap/tests-gcoap.c @@ -111,7 +111,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; @@ -126,7 +126,7 @@ static void test_gcoap__client_get_resp(void) res = coap_parse(&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)); @@ -201,7 +201,7 @@ 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); @@ -237,7 +237,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, @@ -258,9 +258,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)); @@ -313,7 +313,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, @@ -330,9 +330,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 d183a9738c..4c07f64ffd 100644 --- a/tests/unittests/tests-nanocoap/tests-nanocoap.c +++ b/tests/unittests/tests-nanocoap/tests-nanocoap.c @@ -21,7 +21,6 @@ #include "net/nanocoap.h" -#include "unittests-constants.h" #include "tests-nanocoap.h" #define _BUF_SIZE (128U) @@ -38,7 +37,7 @@ 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); @@ -72,8 +71,8 @@ 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; @@ -123,8 +122,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); @@ -161,8 +160,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); @@ -199,8 +198,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)); @@ -225,8 +224,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)); @@ -250,8 +249,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)); @@ -274,8 +273,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)); @@ -302,8 +301,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)); @@ -330,8 +329,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)); @@ -375,8 +374,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)); @@ -444,8 +443,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)); @@ -502,8 +501,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)); @@ -527,8 +526,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)); @@ -688,8 +687,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)); @@ -719,7 +718,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, @@ -738,9 +737,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)); @@ -759,12 +758,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)); @@ -777,7 +776,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, @@ -795,9 +794,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)); } @@ -849,11 +848,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(&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; @@ -891,8 +890,8 @@ 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(&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); @@ -912,7 +911,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, @@ -936,8 +935,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}; @@ -960,7 +959,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); @@ -981,15 +980,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); @@ -1012,9 +1011,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(&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)); @@ -1046,8 +1045,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)); @@ -1072,8 +1071,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)); @@ -1111,9 +1110,9 @@ 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(&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)); @@ -1132,7 +1131,7 @@ 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) */ @@ -1142,9 +1141,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(&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)); @@ -1160,7 +1159,7 @@ static void test_nanocoap__token_length_ext_16(void) sizeof(rbuf), 0, NULL, NULL); TEST_ASSERT_EQUAL_INT(21, len); res = coap_parse(&pkt, rbuf, 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_CODE_DELETED, coap_get_code_raw(&pkt)); TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt)); @@ -1181,7 +1180,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) */ @@ -1191,9 +1190,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(&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)); @@ -1210,7 +1209,7 @@ static void test_nanocoap__token_length_ext_269(void) TEST_ASSERT_EQUAL_INT(275, len); res = coap_parse(&pkt, rbuf, 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_CODE_DELETED, coap_get_code_raw(&pkt)); TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt)); @@ -1243,7 +1242,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(&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)); @@ -1256,7 +1255,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(&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 a108c00ab9..4071d79372 100644 --- a/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c +++ b/tests/unittests/tests-nanocoap_cache/tests-nanocoap_cache.c @@ -11,7 +11,6 @@ * * @file */ -#include #include #include #include @@ -22,7 +21,6 @@ #include "ztimer.h" #include "hashes/sha256.h" -#include "unittests-constants.h" #include "tests-nanocoap_cache.h" #define _BUF_SIZE (128U) @@ -41,15 +39,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); @@ -61,8 +59,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); @@ -95,8 +93,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); @@ -106,8 +104,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); @@ -159,15 +157,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); @@ -218,15 +216,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); @@ -264,15 +262,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); @@ -290,8 +288,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);