Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Willis
committed
Mar 15, 2013
0 parents
commit 2ff35ae
Showing
5 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |