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