1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

gcoap/forward_proxy: reply from request destination address

This commit is contained in:
Fabian Hüßler 2024-05-25 20:57:00 +02:00
parent f4b1306460
commit c12375eec4
3 changed files with 13 additions and 9 deletions

View File

@ -59,6 +59,7 @@ void gcoap_forward_proxy_init(void);
*
* @param[in] pkt Packet to parse
* @param[in] client Endpoint of the client
* @param[in] local Local endpoint
*
* @return 0 if parsing was successful
* @return -ENOTSUP if the forward proxy is not compiled in
@ -66,7 +67,7 @@ void gcoap_forward_proxy_init(void);
* @return -EINVAL if Proxy-Uri is malformed
*/
int gcoap_forward_proxy_request_process(coap_pkt_t *pkt,
const sock_udp_ep_t *client);
const sock_udp_ep_t *client, const sock_udp_ep_t *local);
/**
* @brief Finds the memo for an outstanding request within the

View File

@ -142,8 +142,9 @@ static ssize_t _forward_proxy_handler(coap_pkt_t *pdu, uint8_t *buf,
{
int pdu_len;
const sock_udp_ep_t *remote = coap_request_ctx_get_remote_udp(ctx);
const sock_udp_ep_t *local = coap_request_ctx_get_local_udp(ctx);
pdu_len = gcoap_forward_proxy_request_process(pdu, remote);
pdu_len = gcoap_forward_proxy_request_process(pdu, remote, local);
/* Out of memory, reply with 5.00 */
if (pdu_len == -ENOMEM) {
@ -226,12 +227,13 @@ static bool _parse_endpoint(sock_udp_ep_t *remote,
return true;
}
static ssize_t _dispatch_msg(const void *buf, size_t len, sock_udp_ep_t *remote)
static ssize_t _dispatch_msg(const void *buf, size_t len, sock_udp_ep_t *remote,
const sock_udp_ep_t *local)
{
/* Yes it's not a request -- but turns out there is nothing in
* gcoap_req_send that is actually request specific, especially if we
* don't assign a callback. */
ssize_t res = gcoap_req_send(buf, len, remote, NULL, NULL,
ssize_t res = gcoap_req_send(buf, len, remote, local, NULL, NULL,
GCOAP_SOCKET_TYPE_UDP);
if (res <= 0) {
DEBUG("gcoap_forward_proxy: unable to dispatch message: %d\n", -res);
@ -252,7 +254,7 @@ static void _send_empty_ack(event_t *event)
/* Flipping byte order as unlike in the other places where mid is
* used, coap_build_hdr would actually flip it back */
coap_build_hdr(&buf, COAP_TYPE_ACK, NULL, 0, 0, ntohs(cep->mid));
_dispatch_msg(&buf, sizeof(buf), &cep->ep);
_dispatch_msg(&buf, sizeof(buf), &cep->ep, &cep->proxy_ep);
}
static void _set_response_type(coap_pkt_t *pdu, uint8_t resp_type)
@ -316,7 +318,7 @@ static void _forward_resp_handler(const gcoap_request_memo_t *memo,
}
_set_response_type(pdu, _cep_get_response_type(cep));
/* don't use buf_len here, in case the above `gcoap_resp_init`s changed `pdu` */
_dispatch_msg(pdu->hdr, coap_get_total_len(pdu), &cep->ep);
_dispatch_msg(pdu->hdr, coap_get_total_len(pdu), &cep->ep, &cep->proxy_ep);
_free_client_ep(cep);
}
@ -407,7 +409,7 @@ int gcoap_forward_proxy_req_send(client_ep_t *cep)
{
int len;
if ((len = gcoap_req_send((uint8_t *)cep->pdu.hdr, coap_get_total_len(&cep->pdu),
&cep->server_ep, _forward_resp_handler, cep,
&cep->server_ep, NULL, _forward_resp_handler, cep,
GCOAP_SOCKET_TYPE_UNDEF)) <= 0) {
DEBUG("gcoap_forward_proxy_req_send(): gcoap_req_send failed %d\n", len);
_free_client_ep(cep);
@ -479,13 +481,13 @@ static int _gcoap_forward_proxy_via_coap(coap_pkt_t *client_pkt,
}
int gcoap_forward_proxy_request_process(coap_pkt_t *pkt,
const sock_udp_ep_t *client) {
const sock_udp_ep_t *client, const sock_udp_ep_t *local) {
char *uri;
uri_parser_result_t urip;
ssize_t optlen = 0;
client_ep_t *cep = _allocate_client_ep(client);
cep->proxy_ep = local ? *local : (sock_udp_ep_t){ 0 };
if (!cep) {
return -ENOMEM;
}

View File

@ -40,6 +40,7 @@ typedef struct {
coap_pkt_t pdu; /**< forward CoAP PDU */
sock_udp_ep_t server_ep; /**< forward Server endpoint */
sock_udp_ep_t ep; /**< client endpoint */
sock_udp_ep_t proxy_ep; /**< proxy endpoint */
uint16_t mid; /**< message ID */
uint8_t flags; /**< client flags */
#if IS_USED(MODULE_NANOCOAP_CACHE)