Move dns.isIP to net.isIP
authorPaul Querna <pquerna@apache.org>
Thu, 16 Sep 2010 21:58:04 +0000 (14:58 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 17 Sep 2010 00:27:10 +0000 (17:27 -0700)
Add tests and docs.

doc/api.markdown
lib/dgram.js
lib/dns.js
lib/net.js
src/node_cares.cc
test/simple/test-net-isip.js [new file with mode: 0644]

index f2933ed..28e8476 100644 (file)
@@ -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
 
index c6eab71..7325b67 100644 (file)
@@ -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 {
index 532f15a..8f9ce76 100644 (file)
@@ -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);
index 7fca4da..be862dc 100644 (file)
@@ -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;
index b110a45..4bd07f8 100644 (file)
@@ -22,31 +22,6 @@ 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);
@@ -139,8 +114,6 @@ 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);
 }
 
diff --git a/test/simple/test-net-isip.js b/test/simple/test-net-isip.js
new file mode 100644 (file)
index 0000000..1ae5806
--- /dev/null
@@ -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);
+