0.6.3 • Published 4 years ago
iipp v0.6.3
iipp
Small module to parse and work with IPv4 and IPv6 addresses.
Features:
- Parsing IPv4 and IPv6 Addresses with CIDR
- Printing IPv4 and IPv6 (compressed and uncompressed)
- Checking whether an address or an subnet is covered/included in another one
Usage
Parse IPv4 address
let address = V4Address.parse('1.2.3.4');If an invalid address is given, null is returned.
Parse IPv6 address
let address = V6Address.parse('::1');If an invalid address is given, null is returned.
Check whether a subnet covers/includes another one
let net = V4Address.parse('10.0.0.0/8');
let address = V4Address.parse('5.0.0.0');
net.covers(address); // => falseThe covers method also accepts a string.
If the given string is not a valid address, false is returned.
let net = V4Address.parse('10.0.0.0/8');
net.covers('5.0.0.0'); // => false
net.covers('10.2.3.4'); // => true
net.covers('10.0.0.300'); // => falseAvailable types
Address
Base class representing an address or net.
Attributes
addressType:int4 or 6- size:intAddress size in bits (32 or 128)bytes:int[]Address data as 8-bit unsignedsubnetSize:intSize of the subnet mask in bits
Methods
covers(Address | string):boolChecks whether the current net covers/includes the given addresstoString({ appendCIDR = undefined, uncompressed = false }):stringFormats the address to a string- appendCIDR: When appendCIDR is undefined, the CIDR is appended only when the address object defines a subnet - uncompressed: The uncompressed parameter takes effect only on V6Address objects
V4Address extends Address
IPv4 address or net.
Attributes
addressType:int=>4size:int=>32bytes:int[4]Address data as 8-bit unsigned intssubnetSize:intSize of the subnet mask in bits
Methods
covers(Address | string): bool Checks whether the current net covers/includes the given addresstoString({ appendCIDR = undefined }):stringFormats the address to a string- appendCIDR: When appendCIDR is undefined, the CIDR is appended only when the address object defines a subnet
clone():V4AddressCreates clone of this address objectstatic parse(string):V4AddressParses an IPv4 address or net. Returnsnullfor invalid data.
V6Address extends Address
IPv6 address or net.
Attributes
addressType:int=>6size:int=>128bytes:int[16]Address data as 8-bit unsigned intsblocks:int[8]Address data as 16-bit unsigned intssubnetSize:intSize of the subnet mask in bits
Methods
covers(Address | string): bool Checks whether the current net covers/includes the given addresstoString({ appendCIDR = true, uncompressed = false }):stringFormats the address to a string- appendCIDR: When appendCIDR is undefined, the CIDR is appended only when the address object defines a subnet
toUncompressedString({ appendCIDR = undefined }):stringFormats the address to a string- appendCIDR: When appendCIDR is undefined, the CIDR is appended only when the address object defines a subnet
clone():V6AddressCreates clone of this address objectstatic parse(string):V6AddressParses an IPv6 address or net. Returnsnullfor invalid data.
Examples
let address = V4Address.parse('1.2.3.4');
/* address => V4Address
- addressType: 4
- size: 32
- bytes: [1, 2, 3, 4]
- subnetSize: 32
*/
let address = V4Address.parse('5.6.7.8/12');
/* address => V4Address
- addressType: 4
- size: 32
- bytes: [5, 6, 7, 8]
- subnetSize: 12
*/
let address = V4Address.parse('1.2.288.4'); //Invalid valid at position 3
/* address => null */
let address = V4Address.parse('1.2.3.4/42'); //Invalid CIDR value
/* address => null */
let address = V6Address.parse('::1');
/* address => V6Address
- addressType: 6
- size: 128
- bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
- subnetSize: 128
*/
address.toUncompressedString(); // => 0000:0000:0000:0000:0000:0000:0000:0001/128
address.toString(); // => ::1
address.toString({ appendCIDR: true }); // => ::1/128
let address = V6Address.parse('::1:0:0:0:0:0/100');
/* address => V6Address
- addressType: 6
- size: 128
- bytes: [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- subnetSize: 128
*/
address.toUncompressedString(); // => 0000:0000:0001:0000:0000:0000:0000:0000/100
address.toString(); // => 0:0:1::/100
address.toString({ appendCIDR: false }); // => 0:0:1::