Skip to content

Commit

Permalink
gcoap/forward_proxy: minor fix and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian18 committed Apr 8, 2024
1 parent 4dcab10 commit 920b44d
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions sys/net/application_layer/gcoap/forward_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ gcoap_listener_t forward_proxy_listener = {
_request_matcher_forward_proxy
};

static void _cep_set_timeout(client_ep_t *cep, ztimer_t *timer, uint32_t timeout_ms,
event_handler_t handler)
{
assert(!ztimer_is_set(ZTIMER_MSEC, timer));
timer->callback = gcoap_forward_proxy_post_event;
timer->arg = &cep->event;
cep->event.handler = handler;
ztimer_set(ZTIMER_MSEC, timer, timeout_ms);
}


Check warning on line 89 in sys/net/application_layer/gcoap/forward_proxy.c

View workflow job for this annotation

GitHub Actions / static-tests

too many consecutive empty lines
void gcoap_forward_proxy_init(void)
{
gcoap_register_listener(&forward_proxy_listener);
Expand All @@ -88,9 +99,10 @@ static client_ep_t *_allocate_client_ep(const sock_udp_ep_t *ep)
cep < (_client_eps + CONFIG_GCOAP_REQ_WAITING_MAX);
cep++) {
if (!_cep_in_use(cep)) {
_cep_set_in_use(cep);
DEBUG("gcoap_forward_proxy: allocating client context %p\n", (void *)cep);
_cep_set_req_etag_len(cep, 0);
memcpy(&cep->ep, ep, sizeof(*ep));
_cep_set_in_use(cep);
return cep;
}
}
Expand All @@ -99,8 +111,9 @@ static client_ep_t *_allocate_client_ep(const sock_udp_ep_t *ep)

static void _free_client_ep(client_ep_t *cep)
{
DEBUG("gcoap_forward_proxy: freeing client context %p\n", (void *)cep);
ztimer_remove(ZTIMER_MSEC, &cep->empty_ack_timer);
memset(cep, 0, sizeof(*cep));
cep->flags = 0; /* cep->event may be queued - don't touch it */
}

static int _request_matcher_forward_proxy(gcoap_listener_t *listener,
Expand Down Expand Up @@ -406,16 +419,12 @@ static int _gcoap_forward_proxy_via_coap(coap_pkt_t *client_pkt,
gcoap_forward_proxy_find_req_memo(&memo, client_pkt, &origin_server_ep);
if (memo) {
DEBUG("gcoap_forward_proxy: request already exists, ignore!\n");
_free_client_ep(client_ep);
return 0;
}

if (coap_get_type(client_pkt) == COAP_TYPE_CON) {
client_ep->empty_ack_timer.callback = gcoap_forward_proxy_post_event;
client_ep->empty_ack_timer.arg = &client_ep->event;
client_ep->event.handler = _send_empty_ack;
ztimer_set(ZTIMER_MSEC, &client_ep->empty_ack_timer,
CONFIG_GCOAP_FORWARD_PROXY_EMPTY_ACK_MS);
_cep_set_timeout(client_ep, &client_ep->empty_ack_timer,
CONFIG_GCOAP_FORWARD_PROXY_EMPTY_ACK_MS, _send_empty_ack);
}

unsigned token_len = coap_get_token_len(client_pkt);
Expand All @@ -439,9 +448,9 @@ static int _gcoap_forward_proxy_via_coap(coap_pkt_t *client_pkt,
}

len = gcoap_req_send((uint8_t *)pkt.hdr, len,
&origin_server_ep,
_forward_resp_handler, (void *)client_ep,
&origin_server_ep, _forward_resp_handler, client_ep,
GCOAP_SOCKET_TYPE_UNDEF);

return len;
}

Expand Down Expand Up @@ -482,7 +491,7 @@ int gcoap_forward_proxy_request_process(coap_pkt_t *pkt,
/* target is using CoAP */
if (!strncmp("coap", urip.scheme, urip.scheme_len)) {
int res = _gcoap_forward_proxy_via_coap(pkt, cep, &urip);
if (res < 0) {
if (res <= 0) {
_free_client_ep(cep);
return -EINVAL;
}
Expand Down

0 comments on commit 920b44d

Please sign in to comment.