Giter VIP home page Giter VIP logo

node-netmask's Introduction

Netmask

The Netmask class parses and understands IPv4 CIDR blocks so they can be explored and compared. This module is highly inspired by Perl Net::Netmask module.

Synopsis

var Netmask = require('netmask').Netmask

var block = new Netmask('10.0.0.0/12');
block.base;                     // 10.0.0.0
block.mask;                     // 255.240.0.0
block.bitmask;                  // 12
block.hostmask;                 // 0.15.255.255
block.broadcast;                // 10.15.255.255
block.size;                     // 1048576
block.first;                    // 10.0.0.1
block.last;                     // 10.15.255.254

block.contains('10.0.8.10');    // true
block.contains('10.8.0.10');    // true
block.contains('192.168.1.20'); // false

block.forEach(function(ip, long, index));

block.next()                    // Netmask('10.16.0.0/12')

Constructing

Netmask objects are created with an IP address and optionally a mask. There are many forms that are recognized:

'216.240.32.0/24'               // The preferred form.
'216.240.32.0/255.255.255.0'
'216.240.32.0', '255.255.255.0'
'216.240.32.0', 0xffffff00
'216.240.32.4'                  // A /32 block.
'0330.0360.040.04'              // Octal form
'0xd8.0xf0.0x20.0x4'            // Hex form

API

  • .base: The base address of the network block as a string (eg: 216.240.32.0). Base does not give an indication of the size of the network block.
  • .mask: The netmask as a string (eg: 255.255.255.0).
  • .hostmask: The host mask which is the opposite of the netmask (eg: 0.0.0.255).
  • .bitmask: The netmask as a number of bits in the network portion of the address for this block (eg: 24).
  • .size: The number of IP addresses in a block (eg: 256).
  • .broadcast: The blocks broadcast address (eg: 192.168.1.0/24 => 192.168.1.255)
  • .first, .last: First and last useable address
  • .contains(ip or block): Returns a true if the IP number ip is part of the network. That is, a true value is returned if ip is between base and broadcast. If a Netmask object or a block is given, it returns true only of the given block fits inside the network.
  • .forEach(fn): Similar to the Array prototype method. It loops through all the useable addresses, ie between first and last.
  • .next(count): Without a count, return the next block of the same size after the current one. With a count, return the Nth block after the current one. A count of -1 returns the previous block. Undef will be returned if out of legal address space.
  • .toString(): The netmask in base/bitmask format (e.g., '216.240.32.0/24')

Installation

$ npm install netmask

Run all tests (vows plus mocha)

$ npm test

License

(The MIT License)

Copyright (c) 2011 Olivier Poitrey [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-netmask's People

Contributors

ai avatar dependabot[bot] avatar dschenkelman avatar elliot-huffman avatar gmiroshnykov avatar ignoramous avatar jimbledsoe avatar kaoudis avatar meteormatt avatar olleolleolle avatar palmerabollo avatar rs avatar runk avatar ry0tak avatar ryanrolds avatar steve-jansen avatar strml avatar tootallnate avatar yorkie 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  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  avatar  avatar

node-netmask's Issues

Update coffee-script dependency

Noticed this module has a explicit dependency for coffee-script 1.2.0. Does it really need this specific version? I would need to use 1.3.3. Could the dependency be changed to e.g. >=1.2.0?

Checking if netmask is valid

Hi there,

Thanks heaps for the library. Would be awesome to add a isValid property to the netmask object. Maybe something like:

this.isValid = (this.base == '0.0.0.0') ? false : true;

If you agree, I can create a pull request.

Changelog

Would it be possible to add a changelog detailing the breaking changes (if any) that were introduced in v2.0.0?

I'm sure a lot of folks are eager to upgrade to mitigate the security vulnerability present in 1.x.

/0 does not work as expected due to signed ints

0.0.0.0/0 does not match all IPs as expected. I guess there is a problem when calculating maskLong.
when mask = 0, it is expected that maskLong is 0x00000000 but it becomes 0xffffffff instead.

Problem requiring netmask

npm install netmask

Then adding

var metmask = require('netmask').NetMask;

Into my javascript application, gives a module not found.

If I go into the lib and compile netmask.coffee into js

 coffee -c netmask.coffee

This then makes the module work.

I've got coffee script installed globally and am running node 0.6.14

Method first() returns incorrect IP address

The first IP address of a block should end with .0 but first() returns an IP address of .1.

The Perl Net::Netmask, upon which this is based, says in the documentation: "->first() & ->last() Synonyms for ->base() and ->broadcast()" and base should return the base address of the network block and broadcast should return the last IP address.

The code
@FIRST = if @bitmask <= 30 then long2ip(@netlong + 1) else @base
should be moved after @base is set and changed to
@FIRST = @base

In common usage, the first address in a subnet, all binary zero in the host identifier, is reserved for referring to the network itself, while the last address, all binary one in the host identifier, is used as a broadcast address for the network; this reduces the number of addresses available for hosts by 2.

While this is true, the actual CIDR range consists of the full set of IP addresses in the range even if those two IP address shouldn't be assigned to host machines.

contains method returns wrong result

> netm = new Netmask('192.168.1.0/255.255.255.0')
Netmask {
maskLong: 4294967040,
bitmask: 24,
netLong: 3232235776,
size: 256,
base: '192.168.1.0',
mask: '255.255.255.0',
hostmask: '0.0.0.255',
first: '192.168.1.1',
last: '192.168.1.254',
broadcast: '192.168.1.255' }
> netm.contains('192.168.1.2')
false
>

expected result : true

Accepts IPs with fewer than four octets, result doesn't make sense

When constructing an instance with the form x.y I would expect one of two things:

  1. An error because this is not a valid IP address.
  2. The input to be parsed the same as x.y.0.0/16.

Instead, the input is parsed as x.0.0.y/32, which makes very little sense to me:

> (new netmask.Netmask('1.2')).toString()
'1.0.0.2/32'

The output is similarly odd when providing 1 or 3 octets:

> (new netmask.Netmask('1')).toString()
'0.0.0.1/32'
> (new netmask.Netmask('1.2.3')).toString()
'1.2.0.3/32'

Improper Input Validation

  1. In the latest 2.0.2 there was a rewrite of the IP validation. Effectively, in version 2.0.2 the sanity check that IP components are < 0xFFFFFFFF has been removed, because >>> 0 always converts them to a 32-bit unsigned long.
    This can cause trouble, for example this "IP" actually checks out as belonging to the localhost block: "4294967423.0.0.1" (because mod 2^32 it is equal to 127.0.0.1).
    Although it's obviously an invalid IP, this has the potential to create shenanigans, because other libraries probably are not making the exact same mistake.

  2. Another input validation issue is the use of the ParseInt function for the mask parameter. For example two possible inputs that generate a nonsensical state:
    (a) Try weird = new Netmask('1.2.3.4', -1) , and then the state of the netmask object includes a bitmask <0, and size > 2^32 among other oddities. The next() operation has an uncaught error in this case.
    (b) Try weird2 = new Netmask('1.2.3.4', 0.1). The constructor succeeds, but the state is similarly funky.

Best regards,
Alon Navon,
Seal Security

forEach only returns error 'fn not a function'

When I'm trying to use the forEach method, I only get back 'fn is not a function'. No matter what input I use.

What exactly am I supposed to input? The documentation shows ip, long, index. Not sure what long or index means, so I've been guessing.

what kind of function am I supposed to input into the forEach method?

Getting ips within a netmask block as an array

Another helper method to get the list of ip addresses in a block as an array. I can submit a request if interested.

// assuming block is a netmask object
function addresses(block) {
    // array of ips to be returned
    var arr = [];

    // first ip address in long form
    var firstLong = ip2long(block.first);
    // last ip address in long form
    var lastLong = ip2long(block.last);

    // first ip is already in nice ip form
    arr.push(block.first);
    // check if first and last ip is the same
    if (block.first != block.last) {
        for (var i=firstLong+1; i<lastLong; i++) {
            arr.push(
                long2ip(i)
            );
        }
        // last ip is already in nice ip form
        arr.push(block.last);
    }

    return arr;
}

Create a netmask from an IP range

This is an awesome module for creating IP ranges from a given netmask, but it'd be even better if it worked in reverse, eg:

coffee> n = new netmask.Netmask('10.0.0.0/24')
{ bitmask: 24,
maskLong: 4294967040,
netLong: 167772160,
size: 256,
base: '10.0.0.0',
mask: '255.255.255.0',
hostmask: '0.0.0.255',
first: '10.0.0.1',
last: '10.0.0.254',
broadcast: '10.0.0.255' }

coffee> n = new netmask.Netmask('10.0.0.1', '10.0.0.254')
{ bitmask: 24,
maskLong: 4294967040,
netLong: 167772160,
size: 256,
base: '10.0.0.0',
mask: '255.255.255.0',
hostmask: '0.0.0.255',
first: '10.0.0.1',
last: '10.0.0.254',
broadcast: '10.0.0.255' }

Out of memory error for '.forEach' iterator when used on large CIDR blocks

const block = new Netmask('224.0.0.0/4');
console.log(block);
block.forEach(someFunction);

Netmask {
  bitmask: 4,
  maskLong: 4026531840,
  netLong: 3758096384,
  size: 268435456,
  base: '224.0.0.0',
  mask: '240.0.0.0',
  hostmask: '15.255.255.255',
  first: '224.0.0.1',
  last: '239.255.255.254',
  broadcast: '239.255.255.255' }

FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory

Method last() returns incorrect IP address

The last IP address of a block should end with .255 but last() returns an IP address of .254.

The Perl Net::Netmask, upon which this is based, says in the documentation: "->first() & ->last() Synonyms for ->base() and ->broadcast()" and base should return the base address of the network block and broadcast should return the last IP address.

The code
@last = if @bitmask <= 30 then long2ip(@netlong + @SiZe - 2) else long2ip(@netlong + @SiZe - 1)
should be moved after @broadcast is set and changed to
@last =@broadcast

In common usage, the first address in a subnet, all binary zero in the host identifier, is reserved for referring to the network itself, while the last address, all binary one in the host identifier, is used as a broadcast address for the network; this reduces the number of addresses available for hosts by 2.

While this is true, the actual CIDR range consists of the full set of IP addresses in the range even if those two IP address shouldn't be assigned to host machines.

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.