Node by example

Node by example: 6. DNS Resolving

6. DNS Resolving

The complete source code can be downloaded here: http://github.com/Hendrik/node-by-example

The DNS module provides DNS resolving related features, such as IPv4 & IPv6 resolving, specific record types resolving and reverse resolving. [1]
Please note: even though the documentation mentions the lookup capability of MX record types, this is actually not supported at this time. (node v0.1.91)

Use

require('dns')

to access the features of this module.

 

The following is an example which resolves 'google.com' and then reverse resolves the returned IP addresses.

// taken from http://nodejs.org/api.html#dns-204

var sys = require("sys"),
    dns = require("dns");

dns.resolve4("google.com", function(err, addresses) {
  if (err) throw err;

  sys.puts("addresses: " + JSON.stringify(addresses));

  for (var i = 0; i < addresses.length; i++) {
    var a = addresses[i];
    dns.reverse(a, function (err, domains) {
      if (err) {
        sys.puts("reverse for " + a + " failed: " + err.message);
      } else {
        sys.puts("reverse for " + a + ": " + JSON.stringify(domains));
      }
    });
  }
});

output:

addresses: ["72.14.213.103","72.14.213.104","72.14.213.106","72.14.213.147","72.14.213.105","72.14.213.99"]
reverse for 72.14.213.99: ["pv-in-f103.1e100.net"]
reverse for 72.14.213.99: ["pv-in-f104.1e100.net"]
reverse for 72.14.213.99: ["pv-in-f106.1e100.net"]
reverse for 72.14.213.99: ["pv-in-f147.1e100.net"]
reverse for 72.14.213.99: ["pv-in-f105.1e100.net"]
reverse for 72.14.213.99: ["pv-in-f99.1e100.net"]

 

dns.resolve4(domain, callback) resolves a domain based on IPv4 queries.
You can use IPv6 queries with dns.resolve6(domain, callback).

dns.reverse(ip, callback) reverse resolves an IP address to an array of domain names.

Each DNS query can return an error code: dns.TEMPFAIL: timeout, SERVFAIL or similar. dns.PROTOCOL: got garbled reply. dns.NXDOMAIN: domain does not exists. dns.NODATA: domain exists but no data of reqd type. dns.NOMEM: out of memory while processing. dns.BADQUERY: the query is malformed.

The following is an example that resolves specific record types for the provided domain and checks for the dns.NODATA error code:

var sys = require("sys"),
    dns = require("dns"),
 domain = process.ARGV[2];

if (!domain)
  return sys.puts("Usage: node " + __filename.replace(__dirname + "/", "") + " domainname");

// resolve A entries
dns.resolve(domain, 'A', function(err, addresses) {
  if (err) {
    if (err.errno == dns.NODATA) sys.puts("No A entry for " + domain);
    else throw err;
  } else {
    sys.puts("addresses: " + JSON.stringify(addresses));
  }
});

// resolve TXT entries
dns.resolve(domain, 'TXT', function(err, addresses) {
  if (err) {
    if (err.errno == dns.NODATA) sys.puts("No TXT entry for " + domain);
    else throw err;
  } else {
    sys.puts("addresses: " + JSON.stringify(addresses));
  }
});

Run via:

node dns_2.js google.com

output:

addresses: ["72.14.213.99","72.14.213.103","72.14.213.105","72.14.213.106","72.14.213.147","72.14.213.104"]
addresses: ["v=spf1 include:_netblocks.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all"]

 

[1] http://nodejs.org/api.html#dns-204

Comments

Eddie Du wrote on January 30, 2011:
Uh oh! Looks like the "a" in your reverse code is not scoped right. Maybe I suggest perhaps: for (var i = 0; i < addresses.length; i++) { var a = addresses[i]; (function(a){ dns.reverse(a, function (err, domains) { if (err) { sys.puts("reverse for " + a + " failed: " + err.message); } else { sys.puts("reverse for " + a + ": " + JSON.stringify(domains)); } }) })(a); } or function dnsReverse(a) { dns.reverse(a, function (err, domains) { if (err) { sys.puts("reverse for " + a + " failed: " + err.message); } else { sys.puts("reverse for " + a + ": " + JSON.stringify(domains)); } }); } for (var i = 0; i < addresses.length; i++) { var a = addresses[i]; dnsReverse(a); }