| 1 | # ipaddr.js — an IPv6 and IPv4 address manipulation library
|
|---|
| 2 |
|
|---|
| 3 | [](https://github.com/whitequark/ipaddr.js/actions?query=workflow%3A%22CI+Tests%22)
|
|---|
| 4 |
|
|---|
| 5 | ipaddr.js is a small (1.9K minified and gzipped) library for manipulating
|
|---|
| 6 | IP addresses in JavaScript environments. It runs on both CommonJS runtimes
|
|---|
| 7 | (e.g. [nodejs]) and in a web browser.
|
|---|
| 8 |
|
|---|
| 9 | ipaddr.js allows you to verify and parse string representation of an IP
|
|---|
| 10 | address, match it against a CIDR range or range list, determine if it falls
|
|---|
| 11 | into some reserved ranges (examples include loopback and private ranges),
|
|---|
| 12 | and convert between IPv4 and IPv4-mapped IPv6 addresses.
|
|---|
| 13 |
|
|---|
| 14 | [nodejs]: http://nodejs.org
|
|---|
| 15 |
|
|---|
| 16 | ## Installation
|
|---|
| 17 |
|
|---|
| 18 | `npm install ipaddr.js`
|
|---|
| 19 |
|
|---|
| 20 | ## Older Node support
|
|---|
| 21 |
|
|---|
| 22 | Use 2.x release for nodejs versions 10+.
|
|---|
| 23 | Use the 1.x release for versions of nodejs older than 10.
|
|---|
| 24 |
|
|---|
| 25 | ## API
|
|---|
| 26 |
|
|---|
| 27 | ipaddr.js defines one object in the global scope: `ipaddr`. In CommonJS,
|
|---|
| 28 | it is exported from the module:
|
|---|
| 29 |
|
|---|
| 30 | ```js
|
|---|
| 31 | const ipaddr = require('ipaddr.js');
|
|---|
| 32 | ```
|
|---|
| 33 |
|
|---|
| 34 | The API consists of several global methods and two classes: ipaddr.IPv6 and ipaddr.IPv4.
|
|---|
| 35 |
|
|---|
| 36 | ### Global methods
|
|---|
| 37 |
|
|---|
| 38 | There are four global methods defined: `ipaddr.isValid`, `ipaddr.isValidCIDR`,
|
|---|
| 39 | `ipaddr.parse`, and `ipaddr.process`. All of them receive a string as a single
|
|---|
| 40 | parameter.
|
|---|
| 41 |
|
|---|
| 42 | The `ipaddr.isValid` method returns `true` if the address is a valid IPv4 or
|
|---|
| 43 | IPv6 address, and `false` otherwise. It does not throw any exceptions.
|
|---|
| 44 |
|
|---|
| 45 | The `ipaddr.isValidCIDR` method returns `true` if the address is a valid IPv4 or
|
|---|
| 46 | IPv6 address in CIDR notation, and `false` otherwise. It does not throw any exceptions.
|
|---|
| 47 |
|
|---|
| 48 | The `ipaddr.parse` method returns an object representing the IP address,
|
|---|
| 49 | or throws an `Error` if the passed string is not a valid representation of an
|
|---|
| 50 | IP address.
|
|---|
| 51 |
|
|---|
| 52 | The `ipaddr.process` method works just like the `ipaddr.parse` one, but it
|
|---|
| 53 | automatically converts IPv4-mapped IPv6 addresses to their IPv4 counterparts
|
|---|
| 54 | before returning. It is useful when you have a Node.js instance listening
|
|---|
| 55 | on an IPv6 socket, and the `net.ipv6.bindv6only` sysctl parameter (or its
|
|---|
| 56 | equivalent on non-Linux OS) is set to 0. In this case, you can accept IPv4
|
|---|
| 57 | connections on your IPv6-only socket, but the remote address will be mangled.
|
|---|
| 58 | Use `ipaddr.process` method to automatically demangle it.
|
|---|
| 59 |
|
|---|
| 60 | ### Object representation
|
|---|
| 61 |
|
|---|
| 62 | Parsing methods return an object which descends from `ipaddr.IPv6` or
|
|---|
| 63 | `ipaddr.IPv4`. These objects share some properties, but most of them differ.
|
|---|
| 64 |
|
|---|
| 65 | #### Shared properties
|
|---|
| 66 |
|
|---|
| 67 | One can determine the type of address by calling `addr.kind()`. It will return
|
|---|
| 68 | either `"ipv6"` or `"ipv4"`.
|
|---|
| 69 |
|
|---|
| 70 | An address can be converted back to its string representation with `addr.toString()`.
|
|---|
| 71 | Note that this method:
|
|---|
| 72 | * does not return the original string used to create the object (in fact, there is
|
|---|
| 73 | no way of getting that string)
|
|---|
| 74 | * returns a compact representation (when it is applicable)
|
|---|
| 75 |
|
|---|
| 76 | A `match(range, bits)` method can be used to check if the address falls into a
|
|---|
| 77 | certain CIDR range. Note that an address can be (obviously) matched only against an address of the same type.
|
|---|
| 78 |
|
|---|
| 79 | For example:
|
|---|
| 80 |
|
|---|
| 81 | ```js
|
|---|
| 82 | const addr = ipaddr.parse('2001:db8:1234::1');
|
|---|
| 83 | const range = ipaddr.parse('2001:db8::');
|
|---|
| 84 |
|
|---|
| 85 | addr.match(range, 32); // => true
|
|---|
| 86 | ```
|
|---|
| 87 |
|
|---|
| 88 | Alternatively, `match` can also be called as `match([range, bits])`. In this way, it can be used together with the `parseCIDR(string)` method, which parses an IP address together with a CIDR range.
|
|---|
| 89 |
|
|---|
| 90 | For example:
|
|---|
| 91 |
|
|---|
| 92 | ```js
|
|---|
| 93 | const addr = ipaddr.parse('2001:db8:1234::1');
|
|---|
| 94 |
|
|---|
| 95 | addr.match(ipaddr.parseCIDR('2001:db8::/32')); // => true
|
|---|
| 96 | ```
|
|---|
| 97 |
|
|---|
| 98 | A `range()` method returns one of predefined names for several special ranges defined by IP protocols. The exact names (and their respective CIDR ranges) can be looked up in the source: [IPv6 ranges] and [IPv4 ranges]. Some common ones include `"unicast"` (the default one) and `"reserved"`.
|
|---|
| 99 |
|
|---|
| 100 | You can match against your own range list by using
|
|---|
| 101 | `ipaddr.subnetMatch(address, rangeList, defaultName)` method. It can work with a mix of IPv6 or IPv4 addresses, and accepts a name-to-subnet map as the range list. For example:
|
|---|
| 102 |
|
|---|
| 103 | ```js
|
|---|
| 104 | const rangeList = {
|
|---|
| 105 | documentationOnly: [ ipaddr.parse('2001:db8::'), 32 ],
|
|---|
| 106 | tunnelProviders: [
|
|---|
| 107 | [ ipaddr.parse('2001:470::'), 32 ], // he.net
|
|---|
| 108 | [ ipaddr.parse('2001:5c0::'), 32 ] // freenet6
|
|---|
| 109 | ]
|
|---|
| 110 | };
|
|---|
| 111 | ipaddr.subnetMatch(ipaddr.parse('2001:470:8:66::1'), rangeList, 'unknown'); // => "tunnelProviders"
|
|---|
| 112 | ```
|
|---|
| 113 |
|
|---|
| 114 | The addresses can be converted to their byte representation with `toByteArray()`. (Actually, JavaScript mostly does not know about byte buffers. They are emulated with arrays of numbers, each in range of 0..255.)
|
|---|
| 115 |
|
|---|
| 116 | ```js
|
|---|
| 117 | const bytes = ipaddr.parse('2a00:1450:8007::68').toByteArray(); // ipv6.google.com
|
|---|
| 118 | bytes // => [42, 0x00, 0x14, 0x50, 0x80, 0x07, 0x00, <zeroes...>, 0x00, 0x68 ]
|
|---|
| 119 | ```
|
|---|
| 120 |
|
|---|
| 121 | The `ipaddr.IPv4` and `ipaddr.IPv6` objects have some methods defined, too. All of them have the same interface for both protocols, and are similar to global methods.
|
|---|
| 122 |
|
|---|
| 123 | `ipaddr.IPvX.isValid(string)` can be used to check if the string is a valid address for particular protocol, and `ipaddr.IPvX.parse(string)` is the error-throwing parser.
|
|---|
| 124 |
|
|---|
| 125 | `ipaddr.IPvX.isValid(string)` uses the same format for parsing as the POSIX `inet_ntoa` function, which accepts unusual formats like `0xc0.168.1.1` or `0x10000000`. The function `ipaddr.IPv4.isValidFourPartDecimal(string)` validates the IPv4 address and also ensures that it is written in four-part decimal format.
|
|---|
| 126 | `ipaddr.IPv4.isValidCIDRFourPartDecimal(string)` validates an IPv4 address in CIDR notation and also ensures that its address portion is written in four-part decimal format.
|
|---|
| 127 |
|
|---|
| 128 | [IPv6 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js#L530
|
|---|
| 129 | [IPv4 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js#L182
|
|---|
| 130 |
|
|---|
| 131 | #### IPv6 properties
|
|---|
| 132 |
|
|---|
| 133 | Sometimes you will want to convert IPv6 not to a compact string representation (with the `::` substitution); the `toNormalizedString()` method will return an address where all zeroes are explicit.
|
|---|
| 134 |
|
|---|
| 135 | For example:
|
|---|
| 136 |
|
|---|
| 137 | ```js
|
|---|
| 138 | const addr = ipaddr.parse('2001:0db8::0001');
|
|---|
| 139 | addr.toString(); // => '2001:db8::1'
|
|---|
| 140 | addr.toNormalizedString(); // => '2001:db8:0:0:0:0:0:1'
|
|---|
| 141 | ```
|
|---|
| 142 |
|
|---|
| 143 | The `isIPv4MappedAddress()` method will return `true` if this address is an IPv4-mapped
|
|---|
| 144 | one, and `toIPv4Address()` will return an IPv4 object address.
|
|---|
| 145 |
|
|---|
| 146 | To access the underlying binary representation of the address, use `addr.parts`.
|
|---|
| 147 |
|
|---|
| 148 | ```js
|
|---|
| 149 | const addr = ipaddr.parse('2001:db8:10::1234:DEAD');
|
|---|
| 150 | addr.parts // => [0x2001, 0xdb8, 0x10, 0, 0, 0, 0x1234, 0xdead]
|
|---|
| 151 | ```
|
|---|
| 152 |
|
|---|
| 153 | A IPv6 zone index can be accessed via `addr.zoneId`:
|
|---|
| 154 |
|
|---|
| 155 | ```js
|
|---|
| 156 | const addr = ipaddr.parse('2001:db8::%eth0');
|
|---|
| 157 | addr.zoneId // => 'eth0'
|
|---|
| 158 | ```
|
|---|
| 159 |
|
|---|
| 160 | #### IPv4 properties
|
|---|
| 161 |
|
|---|
| 162 | `toIPv4MappedAddress()` will return a corresponding IPv4-mapped IPv6 address.
|
|---|
| 163 |
|
|---|
| 164 | To access the underlying representation of the address, use `addr.octets`.
|
|---|
| 165 |
|
|---|
| 166 | ```js
|
|---|
| 167 | const addr = ipaddr.parse('192.168.1.1');
|
|---|
| 168 | addr.octets // => [192, 168, 1, 1]
|
|---|
| 169 | ```
|
|---|
| 170 |
|
|---|
| 171 | `prefixLengthFromSubnetMask()` will return a CIDR prefix length for a valid IPv4 netmask or
|
|---|
| 172 | null if the netmask is not valid.
|
|---|
| 173 |
|
|---|
| 174 | ```js
|
|---|
| 175 | ipaddr.IPv4.parse('255.255.255.240').prefixLengthFromSubnetMask() == 28
|
|---|
| 176 | ipaddr.IPv4.parse('255.192.164.0').prefixLengthFromSubnetMask() == null
|
|---|
| 177 | ```
|
|---|
| 178 |
|
|---|
| 179 | `subnetMaskFromPrefixLength()` will return an IPv4 netmask for a valid CIDR prefix length.
|
|---|
| 180 |
|
|---|
| 181 | ```js
|
|---|
| 182 | ipaddr.IPv4.subnetMaskFromPrefixLength(24) == '255.255.255.0'
|
|---|
| 183 | ipaddr.IPv4.subnetMaskFromPrefixLength(29) == '255.255.255.248'
|
|---|
| 184 | ```
|
|---|
| 185 |
|
|---|
| 186 | `broadcastAddressFromCIDR()` will return the broadcast address for a given IPv4 interface and netmask in CIDR notation.
|
|---|
| 187 | ```js
|
|---|
| 188 | ipaddr.IPv4.broadcastAddressFromCIDR('172.0.0.1/24') == '172.0.0.255'
|
|---|
| 189 | ```
|
|---|
| 190 | `networkAddressFromCIDR()` will return the network address for a given IPv4 interface and netmask in CIDR notation.
|
|---|
| 191 | ```js
|
|---|
| 192 | ipaddr.IPv4.networkAddressFromCIDR('172.0.0.1/24') == '172.0.0.0'
|
|---|
| 193 | ```
|
|---|
| 194 |
|
|---|
| 195 | #### Conversion
|
|---|
| 196 |
|
|---|
| 197 | IPv4 and IPv6 can be converted bidirectionally to and from network byte order (MSB) byte arrays.
|
|---|
| 198 |
|
|---|
| 199 | The `fromByteArray()` method will take an array and create an appropriate IPv4 or IPv6 object
|
|---|
| 200 | if the input satisfies the requirements. For IPv4 it has to be an array of four 8-bit values,
|
|---|
| 201 | while for IPv6 it has to be an array of sixteen 8-bit values.
|
|---|
| 202 |
|
|---|
| 203 | For example:
|
|---|
| 204 | ```js
|
|---|
| 205 | const addr = ipaddr.fromByteArray([0x7f, 0, 0, 1]);
|
|---|
| 206 | addr.toString(); // => '127.0.0.1'
|
|---|
| 207 | ```
|
|---|
| 208 |
|
|---|
| 209 | or
|
|---|
| 210 |
|
|---|
| 211 | ```js
|
|---|
| 212 | const addr = ipaddr.fromByteArray([0x20, 1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
|
|---|
| 213 | addr.toString(); // => '2001:db8::1'
|
|---|
| 214 | ```
|
|---|
| 215 |
|
|---|
| 216 | Both objects also offer a `toByteArray()` method, which returns an array in network byte order (MSB).
|
|---|
| 217 |
|
|---|
| 218 | For example:
|
|---|
| 219 | ```js
|
|---|
| 220 | const addr = ipaddr.parse('127.0.0.1');
|
|---|
| 221 | addr.toByteArray(); // => [0x7f, 0, 0, 1]
|
|---|
| 222 | ```
|
|---|
| 223 |
|
|---|
| 224 | or
|
|---|
| 225 |
|
|---|
| 226 | ```js
|
|---|
| 227 | const addr = ipaddr.parse('2001:db8::1');
|
|---|
| 228 | addr.toByteArray(); // => [0x20, 1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
|
|---|
| 229 | ```
|
|---|