Skip to content

Commit

Permalink
Add a simple c-ares test, dns_cares.lookup() for easy resolv
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Apr 7, 2010
1 parent f56ff0d commit f13e2f9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/dns_cares.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var dns = process.binding('cares');
var sys = require('sys'); // TODO remove me


var watchers = {};
Expand Down Expand Up @@ -88,6 +89,28 @@ exports.resolve = function (domain, type_, callback_) {
}


exports.getHostByName = function (domain, callback) {
channel.getHostByName(domain, dns.AF_INET, callback);
};

// Easy DNS A/AAAA look up
exports.lookup = function (domain, callback) {
channel.getHostByName(domain, dns.AF_INET, function (err, domains4) {
if (domains4 && domains4.length) {
callback(null, domains4[0], 4);
} else {
channel.getHostByName(domain, dns.AF_INET6, function (err, domains6) {
if (domains6 && domains6.length) {
callback(null, domains6[0], 6);
} else {
callback(err, []);
}
});
}
});
};


exports.resolve4 = function(domain, callback) { channel.query(domain, dns.A, callback) };
exports.resolve6 = function(domain, callback) { channel.query(domain, dns.AAAA, callback) };
exports.resolveTxt = function(domain, callback) { channel.query(domain, dns.TXT, callback) };
Expand Down
28 changes: 28 additions & 0 deletions test/simple/test-c-ares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require('../common');

var dns = require("dns_cares");


// Try resolution without callback

dns.getHostByName('localhost', function (error, result) {
p(result);
assert.deepEqual(['127.0.0.1'], result);
});

dns.getHostByName('127.0.0.1', function (error, result) {
p(result);
assert.deepEqual(['127.0.0.1'], result);
});

dns.lookup('127.0.0.1', function (error, result, ipVersion) {
assert.deepEqual('127.0.0.1', result);
assert.equal(4, ipVersion);
});

dns.lookup('ipv6.google.com', function (error, result, ipVersion) {
if (error) throw error;
p(arguments);
//assert.equal('string', typeof result);
assert.equal(6, ipVersion);
});

0 comments on commit f13e2f9

Please sign in to comment.