Skip to content
Browse files

highly experimental dns_server

  • Loading branch information...
1 parent d8365ad commit 5f054c53b890277330248e0a520eead24eee888a @astro astro committed Jan 25, 2011
Showing with 58 additions and 0 deletions.
  1. +58 −0 dns_server.js
View
58 dns_server.js
@@ -0,0 +1,58 @@
+/**
+ * ndns appears very buggy. commands to test:
+ *
+ * dig foo @localhost -p 5353
+ * dig ANY foo @localhost -p 5353
+ * dig AAAA foo @localhost -p 5353
+ * dig NS foo @localhost -p 5353
+ *
+ * also, try exposing it to mdns traffic on port 5353 :-(
+ */
+var sys = require('sys');
+var ndns = require('ndns');
+
+var server = ndns.createServer ('udp4');
+
+var TTL = 60;
+var NAMESERVER_A = '10.0.0.1';
+var NAMESERVER_AAAA = 'fec0::1';
+var TARGETSERVER_A = '10.0.0.1';
+var TARGETSERVER_AAAA = 'fec0::1';
+
+server.bind (5353);
+server.on ('request', function onRequest (req, res) {
+console.log({header:req.header,question:req.question});
+ res.header = req.header;
+ res.question = req.question;
+ res.header.qr = 1; /* is response */
+ res.header.ancount = 0;
+ res.header.aa = 1; /* authoritative */
+
+ req.question.forEach(function(q) {
+ switch(q.type) {
+ case ndns.ns_t.a:
+ res.addRR(q.name, ndns.ns_t.a, ndns.ns_c.in, TTL, TARGETSERVER_A);
+ res.header.ancount++;
+ break;
+ case ndns.ns_t.aaaa:
+ res.addRR(q.name, ndns.ns_t.aaaa, ndns.ns_c.in, TTL, TARGETSERVER_AAAA);
+ res.header.ancount++;
+ break;
+ case ndns.ns_t.any:
+ res.addRR(q.name, ndns.ns_t.a, ndns.ns_c.in, TTL, TARGETSERVER_A);
+ res.addRR(q.name, ndns.ns_t.aaaa, ndns.ns_c.in, TTL, TARGETSERVER_AAAA);
+ res.header.ancount += 2;
+ break;
+ case ndns.ns_t.ns:
+ res.addRR(q.name, ndns.ns_t.ns, ndns.ns_c.in, TTL, 'ns.' + q.name);
+ /* these two may need to go into the additional section */
+ res.addRR('ns.' + q.name, ndns.ns_t.a, ndns.ns_c.in, TTL, NAMESERVER_A);
+ res.addRR('ns.' + q.name, ndns.ns_t.aaaa, ndns.ns_c.in, TTL, NAMESERVER_AAAA);
+ res.header.ancount += 3;
+ break;
+ }
+ });
+
+ res.send();
+});
+

0 comments on commit 5f054c5

Please sign in to comment.
Something went wrong with that request. Please try again.