ip-utilties 3.6.2 → 3.7.0
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 +41 -40
- package/types/ip.d.mts +2 -1
package/package.json
CHANGED
package/src/ip.mjs
CHANGED
|
@@ -23,22 +23,24 @@ const ipv4 = {
|
|
|
23
23
|
|
|
24
24
|
// https://www.geeksforgeeks.org/computer-networks/subnet-cheat-sheet/
|
|
25
25
|
wellKnownAddresses: [
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
[new Uint8Array([
|
|
30
|
-
[new Uint8Array([
|
|
31
|
-
[new Uint8Array([
|
|
32
|
-
[new Uint8Array([127, 0,
|
|
33
|
-
[new Uint8Array([172, 16, 0, 0]), 12], // Private network (RFC 1918)
|
|
34
|
-
[new Uint8Array([192, 0, 0, 0]), 24], // IETF protocol assignments
|
|
35
|
-
[new Uint8Array([192, 0, 2, 0]), 24], // TEST-NET-1
|
|
36
|
-
[new Uint8Array([192, 168, 0, 0]), 16], // Private network (RFC 1918)
|
|
37
|
-
[new Uint8Array([198, 18, 0, 0]), 15], // Network benchmark testing
|
|
38
|
-
[new Uint8Array([198, 51, 100, 0]), 24], // TEST-NET-2
|
|
39
|
-
[new Uint8Array([203, 0, 113, 0]), 24], // Reserved address space used for documentation
|
|
40
|
-
[new Uint8Array([240, 0, 0, 0]), 4], // Reserved for future use or experimental purposes
|
|
41
|
-
|
|
26
|
+
// mask length, prefix, link local,
|
|
27
|
+
[new Uint8Array([169, 254, 0, 0]), /* */ 16, 16, true], // Link-local address (Autoconfiguration)
|
|
28
|
+
|
|
29
|
+
[new Uint8Array([0, 0, 0, 0]), /* */ 8, 8], // This network
|
|
30
|
+
[new Uint8Array([10, 0, 0, 0]), /* */ 8, 8], // Private network (RFC 1918)
|
|
31
|
+
[new Uint8Array([100, 64, 0, 0]), /* */ 10, 10], // Carrier-grade NAT / Shared address space (CGN)
|
|
32
|
+
[new Uint8Array([127, 0, 0, 0]), /* */ 8, 8], // Loopback
|
|
33
|
+
[new Uint8Array([172, 16, 0, 0]), /* */ 12, 12], // Private network (RFC 1918)
|
|
34
|
+
[new Uint8Array([192, 0, 0, 0]), /* */ 24, 24], // IETF protocol assignments
|
|
35
|
+
[new Uint8Array([192, 0, 2, 0]), /* */ 24, 24], // TEST-NET-1
|
|
36
|
+
[new Uint8Array([192, 168, 0, 0]), /* */ 16, 16], // Private network (RFC 1918)
|
|
37
|
+
[new Uint8Array([198, 18, 0, 0]), /* */ 15, 15], // Network benchmark testing
|
|
38
|
+
[new Uint8Array([198, 51, 100, 0]), /* */ 24, 24], // TEST-NET-2
|
|
39
|
+
[new Uint8Array([203, 0, 113, 0]), /* */ 24, 24], // Reserved address space used for documentation
|
|
40
|
+
[new Uint8Array([240, 0, 0, 0]), /* */ 4, 4], // Reserved for future use or experimental purposes
|
|
41
|
+
|
|
42
|
+
[new Uint8Array([127, 0, 53, 53]), /* */ 0, 0], // Name collision occurrence
|
|
43
|
+
[new Uint8Array([255, 255, 255, 255]), /**/ 0, 0] // Limited Broadcast address
|
|
42
44
|
]
|
|
43
45
|
};
|
|
44
46
|
|
|
@@ -63,9 +65,10 @@ const ipv6 = {
|
|
|
63
65
|
localHost: new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]),
|
|
64
66
|
localHostPrefixLenth: 128,
|
|
65
67
|
wellKnownAddresses: [
|
|
66
|
-
[new Uint16Array([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 64, true],
|
|
67
|
-
[new Uint16Array([0xfd00, 0, 0, 0, 0, 0, 0, 0]), 8, false, true],
|
|
68
|
-
[new Uint16Array([
|
|
68
|
+
[new Uint16Array([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 64, 64, true],
|
|
69
|
+
[new Uint16Array([0xfd00, 0, 0, 0, 0, 0, 0, 0]), 8, 64, false, true],
|
|
70
|
+
[new Uint16Array([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7, 64, false, true],
|
|
71
|
+
[new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]), 128, 128, false, true]
|
|
69
72
|
]
|
|
70
73
|
};
|
|
71
74
|
|
|
@@ -286,18 +289,19 @@ export function normalizeCIDR(address) {
|
|
|
286
289
|
let longPrefix;
|
|
287
290
|
|
|
288
291
|
const family = isIPv6(prefix) ? ipv6 : ipv4;
|
|
292
|
+
let encoded = _encode(family, prefix);
|
|
293
|
+
const wns = _wellKnownSubnet(family, encoded);
|
|
289
294
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
prefixLength = wns[1];
|
|
295
|
+
if (wns && !prefixLength) {
|
|
296
|
+
prefixLength = wns[2];
|
|
293
297
|
prefix = _decode(
|
|
294
298
|
family,
|
|
295
|
-
_prefix(family,
|
|
299
|
+
_prefix(family, encoded, prefixLength),
|
|
296
300
|
prefixLength
|
|
297
301
|
);
|
|
298
|
-
longPrefix = wns[0];
|
|
302
|
+
longPrefix = _decode(family, wns[0]);
|
|
299
303
|
} else {
|
|
300
|
-
if (
|
|
304
|
+
if (_isUniqueLocal(encoded) || _isLinkLocal(family, encoded)) {
|
|
301
305
|
prefixLength = family.wellKnownAddresses[0][1]; // TODO
|
|
302
306
|
const n = _prefix(family, address, prefixLength);
|
|
303
307
|
prefix = _decode(family, n, prefixLength);
|
|
@@ -312,19 +316,15 @@ export function normalizeCIDR(address) {
|
|
|
312
316
|
} else {
|
|
313
317
|
prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
|
|
314
318
|
|
|
315
|
-
let n;
|
|
316
|
-
|
|
317
319
|
if (prefixLength) {
|
|
318
|
-
|
|
320
|
+
encoded = _prefix(family, prefix, prefixLength);
|
|
319
321
|
} else {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
if (isLocalhost(n)) {
|
|
322
|
+
if (_isLocalhost(family, encoded)) {
|
|
323
323
|
prefixLength = family.localHostPrefixLenth;
|
|
324
324
|
}
|
|
325
325
|
}
|
|
326
|
-
prefix = _decode(family,
|
|
327
|
-
longPrefix = _decode(family,
|
|
326
|
+
prefix = _decode(family, encoded, prefixLength);
|
|
327
|
+
longPrefix = _decode(family, encoded);
|
|
328
328
|
}
|
|
329
329
|
}
|
|
330
330
|
|
|
@@ -378,10 +378,14 @@ function _equal(a, b) {
|
|
|
378
378
|
return true;
|
|
379
379
|
}
|
|
380
380
|
|
|
381
|
+
export function _isLocalhost(family, encoded) {
|
|
382
|
+
return _equal(family.localHost, encoded);
|
|
383
|
+
}
|
|
384
|
+
|
|
381
385
|
export function isLocalhost(address) {
|
|
382
386
|
const family = _family(address);
|
|
383
387
|
if (family) {
|
|
384
|
-
return
|
|
388
|
+
return _isLocalhost(family, _encode(family, address));
|
|
385
389
|
}
|
|
386
390
|
return false;
|
|
387
391
|
}
|
|
@@ -389,7 +393,7 @@ export function isLocalhost(address) {
|
|
|
389
393
|
export function isLinkLocal(address) {
|
|
390
394
|
const family = _family(address);
|
|
391
395
|
if (family) {
|
|
392
|
-
return _isLinkLocal(family, address);
|
|
396
|
+
return _isLinkLocal(family, _encode(family, address));
|
|
393
397
|
}
|
|
394
398
|
return false;
|
|
395
399
|
}
|
|
@@ -414,13 +418,11 @@ export function hasWellKnownSubnet(address) {
|
|
|
414
418
|
export function wellKnownSubnet(address) {
|
|
415
419
|
const family = _family(address);
|
|
416
420
|
if (family) {
|
|
417
|
-
return _wellKnownSubnet(family, address);
|
|
421
|
+
return _wellKnownSubnet(family, _encode(family, address));
|
|
418
422
|
}
|
|
419
423
|
}
|
|
420
424
|
|
|
421
|
-
export function _wellKnownSubnet(family,
|
|
422
|
-
const encoded = _encode(family, address);
|
|
423
|
-
|
|
425
|
+
export function _wellKnownSubnet(family, encoded) {
|
|
424
426
|
for (const c of family.wellKnownAddresses) {
|
|
425
427
|
const pl = c[1];
|
|
426
428
|
if (pl > 0 && _prefix(family, c[0], pl) === _prefix(family, encoded, pl)) {
|
|
@@ -439,7 +441,6 @@ export function _wellKnownSubnet(family, address) {
|
|
|
439
441
|
|
|
440
442
|
/*
|
|
441
443
|
prefix global subnet interface
|
|
442
|
-
|
|
443
444
|
ff01:: ff02:: 1
|
|
444
445
|
ff05:: 2 3
|
|
445
446
|
|
package/types/ip.d.mts
CHANGED
|
@@ -49,6 +49,7 @@ export declare function normalizeCIDR(address: any): {
|
|
|
49
49
|
export declare function formatCIDR(address: any, prefixLength: any): any;
|
|
50
50
|
export declare function normalizeIP(address: any): string | undefined;
|
|
51
51
|
export declare function reverseArpa(address: any): string;
|
|
52
|
+
export declare function _isLocalhost(family: any, encoded: any): boolean;
|
|
52
53
|
export declare function isLocalhost(address: any): boolean;
|
|
53
54
|
export declare function isLinkLocal(address: any): boolean;
|
|
54
55
|
export declare function _isLinkLocal(family: any, address: any): boolean;
|
|
@@ -56,7 +57,7 @@ export declare function isUniqueLocal(address: any): boolean;
|
|
|
56
57
|
export declare function _isUniqueLocal(eaddr: any): boolean;
|
|
57
58
|
export declare function hasWellKnownSubnet(address: any): boolean;
|
|
58
59
|
export declare function wellKnownSubnet(address: any): any;
|
|
59
|
-
export declare function _wellKnownSubnet(family: any,
|
|
60
|
+
export declare function _wellKnownSubnet(family: any, encoded: any): any;
|
|
60
61
|
export declare const IPV6_NODE_LOCAL_ALL_NODES: any;
|
|
61
62
|
export declare const IPV6_NODE_LOCAL_ALL_ROUTERS: any;
|
|
62
63
|
export declare const IPV6_LINK_LOCAL_ALL_NODES: any;
|