Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial version

  • Loading branch information...
commit 2ff35aef2de7865ccebe8363dac669aa8b97042d 0 parents
Mark Willis authored
Showing with 194 additions and 0 deletions.
  1. +43 −0 README.md
  2. +11 −0 index.js
  3. +52 −0 lib/cidr.js
  4. +16 −0 package.json
  5. +72 −0 test/index.js
43 README.md
@@ -0,0 +1,43 @@
+ipFunctions
+=======
+
+Providing functionality to check ip validation and convert CIDR notation to IP ranges. also allowing for single ips to be converted to long and back again.
+
+example
+=======
+
+
+``` js
+var cidr = require('./lib/cidr');
+
+console.log('ip2long: 127.0.0.1 -> ' +cidr.ip2long('127.0.0.1'));
+console.log('long2ip: 2130706433 -> ' +cidr.long2ip(2130706433));
+console.log('incorrect IP: ' +cidr.ip2long('test'));
+
+var range = cidr.cidrToRange('127.0.0.1/16');
+console.log('CIDR Range: 127.0.0.1/16 -> ' + range[0] + ' :: ' + range[1]);
+```
+
+functions
+=======
+``` js
+ip2long(); // convert ip String to Number (returns false if invalid)
+long2ip(); // convert ip Number to String (returns false if invalid)
+cidrToRange(); // convert CIDR range to 2 item array (lowest IP and highest IP) (returns false if invalid)
+checkIp(); // check if IP string is valid
+checkCIDR(); // check if CIDR string is valid
+```
+
+install
+=======
+
+With [npm](http://npmjs.org)
+
+```
+npm install -g ipFunctions
+```
+
+license
+=======
+
+MIT
11 index.js
@@ -0,0 +1,11 @@
+var cidr = require('./lib/cidr');
+
+
+console.log('ip2long: 127.0.0.1 -> ' +cidr.ip2long('127.0.0.1'));
+console.log('long2ip: 2130706433 -> ' +cidr.long2ip(2130706433));
+console.log('incorrect IP: ' +cidr.ip2long('test'));
+
+var range = cidr.cidrToRange('127.0.0.1/16');
+console.log('CIDR Range: 127.0.0.1/16 -> ' + range[0] + ' :: ' + range[1]);
+
+
52 lib/cidr.js
@@ -0,0 +1,52 @@
+exports.ip2long = function (ip_address) {
+ if(!exports.checkIp(ip_address)) return false; // invalid IP address
+ var parts = ip_address.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
+ return parts ? parts[1] * 16777216 + parts[2] * 65536 + parts[3] * 256 + parts[4] * 1 : false;
+};
+
+exports.long2ip = function (proper_address) {
+ // Converts an (IPv4) Internet network address into a string in Internet standard dotted format
+ //
+ // version: 1109.2015
+ // discuss at: http://phpjs.org/functions/long2ip
+ // + original by: Waldo Malqui Silva
+ // * example 1: long2ip( 3221234342 );
+ // * returns 1: '192.0.34.166'
+ var output = false;
+ if (!isNaN(proper_address) && (proper_address >= 0 || proper_address <= 4294967295)) {
+ output = Math.floor(proper_address / Math.pow(256, 3)) + '.' + Math.floor((proper_address % Math.pow(256, 3)) / Math.pow(256, 2)) + '.' + Math.floor(((proper_address % Math.pow(256, 3)) % Math.pow(256, 2)) / Math.pow(256, 1)) + '.' + Math.floor((((proper_address % Math.pow(256, 3)) % Math.pow(256, 2)) % Math.pow(256, 1)) / Math.pow(256, 0));
+ }
+ return output;
+};
+
+
+
+exports.cidrToRange = function (cidr) {
+ var range = [2];
+ cidr = cidr.split('/');
+ if(cidr.length == 1) return false;
+ if(!exports.checkIp(cidr[0])) return false; // invalid IP address
+ if(cidr[1] < 0 || cidr[1] > 32 || isNaN(cidr[1])) return false; // invalid cidr address
+
+ if(cidr[1] == 32) { // we are a single IP - do no calc
+ return [cidr[0],cidr[0]];
+ }
+
+ var longStart = exports.ip2long(cidr[0]);
+
+ range[0] = exports.long2ip((longStart) & ((-1 << (32 - cidr[1]))));
+ range[1] = exports.long2ip((longStart) + Math.pow(2, (32 - cidr[1])) - 1);
+ return range;
+};
+
+exports.checkIp = function (ip) {
+ if(typeof ip !== 'string') return false; // only do strings
+ var matches = ip.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
+ return (matches ? true : false);
+};
+
+exports.checkCIDR = function (cidr) {
+ if(typeof ip !== 'string') return false; // only do strings
+ var matches = cidr.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}$/);
+ return (matches ? true : false);
+};
16 package.json
@@ -0,0 +1,16 @@
+{
+ "name": "ipFunctions",
+ "description": "Providing functionality to check ip validation and convert CIDR notation to IP ranges. also allowing for single ips to be converted to long and back again",
+ "version": "0.0.1",
+ "repository": {
+ "url": "git://github.com/markwillis82/ipFunctions.git"
+ },
+ "main": "lib/cidr.js",
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "tap": "~0.2.5"
+ }
+}
72 test/index.js
@@ -0,0 +1,72 @@
+var tap = require("tap"),
+ test = tap.test,
+ cidr;
+
+test("load module", function (t) {
+ cidr = require('../lib/cidr');
+ t.ok(cidr, "object loaded");
+ t.end();
+});
+
+test("test ip2long", function (t) {
+ t.equal(cidr.ip2long('127.0.0.1'), 2130706433);
+
+ t.equal(cidr.ip2long('255.255.255.255'), 4294967295);
+
+ t.equal(cidr.ip2long('0.0.0.0'), 0);
+
+ t.equal(cidr.ip2long('test'), false);
+
+ t.end();
+});
+
+test("test long2ip", function (t) {
+ t.equal(cidr.long2ip(2130706433), '127.0.0.1');
+ t.equal(cidr.long2ip('2130706433'), '127.0.0.1');
+
+ t.equal(cidr.long2ip(0), '0.0.0.0');
+ t.equal(cidr.long2ip(4294967295), '255.255.255.255');
+
+ t.equal(cidr.long2ip('test'), false);
+
+ t.end();
+});
+
+
+
+test("test cidrToRange", function (t) {
+ var goodRange = cidr.cidrToRange('127.0.0.1/16');
+ t.equal(goodRange[0], '127.0.0.0');
+ t.equal(goodRange[1], '127.1.0.0');
+
+ var singleIP = cidr.cidrToRange('127.0.0.1/32');
+ t.equal(singleIP[0], '127.0.0.1');
+ t.equal(singleIP[1], '127.0.0.1');
+
+ var highIP = cidr.cidrToRange('255.255.255.255/32');
+ t.equal(highIP[0], '255.255.255.255');
+ t.equal(highIP[1], '255.255.255.255');
+
+ var lowIP = cidr.cidrToRange('0.0.0.0/32');
+ t.equal(lowIP[0], '0.0.0.0');
+ t.equal(lowIP[1], '0.0.0.0');
+
+ var allIP = cidr.cidrToRange('0.0.0.0/0');
+ t.equal(allIP[0], '0.0.0.0');
+ t.equal(allIP[1], '255.255.255.255');
+
+
+ var badIp = cidr.cidrToRange('broken');
+ t.equal(badIp, false);
+
+ var badIp2 = cidr.cidrToRange('broken/test');
+ t.equal(badIp2, false);
+
+ var badIp3 = cidr.cidrToRange('192.168.10.55/50');
+ t.equal(badIp3, false);
+
+ var badIp4 = cidr.cidrToRange('192.168.10.55/text');
+ t.equal(badIp4, false);
+
+ t.end();
+});
Please sign in to comment.
Something went wrong with that request. Please try again.