Captive DNS Server Example


Let’s check out a DNS server example working with Mongoose Embedded Web Sever.


In this case, it is a DNS server intended to be used to build a captive portal, i.e. where the user needs to login before using public WiFi.


Such a server responds to all queries with its own IP address and an HTTP server running alongside serves the portal page to the user.

Building a DNS server with Mongoose is easy: mg_bind to a udp:// port, call mg_set_protocol_dns on the connection and handle incoming MG_DNS_MESSAGE events.


Our captive_dns_server example puts this all together:

$ ./captive_dns_server
Listening on 'udp://:5533'

ev_handler           Q type 1 name


$ dig @localhost -p 5533

; <<>> DiG 9.9.5-11ubuntu1.3-Ubuntu <<>> @localhost -p 5533
; (1 server found)
;; global options: +cmd
;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44936
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITHY: 0, ADDITIONAL: 0

;                   IN      A

;; ANSWER SECTION:            10      IN      A

;; Query time: 0 msec
;; WHEN: Mon Apr 18 19:56:51 IST 2016
;; MSG SIZE  rcvd: 56


  Download Mongoose Embedded Web Server Captive DNS Server Example Code

Contact Us
Have questions about Cesanta, our licensing, support services or anything else? Let us know and we'll get back to you.