diff --git a/sys/include/net/gcoap/forward_proxy.h b/sys/include/net/gcoap/forward_proxy.h index bee689803940..a71e3d5327e2 100644 --- a/sys/include/net/gcoap/forward_proxy.h +++ b/sys/include/net/gcoap/forward_proxy.h @@ -53,7 +53,7 @@ void gcoap_forward_proxy_init(void); * @return -EINVAL if Proxy-Uri is malformed */ int gcoap_forward_proxy_request_process(coap_pkt_t *pkt, - sock_udp_ep_t *client); + const sock_udp_ep_t *client); /** * @brief Finds the memo for an outstanding request within the diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index c6af7e96d1b7..78a64e723258 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -96,6 +96,12 @@ #include #endif +#if defined(MODULE_SOCK_UDP) || defined(DOXYGEN) +#include "net/sock/udp.h" +#else +typedef void sock_udp_ep_t; +#endif + #ifdef __cplusplus extern "C" { #endif @@ -347,6 +353,18 @@ void *coap_request_ctx_get_context(const coap_request_ctx_t *ctx); */ uint32_t coap_request_ctx_get_tl_type(const coap_request_ctx_t *ctx); +/** + * @brief Get the remote endpoint from which the request was received + * + * @note This is currently only implemented for GCoAP + * + * @param[in] ctx The request context + * + * @return Remote endpoint from which the request originated + * @return NULL The request was not received via UDP + */ +const sock_udp_ep_t *coap_request_ctx_get_remote_udp(const coap_request_ctx_t *ctx); + /** * @brief Block1 helper struct */ diff --git a/sys/net/application_layer/gcoap/forward_proxy.c b/sys/net/application_layer/gcoap/forward_proxy.c index 5beb91567063..6e80ff8597d0 100644 --- a/sys/net/application_layer/gcoap/forward_proxy.c +++ b/sys/net/application_layer/gcoap/forward_proxy.c @@ -61,7 +61,7 @@ void gcoap_forward_proxy_init(void) gcoap_register_listener(&forward_proxy_listener); } -static client_ep_t *_allocate_client_ep(sock_udp_ep_t *ep) +static client_ep_t *_allocate_client_ep(const sock_udp_ep_t *ep) { client_ep_t *cep; for (cep = _client_eps; @@ -102,7 +102,7 @@ static ssize_t _forward_proxy_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx) { int pdu_len; - sock_udp_ep_t *remote = coap_request_ctx_get_context(ctx); + const sock_udp_ep_t *remote = coap_request_ctx_get_remote_udp(ctx); pdu_len = gcoap_forward_proxy_request_process(pdu, remote); @@ -376,7 +376,7 @@ static int _gcoap_forward_proxy_via_coap(coap_pkt_t *client_pkt, } int gcoap_forward_proxy_request_process(coap_pkt_t *pkt, - sock_udp_ep_t *client) { + const sock_udp_ep_t *client) { char *uri; uri_parser_result_t urip; ssize_t optlen = 0; diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index 8e3a53ab8d25..381735199b95 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -706,20 +706,13 @@ static size_t _handle_req(gcoap_socket_t *sock, coap_pkt_t *pdu, uint8_t *buf, } ssize_t pdu_len; - char *offset; coap_request_ctx_t ctx = { .resource = resource, .tl_type = (uint32_t)sock->type, + .remote = remote, }; - if (coap_get_proxy_uri(pdu, &offset) > 0) { - ctx.context = remote; - } - else { - ctx.context = resource->context; - } - pdu_len = resource->handler(pdu, buf, len, &ctx); if (pdu_len < 0) { pdu_len = gcoap_response(pdu, buf, len, diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index ea96670c4e38..78d14bb6b1ba 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -463,7 +463,6 @@ ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, else { coap_request_ctx_t ctx = { .resource = resource, - .context = resource->context, }; return resource->handler(pkt, resp_buf, resp_buf_len, &ctx); } @@ -1244,7 +1243,7 @@ const char *coap_request_ctx_get_path(const coap_request_ctx_t *ctx) void *coap_request_ctx_get_context(const coap_request_ctx_t *ctx) { - return ctx->context; + return ctx->resource->context; } uint32_t coap_request_ctx_get_tl_type(const coap_request_ctx_t *ctx) @@ -1256,3 +1255,13 @@ uint32_t coap_request_ctx_get_tl_type(const coap_request_ctx_t *ctx) return 0; #endif } + +const sock_udp_ep_t *coap_request_ctx_get_remote_udp(const coap_request_ctx_t *ctx) +{ +#ifdef MODULE_GCOAP + return ctx->remote; +#else + (void)ctx; + return NULL; +#endif +} diff --git a/sys/net/application_layer/nanocoap/nanocoap_internal.h b/sys/net/application_layer/nanocoap/nanocoap_internal.h index e858d890ee70..cada93135f5c 100644 --- a/sys/net/application_layer/nanocoap/nanocoap_internal.h +++ b/sys/net/application_layer/nanocoap/nanocoap_internal.h @@ -20,6 +20,11 @@ #define NANOCOAP_INTERNAL_H #include "net/nanocoap.h" +#if defined(MODULE_SOCK_UDP) || defined(DOXYGEN) +#include "net/sock/udp.h" +#else +typedef void sock_udp_ep_t; +#endif #ifdef __cplusplus extern "C" { @@ -30,8 +35,6 @@ extern "C" { */ struct _coap_request_ctx { const coap_resource_t *resource; /**< resource of the request */ - void *context; /**< request context, needed to supply - the remote for the forward proxy */ #if defined(MODULE_GCOAP) || DOXYGEN /** * @brief transport the packet was received over @@ -40,6 +43,7 @@ struct _coap_request_ctx { * cyclically include the @ref net_gcoap header. */ uint32_t tl_type; + sock_udp_ep_t *remote; /**< remote endpoint of the request */ #endif }; diff --git a/tests/netutils/Makefile b/tests/netutils/Makefile index 8cd48494bd43..d16766d6a34f 100644 --- a/tests/netutils/Makefile +++ b/tests/netutils/Makefile @@ -6,7 +6,7 @@ USEMODULE += embunit USEMODULE += sock_util # make sure we have an implementation of sock_types.h -USEMODULE += gnrc_sock_udp +USEMODULE += sock_udp USEMODULE += gnrc_ipv6 USEMODULE += ipv4_addr diff --git a/tests/sock_udp_aux/Makefile b/tests/sock_udp_aux/Makefile index 7c4dc248ab27..9234a4c2a595 100644 --- a/tests/sock_udp_aux/Makefile +++ b/tests/sock_udp_aux/Makefile @@ -9,7 +9,7 @@ USEMODULE += fmt USEMODULE += gnrc_icmpv6_echo USEMODULE += gnrc_ipv6_default USEMODULE += netdev_default -USEMODULE += gnrc_sock_udp +USEMODULE += sock_udp USEMODULE += netstats_ipv6 USEMODULE += netstats_l2 USEMODULE += ps