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