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/nanocoap: Fix sending bogus separate responses #21076

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
10 changes: 8 additions & 2 deletions sys/include/net/nanocoap_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ typedef struct {
*/
typedef struct {
sock_udp_ep_t remote; /**< remote to send response to */
#if defined(MODULE_SOCK_AUX_LOCAL) || DOXYGEN
sock_udp_ep_t local; /**< local from which to send response */
#endif
benpicco marked this conversation as resolved.
Show resolved Hide resolved
uint8_t token[COAP_TOKEN_LENGTH_MAX]; /**< request token */
uint8_t tkl; /**< request token length */
uint8_t no_response; /**< no-response bitmap */
Expand All @@ -246,6 +244,9 @@ typedef struct {
* The CoAP handler should then respond with an empty ACK by calling
* @ref coap_build_empty_ack
*
* @warning This function is only available when using the module
* `nanocoap_server_separate`
*
* @param[out] ctx Context information for separate response
* @param[in] pkt CoAP packet to which the response will be generated
* @param[in] req Context of the CoAP request
Expand All @@ -266,6 +267,11 @@ int nanocoap_server_prepare_separate(nanocoap_server_response_ctx_t *ctx,
*
* @pre @ref nanocoap_server_prepare_separate has been called on @p ctx
* inside the CoAP handler
* @pre Synchronization between calls of this function and calls of
* @ref nanocoap_server_prepare_separate is ensured
*
* @warning This function is only available when using the module
* `nanocoap_server_separate`
*
* @param[in] ctx Context information for the CoAP response
* @param[in] code CoAP response code
Expand Down
6 changes: 2 additions & 4 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@

/* random timeout, deadline for receive retries */
uint32_t timeout = random_uint32_range((uint32_t)CONFIG_COAP_ACK_TIMEOUT_MS * US_PER_MS,
(uint32_t)CONFIG_COAP_ACK_TIMEOUT_MS * CONFIG_COAP_RANDOM_FACTOR_1000);

Check warning on line 210 in sys/net/application_layer/nanocoap/sock.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
uint32_t deadline = _deadline_from_interval(timeout);

/* check if we expect a reply */
Expand Down Expand Up @@ -1075,6 +1075,7 @@
nanocoap_server_start(&local);
}

#if MODULE_NANOCOAP_SERVER_SEPARATE
int nanocoap_server_prepare_separate(nanocoap_server_response_ctx_t *ctx,
coap_pkt_t *pkt, const coap_request_ctx_t *req)
{
Expand All @@ -1092,10 +1093,8 @@
ctx->tkl = tkl;
memcpy(ctx->token, coap_get_token(pkt), tkl);
memcpy(&ctx->remote, req->remote, sizeof(ctx->remote));
#ifdef MODULE_SOCK_AUX_LOCAL
assert(req->local);
memcpy(&ctx->local, req->local, sizeof(ctx->local));
#endif
uint32_t no_response = 0;
coap_opt_get_uint(pkt, COAP_OPT_NO_RESPONSE, &no_response);
ctx->no_response = no_response;
Expand Down Expand Up @@ -1137,7 +1136,6 @@
}

sock_udp_aux_tx_t *aux_out_ptr = NULL;
#ifdef MODULE_SOCK_AUX_LOCAL
/* make sure we reply with the same address that the request was
* destined for -- except in the multicast case */
sock_udp_aux_tx_t aux_out = {
Expand All @@ -1147,6 +1145,6 @@
if (!sock_udp_ep_is_multicast(&ctx->local)) {
aux_out_ptr = &aux_out;
}
#endif
return sock_udp_sendv_aux(NULL, &head, &ctx->remote, aux_out_ptr);
}
#endif
Loading