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