ipaddress 0.2.5 → 0.2.7
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/dist/src/crunchy.d.ts +130 -0
- package/dist/src/crunchy.d.ts.map +1 -0
- package/dist/src/crunchy.js +616 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +21 -0
- package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -7
- package/dist/src/ip_bits.d.ts.map +1 -0
- package/dist/src/ip_bits.js +133 -0
- package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
- package/dist/src/ip_version.d.ts.map +1 -0
- package/dist/src/ip_version.js +6 -0
- package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +38 -35
- package/dist/src/ipaddress.d.ts.map +1 -0
- package/dist/src/ipaddress.js +1243 -0
- package/dist/src/ipv4.d.ts +15 -0
- package/dist/src/ipv4.d.ts.map +1 -0
- package/dist/src/ipv4.js +187 -0
- package/dist/src/ipv6.d.ts +13 -0
- package/dist/src/ipv6.d.ts.map +1 -0
- package/dist/src/ipv6.js +190 -0
- package/dist/src/ipv6_loopback.d.ts +6 -0
- package/dist/src/ipv6_loopback.d.ts.map +1 -0
- package/dist/src/ipv6_loopback.js +51 -0
- package/dist/src/ipv6_mapped.d.ts +6 -0
- package/dist/src/ipv6_mapped.d.ts.map +1 -0
- package/dist/src/ipv6_mapped.js +159 -0
- package/dist/src/ipv6_unspec.d.ts +6 -0
- package/dist/src/ipv6_unspec.d.ts.map +1 -0
- package/dist/src/ipv6_unspec.js +57 -0
- package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
- package/dist/src/prefix.d.ts.map +1 -0
- package/dist/src/prefix.js +142 -0
- package/dist/src/prefix128.d.ts +7 -0
- package/dist/src/prefix128.d.ts.map +1 -0
- package/dist/src/prefix128.js +39 -0
- package/dist/src/prefix32.d.ts +7 -0
- package/dist/src/prefix32.d.ts.map +1 -0
- package/dist/src/prefix32.js +29 -0
- package/dist/src/rle.d.ts +19 -0
- package/dist/src/rle.d.ts.map +1 -0
- package/dist/src/rle.js +73 -0
- package/dist/test/test_crunchy_impls.d.ts +2 -0
- package/dist/test/test_crunchy_impls.d.ts.map +1 -0
- package/dist/test/test_crunchy_impls.js +150 -0
- package/dist/test/test_crunchy_with32bit.d.ts +2 -0
- package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
- package/dist/test/test_crunchy_with32bit.js +382 -0
- package/dist/test/test_ipaddress.d.ts +2 -0
- package/dist/test/test_ipaddress.d.ts.map +1 -0
- package/dist/test/test_ipaddress.js +182 -0
- package/dist/test/test_ipv4.d.ts +2 -0
- package/dist/test/test_ipv4.d.ts.map +1 -0
- package/dist/test/test_ipv4.js +561 -0
- package/dist/test/test_ipv6.d.ts +2 -0
- package/dist/test/test_ipv6.d.ts.map +1 -0
- package/dist/test/test_ipv6.js +313 -0
- package/dist/test/test_ipv6_loopback.d.ts +2 -0
- package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
- package/dist/test/test_ipv6_loopback.js +40 -0
- package/dist/test/test_ipv6_mapped.d.ts +2 -0
- package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
- package/dist/test/test_ipv6_mapped.js +89 -0
- package/dist/test/test_ipv6_unspec.d.ts +2 -0
- package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
- package/dist/test/test_ipv6_unspec.js +39 -0
- package/dist/test/test_prefix128.d.ts +2 -0
- package/dist/test/test_prefix128.d.ts.map +1 -0
- package/dist/test/test_prefix128.js +35 -0
- package/dist/test/test_prefix32.d.ts +2 -0
- package/dist/test/test_prefix32.d.ts.map +1 -0
- package/dist/test/test_prefix32.js +126 -0
- package/dist/test/test_rle.d.ts +2 -0
- package/dist/test/test_rle.d.ts.map +1 -0
- package/dist/test/test_rle.js +141 -0
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +11 -0
- package/package.json +41 -37
- package/src/crunchy.ts +731 -0
- package/src/index.ts +23 -0
- package/src/ip_bits.ts +148 -0
- package/src/ip_version.ts +6 -0
- package/src/ipaddress.ts +1347 -0
- package/src/ipv4.ts +203 -0
- package/src/ipv6.ts +202 -0
- package/src/ipv6_loopback.ts +55 -0
- package/src/ipv6_mapped.ts +163 -0
- package/src/ipv6_unspec.ts +59 -0
- package/src/prefix.ts +170 -0
- package/src/prefix128.ts +42 -0
- package/src/prefix32.ts +32 -0
- package/src/rle.ts +85 -0
- package/test/test_crunchy_impls.ts +179 -0
- package/test/test_crunchy_with32bit.ts +510 -0
- package/test/test_ipaddress.ts +208 -0
- package/test/test_ipv4.ts +596 -0
- package/test/test_ipv6.ts +325 -0
- package/test/test_ipv6_loopback.ts +52 -0
- package/test/test_ipv6_mapped.ts +101 -0
- package/test/test_ipv6_unspec.ts +51 -0
- package/test/test_prefix128.ts +39 -0
- package/test/test_prefix32.ts +130 -0
- package/test/test_rle.ts +144 -0
- package/LICENSE +0 -20
- package/README.md +0 -960
- package/crunchy.d.ts +0 -55
- package/index.d.ts +0 -12
- package/ipaddress.js +0 -2869
- package/ipaddress.js.map +0 -1
- package/ipv4.d.ts +0 -14
- package/ipv6.d.ts +0 -12
- package/ipv6_loopback.d.ts +0 -5
- package/ipv6_mapped.d.ts +0 -5
- package/ipv6_unspec.d.ts +0 -5
- package/prefix128.d.ts +0 -6
- package/prefix32.d.ts +0 -6
- package/rle.d.ts +0 -14
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Crunchy from "./crunchy.js";
|
|
2
|
+
import IPAddress from "./ipaddress.js";
|
|
3
|
+
export declare const Ipv4: {
|
|
4
|
+
from_number(addr: Crunchy, prefix_num: number): IPAddress | null;
|
|
5
|
+
create(str: string): IPAddress | null;
|
|
6
|
+
ipv4_is_private(my: IPAddress): boolean;
|
|
7
|
+
ipv4_is_loopback(my: IPAddress): boolean;
|
|
8
|
+
to_ipv6(ia: IPAddress): IPAddress;
|
|
9
|
+
is_class_a(my: IPAddress): boolean;
|
|
10
|
+
is_class_b(my: IPAddress): boolean;
|
|
11
|
+
is_class_c(my: IPAddress): boolean;
|
|
12
|
+
parse_classful(ip_si: string): IPAddress | null;
|
|
13
|
+
};
|
|
14
|
+
export default Ipv4;
|
|
15
|
+
//# sourceMappingURL=ipv4.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipv4.d.ts","sourceRoot":"","sources":["../../src/ipv4.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAcvC,eAAO,MAAM,IAAI;sBACG,OAAO,cAAc,MAAM,GAAG,SAAS,GAAG,IAAI;gBAgBpD,MAAM,GAAG,SAAS,GAAG,IAAI;wBA0CjB,SAAS,GAAG,OAAO;yBAWlB,SAAS,GAAG,OAAO;gBAI5B,SAAS,GAAG,SAAS;mBA2BlB,SAAS,GAAG,OAAO;mBAgBnB,SAAS,GAAG,OAAO;mBAmBnB,SAAS,GAAG,OAAO;0BA8BZ,MAAM,GAAG,SAAS,GAAG,IAAI;CAkBhD,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
package/dist/src/ipv4.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import Crunchy from "./crunchy.js";
|
|
2
|
+
import Prefix32 from "./prefix32.js";
|
|
3
|
+
import IPAddress from "./ipaddress.js";
|
|
4
|
+
import IpBits from "./ip_bits.js";
|
|
5
|
+
import Prefix128 from "./prefix128.js";
|
|
6
|
+
import Ipv6 from "./ipv6.js";
|
|
7
|
+
function prefix32_or_throw(num) {
|
|
8
|
+
const ret = Prefix32.create(num);
|
|
9
|
+
if (ret === null) {
|
|
10
|
+
throw new Error(`invalid ipv4 prefix: ${num}`);
|
|
11
|
+
}
|
|
12
|
+
return ret;
|
|
13
|
+
}
|
|
14
|
+
export const Ipv4 = {
|
|
15
|
+
from_number(addr, prefix_num) {
|
|
16
|
+
const prefix = Prefix32.create(prefix_num);
|
|
17
|
+
if (!prefix) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return new IPAddress({
|
|
21
|
+
ip_bits: IpBits.v4(),
|
|
22
|
+
host_address: addr.clone(),
|
|
23
|
+
prefix,
|
|
24
|
+
mapped: null,
|
|
25
|
+
vt_is_private: Ipv4.ipv4_is_private,
|
|
26
|
+
vt_is_loopback: Ipv4.ipv4_is_loopback,
|
|
27
|
+
vt_to_ipv6: Ipv4.to_ipv6,
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
create(str) {
|
|
31
|
+
// console.log("create:v4:", str);
|
|
32
|
+
// let enable = str == "0.0.0.0/0";
|
|
33
|
+
const tmp = IPAddress.split_at_slash(str);
|
|
34
|
+
const ip = tmp[0];
|
|
35
|
+
const netmask = tmp[1];
|
|
36
|
+
if (!IPAddress.is_valid_ipv4(ip)) {
|
|
37
|
+
// enable && console.log("xx1");
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
let ip_prefix_num = 32;
|
|
41
|
+
if (netmask) {
|
|
42
|
+
// netmask is defined
|
|
43
|
+
ip_prefix_num = IPAddress.parse_netmask_to_prefix(netmask);
|
|
44
|
+
if (ip_prefix_num === null) {
|
|
45
|
+
// enable && console.log("xx2");
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
// if ip_prefix.ip_bits.version
|
|
49
|
+
}
|
|
50
|
+
const ip_prefix = Prefix32.create(ip_prefix_num);
|
|
51
|
+
if (ip_prefix === null) {
|
|
52
|
+
// enable && console.log("xx3");
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const split_number = IPAddress.split_to_u32(ip);
|
|
56
|
+
if (split_number === null) {
|
|
57
|
+
// enable && console.log("xx4");
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
// console.log(">>>>>>>", ip, ip_prefix);
|
|
61
|
+
return new IPAddress({
|
|
62
|
+
ip_bits: IpBits.v4(),
|
|
63
|
+
host_address: split_number,
|
|
64
|
+
prefix: ip_prefix,
|
|
65
|
+
mapped: null,
|
|
66
|
+
vt_is_private: Ipv4.ipv4_is_private,
|
|
67
|
+
vt_is_loopback: Ipv4.ipv4_is_loopback,
|
|
68
|
+
vt_to_ipv6: Ipv4.to_ipv6,
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
ipv4_is_private(my) {
|
|
72
|
+
return ([
|
|
73
|
+
IPAddress.parse_or_throw("10.0.0.0/8"),
|
|
74
|
+
IPAddress.parse_or_throw("169.254.0.0/16"),
|
|
75
|
+
IPAddress.parse_or_throw("172.16.0.0/12"),
|
|
76
|
+
IPAddress.parse_or_throw("192.168.0.0/16"),
|
|
77
|
+
].find((i) => i.includes(my)) != null);
|
|
78
|
+
},
|
|
79
|
+
ipv4_is_loopback(my) {
|
|
80
|
+
return IPAddress.parse_or_throw("127.0.0.0/8").includes(my);
|
|
81
|
+
},
|
|
82
|
+
to_ipv6(ia) {
|
|
83
|
+
const prefix = Prefix128.create(ia.prefix.num);
|
|
84
|
+
if (prefix === null) {
|
|
85
|
+
throw new Error(`invalid ipv6 prefix: ${ia.prefix.num}`);
|
|
86
|
+
}
|
|
87
|
+
return new IPAddress({
|
|
88
|
+
ip_bits: IpBits.v6(),
|
|
89
|
+
host_address: ia.host_address.clone(),
|
|
90
|
+
prefix,
|
|
91
|
+
mapped: null,
|
|
92
|
+
vt_is_private: Ipv6.ipv6_is_private,
|
|
93
|
+
vt_is_loopback: Ipv6.ipv6_is_loopback,
|
|
94
|
+
vt_to_ipv6: Ipv6.to_ipv6,
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
// Checks whether the ip address belongs to a
|
|
98
|
+
// RFC 791 CLASS A network, no matter
|
|
99
|
+
// what the subnet mask is.
|
|
100
|
+
//
|
|
101
|
+
// Example:
|
|
102
|
+
//
|
|
103
|
+
// ip = IPAddress("10.0.0.1/24")
|
|
104
|
+
//
|
|
105
|
+
// ip.a?
|
|
106
|
+
// // => true
|
|
107
|
+
//
|
|
108
|
+
is_class_a(my) {
|
|
109
|
+
// console.log("is_class_a:", my.to_string(), Crunchy.from_string_or_throw("80000000", 16), my.is_ipv4());
|
|
110
|
+
return my.is_ipv4() && my.host_address.lt(Crunchy.from_string_or_throw("80000000", 16));
|
|
111
|
+
},
|
|
112
|
+
// Checks whether the ip address belongs to a
|
|
113
|
+
// RFC 791 CLASS B network, no matter
|
|
114
|
+
// what the subnet mask is.
|
|
115
|
+
//
|
|
116
|
+
// Example:
|
|
117
|
+
//
|
|
118
|
+
// ip = IPAddress("172.16.10.1/24")
|
|
119
|
+
//
|
|
120
|
+
// ip.b?
|
|
121
|
+
// // => true
|
|
122
|
+
//
|
|
123
|
+
is_class_b(my) {
|
|
124
|
+
return (my.is_ipv4() &&
|
|
125
|
+
Crunchy.from_string_or_throw("80000000", 16).lte(my.host_address) &&
|
|
126
|
+
my.host_address.lt(Crunchy.from_string_or_throw("c0000000", 16)));
|
|
127
|
+
},
|
|
128
|
+
// Checks whether the ip address belongs to a
|
|
129
|
+
// RFC 791 CLASS C network, no matter
|
|
130
|
+
// what the subnet mask is.
|
|
131
|
+
//
|
|
132
|
+
// Example:
|
|
133
|
+
//
|
|
134
|
+
// ip = IPAddress("192.168.1.1/30")
|
|
135
|
+
//
|
|
136
|
+
// ip.c?
|
|
137
|
+
// // => true
|
|
138
|
+
//
|
|
139
|
+
is_class_c(my) {
|
|
140
|
+
return (my.is_ipv4() &&
|
|
141
|
+
Crunchy.from_string_or_throw("c0000000", 16).lte(my.host_address) &&
|
|
142
|
+
my.host_address.lt(Crunchy.from_string_or_throw("e0000000", 16)));
|
|
143
|
+
},
|
|
144
|
+
// Creates a new IPv4 address object by parsing the
|
|
145
|
+
// address in a classful way.
|
|
146
|
+
//
|
|
147
|
+
// Classful addresses have a fixed netmask based on the
|
|
148
|
+
// class they belong to:
|
|
149
|
+
//
|
|
150
|
+
// * Class A, from 0.0.0.0 to 127.255.255.255
|
|
151
|
+
// * Class B, from 128.0.0.0 to 191.255.255.255
|
|
152
|
+
// * Class C, D and E, from 192.0.0.0 to 255.255.255.254
|
|
153
|
+
//
|
|
154
|
+
// Example:
|
|
155
|
+
//
|
|
156
|
+
// ip = IPAddress::IPv4.parse_classful "10.0.0.1"
|
|
157
|
+
//
|
|
158
|
+
// ip.netmask
|
|
159
|
+
// // => "255.0.0.0"
|
|
160
|
+
// ip.a?
|
|
161
|
+
// // => true
|
|
162
|
+
//
|
|
163
|
+
// Note that classes C, D and E will all have a default
|
|
164
|
+
// prefix of /24 or 255.255.255.0
|
|
165
|
+
//
|
|
166
|
+
parse_classful(ip_si) {
|
|
167
|
+
if (!IPAddress.is_valid_ipv4(ip_si)) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
const o_ip = IPAddress.parse(ip_si);
|
|
171
|
+
if (o_ip === null) {
|
|
172
|
+
return o_ip;
|
|
173
|
+
}
|
|
174
|
+
const ip = o_ip;
|
|
175
|
+
if (Ipv4.is_class_a(ip)) {
|
|
176
|
+
ip.prefix = prefix32_or_throw(8);
|
|
177
|
+
}
|
|
178
|
+
else if (Ipv4.is_class_b(ip)) {
|
|
179
|
+
ip.prefix = prefix32_or_throw(16);
|
|
180
|
+
}
|
|
181
|
+
else if (Ipv4.is_class_c(ip)) {
|
|
182
|
+
ip.prefix = prefix32_or_throw(24);
|
|
183
|
+
}
|
|
184
|
+
return ip;
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
export default Ipv4;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import IPAddress from "./ipaddress.js";
|
|
2
|
+
import Crunchy from "./crunchy.js";
|
|
3
|
+
export declare const Ipv6: {
|
|
4
|
+
from_str(str: string, radix: number, prefix: number): IPAddress | null;
|
|
5
|
+
enhance_if_mapped(ip: IPAddress): IPAddress | null;
|
|
6
|
+
from_int(adr: Crunchy, prefix_num: number): IPAddress | null;
|
|
7
|
+
create(str: string): IPAddress | null;
|
|
8
|
+
to_ipv6(ia: IPAddress): IPAddress;
|
|
9
|
+
ipv6_is_loopback(my: IPAddress): boolean;
|
|
10
|
+
ipv6_is_private(my: IPAddress): boolean;
|
|
11
|
+
};
|
|
12
|
+
export default Ipv6;
|
|
13
|
+
//# sourceMappingURL=ipv6.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipv6.d.ts","sourceRoot":"","sources":["../../src/ipv6.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAGvC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,eAAO,MAAM,IAAI;kBA0DD,MAAM,SAAS,MAAM,UAAU,MAAM,GAAG,SAAS,GAAG,IAAI;0BAQhD,SAAS,GAAG,SAAS,GAAG,IAAI;kBAoCpC,OAAO,cAAc,MAAM,GAAG,SAAS,GAAG,IAAI;gBAmChD,MAAM,GAAG,SAAS,GAAG,IAAI;gBA4CzB,SAAS,GAAG,SAAS;yBAIZ,SAAS,GAAG,OAAO;wBAKpB,SAAS,GAAG,OAAO;CAGxC,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
package/dist/src/ipv6.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import IpBits from "./ip_bits.js";
|
|
2
|
+
import IPAddress from "./ipaddress.js";
|
|
3
|
+
import Ipv4 from "./ipv4.js";
|
|
4
|
+
import Prefix128 from "./prefix128.js";
|
|
5
|
+
import Crunchy from "./crunchy.js";
|
|
6
|
+
export const Ipv6 = {
|
|
7
|
+
// =Name
|
|
8
|
+
//
|
|
9
|
+
// IPAddress::IPv6 - IP version 6 address manipulation library
|
|
10
|
+
//
|
|
11
|
+
// =Synopsis
|
|
12
|
+
//
|
|
13
|
+
// require 'ipaddress'
|
|
14
|
+
//
|
|
15
|
+
// =Description
|
|
16
|
+
//
|
|
17
|
+
// Class IPAddress::IPv6 is used to handle IPv6 type addresses.
|
|
18
|
+
//
|
|
19
|
+
// == IPv6 addresses
|
|
20
|
+
//
|
|
21
|
+
// IPv6 addresses are 128 bits long, in contrast with IPv4 addresses
|
|
22
|
+
// which are only 32 bits long. An IPv6 address is generally written as
|
|
23
|
+
// eight groups of four hexadecimal digits, each group representing 16
|
|
24
|
+
// bits or two octect. For example, the following is a valid IPv6
|
|
25
|
+
// address:
|
|
26
|
+
//
|
|
27
|
+
// 2001:0db8:0000:0000:0008:0800:200c:417a
|
|
28
|
+
//
|
|
29
|
+
// Letters in an IPv6 address are usually written downcase, as per
|
|
30
|
+
// RFC. You can create a new IPv6 object using uppercase letters, but
|
|
31
|
+
// they will be converted.
|
|
32
|
+
//
|
|
33
|
+
// === Compression
|
|
34
|
+
//
|
|
35
|
+
// Since IPv6 addresses are very long to write, there are some
|
|
36
|
+
// semplifications and compressions that you can use to shorten them.
|
|
37
|
+
//
|
|
38
|
+
// * Leading zeroes: all the leading zeroes within a group can be
|
|
39
|
+
// omitted: "0008" would become "8"
|
|
40
|
+
//
|
|
41
|
+
// * A string of consecutive zeroes can be replaced by the string
|
|
42
|
+
// "::". This can be only applied once.
|
|
43
|
+
//
|
|
44
|
+
// Using compression, the IPv6 address written above can be shorten into
|
|
45
|
+
// the following, equivalent, address
|
|
46
|
+
//
|
|
47
|
+
// 2001:db8::8:800:200c:417a
|
|
48
|
+
//
|
|
49
|
+
// This short version is often used in human representation.
|
|
50
|
+
//
|
|
51
|
+
// === Network Mask
|
|
52
|
+
//
|
|
53
|
+
// As we used to do with IPv4 addresses, an IPv6 address can be written
|
|
54
|
+
// using the prefix notation to specify the subnet mask:
|
|
55
|
+
//
|
|
56
|
+
// 2001:db8::8:800:200c:417a/64
|
|
57
|
+
//
|
|
58
|
+
// The /64 part means that the first 64 bits of the address are
|
|
59
|
+
// representing the network portion, and the last 64 bits are the host
|
|
60
|
+
// portion.
|
|
61
|
+
//
|
|
62
|
+
//
|
|
63
|
+
from_str(str, radix, prefix) {
|
|
64
|
+
const num = Crunchy.from_string(str, radix);
|
|
65
|
+
if (!num) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return Ipv6.from_int(num, prefix);
|
|
69
|
+
},
|
|
70
|
+
enhance_if_mapped(ip) {
|
|
71
|
+
// console.log("------A");
|
|
72
|
+
// println!("real mapped {:x} {:x}", &ip.host_address, ip.host_address.clone().shr(32));
|
|
73
|
+
if (ip.is_mapped()) {
|
|
74
|
+
// console.log("------B");
|
|
75
|
+
return ip;
|
|
76
|
+
}
|
|
77
|
+
// console.log("------C", ip);
|
|
78
|
+
const ipv6_top_96bit = ip.host_address.shr(32);
|
|
79
|
+
// console.log("------D", ip);
|
|
80
|
+
if (ipv6_top_96bit.eq(Crunchy.from_number(0xffff))) {
|
|
81
|
+
// console.log("------E");
|
|
82
|
+
const num = ip.host_address.mod(Crunchy.one().shl(32));
|
|
83
|
+
// console.log("------F");
|
|
84
|
+
if (num.eq(Crunchy.zero())) {
|
|
85
|
+
return ip;
|
|
86
|
+
}
|
|
87
|
+
// println!("ip:{},{:x}", ip.to_string(), num);
|
|
88
|
+
const ipv4_bits = IpBits.v4();
|
|
89
|
+
if (ipv4_bits.bits < ip.prefix.host_prefix()) {
|
|
90
|
+
// println!("enhance_if_mapped-2:{}:{}", ip.to_string(), ip.prefix.host_prefix());
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
// console.log("------G");
|
|
94
|
+
const mapped = Ipv4.from_number(num, ipv4_bits.bits - ip.prefix.host_prefix());
|
|
95
|
+
// console.log("------H");
|
|
96
|
+
if (!mapped) {
|
|
97
|
+
// println!("enhance_if_mapped-3");
|
|
98
|
+
return mapped;
|
|
99
|
+
}
|
|
100
|
+
// println!("real mapped!!!!!={}", mapped.clone().to_string());
|
|
101
|
+
ip.mapped = mapped;
|
|
102
|
+
}
|
|
103
|
+
return ip;
|
|
104
|
+
},
|
|
105
|
+
from_int(adr, prefix_num) {
|
|
106
|
+
const prefix = Prefix128.create(prefix_num);
|
|
107
|
+
if (prefix === null) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const ret = Ipv6.enhance_if_mapped(new IPAddress({
|
|
111
|
+
ip_bits: IpBits.v6(),
|
|
112
|
+
host_address: adr.clone(),
|
|
113
|
+
prefix,
|
|
114
|
+
mapped: null,
|
|
115
|
+
vt_is_private: Ipv6.ipv6_is_private,
|
|
116
|
+
vt_is_loopback: Ipv6.ipv6_is_loopback,
|
|
117
|
+
vt_to_ipv6: Ipv6.to_ipv6,
|
|
118
|
+
}));
|
|
119
|
+
// console.log("from_int:", adr, prefix, ret);
|
|
120
|
+
return ret;
|
|
121
|
+
},
|
|
122
|
+
// Creates a new IPv6 address object.
|
|
123
|
+
//
|
|
124
|
+
// An IPv6 address can be expressed in any of the following forms:
|
|
125
|
+
//
|
|
126
|
+
// * "2001:0db8:0000:0000:0008:0800:200C:417A": IPv6 address with no compression
|
|
127
|
+
// * "2001:db8:0:0:8:800:200C:417A": IPv6 address with leading zeros compression
|
|
128
|
+
// * "2001:db8::8:800:200C:417A": IPv6 address with full compression
|
|
129
|
+
//
|
|
130
|
+
// In all these 3 cases, a new IPv6 address object will be created, using the default
|
|
131
|
+
// subnet mask /128
|
|
132
|
+
//
|
|
133
|
+
// You can also specify the subnet mask as with IPv4 addresses:
|
|
134
|
+
//
|
|
135
|
+
// ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
|
|
136
|
+
//
|
|
137
|
+
create(str) {
|
|
138
|
+
// console.log("1>>>>>>>>>", str);
|
|
139
|
+
const [ip, o_netmask] = IPAddress.split_at_slash(str);
|
|
140
|
+
// console.log("2>>>>>>>>>", str);
|
|
141
|
+
if (IPAddress.is_valid_ipv6(ip)) {
|
|
142
|
+
// console.log("3>>>>>>>>>", str);
|
|
143
|
+
const o_num = IPAddress.split_to_num(ip);
|
|
144
|
+
if (o_num === null) {
|
|
145
|
+
// console.log("ipv6_create-1", str);
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
// console.log("4>>>>>>>>>", str);
|
|
149
|
+
let netmask = 128;
|
|
150
|
+
if (o_netmask !== null) {
|
|
151
|
+
netmask = IPAddress.parse_dec_str(o_netmask);
|
|
152
|
+
if (netmask === null) {
|
|
153
|
+
// console.log("ipv6_create-2", str);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// console.log("5>>>>>>>>>", str);
|
|
158
|
+
const prefix = Prefix128.create(netmask);
|
|
159
|
+
if (prefix === null) {
|
|
160
|
+
// console.log("ipv6_create-3", str);
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
// console.log("6>>>>>>>>>", str, prefix.num, o_netmask, netmask);
|
|
164
|
+
return Ipv6.enhance_if_mapped(new IPAddress({
|
|
165
|
+
ip_bits: IpBits.v6(),
|
|
166
|
+
host_address: o_num.crunchy,
|
|
167
|
+
prefix,
|
|
168
|
+
mapped: null,
|
|
169
|
+
vt_is_private: Ipv6.ipv6_is_private,
|
|
170
|
+
vt_is_loopback: Ipv6.ipv6_is_loopback,
|
|
171
|
+
vt_to_ipv6: Ipv6.to_ipv6,
|
|
172
|
+
}));
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
// console.log("ipv6_create-4", str);
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
}, // pub fn initialize
|
|
179
|
+
to_ipv6(ia) {
|
|
180
|
+
return ia.clone();
|
|
181
|
+
},
|
|
182
|
+
ipv6_is_loopback(my) {
|
|
183
|
+
// console.log("*************", my.host_address, Crunchy.one());
|
|
184
|
+
return my.host_address.eq(Crunchy.one());
|
|
185
|
+
},
|
|
186
|
+
ipv6_is_private(my) {
|
|
187
|
+
return IPAddress.parse_or_throw("fd00::/8").includes(my);
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
export default Ipv6;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipv6_loopback.d.ts","sourceRoot":"","sources":["../../src/ipv6_loopback.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAKvC,eAAO,MAAM,YAAY;cA4Cb,SAAS,GAAG,IAAI;CAG3B,CAAC;AAEF,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Ipv6 from "./ipv6.js";
|
|
2
|
+
import Crunchy from "./crunchy.js";
|
|
3
|
+
export const Ipv6Loopback = {
|
|
4
|
+
// The loopback address is a unicast localhost address. If an
|
|
5
|
+
// application in a host sends packets to this address, the IPv6 stack
|
|
6
|
+
// will loop these packets back on the same virtual interface.
|
|
7
|
+
//
|
|
8
|
+
// Loopback addresses are expressed in the following form:
|
|
9
|
+
//
|
|
10
|
+
// ::1
|
|
11
|
+
//
|
|
12
|
+
// or, with their appropriate prefix,
|
|
13
|
+
//
|
|
14
|
+
// ::1/128
|
|
15
|
+
//
|
|
16
|
+
// As for the unspecified addresses, IPv6 loopbacks can be created with
|
|
17
|
+
// IPAddress calling their own class:
|
|
18
|
+
//
|
|
19
|
+
// ip = IPAddress::IPv6::Loopback.new
|
|
20
|
+
//
|
|
21
|
+
// ip.to_string
|
|
22
|
+
// // => "::1/128"
|
|
23
|
+
//
|
|
24
|
+
// or by using the wrapper:
|
|
25
|
+
//
|
|
26
|
+
// ip = IPAddress "::1"
|
|
27
|
+
//
|
|
28
|
+
// ip.to_string
|
|
29
|
+
// // => "::1/128"
|
|
30
|
+
//
|
|
31
|
+
// Checking if an address is loopback is easy with the IPv6// loopback?
|
|
32
|
+
// method:
|
|
33
|
+
//
|
|
34
|
+
// ip.loopback?
|
|
35
|
+
// // => true
|
|
36
|
+
//
|
|
37
|
+
// The IPv6 loopback address corresponds to 127.0.0.1 in IPv4.
|
|
38
|
+
//
|
|
39
|
+
//
|
|
40
|
+
// Creates a new IPv6 unspecified address
|
|
41
|
+
//
|
|
42
|
+
// ip = IPAddress::IPv6::Loopback.new
|
|
43
|
+
//
|
|
44
|
+
// ip.to_string
|
|
45
|
+
// // => "::1/128"
|
|
46
|
+
//
|
|
47
|
+
create() {
|
|
48
|
+
return Ipv6.from_int(Crunchy.one(), 128);
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
export default Ipv6Loopback;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipv6_mapped.d.ts","sourceRoot":"","sources":["../../src/ipv6_mapped.ts"],"names":[],"mappings":"AAEA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,eAAO,MAAM,UAAU;gBA6ET,MAAM,GAAG,SAAS,GAAG,IAAI;CA+EtC,CAAC;AAEF,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import Crunchy from "./crunchy.js";
|
|
2
|
+
import IpBits from "./ip_bits.js";
|
|
3
|
+
import IPAddress from "./ipaddress.js";
|
|
4
|
+
export const Ipv6Mapped = {
|
|
5
|
+
// Ac
|
|
6
|
+
// It is usually identified as a IPv4 mapped IPv6 address, a particular
|
|
7
|
+
// IPv6 address which aids the transition from IPv4 to IPv6. The
|
|
8
|
+
// structure of the address is
|
|
9
|
+
//
|
|
10
|
+
// ::ffff:w.y.x.z
|
|
11
|
+
//
|
|
12
|
+
// where w.x.y.z is a normal IPv4 address. For example, the following is
|
|
13
|
+
// a mapped IPv6 address:
|
|
14
|
+
//
|
|
15
|
+
// ::ffff:192.168.100.1
|
|
16
|
+
//
|
|
17
|
+
// IPAddress is very powerful in handling mapped IPv6 addresses, as the
|
|
18
|
+
// IPv4 portion is stored internally as a normal IPv4 object. Let's have
|
|
19
|
+
// a look at some examples. To create a new mapped address, just use the
|
|
20
|
+
// class builder itself
|
|
21
|
+
//
|
|
22
|
+
// ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
|
|
23
|
+
//
|
|
24
|
+
// or just use the wrapper method
|
|
25
|
+
//
|
|
26
|
+
// ip6 = IPAddress "::ffff:172.16.10.1/128"
|
|
27
|
+
//
|
|
28
|
+
// Let's check it's really a mapped address:
|
|
29
|
+
//
|
|
30
|
+
// ip6.mapped?
|
|
31
|
+
// // => true
|
|
32
|
+
//
|
|
33
|
+
// ip6.to_string
|
|
34
|
+
// // => "::FFFF:172.16.10.1/128"
|
|
35
|
+
//
|
|
36
|
+
// Now with the +ipv4+ attribute, we can easily access the IPv4 portion
|
|
37
|
+
// of the mapped IPv6 address:
|
|
38
|
+
//
|
|
39
|
+
// ip6.ipv4.address
|
|
40
|
+
// // => "172.16.10.1"
|
|
41
|
+
//
|
|
42
|
+
// Internally, the IPv4 address is stored as two 16 bits
|
|
43
|
+
// groups. Therefore all the usual methods for an IPv6 address are
|
|
44
|
+
// working perfectly fine:
|
|
45
|
+
//
|
|
46
|
+
// ip6.to_hex
|
|
47
|
+
// // => "00000000000000000000ffffac100a01"
|
|
48
|
+
//
|
|
49
|
+
// ip6.address
|
|
50
|
+
// // => "0000:0000:0000:0000:0000:ffff:ac10:0a01"
|
|
51
|
+
//
|
|
52
|
+
// A mapped IPv6 can also be created just by specify the address in the
|
|
53
|
+
// following format:
|
|
54
|
+
//
|
|
55
|
+
// ip6 = IPAddress "::172.16.10.1"
|
|
56
|
+
//
|
|
57
|
+
// That is, two colons and the IPv4 address. However, as by RFC, the ffff
|
|
58
|
+
// group will be automatically added at the beginning
|
|
59
|
+
//
|
|
60
|
+
// ip6.to_string
|
|
61
|
+
// => "::ffff:172.16.10.1/128"
|
|
62
|
+
//
|
|
63
|
+
// making it a mapped IPv6 compatible address.
|
|
64
|
+
//
|
|
65
|
+
//
|
|
66
|
+
// Creates a new IPv6 IPv4-mapped address
|
|
67
|
+
//
|
|
68
|
+
// ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
|
|
69
|
+
//
|
|
70
|
+
// ipv6.ipv4.class
|
|
71
|
+
// // => IPAddress::IPv4
|
|
72
|
+
//
|
|
73
|
+
// An IPv6 IPv4-mapped address can also be created using the
|
|
74
|
+
// IPv6 only format of the address:
|
|
75
|
+
//
|
|
76
|
+
// ip6 = IPAddress::IPv6::Mapped.new "::0d01:4403"
|
|
77
|
+
//
|
|
78
|
+
// ip6.to_string
|
|
79
|
+
// // => "::ffff:13.1.68.3"
|
|
80
|
+
//
|
|
81
|
+
create(str) {
|
|
82
|
+
// console.log("mapped-1");
|
|
83
|
+
const i = IPAddress.split_at_slash(str);
|
|
84
|
+
const ip = i[0];
|
|
85
|
+
const o_netmask = i[1];
|
|
86
|
+
const split_colon = ip.split(":");
|
|
87
|
+
if (split_colon.length <= 1) {
|
|
88
|
+
// console.log("mapped-2");
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
// if split_colon.get(0).unwrap().len() > 0 {
|
|
92
|
+
// // println!("---1a");
|
|
93
|
+
// return Err(format!("not mapped format-2: {}", &str));
|
|
94
|
+
// }
|
|
95
|
+
// let mapped: Option<IPAddress> = None;
|
|
96
|
+
let netmask = "";
|
|
97
|
+
if (o_netmask !== null) {
|
|
98
|
+
netmask = `/${o_netmask}`;
|
|
99
|
+
}
|
|
100
|
+
const ipv4_str = split_colon[split_colon.length - 1];
|
|
101
|
+
if (IPAddress.is_valid_ipv4(ipv4_str)) {
|
|
102
|
+
const ipv4 = IPAddress.parse(`${ipv4_str}${netmask}`);
|
|
103
|
+
if (ipv4 === null) {
|
|
104
|
+
// console.log("mapped-3");
|
|
105
|
+
return ipv4;
|
|
106
|
+
}
|
|
107
|
+
// mapped = Some(ipv4.unwrap());
|
|
108
|
+
const addr = ipv4;
|
|
109
|
+
const ipv6_bits = IpBits.v6();
|
|
110
|
+
const part_mod = Crunchy.from_number(ipv6_bits.part_mod);
|
|
111
|
+
const up_addr = addr.host_address.clone();
|
|
112
|
+
const down_addr = addr.host_address.clone();
|
|
113
|
+
let rebuild_ipv6 = "";
|
|
114
|
+
let colon = "";
|
|
115
|
+
for (let i = 0; i < split_colon.length - 1; ++i) {
|
|
116
|
+
rebuild_ipv6 += colon;
|
|
117
|
+
rebuild_ipv6 += split_colon[i];
|
|
118
|
+
colon = ":";
|
|
119
|
+
}
|
|
120
|
+
rebuild_ipv6 += colon;
|
|
121
|
+
const high_part = up_addr.shr(IpBits.v6().part_bits).mod(part_mod).toString(16);
|
|
122
|
+
const low_part = down_addr.mod(part_mod).toString(16);
|
|
123
|
+
const bits = ipv6_bits.bits - addr.prefix.host_prefix();
|
|
124
|
+
const rebuild_ipv4 = `${high_part}:${low_part}/${bits}`;
|
|
125
|
+
rebuild_ipv6 += rebuild_ipv4;
|
|
126
|
+
// console.log("-----A", rebuild_ipv6, part_mod);
|
|
127
|
+
const r_ipv6 = IPAddress.parse(rebuild_ipv6);
|
|
128
|
+
if (r_ipv6 === null) {
|
|
129
|
+
// println!("---3|{}", &rebuild_ipv6);
|
|
130
|
+
// console.log("mapped-4");
|
|
131
|
+
return r_ipv6;
|
|
132
|
+
}
|
|
133
|
+
if (r_ipv6.is_mapped()) {
|
|
134
|
+
// console.log("mapped-5");
|
|
135
|
+
return r_ipv6;
|
|
136
|
+
}
|
|
137
|
+
const ipv6 = r_ipv6;
|
|
138
|
+
const p96bit = ipv6.host_address.shr(32);
|
|
139
|
+
if (!p96bit.eq(Crunchy.zero())) {
|
|
140
|
+
// println!("---4|{}", &rebuild_ipv6);
|
|
141
|
+
// console.log("mapped-6",ipv6.host_address, p96bit, Crunchy.zero());
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
{
|
|
145
|
+
const r_ipv6 = IPAddress.parse(`::ffff:${rebuild_ipv4}`);
|
|
146
|
+
if (r_ipv6 === null) {
|
|
147
|
+
// println!("---3|{}", &rebuild_ipv6);
|
|
148
|
+
// console.log("mapped-7");
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
// console.log("mapped-8");
|
|
152
|
+
return r_ipv6;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// console.log("mapped-9");
|
|
156
|
+
return null;
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
export default Ipv6Mapped;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipv6_unspec.d.ts","sourceRoot":"","sources":["../../src/ipv6_unspec.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,eAAO,MAAM,UAAU;cAkDX,SAAS,GAAG,IAAI;CAI3B,CAAC;AAEF,eAAe,UAAU,CAAC"}
|