Skip to content

Commit

Permalink
Merge pull request #21076 from maribu/sys/net/nanocoap/fix-invalid-co…
Browse files Browse the repository at this point in the history
…nf-separate-response

sys/net/nanocoap: Fix sending bogus separate responses
  • Loading branch information
maribu authored Dec 13, 2024
2 parents 28753e3 + ab116ec commit d394f62
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
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
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 @@ -1075,6 +1075,7 @@ void auto_init_nanocoap_server(void)
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 @@ int nanocoap_server_prepare_separate(nanocoap_server_response_ctx_t *ctx,
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 @@ int nanocoap_server_send_separate(const nanocoap_server_response_ctx_t *ctx,
}

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 @@ int nanocoap_server_send_separate(const nanocoap_server_response_ctx_t *ctx,
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

0 comments on commit d394f62

Please sign in to comment.