Git Product home page Git Product logo

salieri / ipsubnetcalculator Goto Github PK

View Code? Open in Web Editor NEW
75.0 6.0 21.0 150 KB

JavaScript module for calculating optimized subnet masks for standard and non-standard IP ranges, e.g. 5.4.3.21 - 6.7.8.9

Home Page: https://salieri.github.io/IPSubnetCalculator/

License: MIT License

JavaScript 33.20% CSS 15.09% HTML 12.10% TypeScript 39.60%
iprange subnet-masks ip-masks javascript cidr-prefixes

ipsubnetcalculator's Introduction

IP Subnet Calculator

This module calculates 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
  • Direct browser use

Installation

> npm install ip-subnet-calculator

Node.js

import * as IPSubnetCalculator from 'ip-subnet-calculator';
// or: const IPSubnetCalculator = require('ip-subnet-calculator');

console.log(IPSubnetCalculator.isIp('127.0.0.1')); // true
console.log(IPSubnetCalculator.toDecimal('127.0.0.1')); // 2130706433

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

Direct browser use

<script src='lib/ip-subnet-calculator.browser.js'></script>

<script>
    console.log(IPSubnetCalculator.isIp( 127.0.0.1')); // true
    console.log(IPSubnetCalculator.toDecimal('127.0.0.1')); // 2130706433
    
    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|number) Lowest IP in the range to be calculated in string (123.123.123.0) or numeric (2071689984) format.

ipEnd (string|number) Highest IP (inclusive) in the range to be calculated in string (123.123.123.255) or numeric (2071690239) format.

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

const 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:

Property Use
ipLow Decimal representation of the lowest IP address in the range
ipLowStr String representation of the lowest IP address in the range
ipHigh Decimal representation of the highest IP address in the range
ipHighStr String representation of the highest IP address in the range
prefixMask Decimal representation of the prefix (subnet) mask
prefixMaskStr String representation of the prefix (subnet) mask
prefixSize Size of the prefix (subnet) mask in bits
invertedMask Decimal representation of the inverted prefix mask
invertedMaskStr String representation of the inverted prefix mask
invertedSize Size of the inverted prefix max in bits

IPSubnetCalculator.calculateSubnetMask(ip, prefixSize)

Calculates a subnet mask from CIDR prefix.

ip (string|number) IP address in string or numeric format

prefixSize (number) 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|number) IP address in string or numeric format

subnetMask (string|number) IP subnet mask in string or numeric format

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

Test Functions

IPSubnetCalculator.isIp(ipStr)

Tests whether string is an IP address.

ipStr (string) A string

The function returns a true if the string is an IP address, false otherwise.

IPSubnetCalculator.isDecimalIp(ipNum)

Tests whether ipNum is a decimal IP address.

ipNum (number) A number

The function returns a true if the number is an IP address, false otherwise.

Conversion Functions

IPSubnetCalculator.toDecimal(ip)

Calculates a decimal integer from an string IP address.

ip (string|number) IP address in string format

The function returns a decimal representation of an IP address as an integer. If a valid numeric representation of an IP is passed to this function, it is returned unmodified.

If an invalid value is passed to the function, it will throw an Error object.

IPSubnetCalculator.toString(num)

num (number|string) Decimal representation of an IP address.

The function returns an IP address as a string. If a valid string representation of an IP is passed to this function, it is returned unmodified.

If an invalid value is passed to the function, it will throw an Error object.

License

MIT

ipsubnetcalculator's People

Contributors

aleksi-abr avatar jnovack avatar salieri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ipsubnetcalculator's Issues

"CIDR Subnet Mask Calculator" (Website): some subnet masks not working correct

The calculation at "CIDR Subnet Mask Calculator" on the website https://salieri.github.io/IPSubnetCalculator/ is working for the common, top listed subnet masks ("/24", "/16", "/8") but not for all values listed after "----------------------".

If you chose e.g. "/24" from the top of the list you get the correct subnet mask 255.255.255.0.
If you chose the second list entry for "/24" (down in the list) you get subnet mask 240.0.0.0.

The problem is at all subnet masks greater "/20" because of wrong "value" for the options. The values start again with "1" (for "/21") but should be "21" and so on.
See

<option value='1'>/21</option>

No IPv6 support

Hi.
Is the package going to support IPv6 addresses in the future?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.