Skip to content

Commit 7d59e98

Browse files
committed
patch(lwip): Add support for DNS per netif
1 parent 28f5e7e commit 7d59e98

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed

patches/lwip_dns_per_netif.diff

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
diff --git a/components/lwip/lwip/src/core/dns.c b/components/lwip/lwip/src/core/dns.c
2+
index e9edc205f7dce821deb4afb4baae48e4ce832d51..eec4c2870985d8fd6f23b4fe91ea11b067fb40c7 100644
3+
--- a/components/lwip/lwip/src/core/dns.c
4+
+++ b/components/lwip/lwip/src/core/dns.c
5+
@@ -101,6 +101,10 @@
6+
static bool s_is_tmr_start = false;
7+
#endif /* ESP_LWIP_DNS_TIMERS_ONDEMAND */
8+
9+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
10+
+static dns_setserver_callback_t s_dns_setserver_callback = NULL;
11+
+#endif /* LWIP_DNS_SETSERVER_WITH_NETIF */
12+
+
13+
#include <string.h>
14+
#if ESP_DNS
15+
#include <stdbool.h>
16+
@@ -1769,4 +1773,27 @@ dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_call
17+
LWIP_DNS_ISMDNS_ARG(is_mdns));
18+
}
19+
20+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
21+
+void
22+
+dns_setserver_with_netif(struct netif* netif, u8_t numdns, const ip_addr_t *dnsserver)
23+
+{
24+
+ if (s_dns_setserver_callback) {
25+
+ s_dns_setserver_callback(netif, numdns, dnsserver);
26+
+ } else {
27+
+ dns_setserver(numdns, dnsserver);
28+
+ }
29+
+}
30+
+
31+
+err_t
32+
+dns_setserver_callback(dns_setserver_callback_t callback)
33+
+{
34+
+ if (s_dns_setserver_callback) {
35+
+ /* fail if the callback has been set already */
36+
+ return ERR_ARG;
37+
+ }
38+
+ s_dns_setserver_callback = callback; /* support only one time configuration */
39+
+ return ERR_OK;
40+
+}
41+
+#endif /* LWIP_DNS_SETSERVER_WITH_NETIF */
42+
+
43+
#endif /* LWIP_DNS */
44+
diff --git a/components/lwip/lwip/src/core/ipv4/dhcp.c b/components/lwip/lwip/src/core/ipv4/dhcp.c
45+
index e05ce8f650375f67d4b06613160a42ec63a6aec8..65e99a2dfebad7d5d4085418f76e04b0ea30a9e3 100644
46+
--- a/components/lwip/lwip/src/core/ipv4/dhcp.c
47+
+++ b/components/lwip/lwip/src/core/ipv4/dhcp.c
48+
@@ -777,7 +777,11 @@ dhcp_handle_ack(struct netif *netif, struct dhcp_msg *msg_in)
49+
}
50+
#endif
51+
ip_addr_set_ip4_u32_val(dns_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
52+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
53+
+ dns_setserver_with_netif(netif, n, &dns_addr);
54+
+#else
55+
dns_setserver(n, &dns_addr);
56+
+#endif
57+
}
58+
#endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
59+
}
60+
diff --git a/components/lwip/lwip/src/core/ipv6/dhcp6.c b/components/lwip/lwip/src/core/ipv6/dhcp6.c
61+
index 41444a4c75bb449310a66a72580f1f2d8b81f2e4..217d0264001050a2a21f16c57a3980bc7f8c9b4a 100644
62+
--- a/components/lwip/lwip/src/core/ipv6/dhcp6.c
63+
+++ b/components/lwip/lwip/src/core/ipv6/dhcp6.c
64+
@@ -547,7 +547,12 @@ dhcp6_handle_config_reply(struct netif *netif, struct pbuf *p_msg_in)
65+
}
66+
ip6_addr_assign_zone(dns_addr6, IP6_UNKNOWN, netif);
67+
/* @todo: do we need a different offset than DHCP(v4)? */
68+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
69+
+ dns_setserver_with_netif(netif, n, &dns_addr);
70+
+#else
71+
dns_setserver(n, &dns_addr);
72+
+#endif
73+
+
74+
}
75+
}
76+
/* @ todo: parse and set Domain Search List */
77+
diff --git a/components/lwip/lwip/src/core/ipv6/nd6.c b/components/lwip/lwip/src/core/ipv6/nd6.c
78+
index 74b395f3baccc2a9aa2f2b7e0f99abb5da6a4d1e..408f23f22ef7c3aad55c1e60a37d5f14a109dba4 100644
79+
--- a/components/lwip/lwip/src/core/ipv6/nd6.c
80+
+++ b/components/lwip/lwip/src/core/ipv6/nd6.c
81+
@@ -780,14 +780,23 @@ nd6_input(struct pbuf *p, struct netif *inp)
82+
83+
if (htonl(rdnss_opt->lifetime) > 0) {
84+
/* TODO implement Lifetime > 0 */
85+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
86+
+ dns_setserver_with_netif(inp, rdnss_server_idx++, &rdnss_address);
87+
+#else
88+
dns_setserver(rdnss_server_idx++, &rdnss_address);
89+
+#endif
90+
+
91+
} else {
92+
/* TODO implement DNS removal in dns.c */
93+
u8_t s;
94+
for (s = 0; s < DNS_MAX_SERVERS; s++) {
95+
const ip_addr_t *addr = dns_getserver(s);
96+
if(ip_addr_cmp(addr, &rdnss_address)) {
97+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
98+
+ dns_setserver_with_netif(inp, s, NULL);
99+
+#else
100+
dns_setserver(s, NULL);
101+
+#endif
102+
}
103+
}
104+
}
105+
diff --git a/components/lwip/lwip/src/include/lwip/dns.h b/components/lwip/lwip/src/include/lwip/dns.h
106+
index fad97230a459c501e4693803b65932837a9668ee..964daafb5fd7dfc031d4ac0fa4f5a320ee38c2a6 100644
107+
--- a/components/lwip/lwip/src/include/lwip/dns.h
108+
+++ b/components/lwip/lwip/src/include/lwip/dns.h
109+
@@ -122,6 +122,12 @@ err_t dns_local_addhost(const char *hostname, const ip_addr_t *addr);
110+
#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
111+
#endif /* DNS_LOCAL_HOSTLIST */
112+
113+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
114+
+typedef void (*dns_setserver_callback_t)(struct netif* netif, u8_t numdns, const ip_addr_t *dnsserver);
115+
+void dns_setserver_with_netif(struct netif* netif, u8_t numdns, const ip_addr_t *dnsserver);
116+
+err_t dns_setserver_callback(dns_setserver_callback_t callback);
117+
+#endif /* LWIP_DNS_SETSERVER_WITH_NETIF */
118+
+
119+
#ifdef __cplusplus
120+
}
121+
#endif
122+
diff --git a/components/lwip/lwip/src/include/lwip/opt.h b/components/lwip/lwip/src/include/lwip/opt.h
123+
index 2c58c53229d0df1d5e60dab8fd65c48562a76bed..91aa3513fde2a285543649c8bbf7795b6d1471d4 100644
124+
--- a/components/lwip/lwip/src/include/lwip/opt.h
125+
+++ b/components/lwip/lwip/src/include/lwip/opt.h
126+
@@ -1161,6 +1161,22 @@
127+
#define DNS_LOCAL_HOSTLIST 0
128+
#endif /* DNS_LOCAL_HOSTLIST */
129+
130+
+/** LWIP_DNS_SETSERVER_WITH_NETIF: If this is turned on, the dns_setserver_with_netif() is enabled and called
131+
+ * from all internal modules (instead of dns_setserver()) allowing to setup a user callback to collect DNS server
132+
+ * information acquired by the related network interface.
133+
+ * This could be used to overcome the LWIP limitation of using DNS servers
134+
+ * globally, which could in some cases overwrite distinct DNS server information of one netif by another.
135+
+ * If you want to use this option, you would have to set LWIP_DNS_SETSERVER_WITH_NETIF=1 and:
136+
+ * 1) Register DNS setserver callback using dns_setserver_callback() and collect the DNS server information
137+
+ * in the callback and store it separately for each netif.
138+
+ * 2) Restore the actual DNS server information using dns_setserver() before every DNS lookup.
139+
+ * */
140+
+#if !defined LWIP_DNS_SETSERVER_WITH_NETIF && defined __DOXYGEN__
141+
+#define LWIP_DNS_SETSERVER_WITH_NETIF 0
142+
+#elif !defined LWIP_DNS_SETSERVER_WITH_NETIF
143+
+#define LWIP_DNS_SETSERVER_WITH_NETIF 1
144+
+#endif /* LWIP_DNS_SETSERVER_WITH_NETIF */
145+
+
146+
/** If this is turned on, the local host-list can be dynamically changed
147+
* at runtime. */
148+
#if !defined DNS_LOCAL_HOSTLIST_IS_DYNAMIC || defined __DOXYGEN__
149+
diff --git a/components/lwip/lwip/src/netif/ppp/ppp.c b/components/lwip/lwip/src/netif/ppp/ppp.c
150+
index be585531357071e6f3fcc467649c83a379abfdff..3609b3c0c256fc6828ab1ea7324dac58e4c861d6 100644
151+
--- a/components/lwip/lwip/src/netif/ppp/ppp.c
152+
+++ b/components/lwip/lwip/src/netif/ppp/ppp.c
153+
@@ -1109,12 +1109,23 @@ int cifproxyarp(ppp_pcb *pcb, u32_t his_adr) {
154+
*/
155+
int sdns(ppp_pcb *pcb, u32_t ns1, u32_t ns2) {
156+
ip_addr_t ns;
157+
+#if !LWIP_DNS_SETSERVER_WITH_NETIF
158+
LWIP_UNUSED_ARG(pcb);
159+
+#endif
160+
161+
ip_addr_set_ip4_u32_val(ns, ns1);
162+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
163+
+ dns_setserver_with_netif(pcb->netif, 0, &ns);
164+
+#else
165+
dns_setserver(0, &ns);
166+
+#endif
167+
+
168+
ip_addr_set_ip4_u32_val(ns, ns2);
169+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
170+
+ dns_setserver_with_netif(pcb->netif, 1, &ns);
171+
+#else
172+
dns_setserver(1, &ns);
173+
+#endif
174+
return 1;
175+
}
176+
177+
@@ -1125,17 +1136,27 @@ int sdns(ppp_pcb *pcb, u32_t ns1, u32_t ns2) {
178+
int cdns(ppp_pcb *pcb, u32_t ns1, u32_t ns2) {
179+
const ip_addr_t *nsa;
180+
ip_addr_t nsb;
181+
+#if !LWIP_DNS_SETSERVER_WITH_NETIF
182+
LWIP_UNUSED_ARG(pcb);
183+
+#endif
184+
185+
nsa = dns_getserver(0);
186+
ip_addr_set_ip4_u32_val(nsb, ns1);
187+
if (ip_addr_cmp(nsa, &nsb)) {
188+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
189+
+ dns_setserver_with_netif(pcb->netif, 0, IP_ADDR_ANY);
190+
+#else
191+
dns_setserver(0, IP_ADDR_ANY);
192+
+#endif
193+
}
194+
nsa = dns_getserver(1);
195+
ip_addr_set_ip4_u32_val(nsb, ns2);
196+
if (ip_addr_cmp(nsa, &nsb)) {
197+
+#if LWIP_DNS_SETSERVER_WITH_NETIF
198+
+ dns_setserver_with_netif(pcb->netif, 1, IP_ADDR_ANY);
199+
+#else
200+
dns_setserver(1, IP_ADDR_ANY);
201+
+#endif
202+
}
203+
return 1;
204+
}

tools/install-esp-idf.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ if [ ! -x $idf_was_installed ] || [ ! -x $commit_predefined ]; then
4040
patch -p1 -N -i ../patches/esp32s2_i2c_ll_master_init.diff
4141
patch -p1 -N -i ../patches/mmu_map.diff
4242
patch -p1 -N -i ../patches/lwip_max_tcp_pcb.diff
43+
patch -p1 -N -i ../patches/lwip_dns_per_netif.diff
4344
cd -
4445
fi
4546

0 commit comments

Comments
 (0)