Skip to content

Commit a7f24ee

Browse files
committed
net/nanocoap: Send separate response from server socket
This makes sure the separate response is send using the network interface the request was received on.
1 parent facf45e commit a7f24ee

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

sys/Makefile.dep

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,6 @@ endif
539539

540540
ifneq (,$(filter nanocoap_server_separate,$(USEMODULE)))
541541
USEMODULE += nanocoap_server
542-
USEMODULE += sock_aux_local
543542
endif
544543

545544
ifneq (,$(filter nanocoap_server_tcp,$(USEMODULE)))

sys/include/net/nanocoap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ struct _coap_request_ctx {
560560
sock_udp_ep_t *local; /**< deprecated alias for local_udp */
561561
};
562562
# endif
563+
sock_udp_t *sock_udp; /** UDP socket the request was received on */
563564
};
564565
#endif
565566
#if MODULE_NANOCOAP_TCP

sys/include/net/nanocoap_sock.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,10 @@ typedef struct {
299299
#if MODULE_NANOCOAP_UDP || MODULE_NANOCOAP_DTLS || DOXYGEN
300300
struct {
301301
sock_udp_ep_t remote_udp; /**< UDP endpoint to send response to */
302-
sock_udp_ep_t local_udp; /**< UDP endpoint from which to send response */
302+
# if MODULE_SOCK_AUX_LOCAL
303+
sock_udp_ep_t local_udp; /**< UDP endpoint to send response from */
304+
# endif
305+
sock_udp_t *sock_udp; /**< UDP socket to send from */
303306
};
304307
#endif
305308
#if MODULE_NANOCOAP_SERVER_TCP || DOXYGEN

sys/net/application_layer/nanocoap/sock.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,7 +2096,8 @@ int nanocoap_server_udp(sock_udp_ep_t *local, void *rsp_buf, size_t rsp_buf_len)
20962096
sock_udp_t sock;
20972097
sock_udp_ep_t remote;
20982098
coap_request_ctx_t ctx = {
2099-
.remote = &remote,
2099+
.remote_udp = &remote,
2100+
.sock_udp = &sock,
21002101
};
21012102

21022103
if (!local->port) {
@@ -2189,8 +2190,10 @@ int nanocoap_server_prepare_separate(nanocoap_server_response_ctx_t *ctx,
21892190
#if MODULE_NANOCOAP_UDP
21902191
case COAP_TRANSPORT_UDP:
21912192
memcpy(&ctx->remote_udp, req->remote_udp, sizeof(ctx->remote_udp));
2192-
assert(req->local);
2193+
# if MODULE_SOCK_AUX_LOCAL
21932194
memcpy(&ctx->local_udp, req->local_udp, sizeof(ctx->local_udp));
2195+
# endif
2196+
ctx->sock_udp = req->sock_udp;
21942197
break;
21952198
#endif
21962199
#if MODULE_NANOCOAP_SERVER_TCP
@@ -2268,6 +2271,7 @@ static int _nanocoap_server_sendv_separate_udp(const nanocoap_server_response_ct
22682271
const iolist_t *reply)
22692272
{
22702273
sock_udp_aux_tx_t *aux_out_ptr = NULL;
2274+
# if MODULE_SOCK_AUX_LOCAL
22712275
/* make sure we reply with the same address that the request was
22722276
* destined for -- except in the multicast case */
22732277
sock_udp_aux_tx_t aux_out = {
@@ -2277,12 +2281,15 @@ static int _nanocoap_server_sendv_separate_udp(const nanocoap_server_response_ct
22772281
if (!sock_udp_ep_is_multicast(&ctx->local_udp)) {
22782282
aux_out_ptr = &aux_out;
22792283
}
2280-
ssize_t retval = sock_udp_sendv_aux(NULL, reply, &ctx->remote_udp, aux_out_ptr);
2284+
# endif
2285+
ssize_t retval = sock_udp_sendv_aux(ctx->sock_udp, reply, &ctx->remote_udp, aux_out_ptr);
22812286

22822287
if (retval < 0) {
22832288
return retval;
22842289
}
22852290

2291+
DEBUG("nanocoap: Sent %u B of separate response\n", (unsigned)retval);
2292+
22862293
return 0;
22872294
}
22882295
# endif

0 commit comments

Comments
 (0)