Socket
Socket
Sign inDemoInstall

ip

Package Overview
Dependencies
0
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.0 to 0.3.0

57

lib/ip.js

@@ -128,2 +128,59 @@ var ip = exports,

ip.cidr = function cidr(cidrString) {
var cidrParts = cidrString.split('/');
if (cidrParts.length != 2)
throw new Error('invalid CIDR subnet: ' + addr);
var addr = cidrParts[0];
var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));
return ip.mask(addr, mask);
}
ip.subnet = function subnet(addr, mask) {
var networkAddress = ip.toLong(ip.mask(addr, mask));
// Calculate the mask's length.
var maskBuffer = ip.toBuffer(mask);
var maskLength = 0;
for (var i = 0; i < maskBuffer.length; i++) {
if (maskBuffer[i] == 0xff) {
maskLength += 8;
} else {
var octet = maskBuffer[i] & 0xff;
while (octet) {
octet = (octet << 1) & 0xff;
maskLength++;
}
}
}
var numberOfAddresses = Math.pow(2, 32 - maskLength);
return {
networkAddress: ip.fromLong(networkAddress),
firstAddress: ip.fromLong(networkAddress + 1),
lastAddress: ip.fromLong(networkAddress + numberOfAddresses - 2),
broadcastAddress: ip.fromLong(networkAddress + numberOfAddresses - 1),
subnetMask: mask,
subnetMaskLength: maskLength,
numHosts: numberOfAddresses - 2,
length: numberOfAddresses
};
}
ip.cidrSubnet = function cidrSubnet(cidrString) {
var cidrParts = cidrString.split('/');
if (cidrParts.length !== 2)
throw new Error('invalid CIDR subnet: ' + addr);
var addr = cidrParts[0];
var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));
return ip.subnet(addr, mask);
}
ip.not = function not(addr) {

@@ -130,0 +187,0 @@ var buff = ip.toBuffer(addr);

2

package.json
{
"name": "ip",
"version": "0.2.0",
"version": "0.3.0",
"author": "Fedor Indutny <fedor@indutny.com>",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/indutny/node-ip",

@@ -16,2 +16,3 @@ # IP

ip.mask('192.168.1.134', '255.255.255.0') // 192.168.1.0
ip.cidr('192.168.1.134/26') // 192.168.1.128
ip.not('255.255.255.0') // 0.0.0.255

@@ -27,2 +28,14 @@ ip.or('192.168.1.134', '0.0.0.255') // 192.168.1.255

// subnet information
ip.subnet('192.168.1.134', '255.255.255.192')
// { networkAddress: '192.168.1.128',
// firstAddress: '192.168.1.129',
// lastAddress: '192.168.1.190',
// broadcastAddress: '192.168.1.191',
// subnetMask: '255.255.255.192',
// subnetMaskLength: 26,
// numHosts: 62,
// length: 64 }
ip.cidr('192.168.1.134/26')
// Same as previous.

@@ -29,0 +42,0 @@ // ipv4 long conversion

@@ -82,2 +82,83 @@ var ip = require('..'),

describe('subnet() method', function() {
// Test cases calculated with http://www.subnet-calculator.com/
var ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.192');
it('should compute ipv4 network address', function() {
assert.equal(ipv4Subnet.networkAddress, '192.168.1.128');
});
it('should compute ipv4 network\'s first address', function() {
assert.equal(ipv4Subnet.firstAddress, '192.168.1.129');
});
it('should compute ipv4 network\'s last address', function() {
assert.equal(ipv4Subnet.lastAddress, '192.168.1.190');
});
it('should compute ipv4 broadcast address', function() {
assert.equal(ipv4Subnet.broadcastAddress, '192.168.1.191');
});
it('should compute ipv4 subnet number of addresses', function() {
assert.equal(ipv4Subnet.length, 64);
});
it('should compute ipv4 subnet number of addressable hosts', function() {
assert.equal(ipv4Subnet.numHosts, 62);
});
it('should compute ipv4 subnet mask', function() {
assert.equal(ipv4Subnet.subnetMask, '255.255.255.192');
});
it('should compute ipv4 subnet mask\'s length', function() {
assert.equal(ipv4Subnet.subnetMaskLength, 26);
});
});
describe('cidrSubnet() method', function() {
// Test cases calculated with http://www.subnet-calculator.com/
var ipv4Subnet = ip.cidrSubnet('192.168.1.134/26');
it('should compute an ipv4 network address', function() {
assert.equal(ipv4Subnet.networkAddress, '192.168.1.128');
});
it('should compute an ipv4 network\'s first address', function() {
assert.equal(ipv4Subnet.firstAddress, '192.168.1.129');
});
it('should compute an ipv4 network\'s last address', function() {
assert.equal(ipv4Subnet.lastAddress, '192.168.1.190');
});
it('should compute an ipv4 broadcast address', function() {
assert.equal(ipv4Subnet.broadcastAddress, '192.168.1.191');
});
it('should compute an ipv4 subnet number of addresses', function() {
assert.equal(ipv4Subnet.length, 64);
});
it('should compute an ipv4 subnet number of addressable hosts', function() {
assert.equal(ipv4Subnet.numHosts, 62);
});
it('should compute an ipv4 subnet mask', function() {
assert.equal(ipv4Subnet.subnetMask, '255.255.255.192');
});
it('should compute an ipv4 subnet mask\'s length', function() {
assert.equal(ipv4Subnet.subnetMaskLength, 26);
});
});
describe('cidr() method', function() {
it('should mask address in CIDR notation', function() {
assert.equal(ip.cidr('192.168.1.134/26'), '192.168.1.128');
assert.equal(ip.cidr('2607:f0d0:1002:51::4/56'), '2607:f0d0:1002::');
});
});
describe('isEqual() method', function() {

@@ -104,3 +185,3 @@ it('should check if addresses are equal', function() {

it('should check if an address is from a 172.(16-31).x.x network', function() {
it('should check if an address is from a 172.16.x.x network', function() {
assert.equal(ip.isPrivate('172.16.0.5'), true);

@@ -107,0 +188,0 @@ assert.equal(ip.isPrivate('172.16.123.254'), true);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc