summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2010-04-07 15:37:08 -0700
committerRyan Dahl <ry@tinyclouds.org>2010-04-07 15:37:08 -0700
commitf13e2f96e40ecdd6e76854508c3b0cad6bcd1a8d (patch)
tree3a50e45c9c58da5a9180a02810c05d878dad174c
parentf56ff0de9280a1371a319e368380043bd8c8fe6c (diff)
downloadnode-new-f13e2f96e40ecdd6e76854508c3b0cad6bcd1a8d.tar.gz
Add a simple c-ares test, dns_cares.lookup() for easy resolv
-rw-r--r--lib/dns_cares.js23
-rw-r--r--test/simple/test-c-ares.js28
2 files changed, 51 insertions, 0 deletions
diff --git a/lib/dns_cares.js b/lib/dns_cares.js
index f51702b440..fbb10d3fb2 100644
--- a/lib/dns_cares.js
+++ b/lib/dns_cares.js
@@ -1,4 +1,5 @@
var dns = process.binding('cares');
+var sys = require('sys'); // TODO remove me
var watchers = {};
@@ -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) };
diff --git a/test/simple/test-c-ares.js b/test/simple/test-c-ares.js
new file mode 100644
index 0000000000..683babc474
--- /dev/null
+++ b/test/simple/test-c-ares.js
@@ -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);
+});