Skip to content

Class: IPv6 ​

The IPv6 toolkit: validate the addresses, convert them between their expanded, compressed and integer (bigint) forms, parse the CIDR blocks, and compare the addresses and blocks. The class is not instantiated. The invalid inputs give false rather than throwing.

Example ​

typescript
import {IPv6, type IPv6Subnet} from 'lakutata/utils'

console.log(IPv6.compressedForm('2001:0db8:0000:0000:0000:0000:0000:0001'), IPv6.expandedForm('::1'))
//2001:db8::1 0000:0000:0000:0000:0000:0000:0000:0001
const block: IPv6Subnet | false = IPv6.parseCIDR('2001:db8::1/64')
if (block) console.log(block.firstHost, block.lastHost, block.ipCount)
//2001:db8:: 2001:db8::ffff:ffff:ffff:ffff 18446744073709551616n
console.log(IPv6.contains('2001:db8::/64', '2001:db8::11'), IPv6.isEqual('::ffff', 65535n))
//true true

Constructors ​

Constructor ​

new IPv6(): IPv6

Returns ​

IPv6

Methods ​

compressedForm() ​

static compressedForm(ip): string | false

Compress an IPv6 address: the leading zeros of each group dropped, the longest run of two or more zero groups (the first one on a tie) replaced with ::, the hexadecimal digits in lower case, as RFC 5952 recommends ('2001:0DB8:0000:0000:0000:0000:0000:0001' gives '2001:db8::1'). An embedded IPv4 address is converted into two groups ('::ffff:192.168.1.1' gives '::ffff:c0a8:101'). The spaces around the address are trimmed, and a zone index is kept after it, as given ('FE80::0001%eth0' gives 'fe80::1%eth0').

Parameters ​

ip ​

string

The address, in any valid form (see IPv6.isValidIP).

Returns ​

string | false

The compressed form, or false when the address is invalid.


contains() ​

static contains(cidr, ip): boolean

Tell whether an IPv6 address is in a CIDR block ('2001:db8::11' is in '2001:db8::1/64', not in '2001:db8::1/128').

Parameters ​

cidr ​

string

The CIDR block (see IPv6.isCIDR).

ip ​

string

The address; an IPv4 address gives false.

Returns ​

boolean

true when the address is in the block, false otherwise or when either is invalid.


expandedForm() ​

static expandedForm(ip): string | false

Expand an IPv6 address into its full form, 8 groups of 4 hexadecimal digits ('2001:db8::1' gives '2001:0db8:0000:0000:0000:0000:0000:0001'). The case of the digits is kept, and an embedded IPv4 address is converted into two groups ('::ffff:192.168.1.1' gives '0000:0000:0000:0000:0000:ffff:c0a8:0101'). The spaces around the address are trimmed, and a zone index is kept after it ('fe80::1%eth0' gives 'fe80:0000:0000:0000:0000:0000:0000:0001%eth0').

Parameters ​

ip ​

string

The address, in any valid form (see IPv6.isValidIP).

Returns ​

string | false

The expanded form, or false when the address is invalid.


ip2long() ​

static ip2long(ip): bigint | false

Convert an IPv6 address into its integer form, a 128-bit unsigned bigint ('::1' gives 1n, '::ffff:9999' gives 4294941081n). A zone index is ignored.

Parameters ​

ip ​

string

The address, in any valid form (see IPv6.isValidIP).

Returns ​

bigint | false

The integer, from 0n to 2n ** 128n - 1n, or false when the address is invalid (an IPv4 address included).


isCIDR() ​

static isCIDR(cidr): boolean

Tell whether a string is a valid IPv6 CIDR block: a valid address (see IPv6.isValidIP), / and a prefix length from 0 to 128 ('2001:db8::/32', '::9999:ffff/128'). The address need not be the first of the block. The prefix length is decimal digits only: '/64.5', '/ 64', '/0x40' and a second / are invalid.

Parameters ​

cidr ​

string

The string to test.

Returns ​

boolean

true when it is a valid CIDR block, false otherwise or when it is not a string.


isConflict() ​

static isConflict(cidrs): boolean

Tell whether two IPv6 CIDR blocks in a list overlap (['2001:db8::1/120', '2001:db8::1/122'] do). The invalid entries, IPv4 blocks included, are ignored.

Parameters ​

cidrs ​

string[]

The CIDR blocks.

Returns ​

boolean

true when two blocks overlap, false otherwise or when the list is empty or not an array.


isEqual() ​

static isEqual(ip1, ip2): boolean

Tell whether two IPv6 addresses are the same, each given in any valid form or as its integer form ('::ffff', '0:0:0:0:0:0:0:ffff' and 65535n are equal).

Parameters ​

ip1 ​

string | bigint

The first address: a string, or a bigint from 0n to 2n ** 128n - 1n.

ip2 ​

string | bigint

The second address: a string, or a bigint from 0n to 2n ** 128n - 1n.

Returns ​

boolean

true when both are valid and equal, false otherwise (a number is never equal: isEqual(65535n, 65535) gives false).


isValidIP() ​

static isValidIP(ip): boolean

Tell whether a string is a valid IPv6 address: 8 groups of 1 to 4 hexadecimal digits separated by :, a run of zero groups possibly shortened to ::, the last 32 bits possibly written as an IPv4 address ('::ffff:1.2.3.4'). A zone index ('fe80::1%eth0') and spaces around the address are accepted. An IPv4 address alone is not valid.

Parameters ​

ip ​

string

The string to test; a value which is not a string gives false.

Returns ​

boolean

true when the address is valid, false otherwise.


long2ip() ​

static long2ip(ip): string | false

Convert the integer form of an IPv6 address into its compressed form (1n gives '::1', 65535n gives '::ffff').

Parameters ​

ip ​

bigint

The integer, a bigint from 0n to 2n ** 128n - 1n.

Returns ​

string | false

The address, or false when the integer is out of range or not a bigint (a number gives false).


parseCIDR() ​

static parseCIDR(cidr): false | IPv6Subnet

Describe an IPv6 CIDR block: its first and last addresses, its size and its prefix length (see IPv6Subnet). The address of the block may be any address in it ('2001:db8::1/64' describes 2001:db8::/64).

Parameters ​

cidr ​

string

The CIDR block (see IPv6.isCIDR), such as '::9999:ffff/118'.

Returns ​

false | IPv6Subnet

The block (for '::9999:ffff/118': ipCount 1024n, firstHost '::9999:fc00', lastHost'::9999:ffff', prefixLength 118), or false when the block is invalid (see IPv6.isCIDR) or not a string.

Released under the MIT License.