Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcoap: add remote sock_udp_ep_t to coap_request_ctx_t #18519

Merged
merged 5 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sys/include/net/gcoap/forward_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
#include <arpa/inet.h>
#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
Expand Down Expand Up @@ -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
*/
Expand Down
6 changes: 3 additions & 3 deletions sys/net/application_layer/gcoap/forward_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
9 changes: 1 addition & 8 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 11 additions & 2 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
8 changes: 6 additions & 2 deletions sys/net/application_layer/nanocoap/nanocoap_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
#define NANOCOAP_INTERNAL_H

#include "net/nanocoap.h"
#if defined(MODULE_SOCK_UDP) || defined(DOXYGEN)
#include "net/sock/udp.h"
benpicco marked this conversation as resolved.
Show resolved Hide resolved
#else
typedef void sock_udp_ep_t;
#endif

#ifdef __cplusplus
extern "C" {
Expand All @@ -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
Expand All @@ -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
};

Expand Down
2 changes: 1 addition & 1 deletion tests/netutils/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/sock_udp_aux/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down