Debian Codegraph @

node/ipaddr.js/README.md

raw
1 # ipaddr.js — an IPv4 and IPv6 address manipulation library
2
3 [![CI Tests][ci-img]][ci-url]
4
5 ipaddr.js is a library for manipulating IPv4 and IPv6 addresses in JavaScript. It runs in Node.js and in browsers.
6
7 It lets you parse and validate addresses, match them against CIDR ranges, classify them into named ranges (loopback, private, reserved, etc.), convert between IPv4 and IPv6 representations, and work with raw byte arrays.
8
9 ## Installation
10
11 ```
12 npm install ipaddr.js
13 ```
14
15 ## Node.js version support
16
17 - ipaddr.js 2.x — Node.js 10+
18 - ipaddr.js 1.x — Node.js < 10
19
20 ## Quick start
21
22 ```js
23 const ipaddr = require('ipaddr.js');
24
25 ipaddr.isValid('192.168.1.1'); // => true
26 ipaddr.isValid('2001:db8::1'); // => true
27 ipaddr.isValid('not an address'); // => false
28
29 const addr = ipaddr.parse('2001:db8::1');
30 addr.kind(); // => 'ipv6'
31 addr.toString(); // => '2001:db8::1'
32
33 const [network, prefix] = ipaddr.parseCIDR('10.0.0.0/8');
34 network.toString(); // => '10.0.0.0'
35 prefix; // => 8
36 ```
37
38 ## Table of contents
39
40 - [Global API](#global-api)
41 - [isValid](#ipaddrIsValid)
42 - [isValidCIDR](#ipaddrIsValidCIDR)
43 - [parse](#ipaddrParse)
44 - [parseCIDR](#ipaddrParseCIDR)
45 - [process](#ipaddrProcess)
46 - [subnetMatch](#ipaddrSubnetMatch)
47 - [fromByteArray](#ipaddrFromByteArray)
48 - [IPv4 API](#ipv4-api)
49 - Static: [isValid](#ipv4-isvalid), [isValidCIDR](#ipv4-isvalidcidr), [isValidFourPartDecimal](#ipv4-isvalidfourpartdecimal), [isValidCIDRFourPartDecimal](#ipv4-isvalidcidrfourpartdecimal), [isIPv4](#ipv4-isipv4), [parse](#ipv4-parse), [parseCIDR](#ipv4-parsecidr), [broadcastAddressFromCIDR](#ipv4-broadcastaddressfromcidr), [networkAddressFromCIDR](#ipv4-networkaddressfromcidr), [subnetMaskFromPrefixLength](#ipv4-subnetmaskfromprefixlength)
50 - Instance: [octets](#ipv4-octets), [kind()](#ipv4-kind), [match()](#ipv4-match), [range()](#ipv4-range), [prefixLengthFromSubnetMask()](#ipv4-prefixlengthfromsubnetmask), [subnetMatch()](#ipv4-subnetmatch-instance), [toByteArray()](#ipv4-tobytearray), [toIPv4MappedAddress()](#ipv4-toipv4mappedaddress), [toNormalizedString()](#ipv4-tonormalizedstring), [toString()](#ipv4-tostring)
51 - [IPv6 API](#ipv6-api)
52 - Static: [isValid](#ipv6-isvalid), [isValidCIDR](#ipv6-isvalidcidr), [isIPv6](#ipv6-isipv6), [parse](#ipv6-parse), [parseCIDR](#ipv6-parsecidr), [broadcastAddressFromCIDR](#ipv6-broadcastaddressfromcidr), [networkAddressFromCIDR](#ipv6-networkaddressfromcidr), [subnetMaskFromPrefixLength](#ipv6-subnetmaskfromprefixlength)
53 - Instance: [parts](#ipv6-parts), [zoneId](#ipv6-zoneid), [kind()](#ipv6-kind), [isIPv4MappedAddress()](#ipv6-isipv4mappedaddress), [match()](#ipv6-match), [range()](#ipv6-range), [prefixLengthFromSubnetMask()](#ipv6-prefixlengthfromsubnetmask), [subnetMatch()](#ipv6-subnetmatch-instance), [toByteArray()](#ipv6-tobytearray), [toFixedLengthString()](#ipv6-tofixedlengthstring), [toIPv4Address()](#ipv6-toipv4address), [toNormalizedString()](#ipv6-tonormalizedstring), [toRFC5952String()](#ipv6-torfc5952string), [toString()](#ipv6-tostring)
54
55 ---
56
57 ## Global API
58
59 ```js
60 const ipaddr = require('ipaddr.js');
61 ```
62
63 <a name="ipaddrIsValid"></a>
64 ### `ipaddr.isValid(string)`
65
66 Returns `true` if the string is a valid IPv4 or IPv6 address; `false` otherwise. Never throws.
67
68 ```js
69 ipaddr.isValid('192.168.1.1'); // => true
70 ipaddr.isValid('2001:db8::1'); // => true
71 ipaddr.isValid('999.0.0.1'); // => false
72 ipaddr.isValid('hello'); // => false
73 ```
74
75 <a name="ipaddrIsValidCIDR"></a>
76 ### `ipaddr.isValidCIDR(string)`
77
78 Returns `true` if the string is a valid IPv4 or IPv6 address in CIDR notation; `false` otherwise. Never throws.
79
80 ```js
81 ipaddr.isValidCIDR('192.168.0.0/24'); // => true
82 ipaddr.isValidCIDR('2001:db8::/32'); // => true
83 ipaddr.isValidCIDR('192.168.0.1/33'); // => false
84 ```
85
86 <a name="ipaddrParse"></a>
87 ### `ipaddr.parse(string)`
88
89 Parses the string and returns an `IPv4` or `IPv6` object. Throws if the string is not a valid address.
90
91 ```js
92 const v4 = ipaddr.parse('192.168.1.1');
93 v4.kind(); // => 'ipv4'
94 v4.toString(); // => '192.168.1.1'
95
96 const v6 = ipaddr.parse('2001:db8::1');
97 v6.kind(); // => 'ipv6'
98 v6.toString(); // => '2001:db8::1'
99 ```
100
101 <a name="ipaddrParseCIDR"></a>
102 ### `ipaddr.parseCIDR(string)`
103
104 Parses an IP address with a CIDR prefix length and returns a two-element array `[address, prefixLength]`. Throws if the input is not valid CIDR notation.
105
106 ```js
107 const [addr, prefix] = ipaddr.parseCIDR('192.168.1.0/24');
108 addr.toString(); // => '192.168.1.0'
109 prefix; // => 24
110 ```
111
112 The returned array can be passed directly to `addr.match()`:
113
114 ```js
115 const addr = ipaddr.parse('192.168.1.42');
116 addr.match(ipaddr.parseCIDR('192.168.1.0/24')); // => true
117 ```
118
119 <a name="ipaddrProcess"></a>
120 ### `ipaddr.process(string)`
121
122 Like `ipaddr.parse()`, but automatically converts IPv4-mapped IPv6 addresses (e.g. `::ffff:192.168.1.1`) to their IPv4 equivalents. All other addresses are returned as-is.
123
124 This is useful when accepting connections on a dual-stack IPv6 socket, where IPv4 client addresses appear as IPv4-mapped IPv6 addresses.
125
126 ```js
127 ipaddr.process('::ffff:192.168.1.1').toString(); // => '192.168.1.1'
128 ipaddr.process('::ffff:192.168.1.1').kind(); // => 'ipv4'
129 ipaddr.process('2001:db8::1').kind(); // => 'ipv6'
130 ipaddr.process('192.168.1.1').kind(); // => 'ipv4'
131 ```
132
133 <a name="ipaddrSubnetMatch"></a>
134 ### `ipaddr.subnetMatch(address, rangeList[, defaultName])`
135
136 Matches `address` against a map of named CIDR ranges and returns the name of the first matching range. Returns `defaultName` (default: `'unicast'`) if no range matches.
137
138 Each `rangeList` value is either a single `[address, prefixLength]` pair or an array of such pairs. The list may mix IPv4 and IPv6 entries; entries of the wrong address family are safely skipped.
139
140 ```js
141 const rangeList = {
142 private: [
143 [ipaddr.parse('10.0.0.0'), 8],
144 [ipaddr.parse('172.16.0.0'), 12],
145 [ipaddr.parse('192.168.0.0'), 16],
146 ],
147 loopback: [ ipaddr.parse('127.0.0.0'), 8 ],
148 };
149
150 ipaddr.subnetMatch(ipaddr.parse('192.168.1.1'), rangeList); // => 'private'
151 ipaddr.subnetMatch(ipaddr.parse('127.0.0.1'), rangeList); // => 'loopback'
152 ipaddr.subnetMatch(ipaddr.parse('8.8.8.8'), rangeList, 'public'); // => 'public'
153 ipaddr.subnetMatch(ipaddr.parse('8.8.8.8'), rangeList); // => 'unicast'
154 ```
155
156 <a name="ipaddrFromByteArray"></a>
157 ### `ipaddr.fromByteArray(bytes)`
158
159 Constructs an IPv4 or IPv6 address from a byte array in network byte order (MSB first). Accepts 4 bytes for IPv4, or 16 bytes for IPv6. Throws if the array length is not 4 or 16.
160
161 ```js
162 ipaddr.fromByteArray([127, 0, 0, 1]).toString(); // => '127.0.0.1'
163
164 ipaddr.fromByteArray([
165 0x20, 0x01, 0x0d, 0xb8,
166 0x00, 0x00, 0x00, 0x00,
167 0x00, 0x00, 0x00, 0x00,
168 0x00, 0x00, 0x00, 0x01,
169 ]).toString(); // => '2001:db8::1'
170 ```
171
172 ---
173
174 ## IPv4 API
175
176 ### Static methods
177
178 <a name="ipv4-isvalid"></a>
179 #### `ipaddr.IPv4.isValid(string)`
180
181 Returns `true` if the string is a valid IPv4 address; `false` otherwise. Never throws.
182
183 Accepts the same extended formats as `ipaddr.IPv4.parse()` (hex, octal, fewer-than-four parts). For strict four-part decimal validation, use `isValidFourPartDecimal()`.
184
185 ```js
186 ipaddr.IPv4.isValid('192.168.1.1'); // => true
187 ipaddr.IPv4.isValid('0xc0.168.1.1'); // => true (hex octet)
188 ipaddr.IPv4.isValid('999.0.0.1'); // => false
189 ```
190
191 <a name="ipv4-isvalidcidr"></a>
192 #### `ipaddr.IPv4.isValidCIDR(string)`
193
194 Returns `true` if the string is a valid IPv4 CIDR address; `false` otherwise. Never throws.
195
196 ```js
197 ipaddr.IPv4.isValidCIDR('192.168.0.0/24'); // => true
198 ipaddr.IPv4.isValidCIDR('192.168.0.0/33'); // => false
199 ```
200
201 <a name="ipv4-isvalidfourpartdecimal"></a>
202 #### `ipaddr.IPv4.isValidFourPartDecimal(string)`
203
204 Like `isValid()`, but only accepts the standard four-part dotted-decimal format. Rejects hex, octal, and other non-standard notations accepted by `inet_aton`.
205
206 ```js
207 ipaddr.IPv4.isValidFourPartDecimal('192.168.1.1'); // => true
208 ipaddr.IPv4.isValidFourPartDecimal('0xc0.168.1.1'); // => false
209 ```
210
211 <a name="ipv4-isvalidcidrfourpartdecimal"></a>
212 #### `ipaddr.IPv4.isValidCIDRFourPartDecimal(string)`
213
214 Like `isValidCIDR()`, but only accepts the standard four-part dotted-decimal format for the address portion.
215
216 ```js
217 ipaddr.IPv4.isValidCIDRFourPartDecimal('192.168.0.0/24'); // => true
218 ipaddr.IPv4.isValidCIDRFourPartDecimal('0xc0.168.0.0/24'); // => false
219 ```
220
221 <a name="ipv4-isipv4"></a>
222 #### `ipaddr.IPv4.isIPv4(string)`
223
224 Returns `true` if the string matches the IPv4 address pattern. This is a lightweight regex check — it does not validate that all octets are in range (0–255). Prefer `isValid()` for authoritative validation.
225
226 ```js
227 ipaddr.IPv4.isIPv4('192.168.1.1'); // => true
228 ipaddr.IPv4.isIPv4('2001:db8::1'); // => false
229 ```
230
231 <a name="ipv4-parse"></a>
232 #### `ipaddr.IPv4.parse(string)`
233
234 Parses the string and returns an `IPv4` object. Throws if the string is not a valid IPv4 address.
235
236 In addition to standard dotted-decimal, the parser accepts formats recognised by the POSIX `inet_aton` function: hex octets (`0xc0.0xa8.0x01.0x01`), octal octets (`0300.0250.01.01`), three-part (`192.168.257`), two-part (`192.11010049`), and single-value (`3232235777`) notation.
237
238 ```js
239 ipaddr.IPv4.parse('192.168.1.1').toString(); // => '192.168.1.1'
240 ipaddr.IPv4.parse('0xc0.168.1.1').toString(); // => '192.168.1.1'
241 ```
242
243 <a name="ipv4-parsecidr"></a>
244 #### `ipaddr.IPv4.parseCIDR(string)`
245
246 Parses an IPv4 CIDR address and returns `[IPv4, prefixLength]`. Throws if the input is invalid.
247
248 ```js
249 const [addr, prefix] = ipaddr.IPv4.parseCIDR('192.168.1.0/24');
250 addr.toString(); // => '192.168.1.0'
251 prefix; // => 24
252 ```
253
254 <a name="ipv4-broadcastaddressfromcidr"></a>
255 #### `ipaddr.IPv4.broadcastAddressFromCIDR(string)`
256
257 Returns the broadcast address for the given IPv4 CIDR block.
258
259 ```js
260 ipaddr.IPv4.broadcastAddressFromCIDR('192.168.1.0/24').toString(); // => '192.168.1.255'
261 ipaddr.IPv4.broadcastAddressFromCIDR('10.0.0.1/8').toString(); // => '10.255.255.255'
262 ```
263
264 <a name="ipv4-networkaddressfromcidr"></a>
265 #### `ipaddr.IPv4.networkAddressFromCIDR(string)`
266
267 Returns the network address for the given IPv4 CIDR block.
268
269 ```js
270 ipaddr.IPv4.networkAddressFromCIDR('192.168.1.42/24').toString(); // => '192.168.1.0'
271 ipaddr.IPv4.networkAddressFromCIDR('10.1.2.3/8').toString(); // => '10.0.0.0'
272 ```
273
274 <a name="ipv4-subnetmaskfromprefixlength"></a>
275 #### `ipaddr.IPv4.subnetMaskFromPrefixLength(prefix)`
276
277 Returns the IPv4 subnet mask corresponding to the given CIDR prefix length.
278
279 ```js
280 ipaddr.IPv4.subnetMaskFromPrefixLength(24).toString(); // => '255.255.255.0'
281 ipaddr.IPv4.subnetMaskFromPrefixLength(16).toString(); // => '255.255.0.0'
282 ```
283
284 ### Instance properties and methods
285
286 <a name="ipv4-octets"></a>
287 #### `addr.octets`
288
289 The four octets of the address as an array of numbers.
290
291 ```js
292 ipaddr.parse('192.168.1.1').octets; // => [192, 168, 1, 1]
293 ```
294
295 <a name="ipv4-kind"></a>
296 #### `addr.kind()`
297
298 Always returns `'ipv4'`.
299
300 ```js
301 ipaddr.parse('192.168.1.1').kind(); // => 'ipv4'
302 ```
303
304 <a name="ipv4-match"></a>
305 #### `addr.match(other, cidrBits)` / `addr.match([address, cidrBits])`
306
307 Returns `true` if the address falls within the given CIDR range.
308
309 ```js
310 const addr = ipaddr.parse('192.168.1.42');
311
312 addr.match(ipaddr.parse('192.168.1.0'), 24); // => true
313 addr.match(ipaddr.parse('10.0.0.0'), 8); // => false
314
315 // Accepts the tuple returned by parseCIDR:
316 addr.match(ipaddr.parseCIDR('192.168.1.0/24')); // => true
317 ```
318
319 <a name="ipv4-range"></a>
320 #### `addr.range()`
321
322 Returns the name of the built-in range the address belongs to, or `'unicast'` if it does not match any special range. Recognised names: `'unspecified'`, `'broadcast'`, `'multicast'`, `'linkLocal'`, `'loopback'`, `'carrierGradeNat'`, `'private'`, `'reserved'`, `'as112'`, `'amt'`.
323
324 See the [source][IPv4 ranges] for the full CIDR list associated with each name.
325
326 ```js
327 ipaddr.parse('127.0.0.1').range(); // => 'loopback'
328 ipaddr.parse('192.168.1.1').range(); // => 'private'
329 ipaddr.parse('169.254.1.1').range(); // => 'linkLocal'
330 ipaddr.parse('8.8.8.8').range(); // => 'unicast'
331 ```
332
333 <a name="ipv4-prefixlengthfromsubnetmask"></a>
334 #### `addr.prefixLengthFromSubnetMask()`
335
336 Returns the CIDR prefix length if this address is a valid contiguous subnet mask, or `null` otherwise.
337
338 ```js
339 ipaddr.parse('255.255.255.0').prefixLengthFromSubnetMask(); // => 24
340 ipaddr.parse('255.255.255.240').prefixLengthFromSubnetMask(); // => 28
341 ipaddr.parse('255.192.168.0').prefixLengthFromSubnetMask(); // => null
342 ```
343
344 <a name="ipv4-subnetmatch-instance"></a>
345 #### `addr.subnetMatch(rangeList[, defaultName])`
346
347 Instance-method shorthand for `ipaddr.subnetMatch(addr, rangeList, defaultName)`.
348
349 ```js
350 const addr = ipaddr.parse('192.168.1.1');
351 addr.subnetMatch({ private: [ipaddr.parse('192.168.0.0'), 16] }); // => 'private'
352 ```
353
354 <a name="ipv4-tobytearray"></a>
355 #### `addr.toByteArray()`
356
357 Returns the address as an array of four bytes in network byte order.
358
359 ```js
360 ipaddr.parse('192.168.1.1').toByteArray(); // => [192, 168, 1, 1]
361 ipaddr.parse('127.0.0.1').toByteArray(); // => [127, 0, 0, 1]
362 ```
363
364 <a name="ipv4-toipv4mappedaddress"></a>
365 #### `addr.toIPv4MappedAddress()`
366
367 Returns the IPv4-mapped IPv6 representation of this address (`::ffff:x.x.x.x`).
368
369 ```js
370 ipaddr.parse('192.168.1.1').toIPv4MappedAddress().toString(); // => '::ffff:c0a8:101'
371 ```
372
373 <a name="ipv4-tonormalizedstring"></a>
374 #### `addr.toNormalizedString()`
375
376 Returns the address in standard four-part dotted-decimal notation. For IPv4, this is the same as `toString()`.
377
378 ```js
379 ipaddr.parse('192.168.1.1').toNormalizedString(); // => '192.168.1.1'
380 ```
381
382 <a name="ipv4-tostring"></a>
383 #### `addr.toString()`
384
385 Returns the address as a dotted-decimal string.
386
387 ```js
388 ipaddr.parse('192.168.001.001').toString(); // => '192.168.1.1'
389 ```
390
391 ---
392
393 ## IPv6 API
394
395 ### Static methods
396
397 <a name="ipv6-isvalid"></a>
398 #### `ipaddr.IPv6.isValid(string)`
399
400 Returns `true` if the string is a valid IPv6 address; `false` otherwise. Never throws. Accepts addresses with a zone ID (e.g. `fe80::1%eth0`).
401
402 ```js
403 ipaddr.IPv6.isValid('2001:db8::1'); // => true
404 ipaddr.IPv6.isValid('::1'); // => true
405 ipaddr.IPv6.isValid('fe80::1%eth0'); // => true
406 ipaddr.IPv6.isValid('192.168.1.1'); // => false
407 ```
408
409 <a name="ipv6-isvalidcidr"></a>
410 #### `ipaddr.IPv6.isValidCIDR(string)`
411
412 Returns `true` if the string is a valid IPv6 CIDR address; `false` otherwise. Never throws.
413
414 ```js
415 ipaddr.IPv6.isValidCIDR('2001:db8::/32'); // => true
416 ipaddr.IPv6.isValidCIDR('2001:db8::/129'); // => false
417 ```
418
419 <a name="ipv6-isipv6"></a>
420 #### `ipaddr.IPv6.isIPv6(string)`
421
422 Returns `true` if the string matches the IPv6 address pattern. This is a lightweight regex check and does not fully validate all groups. Prefer `isValid()` for authoritative validation.
423
424 ```js
425 ipaddr.IPv6.isIPv6('2001:db8::1'); // => true
426 ipaddr.IPv6.isIPv6('192.168.1.1'); // => false
427 ```
428
429 <a name="ipv6-parse"></a>
430 #### `ipaddr.IPv6.parse(string)`
431
432 Parses the string and returns an `IPv6` object. Throws if the string is not a valid IPv6 address.
433
434 ```js
435 ipaddr.IPv6.parse('2001:db8::1').toString(); // => '2001:db8::1'
436 ipaddr.IPv6.parse('::ffff:192.168.1.1').toString(); // => '::ffff:c0a8:101'
437 ```
438
439 <a name="ipv6-parsecidr"></a>
440 #### `ipaddr.IPv6.parseCIDR(string)`
441
442 Parses an IPv6 CIDR address and returns `[IPv6, prefixLength]`. Throws if the input is invalid.
443
444 ```js
445 const [addr, prefix] = ipaddr.IPv6.parseCIDR('2001:db8::/32');
446 addr.toString(); // => '2001:db8::'
447 prefix; // => 32
448 ```
449
450 <a name="ipv6-broadcastaddressfromcidr"></a>
451 #### `ipaddr.IPv6.broadcastAddressFromCIDR(string)`
452
453 Returns the last address in the given IPv6 CIDR block (the IPv6 equivalent of the broadcast address).
454
455 ```js
456 ipaddr.IPv6.broadcastAddressFromCIDR('2001:db8::/120').toString(); // => '2001:db8::ff'
457 ```
458
459 <a name="ipv6-networkaddressfromcidr"></a>
460 #### `ipaddr.IPv6.networkAddressFromCIDR(string)`
461
462 Returns the network address for the given IPv6 CIDR block.
463
464 ```js
465 ipaddr.IPv6.networkAddressFromCIDR('2001:db8::42/32').toString(); // => '2001:db8::'
466 ```
467
468 <a name="ipv6-subnetmaskfromprefixlength"></a>
469 #### `ipaddr.IPv6.subnetMaskFromPrefixLength(prefix)`
470
471 Returns the IPv6 subnet mask corresponding to the given CIDR prefix length.
472
473 ```js
474 ipaddr.IPv6.subnetMaskFromPrefixLength(64).toString(); // => 'ffff:ffff:ffff:ffff::'
475 ipaddr.IPv6.subnetMaskFromPrefixLength(48).toString(); // => 'ffff:ffff:ffff::'
476 ```
477
478 ### Instance properties and methods
479
480 <a name="ipv6-parts"></a>
481 #### `addr.parts`
482
483 The eight 16-bit groups of the address as an array of numbers.
484
485 ```js
486 ipaddr.parse('2001:db8:10::1234:dead').parts;
487 // => [0x2001, 0x0db8, 0x0010, 0, 0, 0, 0x1234, 0xdead]
488 ```
489
490 <a name="ipv6-zoneid"></a>
491 #### `addr.zoneId`
492
493 The zone ID string for link-local addresses, or `undefined` if absent.
494
495 ```js
496 ipaddr.parse('fe80::1%eth0').zoneId; // => 'eth0'
497 ipaddr.parse('2001:db8::1').zoneId; // => undefined
498 ```
499
500 <a name="ipv6-kind"></a>
501 #### `addr.kind()`
502
503 Always returns `'ipv6'`.
504
505 ```js
506 ipaddr.parse('2001:db8::1').kind(); // => 'ipv6'
507 ```
508
509 <a name="ipv6-isipv4mappedaddress"></a>
510 #### `addr.isIPv4MappedAddress()`
511
512 Returns `true` if this is an IPv4-mapped IPv6 address (i.e. in the `::ffff:0:0/96` range).
513
514 ```js
515 ipaddr.parse('::ffff:192.168.1.1').isIPv4MappedAddress(); // => true
516 ipaddr.parse('2001:db8::1').isIPv4MappedAddress(); // => false
517 ```
518
519 <a name="ipv6-match"></a>
520 #### `addr.match(other, cidrBits)` / `addr.match([address, cidrBits])`
521
522 Returns `true` if the address falls within the given CIDR range.
523
524 ```js
525 const addr = ipaddr.parse('2001:db8:1234::1');
526
527 addr.match(ipaddr.parse('2001:db8::'), 32); // => true
528 addr.match(ipaddr.parseCIDR('2001:db8::/32')); // => true
529 addr.match(ipaddr.parseCIDR('2001:db9::/32')); // => false
530 ```
531
532 <a name="ipv6-range"></a>
533 #### `addr.range()`
534
535 Returns the name of the built-in range the address belongs to, or `'unicast'` if it does not match any. Recognised names include: `'unspecified'`, `'linkLocal'`, `'multicast'`, `'loopback'`, `'uniqueLocal'`, `'ipv4Mapped'`, `'reserved'`, and others.
536
537 See the [source][IPv6 ranges] for the full list.
538
539 ```js
540 ipaddr.parse('::1').range(); // => 'loopback'
541 ipaddr.parse('fe80::1').range(); // => 'linkLocal'
542 ipaddr.parse('fc00::1').range(); // => 'uniqueLocal'
543 ipaddr.parse('2001:db8::1').range(); // => 'reserved'
544 ipaddr.parse('2607:f8b0::1').range(); // => 'unicast'
545 ```
546
547 <a name="ipv6-prefixlengthfromsubnetmask"></a>
548 #### `addr.prefixLengthFromSubnetMask()`
549
550 Returns the CIDR prefix length if this address is a valid contiguous subnet mask, or `null` otherwise.
551
552 ```js
553 ipaddr.parse('ffff:ffff:ffff:ffff::').prefixLengthFromSubnetMask(); // => 64
554 ipaddr.parse('ffff:ffff::').prefixLengthFromSubnetMask(); // => 32
555 ```
556
557 <a name="ipv6-subnetmatch-instance"></a>
558 #### `addr.subnetMatch(rangeList[, defaultName])`
559
560 Instance-method shorthand for `ipaddr.subnetMatch(addr, rangeList, defaultName)`.
561
562 ```js
563 const addr = ipaddr.parse('2001:db8::1');
564 const ranges = { documentation: [ipaddr.parse('2001:db8::'), 32] };
565 addr.subnetMatch(ranges); // => 'documentation'
566 ```
567
568 <a name="ipv6-tobytearray"></a>
569 #### `addr.toByteArray()`
570
571 Returns the address as an array of 16 bytes in network byte order.
572
573 ```js
574 ipaddr.parse('2001:db8::1').toByteArray();
575 // => [0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
576 ```
577
578 <a name="ipv6-tofixedlengthstring"></a>
579 #### `addr.toFixedLengthString()`
580
581 Returns the address with all eight groups fully expanded to four hex digits, separated by colons. No `::` abbreviation is used.
582
583 ```js
584 ipaddr.parse('2001:db8::1').toFixedLengthString();
585 // => '2001:0db8:0000:0000:0000:0000:0000:0001'
586 ```
587
588 <a name="ipv6-toipv4address"></a>
589 #### `addr.toIPv4Address()`
590
591 Converts an IPv4-mapped IPv6 address to its IPv4 equivalent. Throws if the address is not IPv4-mapped.
592
593 ```js
594 ipaddr.parse('::ffff:192.168.1.1').toIPv4Address().toString(); // => '192.168.1.1'
595 ```
596
597 <a name="ipv6-tonormalizedstring"></a>
598 #### `addr.toNormalizedString()`
599
600 Returns the address with all eight 16-bit groups written out in lowercase hex, separated by colons, without `::` compression.
601
602 ```js
603 ipaddr.parse('2001:db8::1').toNormalizedString(); // => '2001:db8:0:0:0:0:0:1'
604 ```
605
606 <a name="ipv6-torfc5952string"></a>
607 #### `addr.toRFC5952String()`
608
609 Returns the address in the canonical format defined by [RFC 5952]: lowercase hex, leading zeros omitted, and the longest run of consecutive all-zero groups replaced by `::`.
610
611 ```js
612 ipaddr.parse('2001:0db8:0000:0000:0000:0000:0000:0001').toRFC5952String();
613 // => '2001:db8::1'
614 ```
615
616 <a name="ipv6-tostring"></a>
617 #### `addr.toString()`
618
619 Returns the compact string representation of the address, identical to `toRFC5952String()`.
620
621 ```js
622 ipaddr.parse('2001:0db8::0001').toString(); // => '2001:db8::1'
623 ```
624
625 ---
626
627 [IPv4 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js#L184
628 [IPv6 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js#L562
629 [RFC 5952]: https://datatracker.ietf.org/doc/html/rfc5952
630 [nodejs]: http://nodejs.org
631 [ci-url]: https://github.com/whitequark/ipaddr.js/actions?query=workflow%3A%22CI+Tests%22
632 [ci-img]: https://github.com/whitequark/ipaddr.js/workflows/CI%20Tests/badge.svg