Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/net/gcoap: get rid of API abuse #21125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@
/* 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);

/**
* @brief Get resource path associated with a CoAP request
Expand Down Expand Up @@ -757,6 +758,28 @@
return 0;
}

/**
* @brief Get the Token of a CoAP over UDP (DTLS) packet
* @param[in] hdr CoAP over UDP header
* @return The CoAP Token inside the packet that @p hdr belongs to
*
* @warning This API is super goofy. It assumes that the packet is valid
* and will read more than `sizeof(*hdr)` into the data `hdr`
* points to while crossing fingers hard.
*
* @deprecated This function was introduced to keep legacy code alive.
* Introducing new callers should be avoided. In the RX path an
* @ref coap_pkt_t will be available, so that you can call
* @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)
{
uint8_t *token = (void *)hdr;
token += sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr);
return token;
}

/**
* @brief Get the header length of a CoAP packet.
*
Expand Down Expand Up @@ -1941,7 +1964,7 @@
*
* @returns amount of bytes written to @p buf
*/
size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const void *odata, size_t olen);

Check warning on line 1967 in sys/include/net/nanocoap.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters

/**
* @brief Insert block1 option into buffer
Expand Down Expand Up @@ -2383,4 +2406,4 @@
}
#endif
#endif /* NET_NANOCOAP_H */
/** @} */

Check warning on line 2409 in sys/include/net/nanocoap.h

View workflow job for this annotation

GitHub Actions / static-tests

source file is too long
13 changes: 5 additions & 8 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#define NO_IMMEDIATE_REPLY (-1)

/* End of the range to pick a random timeout */
#define TIMEOUT_RANGE_END ((uint32_t)CONFIG_COAP_ACK_TIMEOUT_MS * CONFIG_COAP_RANDOM_FACTOR_1000 / 1000)

Check warning on line 56 in sys/net/application_layer/gcoap/gcoap.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters

/* Internal functions */
static void *_event_loop(void *arg);
Expand Down Expand Up @@ -399,7 +399,7 @@
/* but store the header to keep the token for Observe notifications */
memcpy(memo->msg.hdr_buf, hdr, sizeof(hdr));
/* no further retransmissions should be made and
gcoap_request_memo_get_hdr() has to know how to get the header for Observe notifications */

Check warning on line 402 in sys/net/application_layer/gcoap/gcoap.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
memo->send_limit = GCOAP_SEND_LIMIT_NON;
}
}
Expand Down Expand Up @@ -945,38 +945,35 @@
static gcoap_request_memo_t* _find_req_memo_by_token(const sock_udp_ep_t *remote,
const uint8_t *token, size_t tkl)
{
/* no need to initialize struct; we only care about buffer contents below */
coap_pkt_t memo_pdu_data;
coap_pkt_t *memo_pdu = &memo_pdu_data;

for (int i = 0; i < CONFIG_GCOAP_REQ_WAITING_MAX; i++) {
if (_coap_state.open_reqs[i].state == GCOAP_MEMO_UNUSED) {
continue;
}

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

/* verbose debug to catch bugs with request/response matching */
#if SOCK_HAS_IPV4
DEBUG("Seeking memo for remote=%s, tkn=0x%02x%02x%02x%02x%02x%02x%02x%02x, tkl=%"PRIuSIZE"\n",

Check warning on line 958 in sys/net/application_layer/gcoap/gcoap.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
ipv4_addr_to_str(_ipv6_addr_str, (ipv4_addr_t *)&remote->addr.ipv4,
IPV6_ADDR_MAX_STR_LEN),
token[0], token[1], token[2], token[3], token[4], token[5], token[6], token[7],
tkl);
#else
DEBUG("Seeking memo for remote=%s, tkn=0x%02x%02x%02x%02x%02x%02x%02x%02x, tkl=%"PRIuSIZE"\n",

Check warning on line 964 in sys/net/application_layer/gcoap/gcoap.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
ipv6_addr_to_str(_ipv6_addr_str, (ipv6_addr_t *)&remote->addr.ipv6,
IPV6_ADDR_MAX_STR_LEN),
token[0], token[1], token[2], token[3], token[4], token[5], token[6], token[7],
tkl);
#endif

if (coap_get_token_len(memo_pdu) != tkl) {
DEBUG("Token length mismatch %u\n", coap_get_token_len(memo_pdu));
size_t memo_tkl = coap_hdr_get_token_len(hdr);
if (memo_tkl != tkl) {
DEBUG("Token length mismatch %" PRIuSIZE "\n", memo_tkl);
continue;
}
const uint8_t *memo_token = coap_get_token(memo_pdu);
const uint8_t *memo_token = coap_hdr_get_token(hdr);
if (memcmp(token, memo_token, tkl)) {
DEBUG("Token mismatch 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
memo_token[0], memo_token[1], memo_token[2], memo_token[3],
Expand Down Expand Up @@ -1679,7 +1676,7 @@
const coap_resource_t *resource = last_resource;

while (listener) {
if ((resource = _match_resource_path_iterator(listener, resource, (const uint8_t *)uri_path))) {

Check warning on line 1679 in sys/net/application_layer/gcoap/gcoap.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
*last_listener = listener;
return resource;
}
Expand Down Expand Up @@ -2106,4 +2103,4 @@
event_post(&_queue, arg);
}

/** @} */

Check warning on line 2106 in sys/net/application_layer/gcoap/gcoap.c

View workflow job for this annotation

GitHub Actions / static-tests

source file is too long
Loading