ip-utilties 3.1.2 → 3.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/ip.mjs +40 -10
package/package.json
CHANGED
package/src/ip.mjs
CHANGED
|
@@ -17,7 +17,8 @@ const ipv4 = {
|
|
|
17
17
|
segments: 4,
|
|
18
18
|
segmentLength: 8,
|
|
19
19
|
segmentMask: 0xffn,
|
|
20
|
-
base: 10
|
|
20
|
+
base: 10,
|
|
21
|
+
linkLocalPrefixLength: 16
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
const ipv6 = {
|
|
@@ -37,7 +38,8 @@ const ipv6 = {
|
|
|
37
38
|
segments: 8,
|
|
38
39
|
segmentLength: 16,
|
|
39
40
|
segmentMask: 0xffffn,
|
|
40
|
-
base: 16
|
|
41
|
+
base: 16,
|
|
42
|
+
linkLocalPrefixLength: 64
|
|
41
43
|
};
|
|
42
44
|
|
|
43
45
|
/**
|
|
@@ -249,19 +251,23 @@ export function normalizeCIDR(address) {
|
|
|
249
251
|
let [prefix, prefixLength] = address.split(/\//);
|
|
250
252
|
let longPrefix;
|
|
251
253
|
|
|
254
|
+
const family = isIPv6(prefix) ? ipv6 : ipv4;
|
|
255
|
+
|
|
252
256
|
if (isUniqueLocal(address) || isLinkLocal(address)) {
|
|
253
|
-
prefixLength =
|
|
254
|
-
const n = _prefix(
|
|
255
|
-
prefix = _decode(
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
257
|
+
prefixLength = family.linkLocalPrefixLength;
|
|
258
|
+
const n = _prefix(family, address, prefixLength);
|
|
259
|
+
prefix = _decode(family, n, prefixLength);
|
|
260
|
+
|
|
261
|
+
if (family == ipv6) {
|
|
262
|
+
if (!prefix.endsWith("::")) {
|
|
263
|
+
// TODO
|
|
264
|
+
prefix += prefix.endsWith(":") ? ":" : "::";
|
|
265
|
+
}
|
|
259
266
|
}
|
|
260
267
|
longPrefix = prefix;
|
|
261
268
|
} else {
|
|
262
269
|
prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
|
|
263
270
|
|
|
264
|
-
const family = /*_family(prefix); */ isIPv6(prefix) ? ipv6 : ipv4;
|
|
265
271
|
let n;
|
|
266
272
|
|
|
267
273
|
if (prefixLength) {
|
|
@@ -331,7 +337,11 @@ export function isLocalhost(address) {
|
|
|
331
337
|
|
|
332
338
|
export function isLinkLocal(address) {
|
|
333
339
|
const eaddr = encodeIP(address);
|
|
334
|
-
|
|
340
|
+
if (eaddr) {
|
|
341
|
+
return eaddr[0] === 0xfe80 || (eaddr[0] === 169 && eaddr[1] === 254);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return false;
|
|
335
345
|
}
|
|
336
346
|
|
|
337
347
|
export function isUniqueLocal(address) {
|
|
@@ -343,6 +353,26 @@ export function hasWellKnownSubnet(address) {
|
|
|
343
353
|
return isLocalhost(address) || isLinkLocal(address) || isUniqueLocal(address);
|
|
344
354
|
}
|
|
345
355
|
|
|
356
|
+
/*
|
|
357
|
+
prefix global subnet interface
|
|
358
|
+
|
|
359
|
+
ff01:: ff02:: 1
|
|
360
|
+
ff05:: 2 3
|
|
361
|
+
|
|
362
|
+
https://en.wikipedia.org/wiki/Link-local_address
|
|
363
|
+
fe80:: 64 link local
|
|
364
|
+
169.254. /16 link local
|
|
365
|
+
|
|
366
|
+
https://en.wikipedia.org/wiki/Unique_local_address
|
|
367
|
+
fc00:: 64 unique local
|
|
368
|
+
fd00:: 64 unique local
|
|
369
|
+
|
|
370
|
+
::1 128. local host
|
|
371
|
+
127 8 local host
|
|
372
|
+
172
|
|
373
|
+
|
|
374
|
+
*/
|
|
375
|
+
|
|
346
376
|
/*
|
|
347
377
|
* https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml
|
|
348
378
|
*/
|