Socket
Socket
Sign inDemoInstall

ip-subnet-calculator

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ip-subnet-calculator

Calculate optimal subnet masks for standard and non-standard IP ranges


Version published
Weekly downloads
10K
decreased by-5.57%
Maintainers
1
Weekly downloads
 
Created
Source

IP Subnet Calculator

JavaScript class for calculating optimal subnet masks for non-standard IP ranges, e.g. 5.4.3.21 - 6.7.8.9

View demo

What does it do?

  • Calculates subnet masks for standard and non-standard IP ranges. For example, 10.0.0.5 - 10.0.0.23 will result in 10.0.0.5/32, 10.0.0.6/31, 10.0.0.8/29, 10.0.0.16/29.

  • Calculates CIDR prefixes from subnet masks, e.g. 10.0.0.5/255.255.128.0 will result in 10.0.0.0/17.

  • Calculates subnet masks from CIDR prefixes, e.g. 10.0.0.5/17 will result in 255.255.128.0.

Support

  • Node.js
  • RequireJS
  • Direct browser use

Installation

> bower install ip-subnet-calculator

> npm install ip-subnet-calculator

Node.js

var IpSubnetCalculator = require( 'ip-subnet-calculator' );

console.log( IpSubnetCalculator.calculate( '5.4.3.21', '6.7.8.9' ) );

RequireJS

require( [ 'ip-subnet-calculator' ],

function( IpSubnetCalculator )
{
   console.log( IpSubnetCalculator.calculate( '5.4.3.21', '6.7.8.9' ) ); 
} );

Direct browser use

<script src='IpSubnetCalculator.js'></script>

<script>
    console.log( IpSubnetCalculator.calculate( '5.4.3.21', '6.7.8.9' ) );
</script>

API

IpSubnetCalculator.calculate( ipStart, ipEnd )

Calculates an optimal set of IP masks for the given IP address range.

ipStart (string) Lowest IP in the range to be calculated in string format (123.123.123.123)

ipEnd (string) Highest IP (inclusive) in the range to be calculated in string format (123.123.123.12)

The function returns null in case of an error. Otherwise, an array containing one or more subnet masks is returned:

var result = [
    {
        ipLow              : 2071689984,
        ipLowStr           : "123.123.123.0",
        ipHigh             : 2071690239,
        ipHighStr          : "123.123.123.255",
        prefixMask         : 4294967040,
        prefixMaskStr      : "255.255.255.0",
        prefixSize         : 24,
        invertedMask       : 255,
        invertedMaskStr    : "0.0.0.255",
        invertedMaskSize   : 8
    },
    
    ...
];

Each object in question contain the following properties:

PropertyUse
ipLowDecimal representation of the lowest IP address in the range
ipLowStrString representation of the lowest IP address in the range
ipHighDecimal representation of the highest IP address in the range
ipHighStrString representation of the highest IP address in the range
prefixMaskDecimal representation of the prefix (subnet) mask
prefixMaskStrString representation of the prefix (subnet) mask
prefixSizeSize of the prefix (subnet) mask in bits
invertedMaskDecimal representation of the inverted prefix mask
invertedMaskStrString representation of the inverted prefix mask
invertedSizeSize of the inverted prefix max in bits

IpSubnetCalculator.calculateSubnetMask( ip, prefixSize )

Calculates a subnet mask from CIDR prefix.

ip (string) IP address in string format

prefixSize Number of relevant bits in the subnet mask

The function returns an object containing full description of the IP range, as described in IpSubnetCalculator.calculate().

IpSubnetCalculator.calculateCIDRPrefix( ip, subnetMask )

Calculates a CIDR prefix from subnet mask.

ip (string) IP address in string format

subnetMask IP subnet mask in string format

The function returns an object containing full description of the IP range, as described in IpSubnetCalculator.calculate().

Conversion Functions

IpSubnetCalculator.toDecimal( ip )

Calculates a decimal integer from an string IP address.

ip (string) IP address in string format

The function returns a decimal representation of an IP address as an integer.

IpSubnetCalculator.toString( integer )

integer (integer) Decimal representation of an IP address.

The function returns an IP address as a string.

License

MIT

Keywords

FAQs

Package last updated on 29 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc