Skip to content

Commit 1b81973

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 375282d commit 1b81973

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

sys/Makefile.dep

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

544544
ifneq (,$(filter nanocoap_server_separate,$(USEMODULE)))
545545
USEMODULE += nanocoap_server
546-
USEMODULE += sock_aux_local
547546
endif
548547

549548
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
@@ -561,6 +561,7 @@ struct _coap_request_ctx {
561561
sock_udp_ep_t *local; /**< deprecated alias for local_udp */
562562
};
563563
# endif
564+
sock_udp_t *sock_udp; /** UDP socket the request was received on */
564565
};
565566
#endif
566567
#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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,7 @@ int nanocoap_server_udp(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
20952095
sock_udp_ep_t remote;
20962096
coap_request_ctx_t ctx = {
20972097
.remote_udp = &remote,
2098+
.sock_udp = &sock,
20982099
};
20992100

21002101
if (!local->port) {
@@ -2178,8 +2179,10 @@ int nanocoap_server_prepare_separate(nanocoap_server_response_ctx_t *ctx,
21782179
#if MODULE_NANOCOAP_UDP
21792180
case COAP_TRANSPORT_UDP:
21802181
memcpy(&ctx->remote_udp, req->remote_udp, sizeof(ctx->remote_udp));
2181-
assert(req->local);
2182+
# if MODULE_SOCK_AUX_LOCAL
21822183
memcpy(&ctx->local_udp, req->local_udp, sizeof(ctx->local_udp));
2184+
# endif
2185+
ctx->sock_udp = req->sock_udp;
21832186
break;
21842187
#endif
21852188
#if MODULE_NANOCOAP_SERVER_TCP
@@ -2257,6 +2260,7 @@ static int _nanocoap_server_sendv_separate_udp(const nanocoap_server_response_ct
22572260
const iolist_t *reply)
22582261
{
22592262
sock_udp_aux_tx_t *aux_out_ptr = NULL;
2263+
# if MODULE_SOCK_AUX_LOCAL
22602264
/* make sure we reply with the same address that the request was
22612265
* destined for -- except in the multicast case */
22622266
sock_udp_aux_tx_t aux_out = {
@@ -2266,12 +2270,15 @@ static int _nanocoap_server_sendv_separate_udp(const nanocoap_server_response_ct
22662270
if (!sock_udp_ep_is_multicast(&ctx->local_udp)) {
22672271
aux_out_ptr = &aux_out;
22682272
}
2269-
ssize_t retval = sock_udp_sendv_aux(NULL, reply, &ctx->remote_udp, aux_out_ptr);
2273+
# endif
2274+
ssize_t retval = sock_udp_sendv_aux(ctx->sock_udp, reply, &ctx->remote_udp, aux_out_ptr);
22702275

22712276
if (retval < 0) {
22722277
return retval;
22732278
}
22742279

2280+
DEBUG("nanocoap: Sent %u B of separate response\n", (unsigned)retval);
2281+
22752282
return 0;
22762283
}
22772284
# endif

0 commit comments

Comments
 (0)