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 example.org

---

$ dig @localhost -p 5533 example.org

; <<>> DiG 9.9.5-11ubuntu1.3-Ubuntu <<>> @localhost -p 5533 example.org
; (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

;; QUESTION SECTION:
;example.org.                   IN      A

;; ANSWER SECTION:
example.org.            10      IN      A       127.0.0.1

;; Query time: 0 msec
;; SERVER: 127.0.0.1#5533(127.0.0.1)
;; 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.
GET IN TOUCH