Network Address
The netaddr module provides functions and the netaddr.range object type for parsing, validating and manipulating IP addresses, network prefixes and MAC addresses.
Address ranges are represented by netaddr.range instances which carry the address, its family and a prefix size (CIDR notation). Instances are created with the new(), IPv4(), IPv6() or MAC() functions:
import * as netaddr from 'netaddr';
const addr = netaddr.new('10.24.0.1/24');
const addr = netaddr.new('10.24.0.1/255.255.255.0');
const addr = netaddr.new('10.24.0.1', '255.255.255.0'); // separate netmask
const addr = netaddr.new('10.24.0.1/24', 16); // override netmask
const addr6 = netaddr.new('fe80::221:63ff:fe75:aa17/64');
const addr6 = netaddr.new('fe80::221:63ff:fe75:aa17/ffff:ffff:ffff:ffff::');
const mac = netaddr.new('00:11:22:cc:dd:ee');
const mac = netaddr.MAC('C0:B6:F9:00:00:00/24');
The checkv4(), checkv6() and checkmac() functions provide non-throwing validation, returning the canonical string representation of the given address or null if the argument is not a valid address of the respective family:
netaddr.checkv4('127.0.0.1'); // "127.0.0.1"
netaddr.checkv6('::1'); // "::1"
netaddr.checkmac('00:11:22:cc:dd:ee'); // "00:11:22:CC:DD:EE"
netaddr.checkv4('nonsense'); // null
- Source
Classes
netaddr.range
Represents an IP address or address range as created by new(), v4(), v6() or mac().
Instances carry the address, its family (4 for IPv4, 6 for IPv6 and 1 for MAC addresses) and a prefix size in bits (32 for IPv4, 128 for IPv6 and 48 for MAC addresses by default).
Instances provide a tostring() method which is also used by print() and string interpolation to render the address in canonical form, including the prefix size if it is smaller than the family's full width.
In addition, instances support property access through __get__() and __set__() metamethods:
- Numeric keys read or write the individual address bytes, e.g.
addr[0]reads the first address byte andaddr[0] = 10rewrites it. Negative indices count from the end of the address. - The
bitsproperty reads or writes the prefix size in bits. - The
familyproperty reads the address family (numericAF_*value). - The
scopeproperty reads or writes the address scope (the numeric interface index) for IPv6 and MAC address instances. - The
scopeidproperty reads or writes the address scope given an interface name or numeric index, silently ignoring non-link-local IPv6 addresses. - The
netmaskproperty reads the netmask as a string, e.g."255.255.255.0"for a/24prefix. - The
hostproperty reads the host address as a new range (full-width prefix). - The
mapped4property reads the mapped IPv4 address as a new range, ornullif the instance is not an IPv6 mapped IPv4 address. - The
unscopenameproperty reads the interface name of the address scope (or an empty string if no scope is set). - The
sizeproperty reads the number of addresses in this range, ornullif the value does not fit into a 64 bit signed integer.
Methods
checkmac(address) → {string}nullable
Verify an ethernet MAC address.
Checks whether the given argument is a preexisting netaddr.range MAC address instance or a string literal convertible to an ethernet MAC and returns a plain string containing the canonical representation of the address.
Returns null if the argument is not a valid MAC address. This function is intended to aid in safely verifying address literals without having to deal with exceptions.
| Name | Type | Description |
|---|---|---|
address | string | | A valid MAC address or existing |
netaddr.checkmac(netaddr.new('00-11-22-cc-dd-ee')); // "00:11:22:CC:DD:EE"
netaddr.checkmac('00:11:22:cc:dd:ee'); // "00:11:22:CC:DD:EE"
netaddr.checkmac('nonsense'); // null
netaddr.checkmac(123); // null
netaddr.checkmac(null); // null- Source
checkv4(address) → {string}nullable
Verify an IPv4 address.
Checks whether the given argument is a preexisting netaddr.range IPv4 address instance or a string literal convertible to an IPv4 address and returns a plain string containing the canonical representation of the address.
Returns null if the argument is not a valid IPv4 address. This function is intended to aid in safely verifying address literals without having to deal with exceptions.
| Name | Type | Description |
|---|---|---|
address | string | | A valid IPv4 address or existing |
netaddr.checkv4(netaddr.new('127.0.0.1')); // "127.0.0.1"
netaddr.checkv4('127.0.0.1'); // "127.0.0.1"
netaddr.checkv4('nonsense'); // null
netaddr.checkv4(123); // null
netaddr.checkv4(null); // null- Source
checkv6(address) → {string}nullable
Verify an IPv6 address.
Checks whether the given argument is a preexisting netaddr.range IPv6 address instance or a string literal convertible to an IPv6 address and returns a plain string containing the canonical representation of the address.
Returns null if the argument is not a valid IPv6 address. This function is intended to aid in safely verifying address literals without having to deal with exceptions.
| Name | Type | Description |
|---|---|---|
address | string | | A valid IPv6 address or existing |
netaddr.checkv6(netaddr.new('0:0:0:0:0:0:0:1')); // "::1"
netaddr.checkv6('0:0:0:0:0:0:0:1'); // "::1"
netaddr.checkv6('nonsense'); // null
netaddr.checkv6(123); // null
netaddr.checkv6(null); // null- Source
mac(address, netmaskopt) → {range}
Construct a new MAC netaddr.range instance.
Raises a runtime exception if the given argument does not represent a valid ethernet MAC address or if the given optional mask is of a different family.
| Name | Type | Description |
|---|---|---|
address | string | | A valid ethernet MAC address, optionally with prefix size (CIDR notation) or mask separated by slash, an unsigned number representing the last 32 bit of the address, an array of exactly |
netmask | string | | (optional) A valid MAC address mask or a number containing a prefix size between |
const intel_macs = netaddr.MAC('C0:B6:F9:00:00:00/24');
const intel_macs = netaddr.MAC('C0:B6:F9:00:00:00/FF:FF:FF:0:0:0');
const intel_macs = netaddr.MAC('C0:B6:F9:00:00:00', 'FF:FF:FF:0:0:0');
const intel_macs = netaddr.MAC('C0:B6:F9:00:00:00/24', 48); // override mask- Source
new(address, netmaskopt) → {range}
Construct a new netaddr.range instance, auto-detecting the address family.
Raises a runtime exception if the given argument does not represent a valid address or if the given optional netmask is of a different family.
| Name | Type | Description |
|---|---|---|
address | string | | A valid IPv4 or IPv6 address, optionally with prefix size (CIDR notation) or netmask separated by slash, an unsigned number, an array of bytes, or a sockaddr-like object (see below). Numbers are interpreted as MAC addresses, except when used with the module:netaddr#v4 or module:netaddr#v6 constructors. The address family of byte arrays is determined by their length: A sockaddr-like object has the following properties:
|
netmask | string | | (optional) A valid IPv4 or IPv6 netmask or a number containing a prefix size in bits ( |
const addr = netaddr.new('10.24.0.1/24');
const addr = netaddr.new('10.24.0.1/255.255.255.0');
const addr = netaddr.new('10.24.0.1', '255.255.255.0'); // separate netmask
const addr = netaddr.new('10.24.0.1/24', 16); // override netmask
const addr6 = netaddr.new('fe80::221:63ff:fe75:aa17/64');
const addr6 = netaddr.new('fe80::221:63ff:fe75:aa17/ffff:ffff:ffff:ffff::');
const mac = netaddr.new('00:11:22:cc:dd:ee');
const mac = netaddr.new(0x001122ccddeeff);
const addr = netaddr.new([ 10, 24, 0, 1 ]); // IPv4
const addr6 = netaddr.new([ 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ]);
const addr = netaddr.new({ address: '10.24.0.1' }); // family guessed
const addr = netaddr.new({ family: 2, address: '10.24.0.1' });
const addr6 = netaddr.new({ family: 10, address: 'fe80::1', interface: 'lo' });- Source
v4(address, netmaskopt) → {range}
Construct a new IPv4 netaddr.range instance.
Raises a runtime exception if the given argument does not represent a valid IPv4 address or if the given optional netmask is of a different family.
| Name | Type | Description |
|---|---|---|
address | string | | A valid IPv4 address, optionally with prefix size (CIDR notation) or netmask separated by slash, an unsigned number representing the address in host byte order, an array of exactly |
netmask | string | | (optional) A valid IPv4 netmask or a number containing a prefix size between |
const addr = netaddr.IPv4('10.24.0.1/24');
const addr = netaddr.IPv4('10.24.0.1/255.255.255.0');
const addr = netaddr.IPv4('10.24.0.1', '255.255.255.0'); // separate netmask
const addr = netaddr.IPv4('10.24.0.1/24', 16); // override netmask- Source
v6(address, netmaskopt, scopeopt) → {range}
Construct a new IPv6 netaddr.range instance.
Raises a runtime exception if the given argument does not represent a valid IPv6 address or if the given optional netmask is of a different family.
| Name | Type | Description |
|---|---|---|
address | string | | A valid IPv6 address, optionally with prefix size (CIDR notation) or netmask separated by slash, an unsigned number representing the last 32 bit of the address, an array of exactly |
netmask | string | | (optional) A valid IPv6 netmask or a number containing a prefix size between |
scope | string | | (optional) An optional address scope for link-local addresses ( |
const addr6 = netaddr.IPv6('fe80::221:63ff:fe75:aa17/64');
const addr6 = netaddr.IPv6('fe80::221:63ff:fe75:aa17/ffff:ffff:ffff:ffff::');
const addr6 = netaddr.IPv6('fe80::221:63ff:fe75:aa17', 'ffff:ffff:ffff:ffff::');
const addr6 = netaddr.IPv6('fe80::221:63ff:fe75:aa17/64', 128); // override
const addr6 = netaddr.IPv6('fe80::221:63ff:fe75:aa17', 64, 'eth0');- Source