Skip to content

Commit 590ca5d

Browse files
maribumguetschowbenpicco
committed
sys/net/gcoap: get rid of API abuse
Calling `coap_get_token()` and `coap_get_token_length()` on an (mostly) uninitialized `coap_pkt_t` did work so far due to implementation details matching the expectations, but this is not backed up by any API contract. This fixes the API abuse by introducing and using a new API that does read a token and token length from a CoAP over UDP packet out of a buffer. This now provides the behavior expected by the caller and commits to it via API contract. Co-authored-by: mguetschow <mikolai.guetschow@tu-dresden.de> Co-authored-by: benpicco <benpicco@googlemail.com>
1 parent e62e388 commit 590ca5d

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

sys/include/net/nanocoap.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ struct _coap_request_ctx {
355355
/* forward declarations */
356356
static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr);
357357
static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr);
358+
static inline const void * coap_hdr_get_token(const coap_hdr_t *hdr);
358359

359360
/**
360361
* @brief Get resource path associated with a CoAP request
@@ -757,6 +758,28 @@ static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr)
757758
return 0;
758759
}
759760

761+
/**
762+
* @brief Get the Token of a CoAP over UDP (DTLS) packet
763+
* @param[in] hdr CoAP over UDP header
764+
* @return The CoAP Token inside the packet that @p hdr belongs to
765+
*
766+
* @warning This API is super goofy. It assumes that the packet is valid
767+
* and will read more than `sizeof(*hdr)` into the data `hdr`
768+
* points to while crossing fingers hard.
769+
*
770+
* @deprecated This function was introduced to keep legacy code alive.
771+
* Introducing new callers should be avoided. In the RX path an
772+
* @ref coap_pkt_t will be available, so that you can call
773+
* @ref coap_get_token instead. In the TX path the token was
774+
* added by us, so we really should know.
775+
*/
776+
static inline const void * coap_hdr_get_token(const coap_hdr_t *hdr)
777+
{
778+
uint8_t *token = (void *)hdr;
779+
token += sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr);
780+
return token;
781+
}
782+
760783
/**
761784
* @brief Get the header length of a CoAP packet.
762785
*

sys/net/application_layer/gcoap/gcoap.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -945,17 +945,13 @@ static int _find_resource(gcoap_socket_type_t tl_type,
945945
static gcoap_request_memo_t* _find_req_memo_by_token(const sock_udp_ep_t *remote,
946946
const uint8_t *token, size_t tkl)
947947
{
948-
/* no need to initialize struct; we only care about buffer contents below */
949-
coap_pkt_t memo_pdu_data;
950-
coap_pkt_t *memo_pdu = &memo_pdu_data;
951-
952948
for (int i = 0; i < CONFIG_GCOAP_REQ_WAITING_MAX; i++) {
953949
if (_coap_state.open_reqs[i].state == GCOAP_MEMO_UNUSED) {
954950
continue;
955951
}
956952

957953
gcoap_request_memo_t *memo = &_coap_state.open_reqs[i];
958-
memo_pdu->hdr = gcoap_request_memo_get_hdr(memo);
954+
coap_hdr_t *hdr = gcoap_request_memo_get_hdr(memo);
959955

960956
/* verbose debug to catch bugs with request/response matching */
961957
#if SOCK_HAS_IPV4
@@ -972,11 +968,12 @@ static gcoap_request_memo_t* _find_req_memo_by_token(const sock_udp_ep_t *remote
972968
tkl);
973969
#endif
974970

975-
if (coap_get_token_len(memo_pdu) != tkl) {
976-
DEBUG("Token length mismatch %u\n", coap_get_token_len(memo_pdu));
971+
size_t memo_tkl = coap_hdr_get_token_len(hdr);
972+
if (memo_tkl != tkl) {
973+
DEBUG("Token length mismatch %" PRIuSIZE "\n", memo_tkl);
977974
continue;
978975
}
979-
const uint8_t *memo_token = coap_get_token(memo_pdu);
976+
const uint8_t *memo_token = coap_hdr_get_token(hdr);
980977
if (memcmp(token, memo_token, tkl)) {
981978
DEBUG("Token mismatch 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
982979
memo_token[0], memo_token[1], memo_token[2], memo_token[3],

0 commit comments

Comments
 (0)