Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Captive DNS Server Example (Mongoose More Than an Embedded Web Server)

DZone's Guide to

Captive DNS Server Example (Mongoose More Than an Embedded Web Server)

Mongoose is simple and solid. It's one of the easiest to use embeddable servers around. It's usable in a wide range of applications, and here's yet another one.

· Web Dev Zone
Free Resource

Get deep insight into Node.js applications with real-time metrics, CPU profiling, and heap snapshots with N|Solid from NodeSource. Learn more.

This week we continue our series of examples for Mongoose Embedded Web Server with an example of a DNS server.

In this case, it is a DNS server intended to be used to build a captive portal, i.e. where the user needs to log in 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_MESSAGEevents.

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

Follow the Example

To follow the example download Mongoose Embedded Webserver. It's open source under GPLv2, so free of charge for non-commercial use. 

Download Mongoose

Node.js application metrics sent directly to any statsd-compliant system. Get N|Solid

Topics:
mongoose ,dns ,embedded development

Published at DZone with permission of Deomid Ryabkov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}