source: frontend/node_modules/ipaddr.js/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 8.6 KB
Line 
1# ipaddr.js — an IPv6 and IPv4 address manipulation library
2
3[![Build Status](https://github.com/whitequark/ipaddr.js/workflows/CI%20Tests/badge.svg)](https://github.com/whitequark/ipaddr.js/actions?query=workflow%3A%22CI+Tests%22)
4
5ipaddr.js is a small (1.9K minified and gzipped) library for manipulating
6IP addresses in JavaScript environments. It runs on both CommonJS runtimes
7(e.g. [nodejs]) and in a web browser.
8
9ipaddr.js allows you to verify and parse string representation of an IP
10address, match it against a CIDR range or range list, determine if it falls
11into some reserved ranges (examples include loopback and private ranges),
12and 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
22Use 2.x release for nodejs versions 10+.
23Use the 1.x release for versions of nodejs older than 10.
24
25## API
26
27ipaddr.js defines one object in the global scope: `ipaddr`. In CommonJS,
28it is exported from the module:
29
30```js
31const ipaddr = require('ipaddr.js');
32```
33
34The API consists of several global methods and two classes: ipaddr.IPv6 and ipaddr.IPv4.
35
36### Global methods
37
38There are four global methods defined: `ipaddr.isValid`, `ipaddr.isValidCIDR`,
39`ipaddr.parse`, and `ipaddr.process`. All of them receive a string as a single
40parameter.
41
42The `ipaddr.isValid` method returns `true` if the address is a valid IPv4 or
43IPv6 address, and `false` otherwise. It does not throw any exceptions.
44
45The `ipaddr.isValidCIDR` method returns `true` if the address is a valid IPv4 or
46IPv6 address in CIDR notation, and `false` otherwise. It does not throw any exceptions.
47
48The `ipaddr.parse` method returns an object representing the IP address,
49or throws an `Error` if the passed string is not a valid representation of an
50IP address.
51
52The `ipaddr.process` method works just like the `ipaddr.parse` one, but it
53automatically converts IPv4-mapped IPv6 addresses to their IPv4 counterparts
54before returning. It is useful when you have a Node.js instance listening
55on an IPv6 socket, and the `net.ipv6.bindv6only` sysctl parameter (or its
56equivalent on non-Linux OS) is set to 0. In this case, you can accept IPv4
57connections on your IPv6-only socket, but the remote address will be mangled.
58Use `ipaddr.process` method to automatically demangle it.
59
60### Object representation
61
62Parsing 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
67One can determine the type of address by calling `addr.kind()`. It will return
68either `"ipv6"` or `"ipv4"`.
69
70An address can be converted back to its string representation with `addr.toString()`.
71Note 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
76A `match(range, bits)` method can be used to check if the address falls into a
77certain CIDR range. Note that an address can be (obviously) matched only against an address of the same type.
78
79For example:
80
81```js
82const addr = ipaddr.parse('2001:db8:1234::1');
83const range = ipaddr.parse('2001:db8::');
84
85addr.match(range, 32); // => true
86```
87
88Alternatively, `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
90For example:
91
92```js
93const addr = ipaddr.parse('2001:db8:1234::1');
94
95addr.match(ipaddr.parseCIDR('2001:db8::/32')); // => true
96```
97
98A `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
100You 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
104const 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};
111ipaddr.subnetMatch(ipaddr.parse('2001:470:8:66::1'), rangeList, 'unknown'); // => "tunnelProviders"
112```
113
114The 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
117const bytes = ipaddr.parse('2a00:1450:8007::68').toByteArray(); // ipv6.google.com
118bytes // => [42, 0x00, 0x14, 0x50, 0x80, 0x07, 0x00, <zeroes...>, 0x00, 0x68 ]
119```
120
121The `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
133Sometimes 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
135For example:
136
137```js
138const addr = ipaddr.parse('2001:0db8::0001');
139addr.toString(); // => '2001:db8::1'
140addr.toNormalizedString(); // => '2001:db8:0:0:0:0:0:1'
141```
142
143The `isIPv4MappedAddress()` method will return `true` if this address is an IPv4-mapped
144one, and `toIPv4Address()` will return an IPv4 object address.
145
146To access the underlying binary representation of the address, use `addr.parts`.
147
148```js
149const addr = ipaddr.parse('2001:db8:10::1234:DEAD');
150addr.parts // => [0x2001, 0xdb8, 0x10, 0, 0, 0, 0x1234, 0xdead]
151```
152
153A IPv6 zone index can be accessed via `addr.zoneId`:
154
155```js
156const addr = ipaddr.parse('2001:db8::%eth0');
157addr.zoneId // => 'eth0'
158```
159
160#### IPv4 properties
161
162`toIPv4MappedAddress()` will return a corresponding IPv4-mapped IPv6 address.
163
164To access the underlying representation of the address, use `addr.octets`.
165
166```js
167const addr = ipaddr.parse('192.168.1.1');
168addr.octets // => [192, 168, 1, 1]
169```
170
171`prefixLengthFromSubnetMask()` will return a CIDR prefix length for a valid IPv4 netmask or
172null if the netmask is not valid.
173
174```js
175ipaddr.IPv4.parse('255.255.255.240').prefixLengthFromSubnetMask() == 28
176ipaddr.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
182ipaddr.IPv4.subnetMaskFromPrefixLength(24) == '255.255.255.0'
183ipaddr.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
188ipaddr.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
192ipaddr.IPv4.networkAddressFromCIDR('172.0.0.1/24') == '172.0.0.0'
193```
194
195#### Conversion
196
197IPv4 and IPv6 can be converted bidirectionally to and from network byte order (MSB) byte arrays.
198
199The `fromByteArray()` method will take an array and create an appropriate IPv4 or IPv6 object
200if the input satisfies the requirements. For IPv4 it has to be an array of four 8-bit values,
201while for IPv6 it has to be an array of sixteen 8-bit values.
202
203For example:
204```js
205const addr = ipaddr.fromByteArray([0x7f, 0, 0, 1]);
206addr.toString(); // => '127.0.0.1'
207```
208
209or
210
211```js
212const addr = ipaddr.fromByteArray([0x20, 1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
213addr.toString(); // => '2001:db8::1'
214```
215
216Both objects also offer a `toByteArray()` method, which returns an array in network byte order (MSB).
217
218For example:
219```js
220const addr = ipaddr.parse('127.0.0.1');
221addr.toByteArray(); // => [0x7f, 0, 0, 1]
222```
223
224or
225
226```js
227const addr = ipaddr.parse('2001:db8::1');
228addr.toByteArray(); // => [0x20, 1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
229```
Note: See TracBrowser for help on using the repository browser.