diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index f8fdd7a5e353..6bf964318fb4 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -395,6 +395,18 @@ static inline unsigned coap_get_token_len(const coap_pkt_t *pkt) return (pkt->hdr->ver_t_tkl & 0xf); } +/** + * @brief Get pointer to a message's token + * + * @param[in] pkt CoAP packet + * + * @returns pointer to the token position + */ +static inline void *coap_get_token(const coap_pkt_t *pkt) +{ + return (uint8_t*)pkt->hdr + sizeof(coap_hdr_t); +} + /** * @brief Get the total header length (4-byte header + token length) * diff --git a/sys/net/application_layer/gcoap/forward_proxy.c b/sys/net/application_layer/gcoap/forward_proxy.c index 662513c67de2..a45cb217dfee 100644 --- a/sys/net/application_layer/gcoap/forward_proxy.c +++ b/sys/net/application_layer/gcoap/forward_proxy.c @@ -292,7 +292,7 @@ static int _gcoap_forward_proxy_via_coap(coap_pkt_t *client_pkt, pkt.hdr->id = client_pkt->hdr->id; if (token_len) { - memcpy(pkt.token, client_pkt->token, token_len); + memcpy(coap_get_token(&pkt), coap_get_token(client_pkt), token_len); } /* copy all options from client_pkt to pkt */ diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index 23dbcba15650..238e71be2ed3 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -608,7 +608,7 @@ static size_t _handle_req(gcoap_socket_t *sock, coap_pkt_t *pdu, uint8_t *buf, memo->token_len = coap_get_token_len(pdu); memo->socket = *sock; if (memo->token_len) { - memcpy(&memo->token[0], pdu->token, memo->token_len); + memcpy(&memo->token[0], coap_get_token(pdu), memo->token_len); } DEBUG("gcoap: Registered observer for: %s\n", memo->resource->path); } @@ -803,7 +803,7 @@ static void _find_req_memo(gcoap_request_memo_t **memo_ptr, coap_pkt_t *src_pdu, } } else if (coap_get_token_len(memo_pdu) == cmplen) { memo_pdu->token = coap_hdr_data_ptr(memo_pdu->hdr); - if ((memcmp(src_pdu->token, memo_pdu->token, cmplen) == 0) + if ((memcmp(coap_get_token(src_pdu), memo_pdu->token, cmplen) == 0) && sock_udp_ep_equal(&memo->remote_ep, remote)) { *memo_ptr = memo; break; diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index bc04d68bb94f..86df6af9374a 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -504,7 +504,7 @@ ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, } } - coap_build_hdr((coap_hdr_t *)rbuf, type, pkt->token, tkl, code, + coap_build_hdr((coap_hdr_t *)rbuf, type, coap_get_token(pkt), tkl, code, ntohs(pkt->hdr->id)); coap_hdr_set_type((coap_hdr_t *)rbuf, type); coap_hdr_set_code((coap_hdr_t *)rbuf, code);