Skip to content

Commit

Permalink
net/nanocoap: Send separate response from server socket
Browse files Browse the repository at this point in the history
This makes sure the separate response is send using the network
interface the request was received on.
  • Loading branch information
maribu committed Mar 3, 2025
1 parent f6c1d4f commit f7518ef
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 0 additions & 1 deletion sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ endif

ifneq (,$(filter nanocoap_server_separate,$(USEMODULE)))
USEMODULE += nanocoap_server
USEMODULE += sock_aux_local
endif

ifneq (,$(filter nanocoap_server_tcp,$(USEMODULE)))
Expand Down
1 change: 1 addition & 0 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ struct _coap_request_ctx {
sock_udp_ep_t *local; /**< deprecated alias for local_udp */
};
# endif
sock_udp_t *sock_udp; /** UDP socket the request was received on */
};
#endif
#if MODULE_NANOCOAP_TCP
Expand Down
5 changes: 4 additions & 1 deletion sys/include/net/nanocoap_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ typedef struct {
#if MODULE_NANOCOAP_UDP || MODULE_NANOCOAP_DTLS || DOXYGEN
struct {
sock_udp_ep_t remote_udp; /**< UDP endpoint to send response to */
sock_udp_ep_t local_udp; /**< UDP endpoint from which to send response */
# if MODULE_SOCK_AUX_LOCAL
sock_udp_ep_t local_udp; /**< UDP endpoint to send response from */
# endif
sock_udp_t *sock_udp; /**< UDP socket to send from */
};
#endif
#if MODULE_NANOCOAP_SERVER_TCP || DOXYGEN
Expand Down
11 changes: 9 additions & 2 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,7 @@ int nanocoap_server_udp(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
sock_udp_ep_t remote;
coap_request_ctx_t ctx = {
.remote_udp = &remote,
.sock_udp = &sock,
};

if (!local->port) {
Expand Down Expand Up @@ -2085,8 +2086,10 @@ int nanocoap_server_prepare_separate(nanocoap_server_response_ctx_t *ctx,
#if MODULE_NANOCOAP_UDP
case COAP_TRANSPORT_UDP:
memcpy(&ctx->remote_udp, req->remote_udp, sizeof(ctx->remote_udp));
assert(req->local);
# if MODULE_SOCK_AUX_LOCAL
memcpy(&ctx->local_udp, req->local_udp, sizeof(ctx->local_udp));
# endif
ctx->sock_udp = req->sock_udp;
break;
#endif
#if MODULE_NANOCOAP_SERVER_TCP
Expand Down Expand Up @@ -2164,6 +2167,7 @@ static int _nanocoap_server_sendv_separate_udp(const nanocoap_server_response_ct
const iolist_t *reply)
{
sock_udp_aux_tx_t *aux_out_ptr = NULL;
# if 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 @@ -2173,12 +2177,15 @@ static int _nanocoap_server_sendv_separate_udp(const nanocoap_server_response_ct
if (!sock_udp_ep_is_multicast(&ctx->local_udp)) {
aux_out_ptr = &aux_out;
}
ssize_t retval = sock_udp_sendv_aux(NULL, reply, &ctx->remote_udp, aux_out_ptr);
# endif
ssize_t retval = sock_udp_sendv_aux(ctx->sock_udp, reply, &ctx->remote_udp, aux_out_ptr);

if (retval < 0) {
return retval;
}

DEBUG("nanocoap: Sent %u B of separate response\n", (unsigned)retval);

return 0;
}
# endif
Expand Down

0 comments on commit f7518ef

Please sign in to comment.