From c8f9728de7510a539b4460b5280f90934b2fa9a5 Mon Sep 17 00:00:00 2001 From: Paul Querna Date: Thu, 16 Sep 2010 14:58:04 -0700 Subject: [PATCH] Move dns.isIP to net.isIP Add tests and docs. --- doc/api.markdown | 19 +++++++++++++++++++ lib/dgram.js | 2 +- lib/dns.js | 7 +++---- lib/net.js | 15 +++++++++++++++ src/node_cares.cc | 27 --------------------------- test/simple/test-net-isip.js | 22 ++++++++++++++++++++++ 6 files changed, 60 insertions(+), 32 deletions(-) create mode 100644 test/simple/test-net-isip.js diff --git a/doc/api.markdown b/doc/api.markdown index f2933ed3ac..28e8476f84 100644 --- a/doc/api.markdown +++ b/doc/api.markdown @@ -2228,6 +2228,25 @@ Set this property to reject connections when the server's connection count gets The number of concurrent connections on the server. +## net.isIP + +### net.isIP(input) + +Tests if input is an IP address. Returns 0 for invalid strings, +returns 4 for IP version 4 addresses, and returns 6 for IP version 6 addresses. + + +### net.isIPv4(input) + +Returns true if input is a version 4 IP address, otherwise returns false. + + +### net.isIPv6(input) + +Returns true if input is a version 6 IP address, otherwise returns false. + + + ## net.Stream diff --git a/lib/dgram.js b/lib/dgram.js index c6eab71878..7325b67bb1 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -175,7 +175,7 @@ Socket.prototype.send = function(buffer, offset, length) { throw new Error(this.type + " sockets must send to port, address"); } - if (dns.isIP(arguments[4])) { + if (binding.isIP(arguments[4])) { self.sendto(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]); } else { diff --git a/lib/dns.js b/lib/dns.js index 532f15abb8..8f9ce768be 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -1,4 +1,5 @@ var dns = process.binding('cares'); +var net = process.binding('net'); var watchers = {}; @@ -71,8 +72,6 @@ var channel = new dns.Channel({SOCK_STATE_CB: function (socket, read, write) { updateTimer(); }}); -exports.isIP = dns.isIP; - exports.resolve = function (domain, type_, callback_) { var type, callback; if (typeof(type_) == 'string') { @@ -133,7 +132,7 @@ exports.lookup = function (domain, family, callback) { callback(null, null, family === 6 ? 6 : 4); return; } - var matchedFamily = dns.isIP(domain); + var matchedFamily = net.isIP(domain); if (matchedFamily) { callback(null, domain, matchedFamily); } else { @@ -149,7 +148,7 @@ exports.lookup = function (domain, family, callback) { var af = familyToSym(family); channel.getHostByName(domain, af, function (err, domains) { if (!err && domains && domains.length) { - if (family !== dns.isIP(domains[0])) { + if (family !== net.isIP(domains[0])) { callback(new Error('not found'), []); } else { callback(null, domains[0], family); diff --git a/lib/net.js b/lib/net.js index 7fca4da155..be862dcec9 100644 --- a/lib/net.js +++ b/lib/net.js @@ -219,6 +219,21 @@ var ioWatchers = new FreeList("iowatcher", 100, function () { return new IOWatcher(); }); +exports.isIP = binding.isIP; + +exports.isIPv4 = function(input) { + if (binding.isIP(input) === 4) { + return true; + } + return false; +}; + +exports.isIPv6 = function(input) { + if (binding.isIP(input) === 6) { + return true; + } + return false; +}; // Allocated on demand. var pool = null; diff --git a/src/node_cares.cc b/src/node_cares.cc index b110a454b4..4bd07f8d67 100644 --- a/src/node_cares.cc +++ b/src/node_cares.cc @@ -22,31 +22,6 @@ namespace node { using namespace v8; -static Handle 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 target); @@ -139,8 +114,6 @@ void Cares::Initialize(Handle 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); } diff --git a/test/simple/test-net-isip.js b/test/simple/test-net-isip.js new file mode 100644 index 0000000000..1ae5806e09 --- /dev/null +++ b/test/simple/test-net-isip.js @@ -0,0 +1,22 @@ +common = require("../common"); +assert = common.assert +net = require("net"); + +assert.equal(net.isIP("127.0.0.1"), 4); +assert.equal(net.isIP("x127.0.0.1"), 0); +assert.equal(net.isIP("example.com"), 0); +assert.equal(net.isIP("0000:0000:0000:0000:0000:0000:0000:0000"), 6); +assert.equal(net.isIP("0000:0000:0000:0000:0000:0000:0000:0000::0000"), 0); +assert.equal(net.isIP("1050:0:0:0:5:600:300c:326b"), 6); +assert.equal(net.isIP("2001:252:0:1::2008:6"), 6); +assert.equal(net.isIP("2001:dead:beef:1::2008:6"), 6); +assert.equal(net.isIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), 6); + +assert.equal(net.isIPv4("127.0.0.1"), true); +assert.equal(net.isIPv4("example.com"), false); +assert.equal(net.isIPv4("2001:252:0:1::2008:6"), false); + +assert.equal(net.isIPv6("127.0.0.1"), false); +assert.equal(net.isIPv4("example.com"), false); +assert.equal(net.isIPv6("2001:252:0:1::2008:6"), true); +