ip-utilties 3.6.0 → 3.6.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/ip.mjs +44 -42
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "3.6.0",
3
+ "version": "3.6.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/ip.mjs CHANGED
@@ -18,19 +18,18 @@ const ipv4 = {
18
18
  segmentLength: 8,
19
19
  segmentMask: 0xffn,
20
20
  base: 10,
21
- linkLocalPrefix: new Uint8Array([169, 254]),
22
- linkLocalPrefixLength: 16,
23
21
  localHost: new Uint8Array([127, 0, 0, 1]),
24
22
  localHostPrefixLenth: 8,
25
23
 
26
24
  // https://www.geeksforgeeks.org/computer-networks/subnet-cheat-sheet/
27
25
  wellKnownAddresses: [
26
+ [new Uint8Array([169, 254, 0, 0]), 16], // Link-local address (Autoconfiguration)
27
+
28
28
  [new Uint8Array([0, 0, 0, 0]), 8], // This network
29
29
  [new Uint8Array([10, 0, 0, 0]), 8], // Private network (RFC 1918)
30
30
  [new Uint8Array([100, 64, 0, 0]), 10], // Carrier-grade NAT / Shared address space (CGN)
31
31
  [new Uint8Array([127, 0, 0, 0]), 8], // Loopback
32
32
  [new Uint8Array([127, 0, 53, 53]), 0], // Name collision occurrence
33
- [new Uint8Array([169, 254, 0, 0]), 16], // Link-local address (Autoconfiguration)
34
33
  [new Uint8Array([172, 16, 0, 0]), 12], // Private network (RFC 1918)
35
34
  [new Uint8Array([192, 0, 0, 0]), 24], // IETF protocol assignments
36
35
  [new Uint8Array([192, 0, 2, 0]), 24], // TEST-NET-1
@@ -61,10 +60,9 @@ const ipv6 = {
61
60
  segmentLength: 16,
62
61
  segmentMask: 0xffffn,
63
62
  base: 16,
64
- linkLocalPrefix: new Uint16Array([0xfe80]),
65
- linkLocalPrefixLength: 64,
66
63
  localHost: new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]),
67
- localHostPrefixLenth: 128
64
+ localHostPrefixLenth: 128,
65
+ wellKnownAddresses: [[new Uint16Array([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 64]]
68
66
  };
69
67
 
70
68
  const families = [ipv4, ipv6];
@@ -285,34 +283,45 @@ export function normalizeCIDR(address) {
285
283
 
286
284
  const family = isIPv6(prefix) ? ipv6 : ipv4;
287
285
 
288
- if (isUniqueLocal(address) || _isLinkLocal(family, address)) {
289
- prefixLength = family.linkLocalPrefixLength;
290
- const n = _prefix(family, address, prefixLength);
291
- prefix = _decode(family, n, prefixLength);
292
-
293
- if (family === ipv6) {
294
- if (!prefix.endsWith("::")) {
295
- // TODO
296
- prefix += prefix.endsWith(":") ? ":" : "::";
297
- }
298
- }
299
- longPrefix = prefix;
286
+ const wns = _wellKnownSubnet(family, address);
287
+ if (family === ipv4 && wns && !prefixLength) {
288
+ prefixLength = wns[1];
289
+ prefix = _decode(
290
+ family,
291
+ _prefix(family, wns[0], prefixLength),
292
+ prefixLength
293
+ );
294
+ longPrefix = wns[0];
300
295
  } else {
301
- prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
296
+ if (isUniqueLocal(address) || _isLinkLocal(family, address)) {
297
+ prefixLength = family.wellKnownAddresses[0][1]; // TODO
298
+ const n = _prefix(family, address, prefixLength);
299
+ prefix = _decode(family, n, prefixLength);
300
+
301
+ if (family === ipv6) {
302
+ if (!prefix.endsWith("::")) {
303
+ // TODO
304
+ prefix += prefix.endsWith(":") ? ":" : "::";
305
+ }
306
+ }
307
+ longPrefix = prefix;
308
+ } else {
309
+ prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
302
310
 
303
- let n;
311
+ let n;
304
312
 
305
- if (prefixLength) {
306
- n = _prefix(family, prefix, prefixLength);
307
- } else {
308
- n = _encode(family, prefix);
313
+ if (prefixLength) {
314
+ n = _prefix(family, prefix, prefixLength);
315
+ } else {
316
+ n = _encode(family, prefix);
309
317
 
310
- if (isLocalhost(n)) {
311
- prefixLength = family.localHostPrefixLenth;
318
+ if (isLocalhost(n)) {
319
+ prefixLength = family.localHostPrefixLenth;
320
+ }
312
321
  }
322
+ prefix = _decode(family, n, prefixLength);
323
+ longPrefix = _decode(family, n);
313
324
  }
314
- prefix = _decode(family, n, prefixLength);
315
- longPrefix = _decode(family, n);
316
325
  }
317
326
 
318
327
  return {
@@ -382,7 +391,7 @@ export function isLinkLocal(address) {
382
391
  }
383
392
 
384
393
  export function _isLinkLocal(family, address) {
385
- return _equal(family.linkLocalPrefix, _encode(family, address));
394
+ return _wellKnownSubnet(family, address) === family.wellKnownAddresses[0];
386
395
  }
387
396
 
388
397
  export function isUniqueLocal(address) {
@@ -411,21 +420,15 @@ export function _wellKnownSubnet(family, address) {
411
420
  if (_equal(family.localHost, encoded)) {
412
421
  return [family.localHost, family.localHostPrefixLenth];
413
422
  }
414
- if (_equal(family.linkLocalPrefix, encoded)) {
415
- return [family.linkLocalPrefix, family.linkLocalPrefixLength];
416
- }
423
+
417
424
  if (_isUniqueLocal(encoded)) {
418
425
  return [encoded[0], 64];
419
426
  }
420
427
 
421
- if (family === ipv4) {
422
- for (const c of family.wellKnownAddresses) {
423
- const pl = c[1];
424
- if (
425
- pl > 0 &&
426
- _prefix(family, c[0], pl) === _prefix(family, encoded, pl)
427
- ) {
428
- /*console.log(
428
+ for (const c of family.wellKnownAddresses) {
429
+ const pl = c[1];
430
+ if (pl > 0 && _prefix(family, c[0], pl) === _prefix(family, encoded, pl)) {
431
+ /*console.log(
429
432
  c,
430
433
  encoded,
431
434
  _prefix(family, c[0], pl),
@@ -433,8 +436,7 @@ export function _wellKnownSubnet(family, address) {
433
436
  prefixIP(c[0], pl),
434
437
  prefixIP(encoded, pl)
435
438
  );*/
436
- return c;
437
- }
439
+ return c;
438
440
  }
439
441
  }
440
442
  }