Skip to content

Commit b19cf86

Browse files
authored
Merge pull request #3203 from cesanta/dns
Built-in TCP/IP (re-)starts DNS connection on DHCP link up
2 parents 19933a4 + 3930634 commit b19cf86

File tree

6 files changed

+81
-29
lines changed

6 files changed

+81
-29
lines changed

mongoose.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,19 +353,24 @@ static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
353353
return mg_send(c, &pkt, sizeof(pkt.header) + n);
354354
}
355355

356-
static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
357-
struct mg_dns *dnsc, bool ipv6) {
358-
struct dns_data *d = NULL;
356+
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc);
357+
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc) {
359358
if (dnsc->url == NULL) {
360-
mg_error(c, "DNS server URL is NULL. Call mg_mgr_init()");
361-
} else if (dnsc->c == NULL) {
362-
dnsc->c = mg_connect(c->mgr, dnsc->url, NULL, NULL);
363-
if (dnsc->c != NULL) {
364-
dnsc->c->pfn = dns_cb;
365-
// dnsc->c->is_hexdumping = 1;
366-
}
359+
mg_error(0, "DNS server URL is NULL. Call mg_mgr_init()");
360+
return false;
367361
}
368362
if (dnsc->c == NULL) {
363+
dnsc->c = mg_connect(mgr, dnsc->url, NULL, NULL);
364+
if (dnsc->c == NULL) return false;
365+
dnsc->c->pfn = dns_cb;
366+
}
367+
return true;
368+
}
369+
370+
static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
371+
struct mg_dns *dnsc, bool ipv6) {
372+
struct dns_data *d = NULL;
373+
if (!mg_dnsc_init(c->mgr, dnsc)) {
369374
mg_error(c, "resolver");
370375
} else if ((d = (struct dns_data *) mg_calloc(1, sizeof(*d))) == NULL) {
371376
mg_error(c, "resolve OOM");
@@ -467,6 +472,7 @@ struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, char *name) {
467472
return c;
468473
}
469474

475+
470476
#ifdef MG_ENABLE_LINES
471477
#line 1 "src/event.c"
472478
#endif
@@ -4556,6 +4562,8 @@ static void rx_icmp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
45564562
}
45574563
}
45584564

4565+
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip);
4566+
45594567
static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
45604568
uint32_t ip = 0, gw = 0, mask = 0, lease = 0, dns = 0, sntp = 0;
45614569
uint8_t msgtype = 0, state = ifp->state;
@@ -4604,8 +4612,10 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
46044612
ifp->state = MG_TCPIP_STATE_IP; // BOUND state
46054613
mg_random(&rand, sizeof(rand));
46064614
srand((unsigned int) (rand + mg_millis()));
4607-
if (ifp->enable_req_dns && dns != 0)
4615+
if (ifp->enable_req_dns && dns != 0) {
4616+
setdns4(ifp, &dns);
46084617
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_DNS, &dns);
4618+
}
46094619
if (ifp->enable_req_sntp && sntp != 0)
46104620
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_SNTP, &sntp);
46114621
} else if (ifp->state == MG_TCPIP_STATE_READY && ifp->ip == ip) { // renew
@@ -5272,6 +5282,7 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) {
52725282
void mg_tcpip_free(struct mg_tcpip_if *ifp) {
52735283
mg_free(ifp->recv_queue.buf);
52745284
mg_free(ifp->tx.buf);
5285+
mg_free(ifp->dns4_url);
52755286
}
52765287

52775288
static void send_syn(struct mg_connection *c) {
@@ -5438,6 +5449,21 @@ void mg_multicast_add(struct mg_connection *c, char *ip) {
54385449
c->mgr->ifp->update_mac_hash_table = true; // mark dirty
54395450
}
54405451

5452+
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc);
5453+
5454+
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip) {
5455+
struct mg_dns *dnsc;
5456+
mg_free(ifp->dns4_url);
5457+
ifp->dns4_url = mg_mprintf("udp://%M:53", mg_print_ip4, ip);
5458+
dnsc = &ifp->mgr->dns4;
5459+
dnsc->url = (const char *) ifp->dns4_url;
5460+
MG_DEBUG(("Set DNS URL to %s", dnsc->url));
5461+
if (ifp->mgr->use_dns6) return;
5462+
if (dnsc->c != NULL) mg_close_conn(dnsc->c);
5463+
if (!mg_dnsc_init(ifp->mgr, dnsc)) // create DNS connection
5464+
MG_ERROR(("DNS connection creation failed"));
5465+
}
5466+
54415467
#endif // MG_ENABLE_TCPIP
54425468

54435469
#ifdef MG_ENABLE_LINES

mongoose.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3092,6 +3092,7 @@ struct mg_tcpip_if {
30923092

30933093
// Internal state, user can use it but should not change it
30943094
uint8_t gwmac[6]; // Router's MAC
3095+
char *dns4_url; // DNS server URL
30953096
uint64_t now; // Current time
30963097
uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state
30973098
uint64_t lease_expire; // Lease expiration time, in ms

src/dns.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,24 @@ static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
232232
return mg_send(c, &pkt, sizeof(pkt.header) + n);
233233
}
234234

235-
static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
236-
struct mg_dns *dnsc, bool ipv6) {
237-
struct dns_data *d = NULL;
235+
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc);
236+
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc) {
238237
if (dnsc->url == NULL) {
239-
mg_error(c, "DNS server URL is NULL. Call mg_mgr_init()");
240-
} else if (dnsc->c == NULL) {
241-
dnsc->c = mg_connect(c->mgr, dnsc->url, NULL, NULL);
242-
if (dnsc->c != NULL) {
243-
dnsc->c->pfn = dns_cb;
244-
// dnsc->c->is_hexdumping = 1;
245-
}
238+
mg_error(0, "DNS server URL is NULL. Call mg_mgr_init()");
239+
return false;
246240
}
247241
if (dnsc->c == NULL) {
242+
dnsc->c = mg_connect(mgr, dnsc->url, NULL, NULL);
243+
if (dnsc->c == NULL) return false;
244+
dnsc->c->pfn = dns_cb;
245+
}
246+
return true;
247+
}
248+
249+
static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
250+
struct mg_dns *dnsc, bool ipv6) {
251+
struct dns_data *d = NULL;
252+
if (!mg_dnsc_init(c->mgr, dnsc)) {
248253
mg_error(c, "resolver");
249254
} else if ((d = (struct dns_data *) mg_calloc(1, sizeof(*d))) == NULL) {
250255
mg_error(c, "resolve OOM");
@@ -345,3 +350,4 @@ struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, char *name) {
345350
if (c != NULL) mg_multicast_add(c, (char *)"224.0.0.251");
346351
return c;
347352
}
353+

src/net_builtin.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ static void rx_icmp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
405405
}
406406
}
407407

408+
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip);
409+
408410
static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
409411
uint32_t ip = 0, gw = 0, mask = 0, lease = 0, dns = 0, sntp = 0;
410412
uint8_t msgtype = 0, state = ifp->state;
@@ -453,8 +455,10 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
453455
ifp->state = MG_TCPIP_STATE_IP; // BOUND state
454456
mg_random(&rand, sizeof(rand));
455457
srand((unsigned int) (rand + mg_millis()));
456-
if (ifp->enable_req_dns && dns != 0)
458+
if (ifp->enable_req_dns && dns != 0) {
459+
setdns4(ifp, &dns);
457460
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_DNS, &dns);
461+
}
458462
if (ifp->enable_req_sntp && sntp != 0)
459463
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_SNTP, &sntp);
460464
} else if (ifp->state == MG_TCPIP_STATE_READY && ifp->ip == ip) { // renew
@@ -1121,6 +1125,7 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) {
11211125
void mg_tcpip_free(struct mg_tcpip_if *ifp) {
11221126
mg_free(ifp->recv_queue.buf);
11231127
mg_free(ifp->tx.buf);
1128+
mg_free(ifp->dns4_url);
11241129
}
11251130

11261131
static void send_syn(struct mg_connection *c) {
@@ -1287,4 +1292,19 @@ void mg_multicast_add(struct mg_connection *c, char *ip) {
12871292
c->mgr->ifp->update_mac_hash_table = true; // mark dirty
12881293
}
12891294

1295+
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc);
1296+
1297+
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip) {
1298+
struct mg_dns *dnsc;
1299+
mg_free(ifp->dns4_url);
1300+
ifp->dns4_url = mg_mprintf("udp://%M:53", mg_print_ip4, ip);
1301+
dnsc = &ifp->mgr->dns4;
1302+
dnsc->url = (const char *) ifp->dns4_url;
1303+
MG_DEBUG(("Set DNS URL to %s", dnsc->url));
1304+
if (ifp->mgr->use_dns6) return;
1305+
if (dnsc->c != NULL) mg_close_conn(dnsc->c);
1306+
if (!mg_dnsc_init(ifp->mgr, dnsc)) // create DNS connection
1307+
MG_ERROR(("DNS connection creation failed"));
1308+
}
1309+
12901310
#endif // MG_ENABLE_TCPIP

src/net_builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct mg_tcpip_if {
5757

5858
// Internal state, user can use it but should not change it
5959
uint8_t gwmac[6]; // Router's MAC
60+
char *dns4_url; // DNS server URL
6061
uint64_t now; // Current time
6162
uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state
6263
uint64_t lease_expire; // Lease expiration time, in ms

tutorials/tcpip/tap-driver/main.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,20 @@ static size_t tap_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
4242
return (size_t) received;
4343
}
4444

45-
char *s_dns = NULL, *s_sntp = NULL;
45+
char *s_sntp = NULL;
4646

4747
static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
4848
if (ev == MG_TCPIP_EV_ST_CHG) {
4949
MG_INFO(("State change: %u", *(uint8_t *) ev_data));
5050
} else if (ev == MG_TCPIP_EV_DHCP_DNS) {
51-
mg_free(s_dns);
52-
s_dns = mg_mprintf("udp://%M:53", mg_print_ip4, (uint32_t *) ev_data);
53-
ifp->mgr->dns4.url = s_dns;
54-
MG_INFO(("Set DNS to %s", ifp->mgr->dns4.url));
51+
MG_INFO(("Got DNS from DHCP: %M", mg_print_ip4, (uint32_t *) ev_data));
5552
} else if (ev == MG_TCPIP_EV_DHCP_SNTP) {
53+
MG_INFO(("Got SNTP from DHCP: %M", mg_print_ip4, (uint32_t *) ev_data));
5654
mg_free(s_sntp);
5755
s_sntp = mg_mprintf("udp://%M:123", mg_print_ip4, (uint32_t *) ev_data);
58-
MG_INFO(("Set SNTP to %s", s_sntp));
56+
MG_INFO(("Set SNTP server to %s", s_sntp)); // though net.c may not use it.
5957
}
58+
(void) ifp;
6059
}
6160

6261
int main(int argc, char *argv[]) {
@@ -128,7 +127,6 @@ int main(int argc, char *argv[]) {
128127
web_init(&mgr);
129128
while (s_signo == 0) mg_mgr_poll(&mgr, 100); // Infinite event loop
130129

131-
mg_free(s_dns);
132130
mg_free(s_sntp);
133131
mg_mgr_free(&mgr);
134132
close(fd);

0 commit comments

Comments
 (0)