@ac-kit/net-address 0.2.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/CHANGELOG.md +46 -0
- package/LICENSE +28 -0
- package/README.md +65 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +10 -0
- package/dist/errors.js.map +1 -0
- package/dist/eui/constants.d.ts +7 -0
- package/dist/eui/constants.d.ts.map +1 -0
- package/dist/eui/constants.js +7 -0
- package/dist/eui/constants.js.map +1 -0
- package/dist/eui/eui-bits.d.ts +99 -0
- package/dist/eui/eui-bits.d.ts.map +1 -0
- package/dist/eui/eui-bits.js +138 -0
- package/dist/eui/eui-bits.js.map +1 -0
- package/dist/eui/eui-convert.d.ts +37 -0
- package/dist/eui/eui-convert.d.ts.map +1 -0
- package/dist/eui/eui-convert.js +76 -0
- package/dist/eui/eui-convert.js.map +1 -0
- package/dist/eui/interface-identifier.d.ts +24 -0
- package/dist/eui/interface-identifier.d.ts.map +1 -0
- package/dist/eui/interface-identifier.js +47 -0
- package/dist/eui/interface-identifier.js.map +1 -0
- package/dist/eui/parse-eui.d.ts +31 -0
- package/dist/eui/parse-eui.d.ts.map +1 -0
- package/dist/eui/parse-eui.js +93 -0
- package/dist/eui/parse-eui.js.map +1 -0
- package/dist/eui/print-eui.d.ts +44 -0
- package/dist/eui/print-eui.d.ts.map +1 -0
- package/dist/eui/print-eui.js +64 -0
- package/dist/eui/print-eui.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/ipv4/classify-ipv4.d.ts +69 -0
- package/dist/ipv4/classify-ipv4.d.ts.map +1 -0
- package/dist/ipv4/classify-ipv4.js +129 -0
- package/dist/ipv4/classify-ipv4.js.map +1 -0
- package/dist/ipv4/constants.d.ts +5 -0
- package/dist/ipv4/constants.d.ts.map +1 -0
- package/dist/ipv4/constants.js +5 -0
- package/dist/ipv4/constants.js.map +1 -0
- package/dist/ipv4/ipv4-prefix.d.ts +52 -0
- package/dist/ipv4/ipv4-prefix.d.ts.map +1 -0
- package/dist/ipv4/ipv4-prefix.js +81 -0
- package/dist/ipv4/ipv4-prefix.js.map +1 -0
- package/dist/ipv4/parse-ipv4.d.ts +49 -0
- package/dist/ipv4/parse-ipv4.d.ts.map +1 -0
- package/dist/ipv4/parse-ipv4.js +169 -0
- package/dist/ipv4/parse-ipv4.js.map +1 -0
- package/dist/ipv4/print-ipv4.d.ts +50 -0
- package/dist/ipv4/print-ipv4.d.ts.map +1 -0
- package/dist/ipv4/print-ipv4.js +76 -0
- package/dist/ipv4/print-ipv4.js.map +1 -0
- package/dist/ipv6/classify-ipv6.d.ts +79 -0
- package/dist/ipv6/classify-ipv6.d.ts.map +1 -0
- package/dist/ipv6/classify-ipv6.js +148 -0
- package/dist/ipv6/classify-ipv6.js.map +1 -0
- package/dist/ipv6/constants.d.ts +5 -0
- package/dist/ipv6/constants.d.ts.map +1 -0
- package/dist/ipv6/constants.js +5 -0
- package/dist/ipv6/constants.js.map +1 -0
- package/dist/ipv6/ipv4-mapped.d.ts +43 -0
- package/dist/ipv6/ipv4-mapped.d.ts.map +1 -0
- package/dist/ipv6/ipv4-mapped.js +65 -0
- package/dist/ipv6/ipv4-mapped.js.map +1 -0
- package/dist/ipv6/ipv6-prefix.d.ts +51 -0
- package/dist/ipv6/ipv6-prefix.d.ts.map +1 -0
- package/dist/ipv6/ipv6-prefix.js +78 -0
- package/dist/ipv6/ipv6-prefix.js.map +1 -0
- package/dist/ipv6/parse-ipv6.d.ts +50 -0
- package/dist/ipv6/parse-ipv6.d.ts.map +1 -0
- package/dist/ipv6/parse-ipv6.js +148 -0
- package/dist/ipv6/parse-ipv6.js.map +1 -0
- package/dist/ipv6/print-ipv6.d.ts +37 -0
- package/dist/ipv6/print-ipv6.d.ts.map +1 -0
- package/dist/ipv6/print-ipv6.js +93 -0
- package/dist/ipv6/print-ipv6.js.map +1 -0
- package/dist/port/endpoint.d.ts +47 -0
- package/dist/port/endpoint.d.ts.map +1 -0
- package/dist/port/endpoint.js +104 -0
- package/dist/port/endpoint.js.map +1 -0
- package/dist/port/ephemeral-port.d.ts +17 -0
- package/dist/port/ephemeral-port.d.ts.map +1 -0
- package/dist/port/ephemeral-port.js +26 -0
- package/dist/port/ephemeral-port.js.map +1 -0
- package/dist/port/port.d.ts +69 -0
- package/dist/port/port.d.ts.map +1 -0
- package/dist/port/port.js +94 -0
- package/dist/port/port.js.map +1 -0
- package/dist/prefix/_numeric-range.d.ts +23 -0
- package/dist/prefix/_numeric-range.d.ts.map +1 -0
- package/dist/prefix/_numeric-range.js +57 -0
- package/dist/prefix/_numeric-range.js.map +1 -0
- package/dist/prefix/_prefix-length.d.ts +7 -0
- package/dist/prefix/_prefix-length.d.ts.map +1 -0
- package/dist/prefix/_prefix-length.js +13 -0
- package/dist/prefix/_prefix-length.js.map +1 -0
- package/dist/prefix/aggregate.d.ts +45 -0
- package/dist/prefix/aggregate.d.ts.map +1 -0
- package/dist/prefix/aggregate.js +97 -0
- package/dist/prefix/aggregate.js.map +1 -0
- package/dist/prefix/bit-address.d.ts +110 -0
- package/dist/prefix/bit-address.d.ts.map +1 -0
- package/dist/prefix/bit-address.js +165 -0
- package/dist/prefix/bit-address.js.map +1 -0
- package/dist/prefix/bit-prefix.d.ts +79 -0
- package/dist/prefix/bit-prefix.d.ts.map +1 -0
- package/dist/prefix/bit-prefix.js +135 -0
- package/dist/prefix/bit-prefix.js.map +1 -0
- package/dist/prefix/bit-range.d.ts +49 -0
- package/dist/prefix/bit-range.d.ts.map +1 -0
- package/dist/prefix/bit-range.js +100 -0
- package/dist/prefix/bit-range.js.map +1 -0
- package/dist/prefix/compare.d.ts +21 -0
- package/dist/prefix/compare.d.ts.map +1 -0
- package/dist/prefix/compare.js +33 -0
- package/dist/prefix/compare.js.map +1 -0
- package/dist/prefix/mask.d.ts +26 -0
- package/dist/prefix/mask.d.ts.map +1 -0
- package/dist/prefix/mask.js +48 -0
- package/dist/prefix/mask.js.map +1 -0
- package/dist/prefix/set-ops.d.ts +38 -0
- package/dist/prefix/set-ops.d.ts.map +1 -0
- package/dist/prefix/set-ops.js +71 -0
- package/dist/prefix/set-ops.js.map +1 -0
- package/package.json +66 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { assertBitAddressWidth, bitAddressAdd, } from "../prefix/bit-address.js";
|
|
2
|
+
import { bitPrefixFirstAddress, bitPrefixLastAddress, } from "../prefix/bit-prefix.js";
|
|
3
|
+
import { IPV4_BIT_WIDTH } from "./constants.js";
|
|
4
|
+
function assertIpv4Prefix(prefix) {
|
|
5
|
+
assertBitAddressWidth(prefix.address, IPV4_BIT_WIDTH);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The network address — the prefix's lowest address, host bits cleared.
|
|
9
|
+
*
|
|
10
|
+
* @throws RangeError When the prefix is not over a 32-bit address.
|
|
11
|
+
*/
|
|
12
|
+
export function ipv4NetworkAddress(prefix) {
|
|
13
|
+
assertIpv4Prefix(prefix);
|
|
14
|
+
return bitPrefixFirstAddress(prefix);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The directed broadcast address — the prefix's highest address.
|
|
18
|
+
*
|
|
19
|
+
* @returns `null` for `/31` and `/32`, which have none: RFC 3021 gives a `/31`
|
|
20
|
+
* two host addresses and no broadcast, and a `/32` is one host.
|
|
21
|
+
* @throws RangeError When the prefix is not over a 32-bit address.
|
|
22
|
+
*/
|
|
23
|
+
export function ipv4BroadcastAddress(prefix) {
|
|
24
|
+
assertIpv4Prefix(prefix);
|
|
25
|
+
return prefix.prefixLength >= IPV4_BIT_WIDTH - 1
|
|
26
|
+
? null
|
|
27
|
+
: bitPrefixLastAddress(prefix);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The addresses usable by a host.
|
|
31
|
+
*
|
|
32
|
+
* `/30` and shorter exclude the network and broadcast addresses; a `/31` is
|
|
33
|
+
* both of its addresses (RFC 3021); a `/32` is its single address.
|
|
34
|
+
*
|
|
35
|
+
* @throws RangeError When the prefix is not over a 32-bit address.
|
|
36
|
+
*/
|
|
37
|
+
export function ipv4UsableHostRange(prefix) {
|
|
38
|
+
assertIpv4Prefix(prefix);
|
|
39
|
+
const first = bitPrefixFirstAddress(prefix);
|
|
40
|
+
const last = bitPrefixLastAddress(prefix);
|
|
41
|
+
if (prefix.prefixLength >= IPV4_BIT_WIDTH - 1) {
|
|
42
|
+
return { first, last };
|
|
43
|
+
}
|
|
44
|
+
return { first: bitAddressAdd(first, 1n), last: bitAddressAdd(last, -1n) };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The first usable host address.
|
|
48
|
+
*
|
|
49
|
+
* @throws RangeError When the prefix is not over a 32-bit address.
|
|
50
|
+
*/
|
|
51
|
+
export function ipv4FirstHost(prefix) {
|
|
52
|
+
return ipv4UsableHostRange(prefix).first;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The last usable host address.
|
|
56
|
+
*
|
|
57
|
+
* @throws RangeError When the prefix is not over a 32-bit address.
|
|
58
|
+
*/
|
|
59
|
+
export function ipv4LastHost(prefix) {
|
|
60
|
+
return ipv4UsableHostRange(prefix).last;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* How many addresses the prefix covers, including network and broadcast.
|
|
64
|
+
*
|
|
65
|
+
* @throws RangeError When the prefix is not over a 32-bit address.
|
|
66
|
+
*/
|
|
67
|
+
export function ipv4AddressCount(prefix) {
|
|
68
|
+
assertIpv4Prefix(prefix);
|
|
69
|
+
return 2 ** (IPV4_BIT_WIDTH - prefix.prefixLength);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* How many addresses a host may take — 254 for a `/24`, 2 for a `/31`, 1 for a
|
|
73
|
+
* `/32`.
|
|
74
|
+
*
|
|
75
|
+
* @throws RangeError When the prefix is not over a 32-bit address.
|
|
76
|
+
*/
|
|
77
|
+
export function ipv4HostCount(prefix) {
|
|
78
|
+
const total = ipv4AddressCount(prefix);
|
|
79
|
+
return prefix.prefixLength >= IPV4_BIT_WIDTH - 1 ? total : total - 2;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=ipv4-prefix.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipv4-prefix.js","sourceRoot":"","sources":["../../src/ipv4/ipv4-prefix.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,qBAAqB,EACrB,aAAa,GACb,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAEN,qBAAqB,EACrB,oBAAoB,GACpB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,SAAS,gBAAgB,CAAC,MAAiB;IAC1C,qBAAqB,CAAC,MAAM,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;AACvD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAiB;IACnD,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAEzB,OAAO,qBAAqB,CAAC,MAAM,CAAC,CAAC;AACtC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAiB;IACrD,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAEzB,OAAO,MAAM,CAAC,YAAY,IAAI,cAAc,GAAG,CAAC;QAC/C,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;AACjC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAiB;IACpD,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAEzB,MAAM,KAAK,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAC;IAC5C,MAAM,IAAI,GAAG,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAE1C,IAAI,MAAM,CAAC,YAAY,IAAI,cAAc,GAAG,CAAC,EAAE,CAAC;QAC/C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IACxB,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,aAAa,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,aAAa,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;AAC5E,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,MAAiB;IAC9C,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC;AAC1C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,MAAiB;IAC7C,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAiB;IACjD,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAEzB,OAAO,CAAC,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AACpD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,MAAiB;IAC9C,MAAM,KAAK,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAEvC,OAAO,MAAM,CAAC,YAAY,IAAI,cAAc,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC;AACtE,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { BitAddress } from "../prefix/bit-address.js";
|
|
2
|
+
import { type BitPrefix } from "../prefix/bit-prefix.js";
|
|
3
|
+
/**
|
|
4
|
+
* Parses a dotted quad, strictly.
|
|
5
|
+
*
|
|
6
|
+
* Exactly four decimal octets, no leading zeros, no octal, no hexadecimal, no
|
|
7
|
+
* abbreviated forms. Everything a C `inet_aton` would also accept — `127.1`,
|
|
8
|
+
* `0177.0.0.1`, `0x7f.0.0.1`, `2130706433` — is rejected, because a parser that
|
|
9
|
+
* accepts them disagrees with the one the next hop uses, and that disagreement
|
|
10
|
+
* is how an allowlist gets bypassed. {@link parseInetAtonIpv4Address} is there
|
|
11
|
+
* when the legacy grammar is genuinely wanted.
|
|
12
|
+
*
|
|
13
|
+
* @returns The address, 32 bits wide.
|
|
14
|
+
* @throws NetAddressSyntaxError When `text` is not a strict dotted quad.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseIpv4Address(text: string): BitAddress;
|
|
17
|
+
/** {@link parseIpv4Address}, answering `null` instead of throwing. O(1). */
|
|
18
|
+
export declare function tryParseIpv4Address(text: string): BitAddress | null;
|
|
19
|
+
/**
|
|
20
|
+
* Parses the legacy C `inet_aton` grammar: one to four parts, each decimal,
|
|
21
|
+
* octal (leading `0`) or hexadecimal (leading `0x`), with the last part filling
|
|
22
|
+
* the remaining octets.
|
|
23
|
+
*
|
|
24
|
+
* `2130706433`, `127.1` and `0177.0.0.1` all name `127.0.0.1` here. **Never
|
|
25
|
+
* validate an allowlist with this.** It exists so a caller reproducing what
|
|
26
|
+
* `ping` or a legacy configuration file accepts can do so deliberately, and so
|
|
27
|
+
* a security check can prove that a hostile string resolves to a blocked
|
|
28
|
+
* address under the lenient grammar too.
|
|
29
|
+
*
|
|
30
|
+
* @returns The address, 32 bits wide.
|
|
31
|
+
* @throws NetAddressSyntaxError When `text` is not an `inet_aton` form.
|
|
32
|
+
*/
|
|
33
|
+
export declare function parseInetAtonIpv4Address(text: string): BitAddress;
|
|
34
|
+
/** {@link parseInetAtonIpv4Address}, answering `null` instead of throwing. */
|
|
35
|
+
export declare function tryParseInetAtonIpv4Address(text: string): BitAddress | null;
|
|
36
|
+
/**
|
|
37
|
+
* Parses CIDR notation — a strict dotted quad, `/`, and a prefix length.
|
|
38
|
+
*
|
|
39
|
+
* Host bits are kept: `192.168.1.10/24` parses to exactly that. A dotted
|
|
40
|
+
* netmask in place of the length is not accepted; convert one with
|
|
41
|
+
* `prefixLengthFromMask` instead.
|
|
42
|
+
*
|
|
43
|
+
* @throws NetAddressSyntaxError When `text` is not `address/length`, or the
|
|
44
|
+
* length is not an integer in `[0, 32]`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function parseIpv4Prefix(text: string): BitPrefix;
|
|
47
|
+
/** {@link parseIpv4Prefix}, answering `null` instead of throwing. O(1). */
|
|
48
|
+
export declare function tryParseIpv4Prefix(text: string): BitPrefix | null;
|
|
49
|
+
//# sourceMappingURL=parse-ipv4.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-ipv4.d.ts","sourceRoot":"","sources":["../../src/ipv4/parse-ipv4.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,KAAK,SAAS,EAAmB,MAAM,yBAAyB,CAAC;AAkC1E;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAQzD;AAED,4EAA4E;AAC5E,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAqBnE;AAmBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAQjE;AAED,8EAA8E;AAC9E,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CA6C3E;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAQvD;AAED,2EAA2E;AAC3E,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAmBjE"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { isAsciiDigit } from "@ac-kit/core";
|
|
2
|
+
import { fixedUIntBigIsInRange } from "@ac-kit/math-numbers";
|
|
3
|
+
import { NetAddressSyntaxError } from "../errors.js";
|
|
4
|
+
import { createBitPrefix } from "../prefix/bit-prefix.js";
|
|
5
|
+
import { IPV4_BIT_WIDTH, IPV4_OCTET_COUNT } from "./constants.js";
|
|
6
|
+
/**
|
|
7
|
+
* Reads one strict decimal octet: one to three digits, no leading zero, at most 255.
|
|
8
|
+
*
|
|
9
|
+
* @returns The value, or `null` when the text is not such an octet.
|
|
10
|
+
*/
|
|
11
|
+
function parseStrictOctet(part) {
|
|
12
|
+
if (part.length === 0 || part.length > 3) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
// A leading zero is what makes `0177.0.0.1` read as 127 to `inet_aton` and as
|
|
16
|
+
// 177 to a naive decimal parser — the classic SSRF allowlist bypass.
|
|
17
|
+
if (part.length > 1 && part.charCodeAt(0) === 0x30) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
let value = 0;
|
|
21
|
+
for (let index = 0; index < part.length; index++) {
|
|
22
|
+
const code = part.charCodeAt(index);
|
|
23
|
+
if (!isAsciiDigit(code)) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
value = value * 10 + (code - 0x30);
|
|
27
|
+
}
|
|
28
|
+
return value <= 255 ? value : null;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Parses a dotted quad, strictly.
|
|
32
|
+
*
|
|
33
|
+
* Exactly four decimal octets, no leading zeros, no octal, no hexadecimal, no
|
|
34
|
+
* abbreviated forms. Everything a C `inet_aton` would also accept — `127.1`,
|
|
35
|
+
* `0177.0.0.1`, `0x7f.0.0.1`, `2130706433` — is rejected, because a parser that
|
|
36
|
+
* accepts them disagrees with the one the next hop uses, and that disagreement
|
|
37
|
+
* is how an allowlist gets bypassed. {@link parseInetAtonIpv4Address} is there
|
|
38
|
+
* when the legacy grammar is genuinely wanted.
|
|
39
|
+
*
|
|
40
|
+
* @returns The address, 32 bits wide.
|
|
41
|
+
* @throws NetAddressSyntaxError When `text` is not a strict dotted quad.
|
|
42
|
+
*/
|
|
43
|
+
export function parseIpv4Address(text) {
|
|
44
|
+
const address = tryParseIpv4Address(text);
|
|
45
|
+
if (address === null) {
|
|
46
|
+
throw new NetAddressSyntaxError(text, "not a dotted-quad IPv4 address");
|
|
47
|
+
}
|
|
48
|
+
return address;
|
|
49
|
+
}
|
|
50
|
+
/** {@link parseIpv4Address}, answering `null` instead of throwing. O(1). */
|
|
51
|
+
export function tryParseIpv4Address(text) {
|
|
52
|
+
const parts = text.split(".");
|
|
53
|
+
if (parts.length !== IPV4_OCTET_COUNT) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
let value = 0n;
|
|
57
|
+
for (let index = 0; index < IPV4_OCTET_COUNT; index++) {
|
|
58
|
+
// Non-null: `parts` has exactly `IPV4_OCTET_COUNT` entries.
|
|
59
|
+
const octet = parseStrictOctet(parts[index]);
|
|
60
|
+
if (octet === null) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
value = (value << 8n) | BigInt(octet);
|
|
64
|
+
}
|
|
65
|
+
return { value, bitWidth: IPV4_BIT_WIDTH };
|
|
66
|
+
}
|
|
67
|
+
/** Reads one `inet_aton` part: hexadecimal `0x…`, octal `0…`, else decimal. */
|
|
68
|
+
function parseLegacyPart(part) {
|
|
69
|
+
if (part.length === 0) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
if (/^0[xX][0-9a-fA-F]+$/.test(part)) {
|
|
73
|
+
return BigInt(`0x${part.slice(2)}`);
|
|
74
|
+
}
|
|
75
|
+
if (/^0[0-7]+$/.test(part)) {
|
|
76
|
+
return BigInt(`0o${part.slice(1)}`);
|
|
77
|
+
}
|
|
78
|
+
return /^(?:0|[1-9][0-9]*)$/.test(part) ? BigInt(part) : null;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Parses the legacy C `inet_aton` grammar: one to four parts, each decimal,
|
|
82
|
+
* octal (leading `0`) or hexadecimal (leading `0x`), with the last part filling
|
|
83
|
+
* the remaining octets.
|
|
84
|
+
*
|
|
85
|
+
* `2130706433`, `127.1` and `0177.0.0.1` all name `127.0.0.1` here. **Never
|
|
86
|
+
* validate an allowlist with this.** It exists so a caller reproducing what
|
|
87
|
+
* `ping` or a legacy configuration file accepts can do so deliberately, and so
|
|
88
|
+
* a security check can prove that a hostile string resolves to a blocked
|
|
89
|
+
* address under the lenient grammar too.
|
|
90
|
+
*
|
|
91
|
+
* @returns The address, 32 bits wide.
|
|
92
|
+
* @throws NetAddressSyntaxError When `text` is not an `inet_aton` form.
|
|
93
|
+
*/
|
|
94
|
+
export function parseInetAtonIpv4Address(text) {
|
|
95
|
+
const address = tryParseInetAtonIpv4Address(text);
|
|
96
|
+
if (address === null) {
|
|
97
|
+
throw new NetAddressSyntaxError(text, "not an inet_aton IPv4 address");
|
|
98
|
+
}
|
|
99
|
+
return address;
|
|
100
|
+
}
|
|
101
|
+
/** {@link parseInetAtonIpv4Address}, answering `null` instead of throwing. */
|
|
102
|
+
export function tryParseInetAtonIpv4Address(text) {
|
|
103
|
+
const parts = text.split(".");
|
|
104
|
+
if (parts.length < 1 || parts.length > IPV4_OCTET_COUNT) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const values = [];
|
|
108
|
+
for (const part of parts) {
|
|
109
|
+
const value = parseLegacyPart(part);
|
|
110
|
+
if (value === null) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
values.push(value);
|
|
114
|
+
}
|
|
115
|
+
// Every part but the last is one octet; the last fills whatever is left.
|
|
116
|
+
const tailBitWidth = (IPV4_OCTET_COUNT - values.length + 1) * 8;
|
|
117
|
+
let value = 0n;
|
|
118
|
+
for (let index = 0; index < values.length - 1; index++) {
|
|
119
|
+
// Non-null: `index` stays below `values.length`.
|
|
120
|
+
const octet = values[index];
|
|
121
|
+
if (octet > 255n) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
value = (value << 8n) | octet;
|
|
125
|
+
}
|
|
126
|
+
// Non-null: `values` holds at least one entry.
|
|
127
|
+
const tail = values.at(-1);
|
|
128
|
+
if (!fixedUIntBigIsInRange(tail, tailBitWidth)) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
value: (value << BigInt(tailBitWidth)) | tail,
|
|
133
|
+
bitWidth: IPV4_BIT_WIDTH,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Parses CIDR notation — a strict dotted quad, `/`, and a prefix length.
|
|
138
|
+
*
|
|
139
|
+
* Host bits are kept: `192.168.1.10/24` parses to exactly that. A dotted
|
|
140
|
+
* netmask in place of the length is not accepted; convert one with
|
|
141
|
+
* `prefixLengthFromMask` instead.
|
|
142
|
+
*
|
|
143
|
+
* @throws NetAddressSyntaxError When `text` is not `address/length`, or the
|
|
144
|
+
* length is not an integer in `[0, 32]`.
|
|
145
|
+
*/
|
|
146
|
+
export function parseIpv4Prefix(text) {
|
|
147
|
+
const prefix = tryParseIpv4Prefix(text);
|
|
148
|
+
if (prefix === null) {
|
|
149
|
+
throw new NetAddressSyntaxError(text, "not an IPv4 CIDR prefix");
|
|
150
|
+
}
|
|
151
|
+
return prefix;
|
|
152
|
+
}
|
|
153
|
+
/** {@link parseIpv4Prefix}, answering `null` instead of throwing. O(1). */
|
|
154
|
+
export function tryParseIpv4Prefix(text) {
|
|
155
|
+
const slash = text.indexOf("/");
|
|
156
|
+
if (slash < 0) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
const address = tryParseIpv4Address(text.slice(0, slash));
|
|
160
|
+
const lengthText = text.slice(slash + 1);
|
|
161
|
+
if (address === null || !/^(?:0|[1-9][0-9]?)$/.test(lengthText)) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
const prefixLength = Number(lengthText);
|
|
165
|
+
return prefixLength <= IPV4_BIT_WIDTH
|
|
166
|
+
? createBitPrefix(address, prefixLength)
|
|
167
|
+
: null;
|
|
168
|
+
}
|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E,KAAK,CAAC,CAAC,CAAC;IAC1D,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;IAEzC,IAAI,OAAO,KAAK,IAAI,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QACjE,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,YAAY,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;IAExC,OAAO,YAAY,IAAI,cAAc;QACpC,CAAC,CAAC,eAAe,CAAC,OAAO,EAAE,YAAY,CAAC;QACxC,CAAC,CAAC,IAAI,CAAC;AACT,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type BitAddress } from "../prefix/bit-address.js";
|
|
2
|
+
import type { BitPrefix } from "../prefix/bit-prefix.js";
|
|
3
|
+
/**
|
|
4
|
+
* Prints the dotted quad.
|
|
5
|
+
*
|
|
6
|
+
* @throws RangeError When the address is not 32 bits wide.
|
|
7
|
+
*/
|
|
8
|
+
export declare function printIpv4Address(address: BitAddress): string;
|
|
9
|
+
/**
|
|
10
|
+
* Prints CIDR notation, keeping any host bits the prefix carries.
|
|
11
|
+
*
|
|
12
|
+
* @throws RangeError When the prefix is not over a 32-bit address.
|
|
13
|
+
*/
|
|
14
|
+
export declare function printIpv4Prefix(prefix: BitPrefix): string;
|
|
15
|
+
/**
|
|
16
|
+
* The address as an unsigned 32-bit integer. Exact: 2³²−1 is well inside the
|
|
17
|
+
* safe-integer range.
|
|
18
|
+
*
|
|
19
|
+
* @throws RangeError When the address is not 32 bits wide.
|
|
20
|
+
*/
|
|
21
|
+
export declare function ipv4ToUint32(address: BitAddress): number;
|
|
22
|
+
/**
|
|
23
|
+
* The inverse of {@link ipv4ToUint32}.
|
|
24
|
+
*
|
|
25
|
+
* @throws RangeError When `value` is not an integer in `[0, 2³² − 1]`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function ipv4FromUint32(value: number): BitAddress;
|
|
28
|
+
/**
|
|
29
|
+
* The address as eight lowercase hexadecimal digits, no prefix — the form
|
|
30
|
+
* `/proc/net/route` and many log formats use.
|
|
31
|
+
*
|
|
32
|
+
* @throws RangeError When the address is not 32 bits wide.
|
|
33
|
+
*/
|
|
34
|
+
export declare function ipv4ToHex(address: BitAddress): string;
|
|
35
|
+
/**
|
|
36
|
+
* The inverse of {@link ipv4ToHex}, also accepting an `0x` prefix and uppercase
|
|
37
|
+
* digits. Exactly eight digits are required, so a truncated value cannot be
|
|
38
|
+
* mistaken for a small address.
|
|
39
|
+
*
|
|
40
|
+
* @throws NetAddressSyntaxError When `text` is not eight hexadecimal digits.
|
|
41
|
+
*/
|
|
42
|
+
export declare function ipv4FromHex(text: string): BitAddress;
|
|
43
|
+
/**
|
|
44
|
+
* The `in-addr.arpa` name for the address, as RFC 1035 §3.5 defines it: the
|
|
45
|
+
* octets reversed, with no trailing dot.
|
|
46
|
+
*
|
|
47
|
+
* @throws RangeError When the address is not 32 bits wide.
|
|
48
|
+
*/
|
|
49
|
+
export declare function ipv4ReverseName(address: BitAddress): string;
|
|
50
|
+
//# sourceMappingURL=print-ipv4.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"print-ipv4.d.ts","sourceRoot":"","sources":["../../src/ipv4/print-ipv4.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,UAAU,EAKf,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGzD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAI5D;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAEzD;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAIxD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAMxD;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAIrD;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAWpD;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAI3D"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { NetAddressSyntaxError } from "../errors.js";
|
|
2
|
+
import { assertBitAddressWidth, bitAddressFromBigInt, bitAddressToBigInt, bitAddressToBytes, } from "../prefix/bit-address.js";
|
|
3
|
+
import { IPV4_BIT_WIDTH } from "./constants.js";
|
|
4
|
+
/**
|
|
5
|
+
* Prints the dotted quad.
|
|
6
|
+
*
|
|
7
|
+
* @throws RangeError When the address is not 32 bits wide.
|
|
8
|
+
*/
|
|
9
|
+
export function printIpv4Address(address) {
|
|
10
|
+
assertBitAddressWidth(address, IPV4_BIT_WIDTH);
|
|
11
|
+
return bitAddressToBytes(address).join(".");
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Prints CIDR notation, keeping any host bits the prefix carries.
|
|
15
|
+
*
|
|
16
|
+
* @throws RangeError When the prefix is not over a 32-bit address.
|
|
17
|
+
*/
|
|
18
|
+
export function printIpv4Prefix(prefix) {
|
|
19
|
+
return `${printIpv4Address(prefix.address)}/${prefix.prefixLength}`;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The address as an unsigned 32-bit integer. Exact: 2³²−1 is well inside the
|
|
23
|
+
* safe-integer range.
|
|
24
|
+
*
|
|
25
|
+
* @throws RangeError When the address is not 32 bits wide.
|
|
26
|
+
*/
|
|
27
|
+
export function ipv4ToUint32(address) {
|
|
28
|
+
assertBitAddressWidth(address, IPV4_BIT_WIDTH);
|
|
29
|
+
return Number(bitAddressToBigInt(address));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The inverse of {@link ipv4ToUint32}.
|
|
33
|
+
*
|
|
34
|
+
* @throws RangeError When `value` is not an integer in `[0, 2³² − 1]`.
|
|
35
|
+
*/
|
|
36
|
+
export function ipv4FromUint32(value) {
|
|
37
|
+
if (!Number.isInteger(value) || value < 0 || value > 4294967295) {
|
|
38
|
+
throw new RangeError(`not an unsigned 32-bit integer: ${value}`);
|
|
39
|
+
}
|
|
40
|
+
return bitAddressFromBigInt(BigInt(value), IPV4_BIT_WIDTH);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The address as eight lowercase hexadecimal digits, no prefix — the form
|
|
44
|
+
* `/proc/net/route` and many log formats use.
|
|
45
|
+
*
|
|
46
|
+
* @throws RangeError When the address is not 32 bits wide.
|
|
47
|
+
*/
|
|
48
|
+
export function ipv4ToHex(address) {
|
|
49
|
+
assertBitAddressWidth(address, IPV4_BIT_WIDTH);
|
|
50
|
+
return address.value.toString(16).padStart(IPV4_BIT_WIDTH / 4, "0");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The inverse of {@link ipv4ToHex}, also accepting an `0x` prefix and uppercase
|
|
54
|
+
* digits. Exactly eight digits are required, so a truncated value cannot be
|
|
55
|
+
* mistaken for a small address.
|
|
56
|
+
*
|
|
57
|
+
* @throws NetAddressSyntaxError When `text` is not eight hexadecimal digits.
|
|
58
|
+
*/
|
|
59
|
+
export function ipv4FromHex(text) {
|
|
60
|
+
const digits = /^0[xX]/.test(text) ? text.slice(2) : text;
|
|
61
|
+
if (!/^[0-9a-fA-F]{8}$/.test(digits)) {
|
|
62
|
+
throw new NetAddressSyntaxError(text, "not eight hexadecimal digits of an IPv4 address");
|
|
63
|
+
}
|
|
64
|
+
return bitAddressFromBigInt(BigInt(`0x${digits}`), IPV4_BIT_WIDTH);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The `in-addr.arpa` name for the address, as RFC 1035 §3.5 defines it: the
|
|
68
|
+
* octets reversed, with no trailing dot.
|
|
69
|
+
*
|
|
70
|
+
* @throws RangeError When the address is not 32 bits wide.
|
|
71
|
+
*/
|
|
72
|
+
export function ipv4ReverseName(address) {
|
|
73
|
+
assertBitAddressWidth(address, IPV4_BIT_WIDTH);
|
|
74
|
+
return `${bitAddressToBytes(address).toReversed().join(".")}.in-addr.arpa`;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=print-ipv4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"print-ipv4.js","sourceRoot":"","sources":["../../src/ipv4/print-ipv4.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAEN,qBAAqB,EACrB,oBAAoB,EACpB,kBAAkB,EAClB,iBAAiB,GACjB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAmB;IACnD,qBAAqB,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IAE/C,OAAO,iBAAiB,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC7C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,MAAiB;IAChD,OAAO,GAAG,gBAAgB,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,YAAY,EAAE,CAAC;AACrE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,OAAmB;IAC/C,qBAAqB,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IAE/C,OAAO,MAAM,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;AAC5C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa;IAC3C,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,C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** `::` — the unspecified address, RFC 4291 §2.5.2. */
|
|
4
|
+
export declare const IPV6_UNSPECIFIED_ADDRESS: BitAddress;
|
|
5
|
+
/** `::1` — the loopback address, RFC 4291 §2.5.3. */
|
|
6
|
+
export declare const IPV6_LOOPBACK_ADDRESS: BitAddress;
|
|
7
|
+
/** `fe80::/10` — link-local unicast, RFC 4291 §2.5.6. */
|
|
8
|
+
export declare const IPV6_LINK_LOCAL_PREFIX: BitPrefix;
|
|
9
|
+
/** `fc00::/7` — unique local addresses, RFC 4193. */
|
|
10
|
+
export declare const IPV6_UNIQUE_LOCAL_PREFIX: BitPrefix;
|
|
11
|
+
/** `ff00::/8` — multicast, RFC 4291 §2.7. */
|
|
12
|
+
export declare const IPV6_MULTICAST_PREFIX: BitPrefix;
|
|
13
|
+
/** `2001:db8::/32` — documentation, RFC 3849. */
|
|
14
|
+
export declare const IPV6_DOCUMENTATION_PREFIX: BitPrefix;
|
|
15
|
+
/** `2001::/32` — Teredo tunnelling, RFC 4380. */
|
|
16
|
+
export declare const IPV6_TEREDO_PREFIX: BitPrefix;
|
|
17
|
+
/** `2002::/16` — 6to4, RFC 3056. */
|
|
18
|
+
export declare const IPV6_6TO4_PREFIX: BitPrefix;
|
|
19
|
+
/**
|
|
20
|
+
* Which special-purpose block an address falls in.
|
|
21
|
+
*
|
|
22
|
+
* `"global"` means none of the blocks below — not that the address is
|
|
23
|
+
* necessarily routable, since the IANA registry holds further assignments this
|
|
24
|
+
* package does not enumerate.
|
|
25
|
+
*/
|
|
26
|
+
export type Ipv6AddressScope = "unspecified" | "loopback" | "ipv4-mapped" | "ipv4-compatible" | "link-local" | "unique-local" | "multicast" | "documentation" | "teredo" | "6to4" | "global";
|
|
27
|
+
/**
|
|
28
|
+
* Classifies an address against RFC 4291 and the RFC 6890 special-purpose
|
|
29
|
+
* registry.
|
|
30
|
+
*
|
|
31
|
+
* Ordered narrowest first, because the blocks nest: `::1` sits inside `::/96`
|
|
32
|
+
* and must be reported as loopback rather than as IPv4-compatible, and
|
|
33
|
+
* `2001:db8::/32` sits beside — not inside — Teredo's `2001::/32`. O(1).
|
|
34
|
+
*
|
|
35
|
+
* @throws RangeError When the address is not 128 bits wide.
|
|
36
|
+
*/
|
|
37
|
+
export declare function classifyIpv6Address(address: BitAddress): Ipv6AddressScope;
|
|
38
|
+
/** Whether the address is `::`. */
|
|
39
|
+
export declare function isIpv6Unspecified(address: BitAddress): boolean;
|
|
40
|
+
/** Whether the address is `::1`. */
|
|
41
|
+
export declare function isIpv6Loopback(address: BitAddress): boolean;
|
|
42
|
+
/** Whether the address is in `fe80::/10`. */
|
|
43
|
+
export declare function isIpv6LinkLocal(address: BitAddress): boolean;
|
|
44
|
+
/** Whether the address is in `fc00::/7`. */
|
|
45
|
+
export declare function isIpv6UniqueLocal(address: BitAddress): boolean;
|
|
46
|
+
/** Whether the address is in `ff00::/8`. */
|
|
47
|
+
export declare function isIpv6Multicast(address: BitAddress): boolean;
|
|
48
|
+
/** Whether the address is in `2001:db8::/32`. */
|
|
49
|
+
export declare function isIpv6Documentation(address: BitAddress): boolean;
|
|
50
|
+
/** Whether the address is in `2001::/32`. */
|
|
51
|
+
export declare function isIpv6Teredo(address: BitAddress): boolean;
|
|
52
|
+
/** Whether the address is in `2002::/16`. */
|
|
53
|
+
export declare function isIpv6SixToFour(address: BitAddress): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Whether the address falls in any enumerated special-purpose block.
|
|
56
|
+
*
|
|
57
|
+
* An SSRF guard wants this on the parsed address, and must also unwrap an
|
|
58
|
+
* IPv4-mapped literal with `ipv4FromIpv6` before classifying:
|
|
59
|
+
* `::ffff:127.0.0.1` is loopback, and no substring test for `127.` finds it.
|
|
60
|
+
*/
|
|
61
|
+
export declare function isIpv6SpecialPurpose(address: BitAddress): boolean;
|
|
62
|
+
/** How far a multicast address is allowed to travel, RFC 4291 §2.7. */
|
|
63
|
+
export type Ipv6MulticastScope = "reserved" | "interface-local" | "link-local" | "realm-local" | "admin-local" | "site-local" | "organization-local" | "global" | "unassigned";
|
|
64
|
+
/**
|
|
65
|
+
* The scope nibble of a multicast address.
|
|
66
|
+
*
|
|
67
|
+
* @returns `null` when the address is not multicast.
|
|
68
|
+
* @throws RangeError When the address is not 128 bits wide.
|
|
69
|
+
*/
|
|
70
|
+
export declare function ipv6MulticastScope(address: BitAddress): Ipv6MulticastScope | null;
|
|
71
|
+
/**
|
|
72
|
+
* Whether the multicast address is transient — assigned dynamically rather than
|
|
73
|
+
* by IANA, which is the `T` flag of RFC 4291 §2.7.
|
|
74
|
+
*
|
|
75
|
+
* @returns `null` when the address is not multicast.
|
|
76
|
+
* @throws RangeError When the address is not 128 bits wide.
|
|
77
|
+
*/
|
|
78
|
+
export declare function isIpv6TransientMulticast(address: BitAddress): boolean | null;
|
|
79
|
+
//# sourceMappingURL=classify-ipv6.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classify-ipv6.d.ts","sourceRoot":"","sources":["../../src/ipv6/classify-ipv6.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,UAAU,EAKf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,KAAK,SAAS,EAAqB,MAAM,yBAAyB,CAAC;AAK5E,uDAAuD;AACvD,eAAO,MAAM,wBAAwB,EAAE,UAAmC,CAAC;AAE3E,qDAAqD;AACrD,eAAO,MAAM,qBAAqB,EAAE,UAAoC,CAAC;AAEzE,yDAAyD;AACzD,eAAO,MAAM,sBAAsB,EAAE,SAAwC,CAAC;AAE9E,qDAAqD;AACrD,eAAO,MAAM,wBAAwB,EAAE,SAAuC,CAAC;AAE/E,6CAA6C;AAC7C,eAAO,MAAM,qBAAqB,EAAE,SAAuC,CAAC;AAE5E,iDAAiD;AACjD,eAAO,MAAM,yBAAyB,EAAE,SACP,CAAC;AAElC,iDAAiD;AACjD,eAAO,MAAM,kBAAkB,EAAE,SAAwC,CAAC;AAE1E,oCAAoC;AACpC,eAAO,MAAM,gBAAgB,EAA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