Skip to content

Commit

Permalink
[dns] use LWIP_HOOK_DNS_GET_NETIF_FOR_SERVER_INDEX hook to bind socke…
Browse files Browse the repository at this point in the history
…ts to a particular netif if required
  • Loading branch information
avtolstoy committed Aug 21, 2024
1 parent 25aebeb commit 6c21a8f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/core/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
#include "lwip/dns.h"
#include "lwip/prot/dns.h"

#ifdef LWIP_HOOK_FILENAME
#include LWIP_HOOK_FILENAME
#endif // LWIP_HOOK_FILENAME

#include <string.h>

/** Random generator function to create random TXIDs and source ports for queries */
Expand Down Expand Up @@ -769,6 +773,9 @@ dns_send(u8_t idx)
const char *hostname, *hostname_part;
u8_t n;
u8_t pcb_idx;
#ifdef LWIP_HOOK_DNS_GET_NETIF_FOR_SERVER_INDEX
u8_t unbind = 0;
#endif // LWIP_HOOK_DNS_GET_NETIF_FOR_SERVER_INDEX
struct dns_table_entry *entry = &dns_table[idx];

LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n",
Expand Down Expand Up @@ -860,8 +867,22 @@ dns_send(u8_t idx)
{
dst_port = DNS_SERVER_PORT;
dst = &dns_servers[entry->server_idx];
#ifdef LWIP_HOOK_DNS_GET_NETIF_FOR_SERVER_INDEX
struct netif* bind_netif = LWIP_HOOK_DNS_GET_NETIF_FOR_SERVER_INDEX(entry->server_idx);
if (bind_netif) {
unbind = 1;
// This forces udp_sendto to pick bind_netif as an outgoing interface, bypassing
// the routing table/hooks.
udp_bind_netif(dns_pcbs[pcb_idx], bind_netif);
}
#endif // LWIP_HOOK_DNS_GET_NETIF_FOR_SERVER_INDEX
}
err = udp_sendto(dns_pcbs[pcb_idx], p, dst, dst_port);
#ifdef LWIP_HOOK_DNS_GET_NETIF_FOR_SERVER_INDEX
if (unbind) {
udp_bind_netif(dns_pcbs[pcb_idx], NULL);
}
#endif // LWIP_HOOK_DNS_GET_NETIF_FOR_SERVER_INDEX

/* free pbuf */
pbuf_free(p);
Expand Down

0 comments on commit 6c21a8f

Please sign in to comment.