Check for ip address in dns_cares.lookup

This commit is contained in:
Ryan Dahl 2010-04-07 16:04:33 -07:00
parent f13e2f96e4
commit 8f671041a5
3 changed files with 68 additions and 18 deletions

View File

@ -95,19 +95,30 @@ exports.getHostByName = function (domain, 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, []);
}
});
}
});
var addressType = dns.isIP(domain);
if (addressType) {
process.nextTick(function () {
callback(null, domain, addressType);
});
} else {
sys.puts('AF_INET look up ' + domain);
channel.getHostByName(domain, dns.AF_INET, function (err, domains4) {
sys.puts('AF_INET result ' + domain + ' ' + sys.inspect(domains4));
if (domains4 && domains4.length) {
callback(null, domains4[0], 4);
} else {
sys.puts('AF_INET6 look up ' + domain);
channel.getHostByName(domain, dns.AF_INET6, function (err, domains6) {
sys.puts('AF_INET6 result ' + domain + ' ' + sys.inspect(domains6));
if (domains6 && domains6.length) {
callback(null, domains6[0], 6);
} else {
callback(err, []);
}
});
}
});
}
};

View File

@ -13,6 +13,31 @@ namespace node {
using namespace v8;
static Handle<Value> IsIP(const Arguments& args) {
HandleScope scope;
if (!args[0]->IsString()) {
return scope.Close(Integer::New(4));
}
String::Utf8Value s(args[0]->ToString());
// avoiding buffer overflows in the following strcat
// 2001:0db8:85a3:08d3:1319:8a2e:0370:7334
// 39 = max ipv6 address.
if (s.length() > INET6_ADDRSTRLEN) {
return scope.Close(Integer::New(0));
}
struct sockaddr_in6 a;
if (inet_pton(AF_INET, *s, &(a.sin6_addr)) > 0) return scope.Close(Integer::New(4));
if (inet_pton(AF_INET6, *s, &(a.sin6_addr)) > 0) return scope.Close(Integer::New(6));
return scope.Close(Integer::New(0));
}
class Channel : public ObjectWrap {
public:
static void Initialize(Handle<Object> target);
@ -104,6 +129,8 @@ void Cares::Initialize(Handle<Object> target) {
target->Set(String::NewSymbol("EREFUSED"), Integer::New(ARES_EREFUSED));
target->Set(String::NewSymbol("SERVFAIL"), Integer::New(ARES_ESERVFAIL));
NODE_SET_METHOD(target, "isIP", IsIP);
Channel::Initialize(target);
}
@ -657,4 +684,6 @@ void Channel::SockStateCb(void *data, int sock, int read, int write) {
}
} // namespace node

View File

@ -15,14 +15,24 @@ dns.getHostByName('127.0.0.1', function (error, 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(null, function (error, result, addressType) {
assert.equal(null, result);
assert.equal(4, addressType);
});
dns.lookup('ipv6.google.com', function (error, result, ipVersion) {
dns.lookup('127.0.0.1', function (error, result, addressType) {
assert.equal('127.0.0.1', result);
assert.equal(4, addressType);
});
dns.lookup('::1', function (error, result, addressType) {
assert.equal('::1', result);
assert.equal(6, addressType);
});
dns.lookup('ipv6.google.com', function (error, result, addressType) {
if (error) throw error;
p(arguments);
//assert.equal('string', typeof result);
assert.equal(6, ipVersion);
assert.equal(6, addressType);
});