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: reduce insanity of hack #20945

Merged
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
84 changes: 64 additions & 20 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@
#endif
};

/* 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);

/**
* @brief Get resource path associated with a CoAP request
*
Expand Down Expand Up @@ -548,28 +552,9 @@
*/
static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
{
uint8_t tkl = pkt->hdr->ver_t_tkl & 0xf;

if (!IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
return tkl;
}

void *ext = pkt->hdr + 1;
switch (tkl) {
case 13:
return tkl + *(uint8_t *)ext;
case 14:
return tkl + 255 + byteorder_bebuftohs(ext);
case 15:
assert(0);
/* fall-through */
default:
return tkl;
}
return coap_hdr_get_token_len(pkt->hdr);
}

static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr);

/**
* @brief Get pointer to a message's token
*
Expand Down Expand Up @@ -714,6 +699,65 @@
hdr->ver_t_tkl &= ~0x30;
hdr->ver_t_tkl |= type << 4;
}

/**
* @brief Get the token length of a CoAP over UDP (DTLS) packet
* @param[in] hdr CoAP over UDP header
* @return The size of the token in bytes
*
* @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 size_t coap_hdr_get_token_len(const coap_hdr_t *hdr)
{
const uint8_t *buf = (const void *)hdr;
/* Regarding use unnamed magic numbers 13 and 269:
* - If token length is < 13 it fits into TKL field (4 bit)
* - If token length is < 269 it fits into 8-bit extended TKL field
* - Otherwise token length goes into 16-bit extended TKL field.
*
* (Not using named constants here, as RFC 8974 also has no names for those
* magic numbers.)
*
* See: https://www.rfc-editor.org/rfc/rfc8974#name-extended-token-length-tkl-f
*/
switch (coap_hdr_tkl_ext_len(hdr)) {
case 0:
return hdr->ver_t_tkl & 0xf;
case 1:
return buf[sizeof(coap_hdr_t)] + 13;
case 2:
return byteorder_bebuftohs(buf + sizeof(coap_hdr_t)) + 269;
}

return 0;
}

/**
* @brief Get the header length of a CoAP packet.
*
* @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_total_hdr_len instead. In the TX path the header
* 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)
{
return sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr) + coap_hdr_get_token_len(hdr);
}
/**@}*/

/**
Expand Down Expand Up @@ -1880,7 +1924,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 1927 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 @@ -2319,4 +2363,4 @@
}
#endif
#endif /* NET_NANOCOAP_H */
/** @} */

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

View workflow job for this annotation

GitHub Actions / static-tests

source file is too long
7 changes: 3 additions & 4 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 @@ -959,13 +959,13 @@

/* 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 962 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 968 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],
Expand Down Expand Up @@ -1454,10 +1454,9 @@

static void _copy_hdr_from_req_memo(coap_pkt_t *pdu, gcoap_request_memo_t *memo)
{
coap_pkt_t req_pdu;

req_pdu.hdr = gcoap_request_memo_get_hdr(memo);
memcpy(pdu->hdr, req_pdu.hdr, coap_get_total_hdr_len(&req_pdu));
const coap_hdr_t *hdr = gcoap_request_memo_get_hdr(memo);
size_t hdr_len = coap_hdr_len(hdr);
memcpy(pdu->hdr, hdr, hdr_len);
}

static void _receive_from_cache_cb(void *ctx)
Expand Down Expand Up @@ -1680,7 +1679,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 1682 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 @@ -2107,4 +2106,4 @@
event_post(&_queue, arg);
}

/** @} */

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

View workflow job for this annotation

GitHub Actions / static-tests

source file is too long
Loading