ip-addresses
An ES6 module to facilitate working with IP addresses, ranges, subnets and iterations.
- Introduction
- Installation
- Usage
- Errors
- API
5.1 Class: IPv4Address
5.1.1 Property: value
5.1.2 Property: class
5.1.3 Property: private
5.1.4 Method: toString
5.1.5 Method: toBin
5.1.6 Method: toArray
5.1.7 Method: mask
5.2 Class: IPv4Subnetmask
5.2.1 Property: value
5.2.2 Property: length
5.2.3 Method: toString
5.2.4 Method: toBin
5.2.5 Method: toArray
5.2.6 Method: invert
5.3 Class: IPv4Range
5.3.1 Property: firstAddress
5.3.2 Property: lastAddress
5.3.3 Property: length
5.3.4 Method: contains
5.3.5 Method: getIterator
5.4 Class: IPv4Subnet
5.4.1 Property: subnetMask
5.4.2 Property: networkAddress
5.4.3 Property: broadcastAddress
5.4.4 Property: hosts
5.3.5 Method: getHostsIterator
Introduction
This module facilitates working with IP addresses (v4 only).
Installation
$ npm install ip-addresses
Usage
IPv4 Addresses
All IPv4Address instances are created through IPAddresses.ipv4.address().
let IPAddresses = ; // Create an IPv4 addresslet ip1 = IPAddressesv4;let ip2 = IPAddressesv4;let ip3 = IPAddressesv4; // creates a duplicate // Get the value for an addresconsole; // yields 3232235777console; // yields 192.168.1.1console; // yields 192.168.001.001console; // yields 11000000101010000000000100000001console; // yields 11000000 10101000 00000001 00000001console; // yields 11000000.10101000.00000001.00000001console; // yields [192,168,1,1] console; // yields Aconsole; // yields Bconsole; // yields Cconsole; // yields Dconsole; // yields E console; // yields trueconsole; // yields falseconsole; // yields trueconsole; // yields trueconsole; // yields false
IPv4 Subnet masks
All IPv4Subnetmask instances are created through IPAddresses.ipv4.subnetmask().
let IPAddresses = ; // Create an IPv4 subnet masklet sm1 = IPAddressesv4;let sm2 = IPAddressesv4;let sm3 = IPAddressesv4;let sm4 = IPAddressesv4; // creates a duplicate // Get the value for an addresconsole; // yields 4294967040console; // yields 255.255.255.0console; // yields 255.255.255.000console; // yields 11111111111111111111111100000000console; // yields 11111111 11111111 11111111 00000000console; // yields 11111111.11111111.11111111.00000000console; // yields [255,255,255,0]console; // yields 24
IPv4 Ranges
All IPv4Range instances are created through IPAddresses.ipv4.range().
let IPAddresses = ; // Create an IPv4 rangelet range1 = IPAddressesv4;let range2 = IPAddressesv4; // creates a duplicate // Get the value for an addresconsole; // yields 192.168.1.10console; // yields 192.168.1.19console; // yields 10console; // yields falseconsole; // yields true let iterator = range1;let item;while!item = iteratornextdone console;// yields 192.168.1.10// yields 192.168.1.11// yields 192.168.1.12// yields 192.168.1.13// yields 192.168.1.14// yields 192.168.1.15// yields 192.168.1.16// yields 192.168.1.17// yields 192.168.1.18// yields 192.168.1.19
IPv4 Subnets
All IPv4Subnet instances are created through IPAddresses.ipv4.subnet().
let IPAddresses = ; // Create an IPv4 rangelet sn1 = IPAddressesv4;let sn2 = IPAddressesv4;let sn3 = IPAddressesv4; // creates a duplicate // Get the value for an addresconsole; // yields 192.168.1.0console; // yields 192.168.1.0console; // yields 192.168.1.255console; // yields 192.168.1.255console; // yields 256console; // yields 254console; // yields falseconsole; // yields true let iterator = sn1;let item;while!item = iteratornextdone console;// yields 192.168.1.1// yields 192.168.1.2// yields 192.168.1.3// ...// yields 192.168.1.252// yields 192.168.1.253// yields 192.168.1.254
Errors
The API throws IPv4Error objects during the creation of new instances if the specification for a new address, subnet mask, range or subnet is invalid. IPv4Error is an extention of Error. IPv4Error is further extended into IPv4AddressError, IPv4SubnetmaskError, IPv4RangeError and IPv4SubnetError.
let IPAddresses = ; try let ip0 = IPAddressesv4;catche console; // yields IPv4AddressError try let sm0 = IPAddressesv4;catche console; // yields IPv4SubnetmaskError try let range0 = IPAddressesv4;catche console; // yields IPv4RangeError try let sn0 = IPAddressesv4;catche console; // yields IPv4SubnetError
API
Class: IPv4Address
This class represents an IPv4 address, typically defined by a single 32bit unsigned integer.
Property: value
Readonly. The 32bit unsigned integer that representst the address.
Property: class
Readonly. The class of the IP address, A through E.
Property: private
Readonly. True if the IP address is in a private range. False otherwise.
Method: toString(use3Digits)
Returns the address in octet format, e.g. 192.168.1.1. If use3Digits is true toString() returns all octets left padded with zeros, e.g. 192.168.001.001.
Method: toBin(separator)
Returns the address in binary format, e.g. 11000000101010000000000100000001. If separator is set then its first character is used as a separator between the octets, e.g. 11000000 10101000 00000001 00000001 for a space character or 11000000.10101000.00000001.00000001 for a dot.
Method: toArray()
Returns the address as an array, e.g. [ 192, 168, 1, 1 ].
Method: mask(subnetmask)
Returns a new IPv4Address object with its address set to that of the original address masked against the given subnet mask, e.g. 192.168.0.0 for a subnet mask 255.255.0.0. The subnetmask may be specified as an IPv4Subnetmask, a string e.g. 255.255.0.0 or an integer specifying the number of bits occupied by the mask.
Class: IPv4Subnetmask
This class represents an IPv4 subnet mask, typically defined by a single 32bit unsigned integer.
Property: value
Readonly. The 32bit unsigned integer that representst the mask.
Property: length
Readonly. The length of the subnet mask (the number of 1's in the binary representation).
Method: toString(use3Digits)
Returns the mask in octet format, e.g. 255.255.255.0. If use3Digits is true toString() returns all octets left padded with zeros, e.g. 255.255.255.000.
Method: toBin(separator)
Returns the mask in binary format, e.g. 11111111111111111111111100000000. If separator is set then its first character is used as a separator between the octets, e.g. 11111111 11111111 11111111 00000000 for a space character or 11111111.11111111.11111111.00000000 for a dot.
Method: toArray()
Returns the mask as an array, e.g. [ 192, 168, 1, 1 ].
Method: invert()
Returns a new Subnet mask with its mask set to be the inversion of the original mask.
Class: IPv4Range
This class represents a range typically used for iteration. It is also the base class for IPv4Subnetmask.
Property: firstAddress
Readonly. An IPv4Address representing the first address in the range.
Property: lastAddress
Readonly. An IPv4Address representing the last address in the range.
Property: length
Readonly. The number of addresses that exist in the range.
Method: contains(ip)
Returns true if the given ip address is inside the range. false otherwise. The ip address can be specified as an IPv4Address, a string or an array.
Method: getIterator()
Returns an ES6 Generator object to iterate over all addresses in the range. Used like so to list all hosts in the 192.168.1.0 subnet:
let iterator = range1;let item;while!item = iteratornextdone console;
Class: IPv4Subnet
This is an extention of IPv4Range with specific data to represent a subnet. All properties and methods from IPv4Range are available.
Property: subnetMask
Readonly. An IPv4Subnetmask that specifies the mask used for this subnet.
Property: networkAddress
Readonly. An IPv4Address that specifies the network address of the subnet. This is identical to firstAddress.
Property: broadcastAddress
Readonly. An IPv4Address that specifies the broadcast address of the subnet. This is identical to lastAddress.
Property: hosts
Readonly. The number of hosts present in the subnet. This is always equal to length - 2.
Method: getHostsIterator()
Similar to getIterator() except that this specific iterator skips the networkAddress and broadcastAddress to as only to iterate hosts.