ip-utilties 3.6.2 → 3.7.1
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 +47 -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
|
|
|
@@ -216,6 +219,12 @@ function _is(family, address) {
|
|
|
216
219
|
return (
|
|
217
220
|
address instanceof family.factory && address.length === family.segments
|
|
218
221
|
);
|
|
222
|
+
|
|
223
|
+
case "bigint": {
|
|
224
|
+
if (family === ipv6 && address > 1n << BigInt(ipv4.bitLength)) {
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
219
228
|
}
|
|
220
229
|
|
|
221
230
|
return false;
|
|
@@ -286,18 +295,19 @@ export function normalizeCIDR(address) {
|
|
|
286
295
|
let longPrefix;
|
|
287
296
|
|
|
288
297
|
const family = isIPv6(prefix) ? ipv6 : ipv4;
|
|
298
|
+
let encoded = _encode(family, prefix);
|
|
299
|
+
const wns = _wellKnownSubnet(family, encoded);
|
|
289
300
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
prefixLength = wns[1];
|
|
301
|
+
if (wns && !prefixLength) {
|
|
302
|
+
prefixLength = wns[2];
|
|
293
303
|
prefix = _decode(
|
|
294
304
|
family,
|
|
295
|
-
_prefix(family,
|
|
305
|
+
_prefix(family, encoded, prefixLength),
|
|
296
306
|
prefixLength
|
|
297
307
|
);
|
|
298
|
-
longPrefix = wns[0];
|
|
308
|
+
longPrefix = _decode(family, wns[0]);
|
|
299
309
|
} else {
|
|
300
|
-
if (
|
|
310
|
+
if (_isUniqueLocal(encoded) || _isLinkLocal(family, encoded)) {
|
|
301
311
|
prefixLength = family.wellKnownAddresses[0][1]; // TODO
|
|
302
312
|
const n = _prefix(family, address, prefixLength);
|
|
303
313
|
prefix = _decode(family, n, prefixLength);
|
|
@@ -312,19 +322,15 @@ export function normalizeCIDR(address) {
|
|
|
312
322
|
} else {
|
|
313
323
|
prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
|
|
314
324
|
|
|
315
|
-
let n;
|
|
316
|
-
|
|
317
325
|
if (prefixLength) {
|
|
318
|
-
|
|
326
|
+
encoded = _prefix(family, prefix, prefixLength);
|
|
319
327
|
} else {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
if (isLocalhost(n)) {
|
|
328
|
+
if (_isLocalhost(family, encoded)) {
|
|
323
329
|
prefixLength = family.localHostPrefixLenth;
|
|
324
330
|
}
|
|
325
331
|
}
|
|
326
|
-
prefix = _decode(family,
|
|
327
|
-
longPrefix = _decode(family,
|
|
332
|
+
prefix = _decode(family, encoded, prefixLength);
|
|
333
|
+
longPrefix = _decode(family, encoded);
|
|
328
334
|
}
|
|
329
335
|
}
|
|
330
336
|
|
|
@@ -378,10 +384,14 @@ function _equal(a, b) {
|
|
|
378
384
|
return true;
|
|
379
385
|
}
|
|
380
386
|
|
|
387
|
+
export function _isLocalhost(family, encoded) {
|
|
388
|
+
return _equal(family.localHost, encoded);
|
|
389
|
+
}
|
|
390
|
+
|
|
381
391
|
export function isLocalhost(address) {
|
|
382
392
|
const family = _family(address);
|
|
383
393
|
if (family) {
|
|
384
|
-
return
|
|
394
|
+
return _isLocalhost(family, _encode(family, address));
|
|
385
395
|
}
|
|
386
396
|
return false;
|
|
387
397
|
}
|
|
@@ -389,7 +399,7 @@ export function isLocalhost(address) {
|
|
|
389
399
|
export function isLinkLocal(address) {
|
|
390
400
|
const family = _family(address);
|
|
391
401
|
if (family) {
|
|
392
|
-
return _isLinkLocal(family, address);
|
|
402
|
+
return _isLinkLocal(family, _encode(family, address));
|
|
393
403
|
}
|
|
394
404
|
return false;
|
|
395
405
|
}
|
|
@@ -414,13 +424,11 @@ export function hasWellKnownSubnet(address) {
|
|
|
414
424
|
export function wellKnownSubnet(address) {
|
|
415
425
|
const family = _family(address);
|
|
416
426
|
if (family) {
|
|
417
|
-
return _wellKnownSubnet(family, address);
|
|
427
|
+
return _wellKnownSubnet(family, _encode(family, address));
|
|
418
428
|
}
|
|
419
429
|
}
|
|
420
430
|
|
|
421
|
-
export function _wellKnownSubnet(family,
|
|
422
|
-
const encoded = _encode(family, address);
|
|
423
|
-
|
|
431
|
+
export function _wellKnownSubnet(family, encoded) {
|
|
424
432
|
for (const c of family.wellKnownAddresses) {
|
|
425
433
|
const pl = c[1];
|
|
426
434
|
if (pl > 0 && _prefix(family, c[0], pl) === _prefix(family, encoded, pl)) {
|
|
@@ -439,7 +447,6 @@ export function _wellKnownSubnet(family, address) {
|
|
|
439
447
|
|
|
440
448
|
/*
|
|
441
449
|
prefix global subnet interface
|
|
442
|
-
|
|
443
450
|
ff01:: ff02:: 1
|
|
444
451
|
ff05:: 2 3
|
|
445
452
|
|
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;
|