ipaddress 0.2.3 → 0.2.6

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 (113) hide show
  1. package/{crunchy.d.ts → dist/src/crunchy.d.ts} +1 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +484 -0
  4. package/dist/src/index.d.ts +13 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +21 -0
  7. package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -5
  8. package/dist/src/ip_bits.d.ts.map +1 -0
  9. package/dist/src/ip_bits.js +133 -0
  10. package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
  11. package/dist/src/ip_version.d.ts.map +1 -0
  12. package/dist/src/ip_version.js +6 -0
  13. package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +18 -16
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1228 -0
  16. package/dist/src/ipv4.d.ts +15 -0
  17. package/dist/src/ipv4.d.ts.map +1 -0
  18. package/dist/src/ipv4.js +176 -0
  19. package/dist/src/ipv6.d.ts +13 -0
  20. package/dist/src/ipv6.d.ts.map +1 -0
  21. package/dist/src/ipv6.js +190 -0
  22. package/dist/src/ipv6_loopback.d.ts +6 -0
  23. package/dist/src/ipv6_loopback.d.ts.map +1 -0
  24. package/dist/src/ipv6_loopback.js +51 -0
  25. package/dist/src/ipv6_mapped.d.ts +6 -0
  26. package/dist/src/ipv6_mapped.d.ts.map +1 -0
  27. package/dist/src/ipv6_mapped.js +159 -0
  28. package/dist/src/ipv6_unspec.d.ts +6 -0
  29. package/dist/src/ipv6_unspec.d.ts.map +1 -0
  30. package/dist/src/ipv6_unspec.js +57 -0
  31. package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
  32. package/dist/src/prefix.d.ts.map +1 -0
  33. package/dist/src/prefix.js +142 -0
  34. package/dist/src/prefix128.d.ts +7 -0
  35. package/dist/src/prefix128.d.ts.map +1 -0
  36. package/dist/src/prefix128.js +35 -0
  37. package/dist/src/prefix32.d.ts +7 -0
  38. package/dist/src/prefix32.d.ts.map +1 -0
  39. package/dist/src/prefix32.js +25 -0
  40. package/dist/src/rle.d.ts +19 -0
  41. package/dist/src/rle.d.ts.map +1 -0
  42. package/dist/src/rle.js +73 -0
  43. package/dist/test/test_crunchy.d.ts +2 -0
  44. package/dist/test/test_crunchy.d.ts.map +1 -0
  45. package/dist/test/test_crunchy.js +451 -0
  46. package/dist/test/test_ipaddress.d.ts +2 -0
  47. package/dist/test/test_ipaddress.d.ts.map +1 -0
  48. package/dist/test/test_ipaddress.js +182 -0
  49. package/dist/test/test_ipv4.d.ts +2 -0
  50. package/dist/test/test_ipv4.d.ts.map +1 -0
  51. package/dist/test/test_ipv4.js +561 -0
  52. package/dist/test/test_ipv6.d.ts +2 -0
  53. package/dist/test/test_ipv6.d.ts.map +1 -0
  54. package/dist/test/test_ipv6.js +313 -0
  55. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  56. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  57. package/dist/test/test_ipv6_loopback.js +40 -0
  58. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  59. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_mapped.js +89 -0
  61. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  62. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_unspec.js +39 -0
  64. package/dist/test/test_prefix128.d.ts +2 -0
  65. package/dist/test/test_prefix128.d.ts.map +1 -0
  66. package/dist/test/test_prefix128.js +35 -0
  67. package/dist/test/test_prefix32.d.ts +2 -0
  68. package/dist/test/test_prefix32.d.ts.map +1 -0
  69. package/dist/test/test_prefix32.js +126 -0
  70. package/dist/test/test_rle.d.ts +2 -0
  71. package/dist/test/test_rle.d.ts.map +1 -0
  72. package/dist/test/test_rle.js +141 -0
  73. package/dist/vitest.config.d.ts +3 -0
  74. package/dist/vitest.config.d.ts.map +1 -0
  75. package/dist/vitest.config.js +11 -0
  76. package/package.json +40 -37
  77. package/src/crunchy.ts +546 -0
  78. package/src/index.ts +23 -0
  79. package/src/ip_bits.ts +148 -0
  80. package/src/ip_version.ts +6 -0
  81. package/src/ipaddress.ts +1331 -0
  82. package/src/ipv4.ts +190 -0
  83. package/src/ipv6.ts +202 -0
  84. package/src/ipv6_loopback.ts +55 -0
  85. package/src/ipv6_mapped.ts +163 -0
  86. package/src/ipv6_unspec.ts +59 -0
  87. package/src/prefix.ts +170 -0
  88. package/src/prefix128.ts +38 -0
  89. package/src/prefix32.ts +28 -0
  90. package/src/rle.ts +85 -0
  91. package/test/test_crunchy.ts +614 -0
  92. package/test/test_ipaddress.ts +208 -0
  93. package/test/test_ipv4.ts +596 -0
  94. package/test/test_ipv6.ts +325 -0
  95. package/test/test_ipv6_loopback.ts +52 -0
  96. package/test/test_ipv6_mapped.ts +101 -0
  97. package/test/test_ipv6_unspec.ts +51 -0
  98. package/test/test_prefix128.ts +39 -0
  99. package/test/test_prefix32.ts +130 -0
  100. package/test/test_rle.ts +144 -0
  101. package/LICENSE +0 -20
  102. package/README.md +0 -956
  103. package/index.d.ts +0 -12
  104. package/ipaddress.js +0 -2854
  105. package/ipaddress.js.map +0 -1
  106. package/ipv4.d.ts +0 -14
  107. package/ipv6.d.ts +0 -12
  108. package/ipv6_loopback.d.ts +0 -5
  109. package/ipv6_mapped.d.ts +0 -5
  110. package/ipv6_unspec.d.ts +0 -5
  111. package/prefix128.d.ts +0 -6
  112. package/prefix32.d.ts +0 -6
  113. package/rle.d.ts +0 -14
@@ -0,0 +1,1331 @@
1
+ import Prefix from "./prefix.js";
2
+ import IpBits from "./ip_bits.js";
3
+ import IpVersion from "./ip_version.js";
4
+ import Ipv4 from "./ipv4.js";
5
+ import Ipv6 from "./ipv6.js";
6
+ import Ipv6Mapped from "./ipv6_mapped.js";
7
+
8
+ import Crunchy from "./crunchy.js";
9
+
10
+ export type Is = (source: IPAddress) => boolean;
11
+
12
+ export type ToIpv4 = (source: IPAddress) => IPAddress;
13
+
14
+ export type EachFn = (source: IPAddress) => void;
15
+
16
+ export class ResultCrunchyParts {
17
+ public crunchy: Crunchy;
18
+ public parts: number;
19
+
20
+ constructor(crunchy: Crunchy, parts: number) {
21
+ this.crunchy = crunchy;
22
+ this.parts = parts;
23
+ // console.log("ResultCrunchyParts:", this);
24
+ }
25
+ }
26
+
27
+ export interface IPAddressParams {
28
+ ip_bits: IpBits;
29
+ host_address: Crunchy;
30
+ prefix: Prefix;
31
+ mapped?: IPAddress;
32
+ vt_is_private?: Is;
33
+ vt_is_loopback?: Is;
34
+ vt_to_ipv6?: ToIpv4;
35
+ }
36
+
37
+ export class IPAddress {
38
+ ip_bits: IpBits;
39
+ host_address: Crunchy;
40
+ prefix: Prefix;
41
+ mapped: IPAddress;
42
+ vt_is_private: Is;
43
+ vt_is_loopback: Is;
44
+ vt_to_ipv6: ToIpv4;
45
+
46
+ constructor(obj: IPAddressParams) {
47
+ this.ip_bits = obj.ip_bits;
48
+ this.host_address = obj.host_address;
49
+ this.prefix = obj.prefix;
50
+ this.mapped = obj.mapped;
51
+ this.vt_is_private = obj.vt_is_private;
52
+ this.vt_is_loopback = obj.vt_is_loopback;
53
+ this.vt_to_ipv6 = obj.vt_to_ipv6;
54
+ }
55
+
56
+ public clone(): IPAddress {
57
+ let mapped: IPAddress = null;
58
+ if (this.mapped) {
59
+ mapped = this.mapped.clone();
60
+ }
61
+ return new IPAddress({
62
+ ip_bits: this.ip_bits.clone(),
63
+ host_address: this.host_address.clone(),
64
+ prefix: this.prefix.clone(),
65
+ mapped,
66
+ vt_is_private: this.vt_is_private,
67
+ vt_is_loopback: this.vt_is_loopback,
68
+ vt_to_ipv6: this.vt_to_ipv6,
69
+ });
70
+ }
71
+
72
+ public lt(oth: IPAddress): boolean {
73
+ return this.cmp(oth) < 0;
74
+ }
75
+
76
+ public lte(oth: IPAddress): boolean {
77
+ return this.cmp(oth) <= 0;
78
+ }
79
+
80
+ public gt(oth: IPAddress): boolean {
81
+ return this.cmp(oth) > 0;
82
+ }
83
+
84
+ public gte(oth: IPAddress): boolean {
85
+ return this.cmp(oth) >= 0;
86
+ }
87
+
88
+ public cmp(oth: IPAddress): number {
89
+ if (this.ip_bits.version != oth.ip_bits.version) {
90
+ if (this.ip_bits.version == IpVersion.V6) {
91
+ return 1;
92
+ }
93
+ return -1;
94
+ }
95
+ // let adr_diff = this.host_address - oth.host_address;
96
+ const hostCmp = this.host_address.compare(oth.host_address);
97
+ if (hostCmp != 0) {
98
+ return hostCmp;
99
+ }
100
+ return this.prefix.cmp(oth.prefix);
101
+ }
102
+
103
+ public eq(other: IPAddress): boolean {
104
+ // if (!!this.mapped != !!this.mapped) {
105
+ // return false;
106
+ // }
107
+ // if (this.mapped) {
108
+ // if (!this.mapped.eq(other.mapped)) {
109
+ // return false;
110
+ // }
111
+ // }
112
+ // console.log("************", this);
113
+ return (
114
+ this.ip_bits.version == other.ip_bits.version && this.prefix.eq(other.prefix) && this.host_address.eq(other.host_address)
115
+ );
116
+ }
117
+
118
+ public ne(other: IPAddress): boolean {
119
+ return !this.eq(other);
120
+ }
121
+
122
+ // Parse the argument string to create a new
123
+ // IPv4, IPv6 or Mapped IP object
124
+ //
125
+ // ip = IPAddress.parse "172.16.10.1/24"
126
+ // ip6 = IPAddress.parse "2001:db8.8:800:200c:417a/64"
127
+ // ip_mapped = IPAddress.parse ".ffff:172.16.10.1/128"
128
+ //
129
+ // All the object created will be instances of the
130
+ // correct class:
131
+ //
132
+ // ip.class
133
+ // //=> IPAddress.IPv4
134
+ // ip6.class
135
+ // //=> IPAddress.IPv6
136
+ // ip_mapped.class
137
+ // //=> IPAddress.IPv6.Mapped
138
+ //
139
+ public static parse(str: string): IPAddress {
140
+ const re_mapped = new RegExp(":.+\\.");
141
+ // const re_ipv4 = new RegExp('\\.')
142
+ // const re_ipv6 = new RegExp(':')
143
+ if (re_mapped.test(str)) {
144
+ // console.log("mapped:", str);
145
+ return Ipv6Mapped.create(str);
146
+ } else {
147
+ if (str.includes(".")) {
148
+ // console.log("ipv4:", str);
149
+ return Ipv4.create(str);
150
+ } else if (str.includes(":")) {
151
+ // console.log("ipv6:", str);
152
+ return Ipv6.create(str);
153
+ }
154
+ }
155
+ return null;
156
+ }
157
+
158
+ public static split_at_slash(str: string): [string, string] {
159
+ const slash: string[] = str.trim().split("/");
160
+ let addr = "";
161
+ if (slash[0]) {
162
+ addr += slash[0].trim();
163
+ }
164
+ if (slash[1]) {
165
+ return [addr, slash[1].trim()];
166
+ } else {
167
+ return [addr, null];
168
+ }
169
+ }
170
+
171
+ public from(addr: Crunchy, prefix: Prefix): IPAddress {
172
+ let mapped: IPAddress = null;
173
+ if (this.mapped) {
174
+ mapped = this.mapped.clone();
175
+ }
176
+ return new IPAddress({
177
+ ip_bits: this.ip_bits,
178
+ host_address: addr.clone(),
179
+ prefix: prefix.clone(),
180
+ mapped,
181
+ vt_is_private: this.vt_is_private,
182
+ vt_is_loopback: this.vt_is_loopback,
183
+ vt_to_ipv6: this.vt_to_ipv6,
184
+ });
185
+ }
186
+
187
+ // True if the object is an IPv4 address
188
+ //
189
+ // ip = IPAddress("192.168.10.100/24")
190
+ //
191
+ // ip.ipv4?
192
+ // //-> true
193
+ //
194
+ public is_ipv4(): boolean {
195
+ return this.ip_bits.version == IpVersion.V4;
196
+ }
197
+
198
+ // True if the object is an IPv6 address
199
+ //
200
+ // ip = IPAddress("192.168.10.100/24")
201
+ //
202
+ // ip.ipv6?
203
+ // //-> false
204
+ //
205
+ public is_ipv6(): boolean {
206
+ return this.ip_bits.version == IpVersion.V6;
207
+ }
208
+
209
+ // Checks if the given string is a valid IP address,
210
+ // either IPv4 or IPv6
211
+ //
212
+ // Example:
213
+ //
214
+ // IPAddress.valid? "2002.1"
215
+ // //=> true
216
+ //
217
+ // IPAddress.valid? "10.0.0.256"
218
+ // //=> false
219
+ //
220
+ public static is_valid(addr: string): boolean {
221
+ return IPAddress.is_valid_ipv4(addr) || IPAddress.is_valid_ipv6(addr);
222
+ }
223
+
224
+ public static parse_dec_str(str: string): number {
225
+ const re_digit = new RegExp("^\\d+$");
226
+ if (!re_digit.test(str)) {
227
+ // console.log("parse_dec_str:-1:", str);
228
+ return null;
229
+ }
230
+ const part = parseInt(str, 10);
231
+ if (isNaN(part)) {
232
+ // console.log("parse_dec_str:-2:", str, part);
233
+ return null;
234
+ }
235
+ // console.log("parse_dec_str:-3:", str, part);
236
+ return part;
237
+ }
238
+
239
+ public static parse_hex_str(str: string): number {
240
+ const re_digit = new RegExp("^[0-9a-fA-F]+$");
241
+ if (!re_digit.test(str)) {
242
+ return null;
243
+ }
244
+ const part = parseInt(str, 16);
245
+ if (isNaN(part)) {
246
+ return null;
247
+ }
248
+ return part;
249
+ }
250
+
251
+ // Checks if the given string is a valid IPv4 address
252
+ //
253
+ // Example:
254
+ //
255
+ // IPAddress.valid_ipv4? "2002.1"
256
+ // //=> false
257
+ //
258
+ // IPAddress.valid_ipv4? "172.16.10.1"
259
+ // //=> true
260
+ //
261
+ public static parse_ipv4_part(i: string): number {
262
+ const part = IPAddress.parse_dec_str(i);
263
+ // console.log("i=", i, part);
264
+ if (part === null || part >= 256) {
265
+ return null;
266
+ }
267
+ return part;
268
+ }
269
+
270
+ public static split_to_u32(addr: string): Crunchy {
271
+ let ip = Crunchy.zero();
272
+ let shift = 24;
273
+ let split_addr = addr.split(".");
274
+ if (split_addr.length > 4) {
275
+ return null;
276
+ }
277
+ const split_addr_len = split_addr.length;
278
+ if (split_addr_len >= 1 && split_addr_len < 4) {
279
+ const part = IPAddress.parse_ipv4_part(split_addr[split_addr_len - 1]);
280
+ if (part === null) {
281
+ return null;
282
+ }
283
+ ip = Crunchy.from_number(part);
284
+ split_addr = split_addr.slice(0, split_addr_len - 1);
285
+ }
286
+ for (const i of split_addr) {
287
+ const part = IPAddress.parse_ipv4_part(i);
288
+ // console.log("u32-", addr, i, part);
289
+ if (part === null) {
290
+ return null;
291
+ }
292
+ // println!("{}-{}", part_num, shift);
293
+ ip = ip.add(Crunchy.from_number(part).shl(shift));
294
+ shift -= 8;
295
+ }
296
+ return ip;
297
+ }
298
+
299
+ public static is_valid_ipv4(addr: string): boolean {
300
+ return IPAddress.split_to_u32(addr) !== null;
301
+ }
302
+
303
+ // Checks if the given string is a valid IPv6 address
304
+ //
305
+ // Example:
306
+ //
307
+ // IPAddress.valid_ipv6? "2002.1"
308
+ // //=> true
309
+ //
310
+ // IPAddress.valid_ipv6? "2002.DEAD.BEEF"
311
+ // //=> false
312
+ //
313
+ public static split_on_colon(addr: string): ResultCrunchyParts {
314
+ const parts = addr.trim().split(":");
315
+ let ip = Crunchy.zero();
316
+ if (parts.length == 1 && parts[0].length == 0) {
317
+ return new ResultCrunchyParts(ip, 0);
318
+ }
319
+ const parts_len = parts.length;
320
+ let shift = (parts_len - 1) * 16;
321
+ for (const i of parts) {
322
+ // println!("{}={}", addr, i);
323
+ const part = IPAddress.parse_hex_str(i);
324
+ if (part === null || part >= 65536) {
325
+ return null;
326
+ }
327
+ ip = ip.add(Crunchy.from_number(part).shl(shift));
328
+ shift -= 16;
329
+ }
330
+ return new ResultCrunchyParts(ip, parts_len);
331
+ }
332
+
333
+ public static split_to_num(addr: string): ResultCrunchyParts {
334
+ // let ip = 0;
335
+ const pre_post = addr.trim().split("::");
336
+ if (pre_post.length > 2) {
337
+ return null;
338
+ }
339
+ if (pre_post.length == 2) {
340
+ // println!("{}=.={}", pre_post[0], pre_post[1]);
341
+ const pre = IPAddress.split_on_colon(pre_post[0]);
342
+ if (!pre) {
343
+ return pre;
344
+ }
345
+ const post = IPAddress.split_on_colon(pre_post[1]);
346
+ if (!post) {
347
+ return post;
348
+ }
349
+ // println!("pre:{} post:{}", pre_parts, post_parts);
350
+ return new ResultCrunchyParts(pre.crunchy.shl(128 - pre.parts * 16).add(post.crunchy), 128 / 16);
351
+ }
352
+ // println!("split_to_num:no double:{}", addr);
353
+ const ret = IPAddress.split_on_colon(addr);
354
+ if (ret == null || ret.parts != 128 / 16) {
355
+ return null;
356
+ }
357
+ return ret;
358
+ }
359
+
360
+ public static is_valid_ipv6(addr: string): boolean {
361
+ return IPAddress.split_to_num(addr) != null;
362
+ }
363
+
364
+ // private helper for summarize
365
+ // assumes that networks is output from reduce_networks
366
+ // means it should be sorted lowers first and uniq
367
+ //
368
+
369
+ public static pos_to_idx(pos: number, len: number): number {
370
+ const ilen = len;
371
+ // let ret = pos % ilen;
372
+ const rem = ((pos % ilen) + ilen) % ilen;
373
+ // println!("pos_to_idx:{}:{}=>{}:{}", pos, len, ret, rem);
374
+ return rem;
375
+ }
376
+
377
+ public static aggregate(networks: IPAddress[]): IPAddress[] {
378
+ if (networks.length == 0) {
379
+ return [];
380
+ }
381
+ if (networks.length == 1) {
382
+ // console.log("aggregate:", networks[0], networks[0].network());
383
+ return [networks[0].network()];
384
+ }
385
+ let stack = networks.map((i) => i.network()).sort((a, b) => a.cmp(b));
386
+ // console.log(IPAddress.to_string_vec(stack));
387
+ // for i in 0..networks.length {
388
+ // println!("{}==={}", &networks[i].to_string_uncompressed(),
389
+ // &stack[i].to_string_uncompressed());
390
+ // }
391
+ // eslint-disable-next-line no-constant-condition
392
+ for (let pos = 0; true; ) {
393
+ if (pos < 0) {
394
+ pos = 0;
395
+ }
396
+ const stack_len = stack.length; // borrow checker
397
+ // println!("loop:{}:{}", pos, stack_len);
398
+ // if stack_len == 1 {
399
+ // println!("exit 1");
400
+ // break;
401
+ // }
402
+ if (pos >= stack_len) {
403
+ // println!("exit first:{}:{}", stack_len, pos);
404
+ break;
405
+ }
406
+ const first = IPAddress.pos_to_idx(pos, stack_len);
407
+ pos = pos + 1;
408
+ if (pos >= stack_len) {
409
+ // println!("exit second:{}:{}", stack_len, pos);
410
+ break;
411
+ }
412
+ const second = IPAddress.pos_to_idx(pos, stack_len);
413
+ pos = pos + 1;
414
+ // let firstUnwrap = first;
415
+ if (stack[first].includes(stack[second])) {
416
+ pos = pos - 2;
417
+ // println!("remove:1:{}:{}:{}=>{}", first, second, stack_len, pos + 1);
418
+ const pidx = IPAddress.pos_to_idx(pos + 1, stack_len);
419
+ stack = stack.slice(0, pidx).concat(stack.slice(pidx + 1));
420
+ } else {
421
+ stack[first].prefix = stack[first].prefix.sub(1);
422
+ // println!("complex:{}:{}:{}:{}:P1:{}:P2:{}", pos, stack_len,
423
+ // first, second,
424
+ // stack[first].to_string(), stack[second].to_string());
425
+ if (stack[first].prefix.num + 1 == stack[second].prefix.num && stack[first].includes(stack[second])) {
426
+ pos = pos - 2;
427
+ const idx = IPAddress.pos_to_idx(pos, stack_len);
428
+ stack[idx] = stack[first].clone(); // kaputt
429
+ const pidx = IPAddress.pos_to_idx(pos + 1, stack_len);
430
+ stack = stack.slice(0, pidx).concat(stack.slice(pidx + 1));
431
+ // println!("remove-2:{}:{}", pos + 1, stack_len);
432
+ pos = pos - 1; // backtrack
433
+ } else {
434
+ stack[first].prefix = stack[first].prefix.add(1); // reset prefix
435
+ // println!("easy:{}:{}=>{}", pos, stack_len, stack[first].to_string());
436
+ pos = pos - 1; // do it with second as first
437
+ }
438
+ }
439
+ }
440
+ // println!("agg={}:{}", pos, stack.length);
441
+ return stack.slice(0, stack.length);
442
+ }
443
+
444
+ public parts(): number[] {
445
+ return this.ip_bits.parts(this.host_address);
446
+ }
447
+
448
+ public parts_hex_str(): string[] {
449
+ const ret: string[] = [];
450
+ const leading = 1 << this.ip_bits.part_bits;
451
+ for (const i of this.parts()) {
452
+ ret.push((leading + i).toString(16).slice(1));
453
+ }
454
+ return ret;
455
+ }
456
+
457
+ // Returns the IP address in in-addr.arpa format
458
+ // for DNS Domain definition entries like SOA Records
459
+ //
460
+ // ip = IPAddress("172.17.100.50/15")
461
+ //
462
+ // ip.dns_rev_domains
463
+ // // => ["16.172.in-addr.arpa","17.172.in-addr.arpa"]
464
+ //
465
+ public dns_rev_domains(): string[] {
466
+ const ret: string[] = [];
467
+ for (const net of this.dns_networks()) {
468
+ // console.log("dns_rev_domains:", this.to_string(), net.to_string());
469
+ ret.push(net.dns_reverse());
470
+ }
471
+ return ret;
472
+ }
473
+
474
+ public dns_reverse(): string {
475
+ let ret = "";
476
+ let dot = "";
477
+ const dns_parts = this.dns_parts();
478
+ for (
479
+ let i = ~~((this.prefix.host_prefix() + (this.ip_bits.dns_bits - 1)) / this.ip_bits.dns_bits);
480
+ i < this.dns_parts().length;
481
+ ++i
482
+ ) {
483
+ // console.log("dns_r", i);
484
+ ret += dot;
485
+ ret += this.ip_bits.dns_part_format(dns_parts[i]);
486
+ dot = ".";
487
+ }
488
+ ret += dot;
489
+ ret += this.ip_bits.rev_domain;
490
+ return ret;
491
+ }
492
+
493
+ public dns_parts(): number[] {
494
+ const ret: number[] = [];
495
+ let num = this.host_address.clone();
496
+ const mask = Crunchy.one().shl(this.ip_bits.dns_bits);
497
+ for (let _ = 0; _ < this.ip_bits.bits / this.ip_bits.dns_bits; _++) {
498
+ const part = +num.clone().mod(mask).toString();
499
+ num = num.shr(this.ip_bits.dns_bits);
500
+ ret.push(part);
501
+ }
502
+ return ret;
503
+ }
504
+
505
+ public dns_networks(): IPAddress[] {
506
+ // +this.ip_bits.dns_bits-1
507
+ const next_bit_mask = this.ip_bits.bits - ~~(this.prefix.host_prefix() / this.ip_bits.dns_bits) * this.ip_bits.dns_bits;
508
+ // console.log("dns_networks-1", this.to_string(), this.prefix.host_prefix();j
509
+ // this.ip_bits.dns_bits, next_bit_mask);
510
+ if (next_bit_mask <= 0) {
511
+ return [this.network()];
512
+ }
513
+ // println!("dns_networks:{}:{}", this.to_string(), next_bit_mask);
514
+ // dns_bits
515
+ const step_bit_net = Crunchy.one().shl(this.ip_bits.bits - next_bit_mask);
516
+ if (step_bit_net.eq(Crunchy.zero())) {
517
+ // console.log("dns_networks-2", this.to_string());
518
+ return [this.network()];
519
+ }
520
+ const ret: IPAddress[] = [];
521
+ let step = this.network().host_address;
522
+ const prefix = this.prefix.from(next_bit_mask);
523
+ while (step.lte(this.broadcast().host_address)) {
524
+ // console.log("dns_networks-3", this.to_string(), step.toString(), next_bit_mask, step_bit_net.toString());
525
+ ret.push(this.from(step, prefix));
526
+ step = step.add(step_bit_net);
527
+ }
528
+ return ret;
529
+ }
530
+
531
+ // Summarization (or aggregation) is the process when two or more
532
+ // networks are taken together to check if a supernet, including all
533
+ // and only these networks, exists. If it exists then this supernet
534
+ // is called the summarized (or aggregated) network.
535
+ //
536
+ // It is very important to understand that summarization can only
537
+ // occur if there are no holes in the aggregated network, or, in other
538
+ // words, if the given networks fill completely the address space
539
+ // of the supernet. So the two rules are:
540
+ //
541
+ // 1) The aggregate network must contain +all+ the IP addresses of the
542
+ // original networks;
543
+ // 2) The aggregate network must contain +only+ the IP addresses of the
544
+ // original networks;
545
+ //
546
+ // A few examples will help clarify the above. Let's consider for
547
+ // instance the following two networks:
548
+ //
549
+ // ip1 = IPAddress("172.16.10.0/24")
550
+ // ip2 = IPAddress("172.16.11.0/24")
551
+ //
552
+ // These two networks can be expressed using only one IP address
553
+ // network if we change the prefix. Let Ruby do the work:
554
+ //
555
+ // IPAddress.IPv4.summarize(ip1,ip2).to_s
556
+ // //=> "172.16.10.0/23"
557
+ //
558
+ // We note how the network "172.16.10.0/23" includes all the addresses
559
+ // specified in the above networks, and (more important) includes
560
+ // ONLY those addresses.
561
+ //
562
+ // If we summarized +ip1+ and +ip2+ with the following network:
563
+ //
564
+ // "172.16.0.0/16"
565
+ //
566
+ // we would have satisfied rule //1 above, but not rule //2. So "172.16.0.0/16"
567
+ // is not an aggregate network for +ip1+ and +ip2+.
568
+ //
569
+ // If it's not possible to compute a single aggregated network for all the
570
+ // original networks, the method returns an array with all the aggregate
571
+ // networks found. For example, the following four networks can be
572
+ // aggregated in a single /22:
573
+ //
574
+ // ip1 = IPAddress("10.0.0.1/24")
575
+ // ip2 = IPAddress("10.0.1.1/24")
576
+ // ip3 = IPAddress("10.0.2.1/24")
577
+ // ip4 = IPAddress("10.0.3.1/24")
578
+ //
579
+ // IPAddress.IPv4.summarize(ip1,ip2,ip3,ip4).to_string
580
+ // //=> "10.0.0.0/22",
581
+ //
582
+ // But the following networks can't be summarized in a single network:
583
+ //
584
+ // ip1 = IPAddress("10.0.1.1/24")
585
+ // ip2 = IPAddress("10.0.2.1/24")
586
+ // ip3 = IPAddress("10.0.3.1/24")
587
+ // ip4 = IPAddress("10.0.4.1/24")
588
+ //
589
+ // IPAddress.IPv4.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
590
+ // //=> ["10.0.1.0/24","10.0.2.0/23","10.0.4.0/24"]
591
+ //
592
+ //
593
+ // Summarization (or aggregation) is the process when two or more
594
+ // networks are taken together to check if a supernet, including all
595
+ // and only these networks, exists. If it exists then this supernet
596
+ // is called the summarized (or aggregated) network.
597
+ //
598
+ // It is very important to understand that summarization can only
599
+ // occur if there are no holes in the aggregated network, or, in other
600
+ // words, if the given networks fill completely the address space
601
+ // of the supernet. So the two rules are:
602
+ //
603
+ // 1) The aggregate network must contain +all+ the IP addresses of the
604
+ // original networks;
605
+ // 2) The aggregate network must contain +only+ the IP addresses of the
606
+ // original networks;
607
+ //
608
+ // A few examples will help clarify the above. Let's consider for
609
+ // instance the following two networks:
610
+ //
611
+ // ip1 = IPAddress("2000:0.4/32")
612
+ // ip2 = IPAddress("2000:1.6/32")
613
+ //
614
+ // These two networks can be expressed using only one IP address
615
+ // network if we change the prefix. Let Ruby do the work:
616
+ //
617
+ // IPAddress.IPv6.summarize(ip1,ip2).to_s
618
+ // // => "2000:0./31"
619
+ //
620
+ // We note how the network "2000:0./31" includes all the addresses
621
+ // specified in the above networks, and (more important) includes
622
+ // ONLY those addresses.
623
+ //
624
+ // If we summarized +ip1+ and +ip2+ with the following network:
625
+ //
626
+ // "2000./16"
627
+ //
628
+ // we would have satisfied rule // 1 above, but not rule // 2. So "2000./16"
629
+ // is not an aggregate network for +ip1+ and +ip2+.
630
+ //
631
+ // If it's not possible to compute a single aggregated network for all the
632
+ // original networks, the method returns an array with all the aggregate
633
+ // networks found. For example, the following four networks can be
634
+ // aggregated in a single /22:
635
+ //
636
+ // ip1 = IPAddress("2000:0./32")
637
+ // ip2 = IPAddress("2000:1./32")
638
+ // ip3 = IPAddress("2000:2./32")
639
+ // ip4 = IPAddress("2000:3./32")
640
+ //
641
+ // IPAddress.IPv6.summarize(ip1,ip2,ip3,ip4).to_string
642
+ // // => ""2000:3./30",
643
+ //
644
+ // But the following networks can't be summarized in a single network:
645
+ //
646
+ // ip1 = IPAddress("2000:1./32")
647
+ // ip2 = IPAddress("2000:2./32")
648
+ // ip3 = IPAddress("2000:3./32")
649
+ // ip4 = IPAddress("2000:4./32")
650
+ //
651
+ // IPAddress.IPv4.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
652
+ // // => ["2000:1./32","2000:2./31","2000:4./32"]
653
+ //
654
+ public static summarize(networks: IPAddress[]): IPAddress[] {
655
+ return IPAddress.aggregate(networks);
656
+ }
657
+
658
+ public static summarize_str(netstr: string[]): IPAddress[] {
659
+ const vec = IPAddress.to_ipaddress_vec(netstr);
660
+ // console.log(netstr, vec);
661
+ if (!vec) {
662
+ return vec;
663
+ }
664
+ return IPAddress.aggregate(vec);
665
+ }
666
+
667
+ public ip_same_kind(oth: IPAddress): boolean {
668
+ return this.ip_bits.version == oth.ip_bits.version;
669
+ }
670
+
671
+ // Returns true if the address is an unspecified address
672
+ //
673
+ // See IPAddress.IPv6.Unspecified for more information
674
+ //
675
+ public is_unspecified(): boolean {
676
+ return this.host_address.eq(Crunchy.zero());
677
+ }
678
+
679
+ // Returns true if the address is a loopback address
680
+ //
681
+ // See IPAddress.IPv6.Loopback for more information
682
+ //
683
+ public is_loopback(): boolean {
684
+ return this.vt_is_loopback(this);
685
+ }
686
+
687
+ // Returns true if the address is a mapped address
688
+ //
689
+ // See IPAddress.IPv6.Mapped for more information
690
+ //
691
+ public is_mapped(): boolean {
692
+ const ret = this.mapped !== null && this.host_address.shr(32).eq(Crunchy.one().shl(16).sub(Crunchy.one()));
693
+ // console.log("+++++++++++", this.mapped, ret);
694
+ return ret;
695
+ }
696
+
697
+ // Returns the prefix portion of the IPv4 object
698
+ // as a IPAddress.Prefix32 object
699
+ //
700
+ // ip = IPAddress("172.16.100.4/22")
701
+ //
702
+ // ip.prefix
703
+ // // => 22
704
+ //
705
+ // ip.prefix.class
706
+ // // => IPAddress.Prefix32
707
+ //
708
+ // public prefix(): Prefix {
709
+ // return this.prefix;
710
+ // }
711
+
712
+ // Checks if the argument is a valid IPv4 netmask
713
+ // expressed in dotted decimal format.
714
+ //
715
+ // IPAddress.valid_ipv4_netmask? "255.255.0.0"
716
+ // //=> true
717
+ //
718
+ public static is_valid_netmask(addr: string): boolean {
719
+ return IPAddress.parse_netmask_to_prefix(addr) !== null;
720
+ }
721
+
722
+ public static netmask_to_prefix(nm: Crunchy, bits: number): number {
723
+ let prefix = 0;
724
+ let addr = nm.clone();
725
+ let in_host_part = true;
726
+ // let two = Crunchy.two();
727
+ for (let _ = 0; _ < bits; _++) {
728
+ const bit = addr.mds(2);
729
+ // console.log(">>>", bits, bit, addr, nm);
730
+ if (in_host_part && bit == 0) {
731
+ prefix = prefix + 1;
732
+ } else if (in_host_part && bit == 1) {
733
+ in_host_part = false;
734
+ } else if (!in_host_part && bit == 0) {
735
+ return null;
736
+ }
737
+ addr = addr.shr(1);
738
+ }
739
+ return bits - prefix;
740
+ }
741
+
742
+ public static parse_netmask_to_prefix(netmask: string): number {
743
+ // console.log("--1", netmask);
744
+ const is_number = IPAddress.parse_dec_str(netmask);
745
+ if (is_number !== null) {
746
+ // console.log("--2", netmask, is_number);
747
+ return is_number;
748
+ }
749
+ const my = IPAddress.parse(netmask);
750
+ // console.log("--3", netmask, my);
751
+ if (!my) {
752
+ // console.log("--4", netmask, my);
753
+ return null;
754
+ }
755
+ // console.log("--5", netmask, my);
756
+ const my_ip = my;
757
+ return IPAddress.netmask_to_prefix(my_ip.host_address, my_ip.ip_bits.bits);
758
+ }
759
+
760
+ // Set a new prefix number for the object
761
+ //
762
+ // This is useful if you want to change the prefix
763
+ // to an object created with IPv4.parse_u32 or
764
+ // if the object was created using the classful
765
+ // mask.
766
+ //
767
+ // ip = IPAddress("172.16.100.4")
768
+ //
769
+ // puts ip
770
+ // // => 172.16.100.4/16
771
+ //
772
+ // ip.prefix = 22
773
+ //
774
+ // puts ip
775
+ // // => 172.16.100.4/22
776
+ //
777
+ public change_prefix(num: number): IPAddress {
778
+ const prefix = this.prefix.from(num);
779
+ if (!prefix) {
780
+ return null;
781
+ }
782
+ return this.from(this.host_address, prefix);
783
+ }
784
+
785
+ public change_netmask(str: string): IPAddress {
786
+ const nm = IPAddress.parse_netmask_to_prefix(str);
787
+ if (!nm) {
788
+ return null;
789
+ }
790
+ return this.change_prefix(nm);
791
+ }
792
+
793
+ // Returns a string with the IP address in canonical
794
+ // form.
795
+ //
796
+ // ip = IPAddress("172.16.100.4/22")
797
+ //
798
+ // ip.to_string
799
+ // // => "172.16.100.4/22"
800
+ //
801
+ public to_string(): string {
802
+ let ret = "";
803
+ ret += this.to_s();
804
+ ret += "/";
805
+ ret += this.prefix.to_s();
806
+ return ret;
807
+ }
808
+
809
+ public to_s(): string {
810
+ return this.ip_bits.as_compressed_string(this.host_address);
811
+ }
812
+
813
+ public to_string_uncompressed(): string {
814
+ let ret = "";
815
+ ret += this.to_s_uncompressed();
816
+ ret += "/";
817
+ ret += this.prefix.to_s();
818
+ return ret;
819
+ }
820
+
821
+ public to_s_uncompressed(): string {
822
+ return this.ip_bits.as_uncompressed_string(this.host_address);
823
+ }
824
+
825
+ public to_s_mapped(): string {
826
+ if (this.is_mapped()) {
827
+ return `::ffff:${this.mapped.to_s()}`;
828
+ }
829
+ return this.to_s();
830
+ }
831
+
832
+ public to_string_mapped(): string {
833
+ if (this.is_mapped()) {
834
+ const mapped = this.mapped.clone();
835
+ return `${this.to_s_mapped()}/${mapped.prefix.num}`;
836
+ }
837
+ return this.to_string();
838
+ }
839
+
840
+ // Returns the address portion of an IP in binary format,
841
+ // as a string containing a sequence of 0 and 1
842
+ //
843
+ // ip = IPAddress("127.0.0.1")
844
+ //
845
+ // ip.bits
846
+ // // => "01111111000000000000000000000001"
847
+ //
848
+ public bits(): string {
849
+ const num = this.host_address.toString(2);
850
+ let ret = "";
851
+ for (let _ = num.length; _ < this.ip_bits.bits; _++) {
852
+ ret += "0";
853
+ }
854
+ ret += num;
855
+ return ret;
856
+ }
857
+
858
+ public to_hex(): string {
859
+ return this.host_address.toString(16);
860
+ }
861
+
862
+ public netmask(): IPAddress {
863
+ return this.from(this.prefix.netmask(), this.prefix);
864
+ }
865
+
866
+ // Returns the broadcast address for the given IP.
867
+ //
868
+ // ip = IPAddress("172.16.10.64/24")
869
+ //
870
+ // ip.broadcast.to_s
871
+ // // => "172.16.10.255"
872
+ //
873
+ public broadcast(): IPAddress {
874
+ return this.from(this.network().host_address.add(this.size().sub(Crunchy.one())), this.prefix);
875
+ // IPv4.parse_u32(this.broadcast_u32, this.prefix)
876
+ }
877
+
878
+ // Checks if the IP address is actually a network
879
+ //
880
+ // ip = IPAddress("172.16.10.64/24")
881
+ //
882
+ // ip.network?
883
+ // // => false
884
+ //
885
+ // ip = IPAddress("172.16.10.64/26")
886
+ //
887
+ // ip.network?
888
+ // // => true
889
+ //
890
+ public is_network(): boolean {
891
+ return this.prefix.num != this.ip_bits.bits && this.host_address.eq(this.network().host_address);
892
+ }
893
+
894
+ // Returns a new IPv4 object with the network number
895
+ // for the given IP.
896
+ //
897
+ // ip = IPAddress("172.16.10.64/24")
898
+ //
899
+ // ip.network.to_s
900
+ // // => "172.16.10.0"
901
+ //
902
+ public network(): IPAddress {
903
+ return this.from(IPAddress.to_network(this.host_address, this.prefix.host_prefix()), this.prefix);
904
+ }
905
+
906
+ public static to_network(adr: Crunchy, host_prefix: number): Crunchy {
907
+ return adr.shr(host_prefix).shl(host_prefix);
908
+ }
909
+
910
+ public sub(other: IPAddress): Crunchy {
911
+ if (this.host_address.gt(other.host_address)) {
912
+ return this.host_address.clone().sub(other.host_address);
913
+ }
914
+ return other.host_address.clone().sub(this.host_address);
915
+ }
916
+
917
+ public add(other: IPAddress): IPAddress[] {
918
+ return IPAddress.aggregate([this.clone(), other.clone()]);
919
+ }
920
+
921
+ public static to_s_vec(vec: IPAddress[]): string[] {
922
+ const ret: string[] = [];
923
+ for (const i of vec) {
924
+ ret.push(i.to_s());
925
+ }
926
+ return ret;
927
+ }
928
+
929
+ public static to_string_vec(vec: IPAddress[]): string[] {
930
+ const ret: string[] = [];
931
+ for (const i of vec) {
932
+ ret.push(i.to_string());
933
+ }
934
+ return ret;
935
+ }
936
+
937
+ public static to_ipaddress_vec(vec: string[]): IPAddress[] {
938
+ const ret: IPAddress[] = [];
939
+ for (const ipstr of vec) {
940
+ const ipa = IPAddress.parse(ipstr);
941
+ if (!ipa) {
942
+ return null;
943
+ }
944
+ ret.push(ipa);
945
+ }
946
+ return ret;
947
+ }
948
+
949
+ // Returns a new IPv4 object with the
950
+ // first host IP address in the range.
951
+ //
952
+ // Example: given the 192.168.100.0/24 network, the first
953
+ // host IP address is 192.168.100.1.
954
+ //
955
+ // ip = IPAddress("192.168.100.0/24")
956
+ //
957
+ // ip.first.to_s
958
+ // // => "192.168.100.1"
959
+ //
960
+ // The object IP doesn't need to be a network: the method
961
+ // automatically gets the network number from it
962
+ //
963
+ // ip = IPAddress("192.168.100.50/24")
964
+ //
965
+ // ip.first.to_s
966
+ // // => "192.168.100.1"
967
+ //
968
+ public first(): IPAddress {
969
+ return this.from(this.network().host_address.add(this.ip_bits.host_ofs), this.prefix);
970
+ }
971
+
972
+ // Like its sibling method IPv4// first, this method
973
+ // returns a new IPv4 object with the
974
+ // last host IP address in the range.
975
+ //
976
+ // Example: given the 192.168.100.0/24 network, the last
977
+ // host IP address is 192.168.100.254
978
+ //
979
+ // ip = IPAddress("192.168.100.0/24")
980
+ //
981
+ // ip.last.to_s
982
+ // // => "192.168.100.254"
983
+ //
984
+ // The object IP doesn't need to be a network: the method
985
+ // automatically gets the network number from it
986
+ //
987
+ // ip = IPAddress("192.168.100.50/24")
988
+ //
989
+ // ip.last.to_s
990
+ // // => "192.168.100.254"
991
+ //
992
+ public last(): IPAddress {
993
+ return this.from(this.broadcast().host_address.sub(this.ip_bits.host_ofs), this.prefix);
994
+ }
995
+
996
+ // Iterates over all the hosts IP addresses for the given
997
+ // network (or IP address).
998
+ //
999
+ // ip = IPAddress("10.0.0.1/29")
1000
+ //
1001
+ // ip.each_host do |i|
1002
+ // p i.to_s
1003
+ // end
1004
+ // // => "10.0.0.1"
1005
+ // // => "10.0.0.2"
1006
+ // // => "10.0.0.3"
1007
+ // // => "10.0.0.4"
1008
+ // // => "10.0.0.5"
1009
+ // // => "10.0.0.6"
1010
+ //
1011
+ public each_host(func: EachFn) {
1012
+ let i = this.first().host_address;
1013
+ while (i.lte(this.last().host_address)) {
1014
+ func(this.from(i, this.prefix));
1015
+ i = i.add(Crunchy.one());
1016
+ }
1017
+ }
1018
+
1019
+ public inc(): IPAddress {
1020
+ const ret = this.clone();
1021
+ ret.host_address = ret.host_address.add(Crunchy.one());
1022
+ if (ret.lte(this.last())) {
1023
+ return ret;
1024
+ }
1025
+ return null;
1026
+ }
1027
+
1028
+ public dec(): IPAddress {
1029
+ const ret = this.clone();
1030
+ ret.host_address = ret.host_address.sub(Crunchy.one());
1031
+ if (ret.gte(this.first())) {
1032
+ return ret;
1033
+ }
1034
+ return null;
1035
+ }
1036
+
1037
+ // Iterates over all the IP addresses for the given
1038
+ // network (or IP address).
1039
+ //
1040
+ // The object yielded is a new IPv4 object created
1041
+ // from the iteration.
1042
+ //
1043
+ // ip = IPAddress("10.0.0.1/29")
1044
+ //
1045
+ // ip.each do |i|
1046
+ // p i.address
1047
+ // end
1048
+ // // => "10.0.0.0"
1049
+ // // => "10.0.0.1"
1050
+ // // => "10.0.0.2"
1051
+ // // => "10.0.0.3"
1052
+ // // => "10.0.0.4"
1053
+ // // => "10.0.0.5"
1054
+ // // => "10.0.0.6"
1055
+ // // => "10.0.0.7"
1056
+ //
1057
+ public each(func: EachFn) {
1058
+ let i = this.network().host_address;
1059
+ while (i.lte(this.broadcast().host_address)) {
1060
+ func(this.from(i, this.prefix));
1061
+ i = i.add(Crunchy.one());
1062
+ }
1063
+ }
1064
+
1065
+ // Spaceship operator to compare IPv4 objects
1066
+ //
1067
+ // Comparing IPv4 addresses is useful to ordinate
1068
+ // them into lists that match our intuitive
1069
+ // perception of ordered IP addresses.
1070
+ //
1071
+ // The first comparison criteria is the u32 value.
1072
+ // For example, 10.100.100.1 will be considered
1073
+ // to be less than 172.16.0.1, because, in a ordered list,
1074
+ // we expect 10.100.100.1 to come before 172.16.0.1.
1075
+ //
1076
+ // The second criteria, in case two IPv4 objects
1077
+ // have identical addresses, is the prefix. An higher
1078
+ // prefix will be considered greater than a lower
1079
+ // prefix. This is because we expect to see
1080
+ // 10.100.100.0/24 come before 10.100.100.0/25.
1081
+ //
1082
+ // Example:
1083
+ //
1084
+ // ip1 = IPAddress "10.100.100.1/8"
1085
+ // ip2 = IPAddress "172.16.0.1/16"
1086
+ // ip3 = IPAddress "10.100.100.1/16"
1087
+ //
1088
+ // ip1 < ip2
1089
+ // // => true
1090
+ // ip1 > ip3
1091
+ // // => false
1092
+ //
1093
+ // [ip1,ip2,ip3].sort.map{|i| i.to_string}
1094
+ // // => ["10.100.100.1/8","10.100.100.1/16","172.16.0.1/16"]
1095
+ //
1096
+
1097
+ // Returns the number of IP addresses included
1098
+ // in the network. It also counts the network
1099
+ // address and the broadcast address.
1100
+ //
1101
+ // ip = IPAddress("10.0.0.1/29")
1102
+ //
1103
+ // ip.size
1104
+ // // => 8
1105
+ //
1106
+ public size(): Crunchy {
1107
+ return Crunchy.one().shl(this.prefix.host_prefix());
1108
+ }
1109
+
1110
+ public is_same_kind(oth: IPAddress): boolean {
1111
+ return this.is_ipv4() == oth.is_ipv4() && this.is_ipv6() == oth.is_ipv6();
1112
+ }
1113
+
1114
+ // Checks whether a subnet includes the given IP address.
1115
+ //
1116
+ // Accepts an IPAddress.IPv4 object.
1117
+ //
1118
+ // ip = IPAddress("192.168.10.100/24")
1119
+ //
1120
+ // addr = IPAddress("192.168.10.102/24")
1121
+ //
1122
+ // ip.include? addr
1123
+ // // => true
1124
+ //
1125
+ // ip.include? IPAddress("172.16.0.48/16")
1126
+ // // => false
1127
+ //
1128
+ public includes(oth: IPAddress): boolean {
1129
+ const ret =
1130
+ this.is_same_kind(oth) &&
1131
+ this.prefix.num <= oth.prefix.num &&
1132
+ this.network().host_address.eq(IPAddress.to_network(oth.host_address, this.prefix.host_prefix()));
1133
+ // println!("includes:{}=={}=>{}", this.to_string(), oth.to_string(), ret);
1134
+ return ret;
1135
+ }
1136
+
1137
+ // Checks whether a subnet includes all the
1138
+ // given IPv4 objects.
1139
+ //
1140
+ // ip = IPAddress("192.168.10.100/24")
1141
+ //
1142
+ // addr1 = IPAddress("192.168.10.102/24")
1143
+ // addr2 = IPAddress("192.168.10.103/24")
1144
+ //
1145
+ // ip.include_all?(addr1,addr2)
1146
+ // // => true
1147
+ //
1148
+ public includes_all(oths: IPAddress[]): boolean {
1149
+ for (const oth of oths) {
1150
+ if (!this.includes(oth)) {
1151
+ return false;
1152
+ }
1153
+ }
1154
+ return true;
1155
+ }
1156
+
1157
+ // Checks if an IPv4 address objects belongs
1158
+ // to a private network RFC1918
1159
+ //
1160
+ // Example:
1161
+ //
1162
+ // ip = IPAddress "10.1.1.1/24"
1163
+ // ip.private?
1164
+ // // => true
1165
+ //
1166
+ public is_private(): boolean {
1167
+ return this.vt_is_private(this);
1168
+ }
1169
+
1170
+ // Splits a network into different subnets
1171
+ //
1172
+ // If the IP Address is a network, it can be divided into
1173
+ // multiple networks. If +self+ is not a network, this
1174
+ // method will calculate the network from the IP and then
1175
+ // subnet it.
1176
+ //
1177
+ // If +subnets+ is an power of two number, the resulting
1178
+ // networks will be divided evenly from the supernet.
1179
+ //
1180
+ // network = IPAddress("172.16.10.0/24")
1181
+ //
1182
+ // network / 4 // implies map{|i| i.to_string}
1183
+ // // => ["172.16.10.0/26",
1184
+ // "172.16.10.64/26",
1185
+ // "172.16.10.128/26",
1186
+ // "172.16.10.192/26"]
1187
+ //
1188
+ // If +num+ is any other number, the supernet will be
1189
+ // divided into some networks with a even number of hosts and
1190
+ // other networks with the remaining addresses.
1191
+ //
1192
+ // network = IPAddress("172.16.10.0/24")
1193
+ //
1194
+ // network / 3 // implies map{|i| i.to_string}
1195
+ // // => ["172.16.10.0/26",
1196
+ // "172.16.10.64/26",
1197
+ // "172.16.10.128/25"]
1198
+ //
1199
+ // Returns an array of IPv4 objects
1200
+ //
1201
+ public sum_first_found(arr: IPAddress[]): IPAddress[] {
1202
+ let dup = arr.slice();
1203
+ if (dup.length < 2) {
1204
+ return dup;
1205
+ }
1206
+ for (let i = dup.length - 2; i >= 0; --i) {
1207
+ // console.log("sum_first_found:", dup[i], dup[i + 1]);
1208
+ const a = IPAddress.summarize([dup[i], dup[i + 1]]);
1209
+ // println!("dup:{}:{}:{}", dup.length, i, a.length);
1210
+ if (a.length == 1) {
1211
+ dup[i] = a[0];
1212
+ dup = dup.slice(0, i + 1).concat(dup.slice(i + 2));
1213
+ break;
1214
+ }
1215
+ }
1216
+ return dup;
1217
+ }
1218
+
1219
+ public split(subnets: number): IPAddress[] {
1220
+ if (subnets == 0 || 1 << this.prefix.host_prefix() <= subnets) {
1221
+ return null;
1222
+ }
1223
+ const networks = this.subnet(this.newprefix(subnets).num);
1224
+ if (!networks) {
1225
+ return networks;
1226
+ }
1227
+ let net = networks;
1228
+ while (net.length != subnets) {
1229
+ net = this.sum_first_found(net);
1230
+ }
1231
+ return net;
1232
+ }
1233
+ // alias_method :/, :split
1234
+
1235
+ // Returns a new IPv4 object from the supernetting
1236
+ // of the instance network.
1237
+ //
1238
+ // Supernetting is similar to subnetting, except
1239
+ // that you getting as a result a network with a
1240
+ // smaller prefix (bigger host space). For example,
1241
+ // given the network
1242
+ //
1243
+ // ip = IPAddress("172.16.10.0/24")
1244
+ //
1245
+ // you can supernet it with a new /23 prefix
1246
+ //
1247
+ // ip.supernet(23).to_string
1248
+ // // => "172.16.10.0/23"
1249
+ //
1250
+ // However if you supernet it with a /22 prefix, the
1251
+ // network address will change:
1252
+ //
1253
+ // ip.supernet(22).to_string
1254
+ // // => "172.16.8.0/22"
1255
+ //
1256
+ // If +new_prefix+ is less than 1, returns 0.0.0.0/0
1257
+ //
1258
+ public supernet(new_prefix: number): IPAddress {
1259
+ if (new_prefix >= this.prefix.num) {
1260
+ return null;
1261
+ }
1262
+ // let new_ip = this.host_address.clone();
1263
+ // for _ in new_prefix..this.prefix.num {
1264
+ // new_ip = new_ip << 1;
1265
+ // }
1266
+ return this.from(this.host_address, this.prefix.from(new_prefix)).network();
1267
+ }
1268
+
1269
+ // This method implements the subnetting function
1270
+ // similar to the one described in RFC3531.
1271
+ //
1272
+ // By specifying a new prefix, the method calculates
1273
+ // the network number for the given IPv4 object
1274
+ // and calculates the subnets associated to the new
1275
+ // prefix.
1276
+ //
1277
+ // For example, given the following network:
1278
+ //
1279
+ // ip = IPAddress "172.16.10.0/24"
1280
+ //
1281
+ // we can calculate the subnets with a /26 prefix
1282
+ //
1283
+ // ip.subnets(26).map(:to_string)
1284
+ // // => ["172.16.10.0/26", "172.16.10.64/26",
1285
+ // "172.16.10.128/26", "172.16.10.192/26"]
1286
+ //
1287
+ // The resulting number of subnets will of course always be
1288
+ // a power of two.
1289
+ //
1290
+ public subnet(subprefix: number): IPAddress[] {
1291
+ if (subprefix < this.prefix.num || this.ip_bits.bits < subprefix) {
1292
+ return null;
1293
+ }
1294
+ const ret: IPAddress[] = [];
1295
+ let net = this.network();
1296
+ net.prefix = net.prefix.from(subprefix);
1297
+ for (let _ = 0; _ < 1 << (subprefix - this.prefix.num); _++) {
1298
+ ret.push(net.clone());
1299
+ net = net.from(net.host_address, net.prefix);
1300
+ const size = net.size();
1301
+ net.host_address = net.host_address.add(size);
1302
+ }
1303
+ return ret;
1304
+ }
1305
+
1306
+ // Return the ip address in a format compatible
1307
+ // with the IPv6 Mapped IPv4 addresses
1308
+ //
1309
+ // Example:
1310
+ //
1311
+ // ip = IPAddress("172.16.10.1/24")
1312
+ //
1313
+ // ip.to_ipv6
1314
+ // // => "ac10:0a01"
1315
+ //
1316
+ public to_ipv6(): IPAddress {
1317
+ return this.vt_to_ipv6(this);
1318
+ }
1319
+
1320
+ public newprefix(num: number): Prefix {
1321
+ for (let i = num; i < this.ip_bits.bits; ++i) {
1322
+ const a = ~~Math.log2(i);
1323
+ if (a == Math.log2(i)) {
1324
+ return this.prefix.add(a);
1325
+ }
1326
+ }
1327
+ return null;
1328
+ }
1329
+ }
1330
+
1331
+ export default IPAddress;