ipaddress 0.2.6 → 0.2.9
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 +107 -33
- package/dist/src/crunchy.d.ts.map +1 -1
- package/dist/src/crunchy.js +274 -142
- package/dist/src/ip_bits.d.ts +2 -2
- package/dist/src/ip_bits.d.ts.map +1 -1
- package/dist/src/ipaddress.d.ts +24 -23
- package/dist/src/ipaddress.d.ts.map +1 -1
- package/dist/src/ipaddress.js +22 -7
- package/dist/src/ipv4.d.ts +3 -3
- package/dist/src/ipv4.d.ts.map +1 -1
- package/dist/src/ipv4.js +26 -15
- package/dist/src/ipv6.d.ts +4 -4
- package/dist/src/ipv6.d.ts.map +1 -1
- package/dist/src/ipv6.js +1 -1
- package/dist/src/ipv6_loopback.d.ts +1 -1
- package/dist/src/ipv6_loopback.d.ts.map +1 -1
- package/dist/src/ipv6_mapped.d.ts +1 -1
- package/dist/src/ipv6_mapped.d.ts.map +1 -1
- package/dist/src/ipv6_unspec.d.ts +1 -1
- package/dist/src/ipv6_unspec.d.ts.map +1 -1
- package/dist/src/prefix128.d.ts +1 -1
- package/dist/src/prefix128.d.ts.map +1 -1
- package/dist/src/prefix128.js +5 -1
- package/dist/src/prefix32.d.ts +1 -1
- package/dist/src/prefix32.d.ts.map +1 -1
- package/dist/src/prefix32.js +5 -1
- 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.js → test_crunchy_with32bit.js} +113 -182
- package/dist/test/test_ipv4.js +1 -1
- package/dist/test/test_ipv6.js +1 -1
- package/package.json +10 -9
- package/src/crunchy.ts +365 -180
- package/src/ip_bits.ts +11 -11
- package/src/ipaddress.ts +49 -33
- package/src/ipv4.ts +32 -19
- package/src/ipv6.ts +6 -6
- package/src/ipv6_loopback.ts +1 -1
- package/src/ipv6_mapped.ts +1 -1
- package/src/ipv6_unspec.ts +1 -1
- package/src/prefix128.ts +6 -2
- package/src/prefix32.ts +6 -2
- package/test/test_crunchy_impls.ts +179 -0
- package/test/{test_crunchy.ts → test_crunchy_with32bit.ts} +131 -235
- package/test/test_ipaddress.ts +9 -9
- package/test/test_ipv4.ts +115 -115
- package/test/test_ipv6.ts +62 -62
- package/test/test_ipv6_loopback.ts +1 -1
- package/test/test_ipv6_mapped.ts +4 -4
- package/test/test_ipv6_unspec.ts +1 -1
- package/test/test_prefix128.ts +2 -2
- package/test/test_prefix32.ts +13 -13
- package/dist/test/test_crunchy.d.ts +0 -2
- package/dist/test/test_crunchy.d.ts.map +0 -1
package/src/ip_bits.ts
CHANGED
|
@@ -8,15 +8,15 @@ export type ToString = (source: IpBits, num: Crunchy) => string;
|
|
|
8
8
|
|
|
9
9
|
// #[derive(Debug, Clone)]
|
|
10
10
|
export class IpBits {
|
|
11
|
-
version
|
|
12
|
-
vt_as_compressed_string
|
|
13
|
-
vt_as_uncompressed_string
|
|
14
|
-
bits
|
|
15
|
-
part_bits
|
|
16
|
-
dns_bits
|
|
17
|
-
rev_domain
|
|
18
|
-
part_mod
|
|
19
|
-
host_ofs
|
|
11
|
+
version!: IpVersion;
|
|
12
|
+
vt_as_compressed_string!: ToString;
|
|
13
|
+
vt_as_uncompressed_string!: ToString;
|
|
14
|
+
bits!: number;
|
|
15
|
+
part_bits!: number;
|
|
16
|
+
dns_bits!: number;
|
|
17
|
+
rev_domain!: string;
|
|
18
|
+
part_mod!: number;
|
|
19
|
+
host_ofs!: Crunchy; // ipv4=1, ipv6=0
|
|
20
20
|
|
|
21
21
|
public clone(): IpBits {
|
|
22
22
|
// let my = new IpBits();
|
|
@@ -62,7 +62,7 @@ export class IpBits {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
public static _v4: IpBits = null;
|
|
65
|
+
public static _v4: IpBits | null = null;
|
|
66
66
|
public static v4(): IpBits {
|
|
67
67
|
if (IpBits._v4) {
|
|
68
68
|
return IpBits._v4;
|
|
@@ -81,7 +81,7 @@ export class IpBits {
|
|
|
81
81
|
return my;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
public static _v6: IpBits = null;
|
|
84
|
+
public static _v6: IpBits | null = null;
|
|
85
85
|
public static v6(): IpBits {
|
|
86
86
|
if (IpBits._v6) {
|
|
87
87
|
return IpBits._v6;
|
package/src/ipaddress.ts
CHANGED
|
@@ -28,17 +28,17 @@ export interface IPAddressParams {
|
|
|
28
28
|
ip_bits: IpBits;
|
|
29
29
|
host_address: Crunchy;
|
|
30
30
|
prefix: Prefix;
|
|
31
|
-
mapped?: IPAddress;
|
|
32
|
-
vt_is_private
|
|
33
|
-
vt_is_loopback
|
|
34
|
-
vt_to_ipv6
|
|
31
|
+
mapped?: IPAddress | null;
|
|
32
|
+
vt_is_private: Is;
|
|
33
|
+
vt_is_loopback: Is;
|
|
34
|
+
vt_to_ipv6: ToIpv4;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export class IPAddress {
|
|
38
38
|
ip_bits: IpBits;
|
|
39
39
|
host_address: Crunchy;
|
|
40
40
|
prefix: Prefix;
|
|
41
|
-
mapped: IPAddress;
|
|
41
|
+
mapped: IPAddress | null;
|
|
42
42
|
vt_is_private: Is;
|
|
43
43
|
vt_is_loopback: Is;
|
|
44
44
|
vt_to_ipv6: ToIpv4;
|
|
@@ -47,14 +47,14 @@ export class IPAddress {
|
|
|
47
47
|
this.ip_bits = obj.ip_bits;
|
|
48
48
|
this.host_address = obj.host_address;
|
|
49
49
|
this.prefix = obj.prefix;
|
|
50
|
-
this.mapped = obj.mapped;
|
|
50
|
+
this.mapped = obj.mapped ?? null;
|
|
51
51
|
this.vt_is_private = obj.vt_is_private;
|
|
52
52
|
this.vt_is_loopback = obj.vt_is_loopback;
|
|
53
53
|
this.vt_to_ipv6 = obj.vt_to_ipv6;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
public clone(): IPAddress {
|
|
57
|
-
let mapped: IPAddress = null;
|
|
57
|
+
let mapped: IPAddress | null = null;
|
|
58
58
|
if (this.mapped) {
|
|
59
59
|
mapped = this.mapped.clone();
|
|
60
60
|
}
|
|
@@ -136,7 +136,7 @@ export class IPAddress {
|
|
|
136
136
|
// ip_mapped.class
|
|
137
137
|
// //=> IPAddress.IPv6.Mapped
|
|
138
138
|
//
|
|
139
|
-
public static parse(str: string): IPAddress {
|
|
139
|
+
public static parse(str: string): IPAddress | null {
|
|
140
140
|
const re_mapped = new RegExp(":.+\\.");
|
|
141
141
|
// const re_ipv4 = new RegExp('\\.')
|
|
142
142
|
// const re_ipv6 = new RegExp(':')
|
|
@@ -155,7 +155,19 @@ export class IPAddress {
|
|
|
155
155
|
return null;
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
|
|
158
|
+
// Like parse(), but for literals that are known to be valid IP
|
|
159
|
+
// addresses (e.g. hardcoded well-known ranges). Throws instead of
|
|
160
|
+
// returning null so call sites don't need to deal with a nullable
|
|
161
|
+
// result they can never actually observe.
|
|
162
|
+
public static parse_or_throw(str: string): IPAddress {
|
|
163
|
+
const ip = IPAddress.parse(str);
|
|
164
|
+
if (ip === null) {
|
|
165
|
+
throw new Error(`invalid literal ip address: ${str}`);
|
|
166
|
+
}
|
|
167
|
+
return ip;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public static split_at_slash(str: string): [string, string | null] {
|
|
159
171
|
const slash: string[] = str.trim().split("/");
|
|
160
172
|
let addr = "";
|
|
161
173
|
if (slash[0]) {
|
|
@@ -169,7 +181,7 @@ export class IPAddress {
|
|
|
169
181
|
}
|
|
170
182
|
|
|
171
183
|
public from(addr: Crunchy, prefix: Prefix): IPAddress {
|
|
172
|
-
let mapped: IPAddress = null;
|
|
184
|
+
let mapped: IPAddress | null = null;
|
|
173
185
|
if (this.mapped) {
|
|
174
186
|
mapped = this.mapped.clone();
|
|
175
187
|
}
|
|
@@ -221,7 +233,7 @@ export class IPAddress {
|
|
|
221
233
|
return IPAddress.is_valid_ipv4(addr) || IPAddress.is_valid_ipv6(addr);
|
|
222
234
|
}
|
|
223
235
|
|
|
224
|
-
public static parse_dec_str(str: string): number {
|
|
236
|
+
public static parse_dec_str(str: string): number | null {
|
|
225
237
|
const re_digit = new RegExp("^\\d+$");
|
|
226
238
|
if (!re_digit.test(str)) {
|
|
227
239
|
// console.log("parse_dec_str:-1:", str);
|
|
@@ -236,7 +248,7 @@ export class IPAddress {
|
|
|
236
248
|
return part;
|
|
237
249
|
}
|
|
238
250
|
|
|
239
|
-
public static parse_hex_str(str: string): number {
|
|
251
|
+
public static parse_hex_str(str: string): number | null {
|
|
240
252
|
const re_digit = new RegExp("^[0-9a-fA-F]+$");
|
|
241
253
|
if (!re_digit.test(str)) {
|
|
242
254
|
return null;
|
|
@@ -258,7 +270,7 @@ export class IPAddress {
|
|
|
258
270
|
// IPAddress.valid_ipv4? "172.16.10.1"
|
|
259
271
|
// //=> true
|
|
260
272
|
//
|
|
261
|
-
public static parse_ipv4_part(i: string): number {
|
|
273
|
+
public static parse_ipv4_part(i: string): number | null {
|
|
262
274
|
const part = IPAddress.parse_dec_str(i);
|
|
263
275
|
// console.log("i=", i, part);
|
|
264
276
|
if (part === null || part >= 256) {
|
|
@@ -267,7 +279,7 @@ export class IPAddress {
|
|
|
267
279
|
return part;
|
|
268
280
|
}
|
|
269
281
|
|
|
270
|
-
public static split_to_u32(addr: string): Crunchy {
|
|
282
|
+
public static split_to_u32(addr: string): Crunchy | null {
|
|
271
283
|
let ip = Crunchy.zero();
|
|
272
284
|
let shift = 24;
|
|
273
285
|
let split_addr = addr.split(".");
|
|
@@ -310,7 +322,7 @@ export class IPAddress {
|
|
|
310
322
|
// IPAddress.valid_ipv6? "2002.DEAD.BEEF"
|
|
311
323
|
// //=> false
|
|
312
324
|
//
|
|
313
|
-
public static split_on_colon(addr: string): ResultCrunchyParts {
|
|
325
|
+
public static split_on_colon(addr: string): ResultCrunchyParts | null {
|
|
314
326
|
const parts = addr.trim().split(":");
|
|
315
327
|
let ip = Crunchy.zero();
|
|
316
328
|
if (parts.length == 1 && parts[0].length == 0) {
|
|
@@ -330,7 +342,7 @@ export class IPAddress {
|
|
|
330
342
|
return new ResultCrunchyParts(ip, parts_len);
|
|
331
343
|
}
|
|
332
344
|
|
|
333
|
-
public static split_to_num(addr: string): ResultCrunchyParts {
|
|
345
|
+
public static split_to_num(addr: string): ResultCrunchyParts | null {
|
|
334
346
|
// let ip = 0;
|
|
335
347
|
const pre_post = addr.trim().split("::");
|
|
336
348
|
if (pre_post.length > 2) {
|
|
@@ -389,7 +401,7 @@ export class IPAddress {
|
|
|
389
401
|
// &stack[i].to_string_uncompressed());
|
|
390
402
|
// }
|
|
391
403
|
// eslint-disable-next-line no-constant-condition
|
|
392
|
-
for (let pos = 0; true;
|
|
404
|
+
for (let pos = 0; true;) {
|
|
393
405
|
if (pos < 0) {
|
|
394
406
|
pos = 0;
|
|
395
407
|
}
|
|
@@ -719,13 +731,13 @@ export class IPAddress {
|
|
|
719
731
|
return IPAddress.parse_netmask_to_prefix(addr) !== null;
|
|
720
732
|
}
|
|
721
733
|
|
|
722
|
-
public static netmask_to_prefix(nm: Crunchy, bits: number): number {
|
|
734
|
+
public static netmask_to_prefix(nm: Crunchy, bits: number): number | null {
|
|
723
735
|
let prefix = 0;
|
|
724
736
|
let addr = nm.clone();
|
|
725
737
|
let in_host_part = true;
|
|
726
|
-
|
|
738
|
+
const two = Crunchy.two();
|
|
727
739
|
for (let _ = 0; _ < bits; _++) {
|
|
728
|
-
const bit = addr.
|
|
740
|
+
const bit = addr.mod(two).eq(Crunchy.one()) ? 1 : 0;
|
|
729
741
|
// console.log(">>>", bits, bit, addr, nm);
|
|
730
742
|
if (in_host_part && bit == 0) {
|
|
731
743
|
prefix = prefix + 1;
|
|
@@ -739,7 +751,7 @@ export class IPAddress {
|
|
|
739
751
|
return bits - prefix;
|
|
740
752
|
}
|
|
741
753
|
|
|
742
|
-
public static parse_netmask_to_prefix(netmask: string): number {
|
|
754
|
+
public static parse_netmask_to_prefix(netmask: string): number | null {
|
|
743
755
|
// console.log("--1", netmask);
|
|
744
756
|
const is_number = IPAddress.parse_dec_str(netmask);
|
|
745
757
|
if (is_number !== null) {
|
|
@@ -774,7 +786,7 @@ export class IPAddress {
|
|
|
774
786
|
// puts ip
|
|
775
787
|
// // => 172.16.100.4/22
|
|
776
788
|
//
|
|
777
|
-
public change_prefix(num: number): IPAddress {
|
|
789
|
+
public change_prefix(num: number): IPAddress | null {
|
|
778
790
|
const prefix = this.prefix.from(num);
|
|
779
791
|
if (!prefix) {
|
|
780
792
|
return null;
|
|
@@ -782,7 +794,7 @@ export class IPAddress {
|
|
|
782
794
|
return this.from(this.host_address, prefix);
|
|
783
795
|
}
|
|
784
796
|
|
|
785
|
-
public change_netmask(str: string): IPAddress {
|
|
797
|
+
public change_netmask(str: string): IPAddress | null {
|
|
786
798
|
const nm = IPAddress.parse_netmask_to_prefix(str);
|
|
787
799
|
if (!nm) {
|
|
788
800
|
return null;
|
|
@@ -823,14 +835,14 @@ export class IPAddress {
|
|
|
823
835
|
}
|
|
824
836
|
|
|
825
837
|
public to_s_mapped(): string {
|
|
826
|
-
if (this.is_mapped()) {
|
|
838
|
+
if (this.is_mapped() && this.mapped !== null) {
|
|
827
839
|
return `::ffff:${this.mapped.to_s()}`;
|
|
828
840
|
}
|
|
829
841
|
return this.to_s();
|
|
830
842
|
}
|
|
831
843
|
|
|
832
844
|
public to_string_mapped(): string {
|
|
833
|
-
if (this.is_mapped()) {
|
|
845
|
+
if (this.is_mapped() && this.mapped !== null) {
|
|
834
846
|
const mapped = this.mapped.clone();
|
|
835
847
|
return `${this.to_s_mapped()}/${mapped.prefix.num}`;
|
|
836
848
|
}
|
|
@@ -939,7 +951,7 @@ export class IPAddress {
|
|
|
939
951
|
for (const ipstr of vec) {
|
|
940
952
|
const ipa = IPAddress.parse(ipstr);
|
|
941
953
|
if (!ipa) {
|
|
942
|
-
return
|
|
954
|
+
return [];
|
|
943
955
|
}
|
|
944
956
|
ret.push(ipa);
|
|
945
957
|
}
|
|
@@ -1016,7 +1028,7 @@ export class IPAddress {
|
|
|
1016
1028
|
}
|
|
1017
1029
|
}
|
|
1018
1030
|
|
|
1019
|
-
public inc(): IPAddress {
|
|
1031
|
+
public inc(): IPAddress | null {
|
|
1020
1032
|
const ret = this.clone();
|
|
1021
1033
|
ret.host_address = ret.host_address.add(Crunchy.one());
|
|
1022
1034
|
if (ret.lte(this.last())) {
|
|
@@ -1025,7 +1037,7 @@ export class IPAddress {
|
|
|
1025
1037
|
return null;
|
|
1026
1038
|
}
|
|
1027
1039
|
|
|
1028
|
-
public dec(): IPAddress {
|
|
1040
|
+
public dec(): IPAddress | null {
|
|
1029
1041
|
const ret = this.clone();
|
|
1030
1042
|
ret.host_address = ret.host_address.sub(Crunchy.one());
|
|
1031
1043
|
if (ret.gte(this.first())) {
|
|
@@ -1216,11 +1228,15 @@ export class IPAddress {
|
|
|
1216
1228
|
return dup;
|
|
1217
1229
|
}
|
|
1218
1230
|
|
|
1219
|
-
public split(subnets: number): IPAddress[] {
|
|
1231
|
+
public split(subnets: number): IPAddress[] | null {
|
|
1220
1232
|
if (subnets == 0 || 1 << this.prefix.host_prefix() <= subnets) {
|
|
1221
1233
|
return null;
|
|
1222
1234
|
}
|
|
1223
|
-
const
|
|
1235
|
+
const newprefix = this.newprefix(subnets);
|
|
1236
|
+
if (newprefix === null) {
|
|
1237
|
+
return null;
|
|
1238
|
+
}
|
|
1239
|
+
const networks = this.subnet(newprefix.num);
|
|
1224
1240
|
if (!networks) {
|
|
1225
1241
|
return networks;
|
|
1226
1242
|
}
|
|
@@ -1255,7 +1271,7 @@ export class IPAddress {
|
|
|
1255
1271
|
//
|
|
1256
1272
|
// If +new_prefix+ is less than 1, returns 0.0.0.0/0
|
|
1257
1273
|
//
|
|
1258
|
-
public supernet(new_prefix: number): IPAddress {
|
|
1274
|
+
public supernet(new_prefix: number): IPAddress | null {
|
|
1259
1275
|
if (new_prefix >= this.prefix.num) {
|
|
1260
1276
|
return null;
|
|
1261
1277
|
}
|
|
@@ -1287,7 +1303,7 @@ export class IPAddress {
|
|
|
1287
1303
|
// The resulting number of subnets will of course always be
|
|
1288
1304
|
// a power of two.
|
|
1289
1305
|
//
|
|
1290
|
-
public subnet(subprefix: number): IPAddress[] {
|
|
1306
|
+
public subnet(subprefix: number): IPAddress[] | null {
|
|
1291
1307
|
if (subprefix < this.prefix.num || this.ip_bits.bits < subprefix) {
|
|
1292
1308
|
return null;
|
|
1293
1309
|
}
|
|
@@ -1317,7 +1333,7 @@ export class IPAddress {
|
|
|
1317
1333
|
return this.vt_to_ipv6(this);
|
|
1318
1334
|
}
|
|
1319
1335
|
|
|
1320
|
-
public newprefix(num: number): Prefix {
|
|
1336
|
+
public newprefix(num: number): Prefix | null {
|
|
1321
1337
|
for (let i = num; i < this.ip_bits.bits; ++i) {
|
|
1322
1338
|
const a = ~~Math.log2(i);
|
|
1323
1339
|
if (a == Math.log2(i)) {
|
package/src/ipv4.ts
CHANGED
|
@@ -4,9 +4,18 @@ import IPAddress from "./ipaddress.js";
|
|
|
4
4
|
import IpBits from "./ip_bits.js";
|
|
5
5
|
import Prefix128 from "./prefix128.js";
|
|
6
6
|
import Ipv6 from "./ipv6.js";
|
|
7
|
+
import Prefix from "./prefix.js";
|
|
8
|
+
|
|
9
|
+
function prefix32_or_throw(num: number): Prefix {
|
|
10
|
+
const ret = Prefix32.create(num);
|
|
11
|
+
if (ret === null) {
|
|
12
|
+
throw new Error(`invalid ipv4 prefix: ${num}`);
|
|
13
|
+
}
|
|
14
|
+
return ret;
|
|
15
|
+
}
|
|
7
16
|
|
|
8
17
|
export const Ipv4 = {
|
|
9
|
-
from_number(addr: Crunchy, prefix_num: number): IPAddress {
|
|
18
|
+
from_number(addr: Crunchy, prefix_num: number): IPAddress | null {
|
|
10
19
|
const prefix = Prefix32.create(prefix_num);
|
|
11
20
|
if (!prefix) {
|
|
12
21
|
return null;
|
|
@@ -22,7 +31,7 @@ export const Ipv4 = {
|
|
|
22
31
|
});
|
|
23
32
|
},
|
|
24
33
|
|
|
25
|
-
create(str: string): IPAddress {
|
|
34
|
+
create(str: string): IPAddress | null {
|
|
26
35
|
// console.log("create:v4:", str);
|
|
27
36
|
// let enable = str == "0.0.0.0/0";
|
|
28
37
|
const tmp = IPAddress.split_at_slash(str);
|
|
@@ -32,7 +41,7 @@ export const Ipv4 = {
|
|
|
32
41
|
// enable && console.log("xx1");
|
|
33
42
|
return null;
|
|
34
43
|
}
|
|
35
|
-
let ip_prefix_num = 32;
|
|
44
|
+
let ip_prefix_num: number | null = 32;
|
|
36
45
|
if (netmask) {
|
|
37
46
|
// netmask is defined
|
|
38
47
|
ip_prefix_num = IPAddress.parse_netmask_to_prefix(netmask);
|
|
@@ -67,23 +76,27 @@ export const Ipv4 = {
|
|
|
67
76
|
ipv4_is_private(my: IPAddress): boolean {
|
|
68
77
|
return (
|
|
69
78
|
[
|
|
70
|
-
IPAddress.
|
|
71
|
-
IPAddress.
|
|
72
|
-
IPAddress.
|
|
73
|
-
IPAddress.
|
|
79
|
+
IPAddress.parse_or_throw("10.0.0.0/8"),
|
|
80
|
+
IPAddress.parse_or_throw("169.254.0.0/16"),
|
|
81
|
+
IPAddress.parse_or_throw("172.16.0.0/12"),
|
|
82
|
+
IPAddress.parse_or_throw("192.168.0.0/16"),
|
|
74
83
|
].find((i) => i.includes(my)) != null
|
|
75
84
|
);
|
|
76
85
|
},
|
|
77
86
|
|
|
78
87
|
ipv4_is_loopback(my: IPAddress): boolean {
|
|
79
|
-
return IPAddress.
|
|
88
|
+
return IPAddress.parse_or_throw("127.0.0.0/8").includes(my);
|
|
80
89
|
},
|
|
81
90
|
|
|
82
91
|
to_ipv6(ia: IPAddress): IPAddress {
|
|
92
|
+
const prefix = Prefix128.create(ia.prefix.num);
|
|
93
|
+
if (prefix === null) {
|
|
94
|
+
throw new Error(`invalid ipv6 prefix: ${ia.prefix.num}`);
|
|
95
|
+
}
|
|
83
96
|
return new IPAddress({
|
|
84
97
|
ip_bits: IpBits.v6(),
|
|
85
98
|
host_address: ia.host_address.clone(),
|
|
86
|
-
prefix
|
|
99
|
+
prefix,
|
|
87
100
|
mapped: null,
|
|
88
101
|
vt_is_private: Ipv6.ipv6_is_private,
|
|
89
102
|
vt_is_loopback: Ipv6.ipv6_is_loopback,
|
|
@@ -103,8 +116,8 @@ export const Ipv4 = {
|
|
|
103
116
|
// // => true
|
|
104
117
|
//
|
|
105
118
|
is_class_a(my: IPAddress): boolean {
|
|
106
|
-
// console.log("is_class_a:", my.to_string(), Crunchy.
|
|
107
|
-
return my.is_ipv4() && my.host_address.lt(Crunchy.
|
|
119
|
+
// console.log("is_class_a:", my.to_string(), Crunchy.from_string_or_throw("80000000", 16), my.is_ipv4());
|
|
120
|
+
return my.is_ipv4() && my.host_address.lt(Crunchy.from_string_or_throw("80000000", 16));
|
|
108
121
|
},
|
|
109
122
|
|
|
110
123
|
// Checks whether the ip address belongs to a
|
|
@@ -121,8 +134,8 @@ export const Ipv4 = {
|
|
|
121
134
|
is_class_b(my: IPAddress): boolean {
|
|
122
135
|
return (
|
|
123
136
|
my.is_ipv4() &&
|
|
124
|
-
Crunchy.
|
|
125
|
-
my.host_address.lt(Crunchy.
|
|
137
|
+
Crunchy.from_string_or_throw("80000000", 16).lte(my.host_address) &&
|
|
138
|
+
my.host_address.lt(Crunchy.from_string_or_throw("c0000000", 16))
|
|
126
139
|
);
|
|
127
140
|
},
|
|
128
141
|
|
|
@@ -140,8 +153,8 @@ export const Ipv4 = {
|
|
|
140
153
|
is_class_c(my: IPAddress): boolean {
|
|
141
154
|
return (
|
|
142
155
|
my.is_ipv4() &&
|
|
143
|
-
Crunchy.
|
|
144
|
-
my.host_address.lt(Crunchy.
|
|
156
|
+
Crunchy.from_string_or_throw("c0000000", 16).lte(my.host_address) &&
|
|
157
|
+
my.host_address.lt(Crunchy.from_string_or_throw("e0000000", 16))
|
|
145
158
|
);
|
|
146
159
|
},
|
|
147
160
|
|
|
@@ -167,7 +180,7 @@ export const Ipv4 = {
|
|
|
167
180
|
// Note that classes C, D and E will all have a default
|
|
168
181
|
// prefix of /24 or 255.255.255.0
|
|
169
182
|
//
|
|
170
|
-
parse_classful(ip_si: string): IPAddress {
|
|
183
|
+
parse_classful(ip_si: string): IPAddress | null {
|
|
171
184
|
if (!IPAddress.is_valid_ipv4(ip_si)) {
|
|
172
185
|
return null;
|
|
173
186
|
}
|
|
@@ -177,11 +190,11 @@ export const Ipv4 = {
|
|
|
177
190
|
}
|
|
178
191
|
const ip = o_ip;
|
|
179
192
|
if (Ipv4.is_class_a(ip)) {
|
|
180
|
-
ip.prefix =
|
|
193
|
+
ip.prefix = prefix32_or_throw(8);
|
|
181
194
|
} else if (Ipv4.is_class_b(ip)) {
|
|
182
|
-
ip.prefix =
|
|
195
|
+
ip.prefix = prefix32_or_throw(16);
|
|
183
196
|
} else if (Ipv4.is_class_c(ip)) {
|
|
184
|
-
ip.prefix =
|
|
197
|
+
ip.prefix = prefix32_or_throw(24);
|
|
185
198
|
}
|
|
186
199
|
return ip;
|
|
187
200
|
},
|
package/src/ipv6.ts
CHANGED
|
@@ -62,7 +62,7 @@ export const Ipv6 = {
|
|
|
62
62
|
//
|
|
63
63
|
//
|
|
64
64
|
|
|
65
|
-
from_str(str: string, radix: number, prefix: number): IPAddress {
|
|
65
|
+
from_str(str: string, radix: number, prefix: number): IPAddress | null {
|
|
66
66
|
const num = Crunchy.from_string(str, radix);
|
|
67
67
|
if (!num) {
|
|
68
68
|
return null;
|
|
@@ -70,7 +70,7 @@ export const Ipv6 = {
|
|
|
70
70
|
return Ipv6.from_int(num, prefix);
|
|
71
71
|
},
|
|
72
72
|
|
|
73
|
-
enhance_if_mapped(ip: IPAddress): IPAddress {
|
|
73
|
+
enhance_if_mapped(ip: IPAddress): IPAddress | null {
|
|
74
74
|
// console.log("------A");
|
|
75
75
|
// println!("real mapped {:x} {:x}", &ip.host_address, ip.host_address.clone().shr(32));
|
|
76
76
|
if (ip.is_mapped()) {
|
|
@@ -106,7 +106,7 @@ export const Ipv6 = {
|
|
|
106
106
|
return ip;
|
|
107
107
|
},
|
|
108
108
|
|
|
109
|
-
from_int(adr: Crunchy, prefix_num: number): IPAddress {
|
|
109
|
+
from_int(adr: Crunchy, prefix_num: number): IPAddress | null {
|
|
110
110
|
const prefix = Prefix128.create(prefix_num);
|
|
111
111
|
if (prefix === null) {
|
|
112
112
|
return null;
|
|
@@ -141,7 +141,7 @@ export const Ipv6 = {
|
|
|
141
141
|
//
|
|
142
142
|
// ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
|
|
143
143
|
//
|
|
144
|
-
create(str: string): IPAddress {
|
|
144
|
+
create(str: string): IPAddress | null {
|
|
145
145
|
// console.log("1>>>>>>>>>", str);
|
|
146
146
|
const [ip, o_netmask] = IPAddress.split_at_slash(str);
|
|
147
147
|
// console.log("2>>>>>>>>>", str);
|
|
@@ -153,7 +153,7 @@ export const Ipv6 = {
|
|
|
153
153
|
return null;
|
|
154
154
|
}
|
|
155
155
|
// console.log("4>>>>>>>>>", str);
|
|
156
|
-
let netmask = 128;
|
|
156
|
+
let netmask: number | null = 128;
|
|
157
157
|
if (o_netmask !== null) {
|
|
158
158
|
netmask = IPAddress.parse_dec_str(o_netmask);
|
|
159
159
|
if (netmask === null) {
|
|
@@ -195,7 +195,7 @@ export const Ipv6 = {
|
|
|
195
195
|
},
|
|
196
196
|
|
|
197
197
|
ipv6_is_private(my: IPAddress): boolean {
|
|
198
|
-
return IPAddress.
|
|
198
|
+
return IPAddress.parse_or_throw("fd00::/8").includes(my);
|
|
199
199
|
},
|
|
200
200
|
};
|
|
201
201
|
|
package/src/ipv6_loopback.ts
CHANGED
package/src/ipv6_mapped.ts
CHANGED
package/src/ipv6_unspec.ts
CHANGED
package/src/prefix128.ts
CHANGED
|
@@ -14,7 +14,7 @@ const Prefix128 = {
|
|
|
14
14
|
// // => 64
|
|
15
15
|
//
|
|
16
16
|
// #[allow(unused_comparisons)]
|
|
17
|
-
create(num: number): Prefix {
|
|
17
|
+
create(num: number): Prefix | null {
|
|
18
18
|
if (num <= 128) {
|
|
19
19
|
// static _FROM: &'static (Fn(&Prefix, usize) -> Result<Prefix, String>) = &from;
|
|
20
20
|
// static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &Prefix128::to_ip_str;
|
|
@@ -31,7 +31,11 @@ const Prefix128 = {
|
|
|
31
31
|
},
|
|
32
32
|
|
|
33
33
|
from(my: Prefix, num: number): Prefix {
|
|
34
|
-
|
|
34
|
+
const ret = Prefix128.create(num);
|
|
35
|
+
if (ret === null) {
|
|
36
|
+
throw new Error(`invalid ipv6 prefix: ${num}`);
|
|
37
|
+
}
|
|
38
|
+
return ret;
|
|
35
39
|
},
|
|
36
40
|
};
|
|
37
41
|
|
package/src/prefix32.ts
CHANGED
|
@@ -3,10 +3,14 @@ import IpBits from "./ip_bits.js";
|
|
|
3
3
|
|
|
4
4
|
const Prefix32 = {
|
|
5
5
|
from(my: Prefix, num: number): Prefix {
|
|
6
|
-
|
|
6
|
+
const ret = Prefix32.create(num);
|
|
7
|
+
if (ret === null) {
|
|
8
|
+
throw new Error(`invalid ipv4 prefix: ${num}`);
|
|
9
|
+
}
|
|
10
|
+
return ret;
|
|
7
11
|
},
|
|
8
12
|
|
|
9
|
-
create(num: number): Prefix {
|
|
13
|
+
create(num: number): Prefix | null {
|
|
10
14
|
if (num >= 0 && num <= 32) {
|
|
11
15
|
// static _FROM: &'static (Fn(&::prefix::Prefix, usize) -> Result<::prefix::Prefix, String>) =
|
|
12
16
|
// &from;
|