Skip to content

Commit 3930634

Browse files
committed
Built-in TCP/IP (re-)starts DNS connection on link up
1 parent 1494f5e commit 3930634

File tree

6 files changed

+81
-31
lines changed

6 files changed

+81
-31
lines changed

mongoose.c

Lines changed: 37 additions & 13 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
@@ -4577,6 +4583,8 @@ static void rx_icmp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
45774583
}
45784584
}
45794585

4586+
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip);
4587+
45804588
static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
45814589
uint32_t ip = 0, gw = 0, mask = 0, lease = 0, dns = 0, sntp = 0;
45824590
uint8_t msgtype = 0, state = ifp->state;
@@ -4625,8 +4633,10 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
46254633
ifp->state = MG_TCPIP_STATE_IP; // BOUND state
46264634
mg_random(&rand, sizeof(rand));
46274635
srand((unsigned int) (rand + mg_millis()));
4628-
if (ifp->enable_req_dns && dns != 0)
4636+
if (ifp->enable_req_dns && dns != 0) {
4637+
setdns4(ifp, &dns);
46294638
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_DNS, &dns);
4639+
}
46304640
if (ifp->enable_req_sntp && sntp != 0)
46314641
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_SNTP, &sntp);
46324642
} else if (ifp->state == MG_TCPIP_STATE_READY && ifp->ip == ip) { // renew
@@ -5287,6 +5297,7 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) {
52875297
void mg_tcpip_free(struct mg_tcpip_if *ifp) {
52885298
mg_free(ifp->recv_queue.buf);
52895299
mg_free(ifp->tx.buf);
5300+
mg_free(ifp->dns4_url);
52905301
}
52915302

52925303
static void send_syn(struct mg_connection *c) {
@@ -5453,6 +5464,21 @@ void mg_multicast_add(struct mg_connection *c, char *ip) {
54535464
c->mgr->ifp->update_mac_hash_table = true; // mark dirty
54545465
}
54555466

5467+
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc);
5468+
5469+
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip) {
5470+
struct mg_dns *dnsc;
5471+
mg_free(ifp->dns4_url);
5472+
ifp->dns4_url = mg_mprintf("udp://%M:53", mg_print_ip4, ip);
5473+
dnsc = &ifp->mgr->dns4;
5474+
dnsc->url = (const char *) ifp->dns4_url;
5475+
MG_DEBUG(("Set DNS URL to %s", dnsc->url));
5476+
if (ifp->mgr->use_dns6) return;
5477+
if (dnsc->c != NULL) mg_close_conn(dnsc->c);
5478+
if (!mg_dnsc_init(ifp->mgr, dnsc)) // create DNS connection
5479+
MG_ERROR(("DNS connection creation failed"));
5480+
}
5481+
54565482
#endif // MG_ENABLE_TCPIP
54575483

54585484
#ifdef MG_ENABLE_LINES
@@ -8600,10 +8626,8 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
86008626
static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) {
86018627
uint64_t *expiration_time = (uint64_t *) c->data;
86028628
if (ev == MG_EV_OPEN) {
8603-
MG_INFO(("%lu PFN OPEN", c->id));
86048629
*expiration_time = mg_millis() + 3000; // Store expiration time in 3s
86058630
} else if (ev == MG_EV_CONNECT) {
8606-
MG_INFO(("%lu PFN CONNECT, sending request", c->id));
86078631
mg_sntp_request(c);
86088632
} else if (ev == MG_EV_READ) {
86098633
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);

mongoose.h

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

30043004
// Internal state, user can use it but should not change it
30053005
uint8_t gwmac[6]; // Router's MAC
3006+
char *dns4_url; // DNS server URL
30063007
uint64_t now; // Current time
30073008
uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state
30083009
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
@@ -407,6 +407,8 @@ static void rx_icmp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
407407
}
408408
}
409409

410+
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip);
411+
410412
static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
411413
uint32_t ip = 0, gw = 0, mask = 0, lease = 0, dns = 0, sntp = 0;
412414
uint8_t msgtype = 0, state = ifp->state;
@@ -455,8 +457,10 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
455457
ifp->state = MG_TCPIP_STATE_IP; // BOUND state
456458
mg_random(&rand, sizeof(rand));
457459
srand((unsigned int) (rand + mg_millis()));
458-
if (ifp->enable_req_dns && dns != 0)
460+
if (ifp->enable_req_dns && dns != 0) {
461+
setdns4(ifp, &dns);
459462
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_DNS, &dns);
463+
}
460464
if (ifp->enable_req_sntp && sntp != 0)
461465
mg_tcpip_call(ifp, MG_TCPIP_EV_DHCP_SNTP, &sntp);
462466
} else if (ifp->state == MG_TCPIP_STATE_READY && ifp->ip == ip) { // renew
@@ -1117,6 +1121,7 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) {
11171121
void mg_tcpip_free(struct mg_tcpip_if *ifp) {
11181122
mg_free(ifp->recv_queue.buf);
11191123
mg_free(ifp->tx.buf);
1124+
mg_free(ifp->dns4_url);
11201125
}
11211126

11221127
static void send_syn(struct mg_connection *c) {
@@ -1283,4 +1288,19 @@ void mg_multicast_add(struct mg_connection *c, char *ip) {
12831288
c->mgr->ifp->update_mac_hash_table = true; // mark dirty
12841289
}
12851290

1291+
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc);
1292+
1293+
static void setdns4(struct mg_tcpip_if *ifp, uint32_t *ip) {
1294+
struct mg_dns *dnsc;
1295+
mg_free(ifp->dns4_url);
1296+
ifp->dns4_url = mg_mprintf("udp://%M:53", mg_print_ip4, ip);
1297+
dnsc = &ifp->mgr->dns4;
1298+
dnsc->url = (const char *) ifp->dns4_url;
1299+
MG_DEBUG(("Set DNS URL to %s", dnsc->url));
1300+
if (ifp->mgr->use_dns6) return;
1301+
if (dnsc->c != NULL) mg_close_conn(dnsc->c);
1302+
if (!mg_dnsc_init(ifp->mgr, dnsc)) // create DNS connection
1303+
MG_ERROR(("DNS connection creation failed"));
1304+
}
1305+
12861306
#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)