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
package/ipaddress.js DELETED
@@ -1,2854 +0,0 @@
1
- module.exports =
2
- /******/ (function(modules) { // webpackBootstrap
3
- /******/ // The module cache
4
- /******/ var installedModules = {};
5
- /******/
6
- /******/ // The require function
7
- /******/ function __webpack_require__(moduleId) {
8
- /******/
9
- /******/ // Check if module is in cache
10
- /******/ if(installedModules[moduleId])
11
- /******/ return installedModules[moduleId].exports;
12
- /******/
13
- /******/ // Create a new module (and put it into the cache)
14
- /******/ var module = installedModules[moduleId] = {
15
- /******/ exports: {},
16
- /******/ id: moduleId,
17
- /******/ loaded: false
18
- /******/ };
19
- /******/
20
- /******/ // Execute the module function
21
- /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
22
- /******/
23
- /******/ // Flag the module as loaded
24
- /******/ module.loaded = true;
25
- /******/
26
- /******/ // Return the exports of the module
27
- /******/ return module.exports;
28
- /******/ }
29
- /******/
30
- /******/
31
- /******/ // expose the modules object (__webpack_modules__)
32
- /******/ __webpack_require__.m = modules;
33
- /******/
34
- /******/ // expose the module cache
35
- /******/ __webpack_require__.c = installedModules;
36
- /******/
37
- /******/ // __webpack_public_path__
38
- /******/ __webpack_require__.p = "";
39
- /******/
40
- /******/ // Load entry module and return exports
41
- /******/ return __webpack_require__(0);
42
- /******/ })
43
- /************************************************************************/
44
- /******/ ([
45
- /* 0 */
46
- /***/ function(module, exports, __webpack_require__) {
47
-
48
- "use strict";
49
-
50
- function __export(m) {
51
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
52
- }
53
- __export(__webpack_require__(1));
54
- __export(__webpack_require__(2));
55
- __export(__webpack_require__(3));
56
- __export(__webpack_require__(5));
57
- __export(__webpack_require__(6));
58
- __export(__webpack_require__(7));
59
- __export(__webpack_require__(10));
60
- __export(__webpack_require__(11));
61
- __export(__webpack_require__(12));
62
- __export(__webpack_require__(13));
63
- const ipaddress_2 = __webpack_require__(6);
64
- Object.defineProperty(exports, "__esModule", { value: true });
65
- exports.default = ipaddress_2.default;
66
- //declare let Ip: IPAddress;
67
-
68
- /***/ },
69
- /* 1 */
70
- /***/ function(module, exports) {
71
-
72
- /**
73
- * based
74
- * Crunch - Arbitrary-precision integer arithmetic library
75
- * Copyright (C) 2014 Nenad Vukicevic crunch.secureroom.net/license
76
- *
77
- */
78
- /**
79
- * @module Crunch
80
- * Radix: 28 bits
81
- * Endianness: Big
82
- *
83
- * @param {boolean} rawIn - expect 28-bit arrays
84
- * @param {boolean} rawOut - return 28-bit arrays
85
- */
86
- // import Crunch from './crunch';
87
- "use strict";
88
-
89
- class Crunchy {
90
- constructor() {
91
- this.num = [];
92
- this.negative = false;
93
- }
94
- clone() {
95
- let ret = new Crunchy();
96
- ret.num = this.num.slice();
97
- ret.negative = this.negative;
98
- return ret;
99
- }
100
- static removeLeadingZeros(inn) {
101
- let out = inn.slice();
102
- while (out[0] === 0 && out.length > 1) {
103
- out.shift();
104
- }
105
- return out; //.transformOut();
106
- }
107
- static from_14bit(a) {
108
- let ret = new Crunchy();
109
- ret.num = a;
110
- return ret;
111
- }
112
- static from_8bit(a) {
113
- let x = [0, 0, 0, 0, 0, 0].slice((a.length - 1) % 7);
114
- let z = new Crunchy();
115
- if (a[0] < 0) {
116
- a[0] *= -1;
117
- z.negative = true;
118
- } else {
119
- z.negative = false;
120
- }
121
- x = x.concat(a);
122
- for (let i = 0; i < x.length; i += 7) {
123
- z.num.push(x[i] * 1048576 + x[i + 1] * 4096 + x[i + 2] * 16 + (x[i + 3] >> 4), (x[i + 3] & 15) * 16777216 + x[i + 4] * 65536 + x[i + 5] * 256 + x[i + 6]);
124
- }
125
- z.num = Crunchy.removeLeadingZeros(z.num);
126
- return z;
127
- }
128
- static parse(val) {
129
- return Crunchy.from_string(val, 10);
130
- }
131
- static from_number(val) {
132
- return Crunchy.parse("" + val);
133
- }
134
- static from_string(val, radix = 10) {
135
- let x = val.split("");
136
- let p = Crunchy.one();
137
- let a = Crunchy.zero();
138
- let b = Crunchy.from_8bit([radix]);
139
- // console.log("fromstring:one:", p);
140
- // console.log("fromstring:zero:", a);
141
- // console.log("fromstring:radix:", b);
142
- let n = false;
143
- if (x[0] === "-") {
144
- n = true;
145
- x.shift();
146
- }
147
- while (x.length) {
148
- let c = parseInt(x.pop(), radix);
149
- if (isNaN(c)) {
150
- console.error("from_string:", val);
151
- return null;
152
- }
153
- // console.log(a,p,b);
154
- a = a.add(p.mul(Crunchy.from_8bit([c])));
155
- p = p.mul(b);
156
- }
157
- a.negative = n;
158
- return a;
159
- }
160
- to_8bit() {
161
- let x = [0].slice((this.num.length - 1) % 2).concat(this.num);
162
- let z = [];
163
- // z.num = [];
164
- for (let i = 0; i < x.length;) {
165
- let u = x[i++];
166
- let v = x[i++];
167
- z.push(u >> 20, u >> 12 & 255, u >> 4 & 255, (u << 4 | v >> 24) & 255, v >> 16 & 255, v >> 8 & 255, v & 255);
168
- }
169
- // console.log("co:", a, z);
170
- z = Crunchy.removeLeadingZeros(z);
171
- if (this.negative) {
172
- z[0] *= -1;
173
- }
174
- return z;
175
- }
176
- compare(y) {
177
- let xl = this.num.length,
178
- yl = y.num.length; //zero front pad problem
179
- if (xl < yl) {
180
- return -1;
181
- } else if (xl > yl) {
182
- return 1;
183
- }
184
- for (let i = 0; i < xl; i++) {
185
- //console.log("x=y:", i, x, y);
186
- if (this.num[i] < y.num[i]) return -1;
187
- if (this.num[i] > y.num[i]) return 1;
188
- }
189
- return 0;
190
- }
191
- eq(oth) {
192
- return this.compare(oth) == 0;
193
- }
194
- lte(oth) {
195
- return this.compare(oth) <= 0;
196
- }
197
- lt(oth) {
198
- return this.compare(oth) < 0;
199
- }
200
- gt(oth) {
201
- return this.compare(oth) > 0;
202
- }
203
- gte(oth) {
204
- return this.compare(oth) >= 0;
205
- }
206
- add(y) {
207
- let z;
208
- if (this.negative) {
209
- if (y.negative) {
210
- z = this.unsigned_add(y);
211
- z.negative = true;
212
- } else {
213
- z = y.unsigned_sub(this, false).cut();
214
- }
215
- } else {
216
- z = y.negative ? this.unsigned_sub(y, false).cut() : this.unsigned_add(y);
217
- }
218
- return z;
219
- }
220
- unsigned_add(_y) {
221
- let n = this.num.length,
222
- t = _y.num.length,
223
- i = Math.max(n, t),
224
- c = 0,
225
- z = Crunchy.zeroes.slice(0, i);
226
- // console.log("add:1:", new Date());
227
- let x = this.num;
228
- let y = _y.num;
229
- if (n < t) {
230
- x = Crunchy.zeroes.slice(0, t - n).concat(this.num);
231
- } else if (n > t) {
232
- y = Crunchy.zeroes.slice(0, n - t).concat(y);
233
- }
234
- // console.log("add:2:", new Date());
235
- for (i -= 1; i >= 0; i--) {
236
- z[i] = x[i] + y[i] + c;
237
- if (z[i] > 268435455) {
238
- c = 1;
239
- z[i] -= 268435456;
240
- } else {
241
- c = 0;
242
- }
243
- }
244
- // console.log("add:3:", new Date());
245
- if (c === 1) {
246
- z.unshift(c);
247
- }
248
- let ret = new Crunchy();
249
- ret.num = z;
250
- // console.log("add:4:", new Date());
251
- return ret;
252
- }
253
- sub(y) {
254
- let z;
255
- if (this.negative) {
256
- if (y.negative) {
257
- // console.log("sub-c1");
258
- z = y.unsigned_sub(this, false).cut();
259
- } else {
260
- // console.log("sub-c2");
261
- z = this.unsigned_add(y);
262
- z.negative = true;
263
- }
264
- } else {
265
- // console.log("sub-c3", this, y);
266
- z = y.negative ? this.unsigned_add(y) : this.unsigned_sub(y, false).cut();
267
- }
268
- return z;
269
- }
270
- unsigned_sub(_y, internal = false) {
271
- let n = this.num.length,
272
- t = _y.num.length,
273
- i = Math.max(n, t),
274
- c = 0,
275
- z = Crunchy.zeroes.slice(0, i);
276
- let x = this.num;
277
- let y = _y.num;
278
- if (n < t) {
279
- x = Crunchy.zeroes.slice(0, t - n).concat(x);
280
- } else if (n > t) {
281
- y = Crunchy.zeroes.slice(0, n - t).concat(y);
282
- }
283
- for (i -= 1; i >= 0; i--) {
284
- z[i] = x[i] - y[i] - c;
285
- // console.log(z, x, y);
286
- if (z[i] < 0) {
287
- c = 1;
288
- // console.log("pre:+", z[i], z);
289
- z[i] += 268435456;
290
- } else {
291
- c = 0;
292
- }
293
- }
294
- let cry = new Crunchy();
295
- cry.num = z;
296
- if (c === 1 && !internal) {
297
- let zero = new Crunchy();
298
- zero.num = Crunchy.zeroes.slice(0, z.length);
299
- cry = zero.unsigned_sub(cry, true);
300
- cry.negative = true;
301
- }
302
- return cry;
303
- }
304
- lsh(s) {
305
- var ss = s % 28,
306
- ls = Math.floor(s / 28),
307
- l = this.num.length,
308
- z = this.clone(),
309
- t = 0;
310
- if (ss) {
311
- z.num = [];
312
- while (l--) {
313
- z.num[l] = (this.num[l] << ss) + t & 268435455;
314
- t = this.num[l] >>> 28 - ss;
315
- }
316
- if (t !== 0) {
317
- z.num.unshift(t);
318
- }
319
- z.negative = this.negative;
320
- }
321
- if (ls) {
322
- z.num = z.num.concat(Crunchy.zeroes.slice(0, ls));
323
- }
324
- // console.log(this, s, z);
325
- return z;
326
- }
327
- // public leftShift(s: number): Crunchy {
328
- // return this.transformIn()
329
- // Crunchy.transformOut(Crunch.lsh(.pop(), s));
330
- // }
331
- // public shl(num: number): Crunchy {
332
- // return Crunchy.from_8bit(Crunch.leftShift(this.num, num));
333
- // }
334
- rsh(s) {
335
- let ss = s % 28,
336
- ls = Math.floor(s / 28),
337
- l = this.num.length - ls,
338
- z = this.clone();
339
- z.num = this.num.slice(0, l);
340
- if (ss) {
341
- while (--l >= 0) {
342
- z.num[l] = (z.num[l] >> ss | z.num[l - 1] << 28 - ss) & 268435455;
343
- }
344
- z.num[l] = z.num[l] >> ss;
345
- if (z.num[0] === 0) {
346
- z.num.shift();
347
- }
348
- }
349
- return z;
350
- }
351
- // public shr(num: number): Crunchy {
352
- // //console.log("shr<<", num, this.num);
353
- // let res = this.rightShift(this.num, num);
354
- // // console.log("shr>>", num, this.num, res);
355
- // return Crunchy.from_8bit(res);
356
- // }
357
- mul(y) {
358
- let yl,
359
- yh,
360
- c,
361
- n = this.num.length,
362
- i = y.num.length,
363
- z = Crunchy.zeroes.slice(0, n + i);
364
- while (i--) {
365
- c = 0;
366
- yl = y.num[i] & 16383;
367
- yh = y.num[i] >> 14;
368
- for (let j = n - 1, xl, xh, t1, t2; j >= 0; j--) {
369
- xl = this.num[j] & 16383;
370
- xh = this.num[j] >> 14;
371
- t1 = yh * xl + xh * yl;
372
- t2 = yl * xl + ((t1 & 16383) << 14) + z[j + i + 1] + c;
373
- z[j + i + 1] = t2 & 268435455;
374
- c = yh * xh + (t1 >> 14) + (t2 >> 28);
375
- }
376
- z[i] = c;
377
- }
378
- if (z[0] === 0) {
379
- z.shift();
380
- }
381
- let ret = new Crunchy();
382
- ret.negative = this.negative !== y.negative;
383
- ret.num = z;
384
- return ret;
385
- }
386
- static msb(x) {
387
- if (x !== 0) {
388
- let z = 0;
389
- for (let i = 134217728; i > x; z++) {
390
- i /= 2;
391
- }
392
- return z;
393
- }
394
- }
395
- // public transformIn(): Crunchy {
396
- // // return Array.prototype.slice.call(a).map((v: Crunchy) => {
397
- // // return Crunchy.from_8bit(v.num.slice())
398
- // // });
399
- // return null;
400
- // }
401
- // public transformOut(): Crunchy {
402
- // console.log("in-transformOut:", this);
403
- // let ret = Crunchy.co(this);
404
- // console.log("out-transformOut:", this, ret);
405
- // return ret;
406
- // }
407
- shr(s) {
408
- let my = this.rsh(s);
409
- return my.cut();
410
- }
411
- shl(s) {
412
- return this.lsh(s).cut();
413
- // Crunch.transformOut(Crunch.lsh(Crunch.transformIn([x]).pop(), s));
414
- }
415
- cut() {
416
- let out = this.clone();
417
- // beasty hack
418
- if (out.num.length == 0) {
419
- out.num = [0];
420
- return out;
421
- }
422
- while (out.num[0] === 0 && out.num.length > 1) {
423
- out.num.shift();
424
- }
425
- return out; //.transformOut();
426
- }
427
- // return Crunchy.transformOut(this.num
428
- // cut.apply(null, transformIn(arguments))
429
- // );
430
- // }
431
- div(y, internal = false) {
432
- if (y.num.length === 1 && y.num[0] === 0) {
433
- return null;
434
- }
435
- // var u, v, xt, yt, d, q, k, i, z;
436
- let u;
437
- let v;
438
- let s = Crunchy.msb(y.num[0]) - 1;
439
- if (s > 0) {
440
- u = this.lsh(s);
441
- v = y.lsh(s);
442
- } else {
443
- u = this.clone();
444
- v = y.clone();
445
- }
446
- let d = u.num.length - v.num.length;
447
- let q = [0];
448
- let k = Crunchy.from_14bit(v.num.concat(Crunchy.zeroes.slice(0, d)));
449
- let yt = v.num[0] * 268435456 + v.num[1];
450
- // only cmp as last resort
451
- while (u.num[0] > k.num[0] || u.num[0] === k.num[0] && u.compare(k) > -1) {
452
- q[0]++;
453
- u = u.unsigned_sub(k, false);
454
- }
455
- for (let i = 1; i <= d; i++) {
456
- q[i] = u.num[i - 1] === v.num[0] ? 268435455 : ~~((u.num[i - 1] * 268435456 + u.num[i]) / v.num[0]);
457
- let xt = u.num[i - 1] * 72057594037927936 + u.num[i] * 268435456 + u.num[i + 1];
458
- while (q[i] * yt > xt) {
459
- q[i]--;
460
- }
461
- k = v.mul(Crunchy.from_14bit([q[i]]));
462
- k.num = k.num.concat(Crunchy.zeroes.slice(0, d - i)); //concat after multiply, save cycles
463
- u = u.unsigned_sub(k, false);
464
- if (u.negative) {
465
- u = Crunchy.from_14bit(v.num.concat(Crunchy.zeroes.slice(0, d - i))).unsigned_sub(u, false);
466
- q[i]--;
467
- }
468
- }
469
- let z;
470
- if (internal) {
471
- z = s > 0 ? u.cut().rsh(s) : u.cut();
472
- } else {
473
- z = Crunchy.from_14bit(Crunchy.removeLeadingZeros(q));
474
- z.negative = this.negative !== y.negative ? true : false;
475
- }
476
- return z;
477
- }
478
- // public sub(cry: Crunchy): Crunchy {
479
- // return Crunchy.from_8bit(Crunch.sub(this.num, cry.num));
480
- // }
481
- mod(y) {
482
- //For negative x, cmp doesn't work and result of div is negative
483
- //so take result away from the modulus to get the correct result
484
- if (this.negative) {
485
- return y.sub(this.div(y, true));
486
- }
487
- switch (this.compare(y)) {
488
- case -1:
489
- return this;
490
- case 0:
491
- return Crunchy.from_8bit([0]);
492
- default:
493
- return this.div(y, true);
494
- }
495
- }
496
- mds(n) {
497
- let z = 0;
498
- for (let i = 0, l = this.num.length; i < l; i++) {
499
- z = ((this.num[i] >> 14) + (z << 14)) % n;
500
- z = ((this.num[i] & 16383) + (z << 14)) % n;
501
- }
502
- return z;
503
- }
504
- toString(radix = 10) {
505
- let a = [],
506
- i = 0;
507
- let x = this.clone();
508
- // console.log("toString:", new Date());
509
- let cradix = Crunchy.from_8bit([radix]);
510
- let zero = Crunchy.zero();
511
- do {
512
- let digit = x.mds(radix);
513
- x = x.div(cradix);
514
- a[i++] = "0123456789abcdef"[digit];
515
- } while (!x.eq(zero));
516
- // console.log("Crunchy.tostring-1:", a);
517
- let ret = a.reverse().join("");
518
- // console.log("Crunchy.tostring-2:", ret, new Date());
519
- return ret;
520
- }
521
- static zero() {
522
- return Crunchy._zero;
523
- }
524
- static one() {
525
- return Crunchy._one;
526
- }
527
- static two() {
528
- return Crunchy._two;
529
- }
530
- }
531
- Crunchy.zeroes = (n => {
532
- let z = [];
533
- for (; z.push(0) < n;) {}
534
- return z;
535
- })(60);
536
- // public toNumber() : number {
537
- // return null;
538
- // }
539
- Crunchy._zero = Crunchy.from_8bit([0]);
540
- Crunchy._one = Crunchy.from_8bit([1]);
541
- Crunchy._two = Crunchy.from_8bit([2]);
542
- exports.Crunchy = Crunchy;
543
- Object.defineProperty(exports, "__esModule", { value: true });
544
- exports.default = Crunchy;
545
-
546
- /***/ },
547
- /* 2 */
548
- /***/ function(module, exports, __webpack_require__) {
549
-
550
- "use strict";
551
-
552
- const crunchy_1 = __webpack_require__(1);
553
- class Prefix {
554
- constructor(obj) {
555
- this.num = obj['num'];
556
- this.ip_bits = obj['ip_bits'];
557
- this.net_mask = obj['net_mask'];
558
- this.vt_from = obj['vt_from'];
559
- }
560
- clone() {
561
- return new Prefix({
562
- num: this.num,
563
- ip_bits: this.ip_bits,
564
- net_mask: this.net_mask,
565
- vt_from: this.vt_from
566
- });
567
- }
568
- eq(other) {
569
- return this.ip_bits.version == other.ip_bits.version && this.num == other.num;
570
- }
571
- ne(other) {
572
- return !this.eq(other);
573
- }
574
- cmp(oth) {
575
- if (this.ip_bits.version < oth.ip_bits.version) {
576
- return -1;
577
- } else if (this.ip_bits.version > oth.ip_bits.version) {
578
- return 1;
579
- } else {
580
- if (this.num < oth.num) {
581
- return -1;
582
- } else if (this.num > oth.num) {
583
- return 1;
584
- } else {
585
- return 0;
586
- }
587
- }
588
- }
589
- //#[allow(dead_code)]
590
- from(num) {
591
- return this.vt_from(this, num);
592
- }
593
- to_ip_str() {
594
- return this.ip_bits.vt_as_compressed_string(this.ip_bits, this.net_mask);
595
- }
596
- size() {
597
- return crunchy_1.default.one().shl(this.ip_bits.bits - this.num);
598
- }
599
- static new_netmask(prefix, bits) {
600
- let mask = crunchy_1.default.zero();
601
- let host_prefix = bits - prefix;
602
- for (let i = 0; i < prefix; ++i) {
603
- // console.log(">>>", i, host_prefix, mask);
604
- mask = mask.add(crunchy_1.default.one().shl(host_prefix + i));
605
- }
606
- return mask;
607
- }
608
- netmask() {
609
- return this.net_mask;
610
- }
611
- get_prefix() {
612
- return this.num;
613
- }
614
- // The hostmask is the contrary of the subnet mask,
615
- // as it shows the bits that can change within the
616
- // hosts
617
- //
618
- // prefix = IPAddress::Prefix32.new 24
619
- //
620
- // prefix.hostmask
621
- // // => "0.0.0.255"
622
- //
623
- host_mask() {
624
- let ret = crunchy_1.default.zero();
625
- for (let _ = 0; _ < this.ip_bits.bits - this.num; _++) {
626
- ret = ret.shl(1).add(crunchy_1.default.one());
627
- }
628
- return ret;
629
- }
630
- //
631
- // Returns the length of the host portion
632
- // of a netmask.
633
- //
634
- // prefix = Prefix128.new 96
635
- //
636
- // prefix.host_prefix
637
- // // => 128
638
- //
639
- host_prefix() {
640
- return this.ip_bits.bits - this.num;
641
- }
642
- //
643
- // Transforms the prefix into a string of bits
644
- // representing the netmask
645
- //
646
- // prefix = IPAddress::Prefix128.new 64
647
- //
648
- // prefix.bits
649
- // // => "1111111111111111111111111111111111111111111111111111111111111111"
650
- // "0000000000000000000000000000000000000000000000000000000000000000"
651
- //
652
- bits() {
653
- return this.netmask().toString(2);
654
- }
655
- // #[allow(dead_code)]
656
- // public net_mask(&self) -> BigUint {
657
- // return (self.in_mask.clone() >> (self.host_prefix() as usize)) << (self.host_prefix() as usize);
658
- // }
659
- to_s() {
660
- return this.get_prefix().toString();
661
- }
662
- //#[allow(dead_code)]
663
- // public inspect(&self) -> String {
664
- // return self.to_s();
665
- // }
666
- to_i() {
667
- return this.get_prefix();
668
- }
669
- add_prefix(other) {
670
- return this.from(this.get_prefix() + other.get_prefix());
671
- }
672
- add(other) {
673
- return this.from(this.get_prefix() + other);
674
- }
675
- sub_prefix(other) {
676
- return this.sub(other.get_prefix());
677
- }
678
- sub(other) {
679
- if (other > this.get_prefix()) {
680
- return this.from(other - this.get_prefix());
681
- }
682
- return this.from(this.get_prefix() - other);
683
- }
684
- }
685
- exports.Prefix = Prefix;
686
- Object.defineProperty(exports, "__esModule", { value: true });
687
- exports.default = Prefix;
688
-
689
- /***/ },
690
- /* 3 */
691
- /***/ function(module, exports, __webpack_require__) {
692
-
693
- "use strict";
694
-
695
- const crunchy_1 = __webpack_require__(1);
696
- const rle_1 = __webpack_require__(4);
697
- const ip_version_1 = __webpack_require__(5);
698
- // #[derive(Debug, Clone)]
699
- class IpBits {
700
- clone() {
701
- let my = new IpBits();
702
- my.version = this.version;
703
- my.vt_as_compressed_string = this.vt_as_compressed_string;
704
- my.vt_as_uncompressed_string = this.vt_as_uncompressed_string;
705
- my.bits = this.bits;
706
- my.part_bits = this.part_bits;
707
- my.dns_bits = this.dns_bits;
708
- my.rev_domain = this.rev_domain;
709
- my.part_mod = this.part_mod;
710
- my.host_ofs = this.host_ofs.clone();
711
- return my;
712
- }
713
- parts(bu) {
714
- let vec = [];
715
- let my = bu.clone();
716
- let part_mod = crunchy_1.default.one().shl(this.part_bits); // - Crunchy::one();
717
- for (let i = 0; i < this.bits / this.part_bits; ++i) {
718
- // console.log("parts-1:", my, part_mod, my.mod(part_mod), my.mod(part_mod).toString());
719
- vec.push(+my.mod(part_mod).toString());
720
- my = my.shr(this.part_bits);
721
- }
722
- // console.log("parts:", vec);
723
- return vec.reverse();
724
- }
725
- as_compressed_string(bu) {
726
- return this.vt_as_compressed_string(this, bu);
727
- }
728
- as_uncompressed_string(bu) {
729
- return this.vt_as_uncompressed_string(this, bu);
730
- }
731
- dns_part_format(i) {
732
- switch (this.version) {
733
- case ip_version_1.default.V4:
734
- return `${ i }`;
735
- case ip_version_1.default.V6:
736
- return `${ i.toString(16) }`;
737
- }
738
- }
739
- static v4() {
740
- let my = new IpBits();
741
- my.version = ip_version_1.default.V4;
742
- my.vt_as_compressed_string = IpBits.ipv4_as_compressed;
743
- my.vt_as_uncompressed_string = IpBits.ipv4_as_compressed;
744
- my.bits = 32;
745
- my.part_bits = 8;
746
- my.dns_bits = 8;
747
- my.rev_domain = "in-addr.arpa";
748
- my.part_mod = 1 << 8;
749
- my.host_ofs = crunchy_1.default.one();
750
- return my;
751
- }
752
- static v6() {
753
- let my = new IpBits();
754
- my.version = ip_version_1.default.V6;
755
- my.vt_as_compressed_string = IpBits.ipv6_as_compressed;
756
- my.vt_as_uncompressed_string = IpBits.ipv6_as_uncompressed;
757
- my.bits = 128;
758
- my.part_bits = 16;
759
- my.dns_bits = 4;
760
- my.rev_domain = "ip6.arpa";
761
- my.part_mod = 1 << 16;
762
- my.host_ofs = crunchy_1.default.zero();
763
- return my;
764
- }
765
- static ipv4_as_compressed(ip_bits, host_address) {
766
- let ret = "";
767
- let sep = "";
768
- for (let part of ip_bits.parts(host_address)) {
769
- ret += sep;
770
- ret += `${ part }`;
771
- sep = ".";
772
- }
773
- return ret;
774
- }
775
- static ipv6_as_compressed(ip_bits, host_address) {
776
- //println!("ipv6_as_compressed:{}", host_address);
777
- let ret = "";
778
- let colon = "";
779
- let done = false;
780
- for (let rle of rle_1.default.code(ip_bits.parts(host_address))) {
781
- // console.log(rle.toString());
782
- for (let _ = 0; _ < rle.cnt; _++) {
783
- if (done || !(rle.part == 0 && rle.max)) {
784
- ret += `${ colon }${ rle.part.toString(16) }`;
785
- colon = ":";
786
- } else if (rle.part == 0 && rle.max) {
787
- ret += "::";
788
- colon = "";
789
- done = true;
790
- break;
791
- }
792
- }
793
- }
794
- return ret;
795
- }
796
- static ipv6_as_uncompressed(ip_bits, host_address) {
797
- let ret = "";
798
- let sep = "";
799
- for (let part of ip_bits.parts(host_address)) {
800
- ret += sep;
801
- ret += (0x10000 + part).toString(16).slice(1);
802
- sep = ":";
803
- }
804
- return ret;
805
- }
806
- }
807
- exports.IpBits = IpBits;
808
- Object.defineProperty(exports, "__esModule", { value: true });
809
- exports.default = IpBits;
810
-
811
- /***/ },
812
- /* 4 */
813
- /***/ function(module, exports) {
814
-
815
- "use strict";
816
-
817
- class Last {
818
- constructor() {
819
- this.val = null;
820
- this.max_poses = {};
821
- this.ret = [];
822
- }
823
- handle_last() {
824
- if (null == this.val) {
825
- return;
826
- }
827
- let _last = this.val;
828
- let max_rles = this.max_poses[_last.part];
829
- if (max_rles == null) {
830
- max_rles = this.max_poses[_last.part] = [];
831
- }
832
- // console.log(_last.part, this.max_poses);
833
- for (let idx in max_rles) {
834
- let prev = this.ret[max_rles[idx]];
835
- if (prev.cnt > _last.cnt) {
836
- // console.log(`>>>>> last=${_last}->${idx}->prev=${prev}`);
837
- _last.max = false;
838
- } else if (prev.cnt == _last.cnt) {} else if (prev.cnt < _last.cnt) {
839
- // console.log(`<<<<< last=${_last}->${idx}->prev=${prev}`);
840
- prev.max = false;
841
- }
842
- }
843
- //println!("push:{}:{:?}", self.ret.len(), _last);
844
- max_rles.push(this.ret.length);
845
- _last.pos = this.ret.length;
846
- this.ret.push(_last);
847
- }
848
- }
849
- class Rle {
850
- constructor(obj) {
851
- this.part = obj['part'];
852
- this.pos = obj['pos'];
853
- this.cnt = obj['cnt'];
854
- this.max = obj['max'];
855
- }
856
- toString() {
857
- return `<Rle@part:${ this.part },pos:${ this.pos },cnt:${ this.cnt },max:${ this.max }>`;
858
- }
859
- eq(other) {
860
- return this.part == other.part && this.pos == other.pos && this.cnt == other.cnt && this.max == other.max;
861
- }
862
- ne(other) {
863
- return !this.eq(other);
864
- }
865
- static code(parts) {
866
- let last = new Last();
867
- // println!("code");
868
- for (let i = 0; i < parts.length; ++i) {
869
- let part = parts[i];
870
- // console.log(`part:${part}`);
871
- if (last.val && last.val.part == part) {
872
- last.val.cnt += 1;
873
- } else {
874
- last.handle_last();
875
- last.val = new Rle({ part: part, pos: 0, cnt: 1, max: true });
876
- }
877
- }
878
- last.handle_last();
879
- return last.ret;
880
- }
881
- }
882
- Object.defineProperty(exports, "__esModule", { value: true });
883
- exports.default = Rle;
884
-
885
- /***/ },
886
- /* 5 */
887
- /***/ function(module, exports) {
888
-
889
- "use strict";
890
-
891
- var IpVersion;
892
- (function (IpVersion) {
893
- IpVersion[IpVersion["V4"] = 4] = "V4";
894
- IpVersion[IpVersion["V6"] = 6] = "V6";
895
- })(IpVersion || (IpVersion = {}));
896
- Object.defineProperty(exports, "__esModule", { value: true });
897
- exports.default = IpVersion;
898
-
899
- /***/ },
900
- /* 6 */
901
- /***/ function(module, exports, __webpack_require__) {
902
-
903
- "use strict";
904
-
905
- const ip_version_1 = __webpack_require__(5);
906
- const ipv4_1 = __webpack_require__(7);
907
- const ipv6_1 = __webpack_require__(10);
908
- const ipv6_mapped_1 = __webpack_require__(11);
909
- const crunchy_1 = __webpack_require__(1);
910
- class ResultCrunchyParts {
911
- constructor(crunchy, parts) {
912
- this.crunchy = crunchy;
913
- this.parts = parts;
914
- // console.log("ResultCrunchyParts:", this);
915
- }
916
- }
917
- exports.ResultCrunchyParts = ResultCrunchyParts;
918
- class IPAddress {
919
- constructor(obj) {
920
- this.ip_bits = obj['ip_bits'];
921
- this.host_address = obj['host_address'];
922
- this.prefix = obj['prefix'];
923
- this.mapped = obj['mapped'];
924
- this.vt_is_private = obj['vt_is_private'];
925
- this.vt_is_loopback = obj['vt_is_loopback'];
926
- this.vt_to_ipv6 = obj['vt_to_ipv6'];
927
- }
928
- clone() {
929
- let mapped = null;
930
- if (this.mapped) {
931
- mapped = this.mapped.clone();
932
- }
933
- return new IPAddress({
934
- ip_bits: this.ip_bits.clone(),
935
- host_address: this.host_address.clone(),
936
- prefix: this.prefix.clone(),
937
- mapped: mapped,
938
- vt_is_private: this.vt_is_private,
939
- vt_is_loopback: this.vt_is_loopback,
940
- vt_to_ipv6: this.vt_to_ipv6
941
- });
942
- }
943
- lt(oth) {
944
- return this.cmp(oth) == -1;
945
- }
946
- lte(oth) {
947
- return this.cmp(oth) <= 0;
948
- }
949
- gt(oth) {
950
- return this.cmp(oth) == 1;
951
- }
952
- gte(oth) {
953
- return this.cmp(oth) >= 0;
954
- }
955
- cmp(oth) {
956
- if (this.ip_bits.version != oth.ip_bits.version) {
957
- if (this.ip_bits.version == ip_version_1.default.V6) {
958
- return 1;
959
- }
960
- return -1;
961
- }
962
- //let adr_diff = this.host_address - oth.host_address;
963
- let hostCmp = this.host_address.compare(oth.host_address);
964
- if (hostCmp != 0) {
965
- return hostCmp;
966
- }
967
- return this.prefix.cmp(oth.prefix);
968
- }
969
- eq(other) {
970
- // if (!!this.mapped != !!this.mapped) {
971
- // return false;
972
- // }
973
- // if (this.mapped) {
974
- // if (!this.mapped.eq(other.mapped)) {
975
- // return false;
976
- // }
977
- // }
978
- // console.log("************", this);
979
- return this.ip_bits.version == other.ip_bits.version && this.prefix.eq(other.prefix) && this.host_address.eq(other.host_address);
980
- }
981
- ne(other) {
982
- return !this.eq(other);
983
- }
984
- // Parse the argument string to create a new
985
- // IPv4, IPv6 or Mapped IP object
986
- //
987
- // ip = IPAddress.parse "172.16.10.1/24"
988
- // ip6 = IPAddress.parse "2001:db8.8:800:200c:417a/64"
989
- // ip_mapped = IPAddress.parse ".ffff:172.16.10.1/128"
990
- //
991
- // All the object created will be instances of the
992
- // correct class:
993
- //
994
- // ip.class
995
- // //=> IPAddress.IPv4
996
- // ip6.class
997
- // //=> IPAddress.IPv6
998
- // ip_mapped.class
999
- // //=> IPAddress.IPv6.Mapped
1000
- //
1001
- static parse(str) {
1002
- let re_mapped = new RegExp(":.+\\.");
1003
- let re_ipv4 = new RegExp("\\.");
1004
- let re_ipv6 = new RegExp(":");
1005
- if (re_mapped.test(str)) {
1006
- // console.log("mapped:", str);
1007
- return ipv6_mapped_1.default.create(str);
1008
- } else {
1009
- if (re_ipv4.test(str)) {
1010
- // console.log("ipv4:", str);
1011
- return ipv4_1.default.create(str);
1012
- } else if (re_ipv6.test(str)) {
1013
- // console.log("ipv6:", str);
1014
- return ipv6_1.default.create(str);
1015
- }
1016
- }
1017
- return null;
1018
- }
1019
- static split_at_slash(str) {
1020
- let slash = str.trim().split("/");
1021
- let addr = "";
1022
- if (slash[0]) {
1023
- addr += slash[0].trim();
1024
- }
1025
- if (slash[1]) {
1026
- return [addr, slash[1].trim()];
1027
- } else {
1028
- return [addr, null];
1029
- }
1030
- }
1031
- from(addr, prefix) {
1032
- let mapped = null;
1033
- if (this.mapped) {
1034
- mapped = this.mapped.clone();
1035
- }
1036
- return new IPAddress({
1037
- ip_bits: this.ip_bits,
1038
- host_address: addr.clone(),
1039
- prefix: prefix.clone(),
1040
- mapped: mapped,
1041
- vt_is_private: this.vt_is_private,
1042
- vt_is_loopback: this.vt_is_loopback,
1043
- vt_to_ipv6: this.vt_to_ipv6
1044
- });
1045
- }
1046
- // True if the object is an IPv4 address
1047
- //
1048
- // ip = IPAddress("192.168.10.100/24")
1049
- //
1050
- // ip.ipv4?
1051
- // //-> true
1052
- //
1053
- is_ipv4() {
1054
- return this.ip_bits.version == ip_version_1.default.V4;
1055
- }
1056
- // True if the object is an IPv6 address
1057
- //
1058
- // ip = IPAddress("192.168.10.100/24")
1059
- //
1060
- // ip.ipv6?
1061
- // //-> false
1062
- //
1063
- is_ipv6() {
1064
- return this.ip_bits.version == ip_version_1.default.V6;
1065
- }
1066
- // Checks if the given string is a valid IP address,
1067
- // either IPv4 or IPv6
1068
- //
1069
- // Example:
1070
- //
1071
- // IPAddress.valid? "2002.1"
1072
- // //=> true
1073
- //
1074
- // IPAddress.valid? "10.0.0.256"
1075
- // //=> false
1076
- //
1077
- static is_valid(addr) {
1078
- return IPAddress.is_valid_ipv4(addr) || IPAddress.is_valid_ipv6(addr);
1079
- }
1080
- static parse_dec_str(str) {
1081
- let re_digit = new RegExp("^\\d+$");
1082
- if (!re_digit.test(str)) {
1083
- // console.log("parse_dec_str:-1:", str);
1084
- return null;
1085
- }
1086
- let part = parseInt(str, 10);
1087
- if (isNaN(part)) {
1088
- // console.log("parse_dec_str:-2:", str, part);
1089
- return null;
1090
- }
1091
- // console.log("parse_dec_str:-3:", str, part);
1092
- return part;
1093
- }
1094
- static parse_hex_str(str) {
1095
- let re_digit = new RegExp("^[0-9a-fA-F]+$");
1096
- if (!re_digit.test(str)) {
1097
- return null;
1098
- }
1099
- let part = parseInt(str, 16);
1100
- if (isNaN(part)) {
1101
- return null;
1102
- }
1103
- return part;
1104
- }
1105
- // Checks if the given string is a valid IPv4 address
1106
- //
1107
- // Example:
1108
- //
1109
- // IPAddress.valid_ipv4? "2002.1"
1110
- // //=> false
1111
- //
1112
- // IPAddress.valid_ipv4? "172.16.10.1"
1113
- // //=> true
1114
- //
1115
- static parse_ipv4_part(i) {
1116
- let part = IPAddress.parse_dec_str(i);
1117
- //console.log("i=", i, part);
1118
- if (part === null || part >= 256) {
1119
- return null;
1120
- }
1121
- return part;
1122
- }
1123
- static split_to_u32(addr) {
1124
- let ip = crunchy_1.default.zero();
1125
- let shift = 24;
1126
- let split_addr = addr.split(".");
1127
- if (split_addr.length > 4) {
1128
- return null;
1129
- }
1130
- let split_addr_len = split_addr.length;
1131
- if (split_addr_len < 4) {
1132
- let part = IPAddress.parse_ipv4_part(split_addr[split_addr_len - 1]);
1133
- if (part === null) {
1134
- return null;
1135
- }
1136
- ip = crunchy_1.default.from_number(part);
1137
- split_addr = split_addr.slice(0, split_addr_len - 1);
1138
- }
1139
- for (let i of split_addr) {
1140
- let part = IPAddress.parse_ipv4_part(i);
1141
- // console.log("u32-", addr, i, part);
1142
- if (part === null) {
1143
- return null;
1144
- }
1145
- //println!("{}-{}", part_num, shift);
1146
- ip = ip.add(crunchy_1.default.from_number(part).shl(shift));
1147
- shift -= 8;
1148
- }
1149
- return ip;
1150
- }
1151
- static is_valid_ipv4(addr) {
1152
- return IPAddress.split_to_u32(addr) !== null;
1153
- }
1154
- // Checks if the given string is a valid IPv6 address
1155
- //
1156
- // Example:
1157
- //
1158
- // IPAddress.valid_ipv6? "2002.1"
1159
- // //=> true
1160
- //
1161
- // IPAddress.valid_ipv6? "2002.DEAD.BEEF"
1162
- // //=> false
1163
- //
1164
- static split_on_colon(addr) {
1165
- let parts = addr.trim().split(":");
1166
- let ip = crunchy_1.default.zero();
1167
- if (parts.length == 1 && parts[0].length == 0) {
1168
- return new ResultCrunchyParts(ip, 0);
1169
- }
1170
- let parts_len = parts.length;
1171
- let shift = (parts_len - 1) * 16;
1172
- for (let i of parts) {
1173
- //println!("{}={}", addr, i);
1174
- let part = IPAddress.parse_hex_str(i);
1175
- if (part === null || part >= 65536) {
1176
- return null;
1177
- }
1178
- ip = ip.add(crunchy_1.default.from_number(part).shl(shift));
1179
- shift -= 16;
1180
- }
1181
- return new ResultCrunchyParts(ip, parts_len);
1182
- }
1183
- static split_to_num(addr) {
1184
- //let ip = 0;
1185
- let pre_post = addr.trim().split("::");
1186
- if (pre_post.length > 2) {
1187
- return null;
1188
- }
1189
- if (pre_post.length == 2) {
1190
- //println!("{}=.={}", pre_post[0], pre_post[1]);
1191
- let pre = IPAddress.split_on_colon(pre_post[0]);
1192
- if (!pre) {
1193
- return pre;
1194
- }
1195
- let post = IPAddress.split_on_colon(pre_post[1]);
1196
- if (!post) {
1197
- return post;
1198
- }
1199
- // println!("pre:{} post:{}", pre_parts, post_parts);
1200
- return new ResultCrunchyParts(pre.crunchy.shl(128 - pre.parts * 16).add(post.crunchy), 128 / 16);
1201
- }
1202
- //println!("split_to_num:no double:{}", addr);
1203
- let ret = IPAddress.split_on_colon(addr);
1204
- if (ret == null || ret.parts != 128 / 16) {
1205
- return null;
1206
- }
1207
- return ret;
1208
- }
1209
- static is_valid_ipv6(addr) {
1210
- return IPAddress.split_to_num(addr) != null;
1211
- }
1212
- // private helper for summarize
1213
- // assumes that networks is output from reduce_networks
1214
- // means it should be sorted lowers first and uniq
1215
- //
1216
- static pos_to_idx(pos, len) {
1217
- let ilen = len;
1218
- // let ret = pos % ilen;
1219
- let rem = (pos % ilen + ilen) % ilen;
1220
- // println!("pos_to_idx:{}:{}=>{}:{}", pos, len, ret, rem);
1221
- return rem;
1222
- }
1223
- static aggregate(networks) {
1224
- if (networks.length == 0) {
1225
- return [];
1226
- }
1227
- if (networks.length == 1) {
1228
- // console.log("aggregate:", networks[0], networks[0].network());
1229
- return [networks[0].network()];
1230
- }
1231
- let stack = networks.map(i => i.network()).sort((a, b) => a.cmp(b));
1232
- // console.log(IPAddress.to_string_vec(stack));
1233
- // for i in 0..networks.length {
1234
- // println!("{}==={}", &networks[i].to_string_uncompressed(),
1235
- // &stack[i].to_string_uncompressed());
1236
- // }
1237
- for (let pos = 0; true;) {
1238
- if (pos < 0) {
1239
- pos = 0;
1240
- }
1241
- let stack_len = stack.length; // borrow checker
1242
- // println!("loop:{}:{}", pos, stack_len);
1243
- // if stack_len == 1 {
1244
- // println!("exit 1");
1245
- // break;
1246
- // }
1247
- if (pos >= stack_len) {
1248
- // println!("exit first:{}:{}", stack_len, pos);
1249
- break;
1250
- }
1251
- let first = IPAddress.pos_to_idx(pos, stack_len);
1252
- pos = pos + 1;
1253
- if (pos >= stack_len) {
1254
- // println!("exit second:{}:{}", stack_len, pos);
1255
- break;
1256
- }
1257
- let second = IPAddress.pos_to_idx(pos, stack_len);
1258
- pos = pos + 1;
1259
- //let firstUnwrap = first;
1260
- if (stack[first].includes(stack[second])) {
1261
- pos = pos - 2;
1262
- // println!("remove:1:{}:{}:{}=>{}", first, second, stack_len, pos + 1);
1263
- let pidx = IPAddress.pos_to_idx(pos + 1, stack_len);
1264
- stack = stack.slice(0, pidx).concat(stack.slice(pidx + 1));
1265
- } else {
1266
- stack[first].prefix = stack[first].prefix.sub(1);
1267
- // println!("complex:{}:{}:{}:{}:P1:{}:P2:{}", pos, stack_len,
1268
- // first, second,
1269
- // stack[first].to_string(), stack[second].to_string());
1270
- if (stack[first].prefix.num + 1 == stack[second].prefix.num && stack[first].includes(stack[second])) {
1271
- pos = pos - 2;
1272
- let idx = IPAddress.pos_to_idx(pos, stack_len);
1273
- stack[idx] = stack[first].clone(); // kaputt
1274
- let pidx = IPAddress.pos_to_idx(pos + 1, stack_len);
1275
- stack = stack.slice(0, pidx).concat(stack.slice(pidx + 1));
1276
- // println!("remove-2:{}:{}", pos + 1, stack_len);
1277
- pos = pos - 1; // backtrack
1278
- } else {
1279
- stack[first].prefix = stack[first].prefix.add(1); //reset prefix
1280
- // println!("easy:{}:{}=>{}", pos, stack_len, stack[first].to_string());
1281
- pos = pos - 1; // do it with second as first
1282
- }
1283
- }
1284
- }
1285
- // println!("agg={}:{}", pos, stack.length);
1286
- return stack.slice(0, stack.length);
1287
- }
1288
- parts() {
1289
- return this.ip_bits.parts(this.host_address);
1290
- }
1291
- parts_hex_str() {
1292
- let ret = [];
1293
- let leading = 1 << this.ip_bits.part_bits;
1294
- for (let i of this.parts()) {
1295
- ret.push((leading + i).toString(16).slice(1));
1296
- }
1297
- return ret;
1298
- }
1299
- // Returns the IP address in in-addr.arpa format
1300
- // for DNS Domain definition entries like SOA Records
1301
- //
1302
- // ip = IPAddress("172.17.100.50/15")
1303
- //
1304
- // ip.dns_rev_domains
1305
- // // => ["16.172.in-addr.arpa","17.172.in-addr.arpa"]
1306
- //
1307
- dns_rev_domains() {
1308
- let ret = [];
1309
- for (let net of this.dns_networks()) {
1310
- // console.log("dns_rev_domains:", this.to_string(), net.to_string());
1311
- ret.push(net.dns_reverse());
1312
- }
1313
- return ret;
1314
- }
1315
- dns_reverse() {
1316
- let ret = "";
1317
- let dot = "";
1318
- let dns_parts = this.dns_parts();
1319
- for (let i = ~~((this.prefix.host_prefix() + (this.ip_bits.dns_bits - 1)) / this.ip_bits.dns_bits); i < this.dns_parts().length; ++i) {
1320
- // console.log("dns_r", i);
1321
- ret += dot;
1322
- ret += this.ip_bits.dns_part_format(dns_parts[i]);
1323
- dot = ".";
1324
- }
1325
- ret += dot;
1326
- ret += this.ip_bits.rev_domain;
1327
- return ret;
1328
- }
1329
- dns_parts() {
1330
- let ret = [];
1331
- let num = this.host_address.clone();
1332
- let mask = crunchy_1.default.one().shl(this.ip_bits.dns_bits);
1333
- for (let _ = 0; _ < this.ip_bits.bits / this.ip_bits.dns_bits; _++) {
1334
- let part = +num.clone().mod(mask).toString();
1335
- num = num.shr(this.ip_bits.dns_bits);
1336
- ret.push(part);
1337
- }
1338
- return ret;
1339
- }
1340
- dns_networks() {
1341
- // +this.ip_bits.dns_bits-1
1342
- let next_bit_mask = this.ip_bits.bits - ~~(this.prefix.host_prefix() / this.ip_bits.dns_bits) * this.ip_bits.dns_bits;
1343
- // console.log("dns_networks-1", this.to_string(), this.prefix.host_prefix();j
1344
- // this.ip_bits.dns_bits, next_bit_mask);
1345
- if (next_bit_mask <= 0) {
1346
- return [this.network()];
1347
- }
1348
- // println!("dns_networks:{}:{}", this.to_string(), next_bit_mask);
1349
- // dns_bits
1350
- let step_bit_net = crunchy_1.default.one().shl(this.ip_bits.bits - next_bit_mask);
1351
- if (step_bit_net.eq(crunchy_1.default.zero())) {
1352
- // console.log("dns_networks-2", this.to_string());
1353
- return [this.network()];
1354
- }
1355
- let ret = [];
1356
- let step = this.network().host_address;
1357
- let prefix = this.prefix.from(next_bit_mask);
1358
- while (step.lte(this.broadcast().host_address)) {
1359
- // console.log("dns_networks-3", this.to_string(), step.toString(), next_bit_mask, step_bit_net.toString());
1360
- ret.push(this.from(step, prefix));
1361
- step = step.add(step_bit_net);
1362
- }
1363
- return ret;
1364
- }
1365
- // Summarization (or aggregation) is the process when two or more
1366
- // networks are taken together to check if a supernet, including all
1367
- // and only these networks, exists. If it exists then this supernet
1368
- // is called the summarized (or aggregated) network.
1369
- //
1370
- // It is very important to understand that summarization can only
1371
- // occur if there are no holes in the aggregated network, or, in other
1372
- // words, if the given networks fill completely the address space
1373
- // of the supernet. So the two rules are:
1374
- //
1375
- // 1) The aggregate network must contain +all+ the IP addresses of the
1376
- // original networks;
1377
- // 2) The aggregate network must contain +only+ the IP addresses of the
1378
- // original networks;
1379
- //
1380
- // A few examples will help clarify the above. Let's consider for
1381
- // instance the following two networks:
1382
- //
1383
- // ip1 = IPAddress("172.16.10.0/24")
1384
- // ip2 = IPAddress("172.16.11.0/24")
1385
- //
1386
- // These two networks can be expressed using only one IP address
1387
- // network if we change the prefix. Let Ruby do the work:
1388
- //
1389
- // IPAddress.IPv4.summarize(ip1,ip2).to_s
1390
- // //=> "172.16.10.0/23"
1391
- //
1392
- // We note how the network "172.16.10.0/23" includes all the addresses
1393
- // specified in the above networks, and (more important) includes
1394
- // ONLY those addresses.
1395
- //
1396
- // If we summarized +ip1+ and +ip2+ with the following network:
1397
- //
1398
- // "172.16.0.0/16"
1399
- //
1400
- // we would have satisfied rule //1 above, but not rule //2. So "172.16.0.0/16"
1401
- // is not an aggregate network for +ip1+ and +ip2+.
1402
- //
1403
- // If it's not possible to compute a single aggregated network for all the
1404
- // original networks, the method returns an array with all the aggregate
1405
- // networks found. For example, the following four networks can be
1406
- // aggregated in a single /22:
1407
- //
1408
- // ip1 = IPAddress("10.0.0.1/24")
1409
- // ip2 = IPAddress("10.0.1.1/24")
1410
- // ip3 = IPAddress("10.0.2.1/24")
1411
- // ip4 = IPAddress("10.0.3.1/24")
1412
- //
1413
- // IPAddress.IPv4.summarize(ip1,ip2,ip3,ip4).to_string
1414
- // //=> "10.0.0.0/22",
1415
- //
1416
- // But the following networks can't be summarized in a single network:
1417
- //
1418
- // ip1 = IPAddress("10.0.1.1/24")
1419
- // ip2 = IPAddress("10.0.2.1/24")
1420
- // ip3 = IPAddress("10.0.3.1/24")
1421
- // ip4 = IPAddress("10.0.4.1/24")
1422
- //
1423
- // IPAddress.IPv4.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
1424
- // //=> ["10.0.1.0/24","10.0.2.0/23","10.0.4.0/24"]
1425
- //
1426
- //
1427
- // Summarization (or aggregation) is the process when two or more
1428
- // networks are taken together to check if a supernet, including all
1429
- // and only these networks, exists. If it exists then this supernet
1430
- // is called the summarized (or aggregated) network.
1431
- //
1432
- // It is very important to understand that summarization can only
1433
- // occur if there are no holes in the aggregated network, or, in other
1434
- // words, if the given networks fill completely the address space
1435
- // of the supernet. So the two rules are:
1436
- //
1437
- // 1) The aggregate network must contain +all+ the IP addresses of the
1438
- // original networks;
1439
- // 2) The aggregate network must contain +only+ the IP addresses of the
1440
- // original networks;
1441
- //
1442
- // A few examples will help clarify the above. Let's consider for
1443
- // instance the following two networks:
1444
- //
1445
- // ip1 = IPAddress("2000:0.4/32")
1446
- // ip2 = IPAddress("2000:1.6/32")
1447
- //
1448
- // These two networks can be expressed using only one IP address
1449
- // network if we change the prefix. Let Ruby do the work:
1450
- //
1451
- // IPAddress.IPv6.summarize(ip1,ip2).to_s
1452
- // // => "2000:0./31"
1453
- //
1454
- // We note how the network "2000:0./31" includes all the addresses
1455
- // specified in the above networks, and (more important) includes
1456
- // ONLY those addresses.
1457
- //
1458
- // If we summarized +ip1+ and +ip2+ with the following network:
1459
- //
1460
- // "2000./16"
1461
- //
1462
- // we would have satisfied rule // 1 above, but not rule // 2. So "2000./16"
1463
- // is not an aggregate network for +ip1+ and +ip2+.
1464
- //
1465
- // If it's not possible to compute a single aggregated network for all the
1466
- // original networks, the method returns an array with all the aggregate
1467
- // networks found. For example, the following four networks can be
1468
- // aggregated in a single /22:
1469
- //
1470
- // ip1 = IPAddress("2000:0./32")
1471
- // ip2 = IPAddress("2000:1./32")
1472
- // ip3 = IPAddress("2000:2./32")
1473
- // ip4 = IPAddress("2000:3./32")
1474
- //
1475
- // IPAddress.IPv6.summarize(ip1,ip2,ip3,ip4).to_string
1476
- // // => ""2000:3./30",
1477
- //
1478
- // But the following networks can't be summarized in a single network:
1479
- //
1480
- // ip1 = IPAddress("2000:1./32")
1481
- // ip2 = IPAddress("2000:2./32")
1482
- // ip3 = IPAddress("2000:3./32")
1483
- // ip4 = IPAddress("2000:4./32")
1484
- //
1485
- // IPAddress.IPv4.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
1486
- // // => ["2000:1./32","2000:2./31","2000:4./32"]
1487
- //
1488
- static summarize(networks) {
1489
- return IPAddress.aggregate(networks);
1490
- }
1491
- static summarize_str(netstr) {
1492
- let vec = IPAddress.to_ipaddress_vec(netstr);
1493
- // console.log(netstr, vec);
1494
- if (!vec) {
1495
- return vec;
1496
- }
1497
- return IPAddress.aggregate(vec);
1498
- }
1499
- ip_same_kind(oth) {
1500
- return this.ip_bits.version == oth.ip_bits.version;
1501
- }
1502
- // Returns true if the address is an unspecified address
1503
- //
1504
- // See IPAddress.IPv6.Unspecified for more information
1505
- //
1506
- is_unspecified() {
1507
- return this.host_address.eq(crunchy_1.default.zero());
1508
- }
1509
- // Returns true if the address is a loopback address
1510
- //
1511
- // See IPAddress.IPv6.Loopback for more information
1512
- //
1513
- is_loopback() {
1514
- return this.vt_is_loopback(this);
1515
- }
1516
- // Returns true if the address is a mapped address
1517
- //
1518
- // See IPAddress.IPv6.Mapped for more information
1519
- //
1520
- is_mapped() {
1521
- let ret = this.mapped !== null && this.host_address.shr(32).eq(crunchy_1.default.one().shl(16).sub(crunchy_1.default.one()));
1522
- // console.log("+++++++++++", this.mapped, ret);
1523
- return ret;
1524
- }
1525
- // Returns the prefix portion of the IPv4 object
1526
- // as a IPAddress.Prefix32 object
1527
- //
1528
- // ip = IPAddress("172.16.100.4/22")
1529
- //
1530
- // ip.prefix
1531
- // // => 22
1532
- //
1533
- // ip.prefix.class
1534
- // // => IPAddress.Prefix32
1535
- //
1536
- // public prefix(): Prefix {
1537
- // return this.prefix;
1538
- // }
1539
- // Checks if the argument is a valid IPv4 netmask
1540
- // expressed in dotted decimal format.
1541
- //
1542
- // IPAddress.valid_ipv4_netmask? "255.255.0.0"
1543
- // //=> true
1544
- //
1545
- static is_valid_netmask(addr) {
1546
- return IPAddress.parse_netmask_to_prefix(addr) !== null;
1547
- }
1548
- static netmask_to_prefix(nm, bits) {
1549
- let prefix = 0;
1550
- let addr = nm.clone();
1551
- let in_host_part = true;
1552
- // let two = Crunchy.two();
1553
- for (let _ = 0; _ < bits; _++) {
1554
- let bit = addr.mds(2);
1555
- // console.log(">>>", bits, bit, addr, nm);
1556
- if (in_host_part && bit == 0) {
1557
- prefix = prefix + 1;
1558
- } else if (in_host_part && bit == 1) {
1559
- in_host_part = false;
1560
- } else if (!in_host_part && bit == 0) {
1561
- return null;
1562
- }
1563
- addr = addr.shr(1);
1564
- }
1565
- return bits - prefix;
1566
- }
1567
- static parse_netmask_to_prefix(netmask) {
1568
- // console.log("--1", netmask);
1569
- let is_number = IPAddress.parse_dec_str(netmask);
1570
- if (is_number !== null) {
1571
- // console.log("--2", netmask, is_number);
1572
- return is_number;
1573
- }
1574
- let my = IPAddress.parse(netmask);
1575
- // console.log("--3", netmask, my);
1576
- if (!my) {
1577
- // console.log("--4", netmask, my);
1578
- return null;
1579
- }
1580
- // console.log("--5", netmask, my);
1581
- let my_ip = my;
1582
- return IPAddress.netmask_to_prefix(my_ip.host_address, my_ip.ip_bits.bits);
1583
- }
1584
- // Set a new prefix number for the object
1585
- //
1586
- // This is useful if you want to change the prefix
1587
- // to an object created with IPv4.parse_u32 or
1588
- // if the object was created using the classful
1589
- // mask.
1590
- //
1591
- // ip = IPAddress("172.16.100.4")
1592
- //
1593
- // puts ip
1594
- // // => 172.16.100.4/16
1595
- //
1596
- // ip.prefix = 22
1597
- //
1598
- // puts ip
1599
- // // => 172.16.100.4/22
1600
- //
1601
- change_prefix(num) {
1602
- let prefix = this.prefix.from(num);
1603
- if (!prefix) {
1604
- return null;
1605
- }
1606
- return this.from(this.host_address, prefix);
1607
- }
1608
- change_netmask(str) {
1609
- let nm = IPAddress.parse_netmask_to_prefix(str);
1610
- if (!nm) {
1611
- return null;
1612
- }
1613
- return this.change_prefix(nm);
1614
- }
1615
- // Returns a string with the IP address in canonical
1616
- // form.
1617
- //
1618
- // ip = IPAddress("172.16.100.4/22")
1619
- //
1620
- // ip.to_string
1621
- // // => "172.16.100.4/22"
1622
- //
1623
- to_string() {
1624
- let ret = "";
1625
- ret += this.to_s();
1626
- ret += "/";
1627
- ret += this.prefix.to_s();
1628
- return ret;
1629
- }
1630
- to_s() {
1631
- return this.ip_bits.as_compressed_string(this.host_address);
1632
- }
1633
- to_string_uncompressed() {
1634
- let ret = "";
1635
- ret += this.to_s_uncompressed();
1636
- ret += "/";
1637
- ret += this.prefix.to_s();
1638
- return ret;
1639
- }
1640
- to_s_uncompressed() {
1641
- return this.ip_bits.as_uncompressed_string(this.host_address);
1642
- }
1643
- to_s_mapped() {
1644
- if (this.is_mapped()) {
1645
- return `::ffff:${ this.mapped.to_s() }`;
1646
- }
1647
- return this.to_s();
1648
- }
1649
- to_string_mapped() {
1650
- if (this.is_mapped()) {
1651
- let mapped = this.mapped.clone();
1652
- return `${ this.to_s_mapped() }/${ mapped.prefix.num }`;
1653
- }
1654
- return this.to_string();
1655
- }
1656
- // Returns the address portion of an IP in binary format,
1657
- // as a string containing a sequence of 0 and 1
1658
- //
1659
- // ip = IPAddress("127.0.0.1")
1660
- //
1661
- // ip.bits
1662
- // // => "01111111000000000000000000000001"
1663
- //
1664
- bits() {
1665
- let num = this.host_address.toString(2);
1666
- let ret = "";
1667
- for (let _ = num.length; _ < this.ip_bits.bits; _++) {
1668
- ret += "0";
1669
- }
1670
- ret += num;
1671
- return ret;
1672
- }
1673
- to_hex() {
1674
- return this.host_address.toString(16);
1675
- }
1676
- netmask() {
1677
- return this.from(this.prefix.netmask(), this.prefix);
1678
- }
1679
- // Returns the broadcast address for the given IP.
1680
- //
1681
- // ip = IPAddress("172.16.10.64/24")
1682
- //
1683
- // ip.broadcast.to_s
1684
- // // => "172.16.10.255"
1685
- //
1686
- broadcast() {
1687
- return this.from(this.network().host_address.add(this.size().sub(crunchy_1.default.one())), this.prefix);
1688
- // IPv4.parse_u32(this.broadcast_u32, this.prefix)
1689
- }
1690
- // Checks if the IP address is actually a network
1691
- //
1692
- // ip = IPAddress("172.16.10.64/24")
1693
- //
1694
- // ip.network?
1695
- // // => false
1696
- //
1697
- // ip = IPAddress("172.16.10.64/26")
1698
- //
1699
- // ip.network?
1700
- // // => true
1701
- //
1702
- is_network() {
1703
- return this.prefix.num != this.ip_bits.bits && this.host_address.eq(this.network().host_address);
1704
- }
1705
- // Returns a new IPv4 object with the network number
1706
- // for the given IP.
1707
- //
1708
- // ip = IPAddress("172.16.10.64/24")
1709
- //
1710
- // ip.network.to_s
1711
- // // => "172.16.10.0"
1712
- //
1713
- network() {
1714
- return this.from(IPAddress.to_network(this.host_address, this.prefix.host_prefix()), this.prefix);
1715
- }
1716
- static to_network(adr, host_prefix) {
1717
- return adr.shr(host_prefix).shl(host_prefix);
1718
- }
1719
- sub(other) {
1720
- if (this.host_address.gt(other.host_address)) {
1721
- return this.host_address.clone().sub(other.host_address);
1722
- }
1723
- return other.host_address.clone().sub(this.host_address);
1724
- }
1725
- add(other) {
1726
- return IPAddress.aggregate([this.clone(), other.clone()]);
1727
- }
1728
- static to_s_vec(vec) {
1729
- let ret = [];
1730
- for (let i of vec) {
1731
- ret.push(i.to_s());
1732
- }
1733
- return ret;
1734
- }
1735
- static to_string_vec(vec) {
1736
- let ret = [];
1737
- for (let i of vec) {
1738
- ret.push(i.to_string());
1739
- }
1740
- return ret;
1741
- }
1742
- static to_ipaddress_vec(vec) {
1743
- let ret = [];
1744
- for (let ipstr of vec) {
1745
- let ipa = IPAddress.parse(ipstr);
1746
- if (!ipa) {
1747
- return null;
1748
- }
1749
- ret.push(ipa);
1750
- }
1751
- return ret;
1752
- }
1753
- // Returns a new IPv4 object with the
1754
- // first host IP address in the range.
1755
- //
1756
- // Example: given the 192.168.100.0/24 network, the first
1757
- // host IP address is 192.168.100.1.
1758
- //
1759
- // ip = IPAddress("192.168.100.0/24")
1760
- //
1761
- // ip.first.to_s
1762
- // // => "192.168.100.1"
1763
- //
1764
- // The object IP doesn't need to be a network: the method
1765
- // automatically gets the network number from it
1766
- //
1767
- // ip = IPAddress("192.168.100.50/24")
1768
- //
1769
- // ip.first.to_s
1770
- // // => "192.168.100.1"
1771
- //
1772
- first() {
1773
- return this.from(this.network().host_address.add(this.ip_bits.host_ofs), this.prefix);
1774
- }
1775
- // Like its sibling method IPv4// first, this method
1776
- // returns a new IPv4 object with the
1777
- // last host IP address in the range.
1778
- //
1779
- // Example: given the 192.168.100.0/24 network, the last
1780
- // host IP address is 192.168.100.254
1781
- //
1782
- // ip = IPAddress("192.168.100.0/24")
1783
- //
1784
- // ip.last.to_s
1785
- // // => "192.168.100.254"
1786
- //
1787
- // The object IP doesn't need to be a network: the method
1788
- // automatically gets the network number from it
1789
- //
1790
- // ip = IPAddress("192.168.100.50/24")
1791
- //
1792
- // ip.last.to_s
1793
- // // => "192.168.100.254"
1794
- //
1795
- last() {
1796
- return this.from(this.broadcast().host_address.sub(this.ip_bits.host_ofs), this.prefix);
1797
- }
1798
- // Iterates over all the hosts IP addresses for the given
1799
- // network (or IP address).
1800
- //
1801
- // ip = IPAddress("10.0.0.1/29")
1802
- //
1803
- // ip.each_host do |i|
1804
- // p i.to_s
1805
- // end
1806
- // // => "10.0.0.1"
1807
- // // => "10.0.0.2"
1808
- // // => "10.0.0.3"
1809
- // // => "10.0.0.4"
1810
- // // => "10.0.0.5"
1811
- // // => "10.0.0.6"
1812
- //
1813
- each_host(func) {
1814
- let i = this.first().host_address;
1815
- while (i.lte(this.last().host_address)) {
1816
- func(this.from(i, this.prefix));
1817
- i = i.add(crunchy_1.default.one());
1818
- }
1819
- }
1820
- inc() {
1821
- let ret = this.clone();
1822
- ret.host_address = ret.host_address.add(crunchy_1.default.one());
1823
- if (ret.lte(this.last())) {
1824
- return ret;
1825
- }
1826
- return null;
1827
- }
1828
- dec() {
1829
- let ret = this.clone();
1830
- ret.host_address = ret.host_address.sub(crunchy_1.default.one());
1831
- if (ret.gte(this.first())) {
1832
- return ret;
1833
- }
1834
- return null;
1835
- }
1836
- // Iterates over all the IP addresses for the given
1837
- // network (or IP address).
1838
- //
1839
- // The object yielded is a new IPv4 object created
1840
- // from the iteration.
1841
- //
1842
- // ip = IPAddress("10.0.0.1/29")
1843
- //
1844
- // ip.each do |i|
1845
- // p i.address
1846
- // end
1847
- // // => "10.0.0.0"
1848
- // // => "10.0.0.1"
1849
- // // => "10.0.0.2"
1850
- // // => "10.0.0.3"
1851
- // // => "10.0.0.4"
1852
- // // => "10.0.0.5"
1853
- // // => "10.0.0.6"
1854
- // // => "10.0.0.7"
1855
- //
1856
- each(func) {
1857
- let i = this.network().host_address;
1858
- while (i <= this.broadcast().host_address) {
1859
- func(this.from(i, this.prefix));
1860
- i = i.add(crunchy_1.default.one());
1861
- }
1862
- }
1863
- // Spaceship operator to compare IPv4 objects
1864
- //
1865
- // Comparing IPv4 addresses is useful to ordinate
1866
- // them into lists that match our intuitive
1867
- // perception of ordered IP addresses.
1868
- //
1869
- // The first comparison criteria is the u32 value.
1870
- // For example, 10.100.100.1 will be considered
1871
- // to be less than 172.16.0.1, because, in a ordered list,
1872
- // we expect 10.100.100.1 to come before 172.16.0.1.
1873
- //
1874
- // The second criteria, in case two IPv4 objects
1875
- // have identical addresses, is the prefix. An higher
1876
- // prefix will be considered greater than a lower
1877
- // prefix. This is because we expect to see
1878
- // 10.100.100.0/24 come before 10.100.100.0/25.
1879
- //
1880
- // Example:
1881
- //
1882
- // ip1 = IPAddress "10.100.100.1/8"
1883
- // ip2 = IPAddress "172.16.0.1/16"
1884
- // ip3 = IPAddress "10.100.100.1/16"
1885
- //
1886
- // ip1 < ip2
1887
- // // => true
1888
- // ip1 > ip3
1889
- // // => false
1890
- //
1891
- // [ip1,ip2,ip3].sort.map{|i| i.to_string}
1892
- // // => ["10.100.100.1/8","10.100.100.1/16","172.16.0.1/16"]
1893
- //
1894
- // Returns the number of IP addresses included
1895
- // in the network. It also counts the network
1896
- // address and the broadcast address.
1897
- //
1898
- // ip = IPAddress("10.0.0.1/29")
1899
- //
1900
- // ip.size
1901
- // // => 8
1902
- //
1903
- size() {
1904
- return crunchy_1.default.one().shl(this.prefix.host_prefix());
1905
- }
1906
- is_same_kind(oth) {
1907
- return this.is_ipv4() == oth.is_ipv4() && this.is_ipv6() == oth.is_ipv6();
1908
- }
1909
- // Checks whether a subnet includes the given IP address.
1910
- //
1911
- // Accepts an IPAddress.IPv4 object.
1912
- //
1913
- // ip = IPAddress("192.168.10.100/24")
1914
- //
1915
- // addr = IPAddress("192.168.10.102/24")
1916
- //
1917
- // ip.include? addr
1918
- // // => true
1919
- //
1920
- // ip.include? IPAddress("172.16.0.48/16")
1921
- // // => false
1922
- //
1923
- includes(oth) {
1924
- let ret = this.is_same_kind(oth) && this.prefix.num <= oth.prefix.num && this.network().host_address.eq(IPAddress.to_network(oth.host_address, this.prefix.host_prefix()));
1925
- // println!("includes:{}=={}=>{}", this.to_string(), oth.to_string(), ret);
1926
- return ret;
1927
- }
1928
- // Checks whether a subnet includes all the
1929
- // given IPv4 objects.
1930
- //
1931
- // ip = IPAddress("192.168.10.100/24")
1932
- //
1933
- // addr1 = IPAddress("192.168.10.102/24")
1934
- // addr2 = IPAddress("192.168.10.103/24")
1935
- //
1936
- // ip.include_all?(addr1,addr2)
1937
- // // => true
1938
- //
1939
- includes_all(oths) {
1940
- for (let oth of oths) {
1941
- if (!this.includes(oth)) {
1942
- return false;
1943
- }
1944
- }
1945
- return true;
1946
- }
1947
- // Checks if an IPv4 address objects belongs
1948
- // to a private network RFC1918
1949
- //
1950
- // Example:
1951
- //
1952
- // ip = IPAddress "10.1.1.1/24"
1953
- // ip.private?
1954
- // // => true
1955
- //
1956
- is_private() {
1957
- return this.vt_is_private(this);
1958
- }
1959
- // Splits a network into different subnets
1960
- //
1961
- // If the IP Address is a network, it can be divided into
1962
- // multiple networks. If +self+ is not a network, this
1963
- // method will calculate the network from the IP and then
1964
- // subnet it.
1965
- //
1966
- // If +subnets+ is an power of two number, the resulting
1967
- // networks will be divided evenly from the supernet.
1968
- //
1969
- // network = IPAddress("172.16.10.0/24")
1970
- //
1971
- // network / 4 // implies map{|i| i.to_string}
1972
- // // => ["172.16.10.0/26",
1973
- // "172.16.10.64/26",
1974
- // "172.16.10.128/26",
1975
- // "172.16.10.192/26"]
1976
- //
1977
- // If +num+ is any other number, the supernet will be
1978
- // divided into some networks with a even number of hosts and
1979
- // other networks with the remaining addresses.
1980
- //
1981
- // network = IPAddress("172.16.10.0/24")
1982
- //
1983
- // network / 3 // implies map{|i| i.to_string}
1984
- // // => ["172.16.10.0/26",
1985
- // "172.16.10.64/26",
1986
- // "172.16.10.128/25"]
1987
- //
1988
- // Returns an array of IPv4 objects
1989
- //
1990
- sum_first_found(arr) {
1991
- let dup = arr.slice();
1992
- if (dup.length < 2) {
1993
- return dup;
1994
- }
1995
- for (let i = dup.length - 2; i >= 0; --i) {
1996
- // console.log("sum_first_found:", dup[i], dup[i + 1]);
1997
- let a = IPAddress.summarize([dup[i], dup[i + 1]]);
1998
- // println!("dup:{}:{}:{}", dup.length, i, a.length);
1999
- if (a.length == 1) {
2000
- dup[i] = a[0];
2001
- dup = dup.slice(0, i + 1).concat(dup.slice(i + 2));
2002
- break;
2003
- }
2004
- }
2005
- return dup;
2006
- }
2007
- split(subnets) {
2008
- if (subnets == 0 || 1 << this.prefix.host_prefix() <= subnets) {
2009
- return null;
2010
- }
2011
- let networks = this.subnet(this.newprefix(subnets).num);
2012
- if (!networks) {
2013
- return networks;
2014
- }
2015
- let net = networks;
2016
- while (net.length != subnets) {
2017
- net = this.sum_first_found(net);
2018
- }
2019
- return net;
2020
- }
2021
- // alias_method :/, :split
2022
- // Returns a new IPv4 object from the supernetting
2023
- // of the instance network.
2024
- //
2025
- // Supernetting is similar to subnetting, except
2026
- // that you getting as a result a network with a
2027
- // smaller prefix (bigger host space). For example,
2028
- // given the network
2029
- //
2030
- // ip = IPAddress("172.16.10.0/24")
2031
- //
2032
- // you can supernet it with a new /23 prefix
2033
- //
2034
- // ip.supernet(23).to_string
2035
- // // => "172.16.10.0/23"
2036
- //
2037
- // However if you supernet it with a /22 prefix, the
2038
- // network address will change:
2039
- //
2040
- // ip.supernet(22).to_string
2041
- // // => "172.16.8.0/22"
2042
- //
2043
- // If +new_prefix+ is less than 1, returns 0.0.0.0/0
2044
- //
2045
- supernet(new_prefix) {
2046
- if (new_prefix >= this.prefix.num) {
2047
- return null;
2048
- }
2049
- // let new_ip = this.host_address.clone();
2050
- // for _ in new_prefix..this.prefix.num {
2051
- // new_ip = new_ip << 1;
2052
- // }
2053
- return this.from(this.host_address, this.prefix.from(new_prefix)).network();
2054
- }
2055
- // This method implements the subnetting function
2056
- // similar to the one described in RFC3531.
2057
- //
2058
- // By specifying a new prefix, the method calculates
2059
- // the network number for the given IPv4 object
2060
- // and calculates the subnets associated to the new
2061
- // prefix.
2062
- //
2063
- // For example, given the following network:
2064
- //
2065
- // ip = IPAddress "172.16.10.0/24"
2066
- //
2067
- // we can calculate the subnets with a /26 prefix
2068
- //
2069
- // ip.subnets(26).map(:to_string)
2070
- // // => ["172.16.10.0/26", "172.16.10.64/26",
2071
- // "172.16.10.128/26", "172.16.10.192/26"]
2072
- //
2073
- // The resulting number of subnets will of course always be
2074
- // a power of two.
2075
- //
2076
- subnet(subprefix) {
2077
- if (subprefix < this.prefix.num || this.ip_bits.bits < subprefix) {
2078
- return null;
2079
- }
2080
- let ret = [];
2081
- let net = this.network();
2082
- net.prefix = net.prefix.from(subprefix);
2083
- for (let _ = 0; _ < 1 << subprefix - this.prefix.num; _++) {
2084
- ret.push(net.clone());
2085
- net = net.from(net.host_address, net.prefix);
2086
- let size = net.size();
2087
- net.host_address = net.host_address.add(size);
2088
- }
2089
- return ret;
2090
- }
2091
- // Return the ip address in a format compatible
2092
- // with the IPv6 Mapped IPv4 addresses
2093
- //
2094
- // Example:
2095
- //
2096
- // ip = IPAddress("172.16.10.1/24")
2097
- //
2098
- // ip.to_ipv6
2099
- // // => "ac10:0a01"
2100
- //
2101
- to_ipv6() {
2102
- return this.vt_to_ipv6(this);
2103
- }
2104
- newprefix(num) {
2105
- for (let i = num; i < this.ip_bits.bits; ++i) {
2106
- let a = ~~Math.log2(i);
2107
- if (a == Math.log2(i)) {
2108
- return this.prefix.add(a);
2109
- }
2110
- }
2111
- return null;
2112
- }
2113
- }
2114
- exports.IPAddress = IPAddress;
2115
- Object.defineProperty(exports, "__esModule", { value: true });
2116
- exports.default = IPAddress;
2117
-
2118
- /***/ },
2119
- /* 7 */
2120
- /***/ function(module, exports, __webpack_require__) {
2121
-
2122
- "use strict";
2123
-
2124
- const crunchy_1 = __webpack_require__(1);
2125
- const prefix32_1 = __webpack_require__(8);
2126
- const ipaddress_1 = __webpack_require__(6);
2127
- const ip_bits_1 = __webpack_require__(3);
2128
- const prefix128_1 = __webpack_require__(9);
2129
- const ipv6_1 = __webpack_require__(10);
2130
- class Ipv4 {
2131
- static from_number(addr, prefix_num) {
2132
- let prefix = prefix32_1.default.create(prefix_num);
2133
- if (!prefix) {
2134
- return null;
2135
- }
2136
- return new ipaddress_1.default({
2137
- ip_bits: ip_bits_1.default.v4(),
2138
- host_address: addr.clone(),
2139
- prefix: prefix,
2140
- mapped: null,
2141
- vt_is_private: Ipv4.ipv4_is_private,
2142
- vt_is_loopback: Ipv4.ipv4_is_loopback,
2143
- vt_to_ipv6: Ipv4.to_ipv6
2144
- });
2145
- }
2146
- static create(str) {
2147
- // console.log("create:v4:", str);
2148
- // let enable = str == "0.0.0.0/0";
2149
- let tmp = ipaddress_1.default.split_at_slash(str);
2150
- let ip = tmp[0];
2151
- let netmask = tmp[1];
2152
- if (!ipaddress_1.default.is_valid_ipv4(ip)) {
2153
- // enable && console.log("xx1");
2154
- return null;
2155
- }
2156
- let ip_prefix_num = 32;
2157
- if (netmask) {
2158
- // netmask is defined
2159
- ip_prefix_num = ipaddress_1.default.parse_netmask_to_prefix(netmask);
2160
- if (ip_prefix_num === null) {
2161
- // enable && console.log("xx2");
2162
- return null;
2163
- }
2164
- }
2165
- let ip_prefix = prefix32_1.default.create(ip_prefix_num);
2166
- if (ip_prefix === null) {
2167
- // enable && console.log("xx3");
2168
- return null;
2169
- }
2170
- let split_number = ipaddress_1.default.split_to_u32(ip);
2171
- if (split_number === null) {
2172
- // enable && console.log("xx4");
2173
- return null;
2174
- }
2175
- // console.log(">>>>>>>", ip, ip_prefix);
2176
- return new ipaddress_1.default({
2177
- ip_bits: ip_bits_1.default.v4(),
2178
- host_address: split_number,
2179
- prefix: ip_prefix,
2180
- mapped: null,
2181
- vt_is_private: Ipv4.ipv4_is_private,
2182
- vt_is_loopback: Ipv4.ipv4_is_loopback,
2183
- vt_to_ipv6: Ipv4.to_ipv6
2184
- });
2185
- }
2186
- static ipv4_is_private(my) {
2187
- return [ipaddress_1.default.parse("10.0.0.0/8"), ipaddress_1.default.parse("172.16.0.0/12"), ipaddress_1.default.parse("192.168.0.0/16")].find(i => i.includes(my)) != null;
2188
- }
2189
- static ipv4_is_loopback(my) {
2190
- return ipaddress_1.default.parse("127.0.0.0/8").includes(my);
2191
- }
2192
- static to_ipv6(ia) {
2193
- return new ipaddress_1.default({
2194
- ip_bits: ip_bits_1.default.v6(),
2195
- host_address: ia.host_address.clone(),
2196
- prefix: prefix128_1.default.create(ia.prefix.num),
2197
- mapped: null,
2198
- vt_is_private: ipv6_1.default.ipv6_is_private,
2199
- vt_is_loopback: ipv6_1.default.ipv6_is_loopback,
2200
- vt_to_ipv6: ipv6_1.default.to_ipv6
2201
- });
2202
- }
2203
- // Checks whether the ip address belongs to a
2204
- // RFC 791 CLASS A network, no matter
2205
- // what the subnet mask is.
2206
- //
2207
- // Example:
2208
- //
2209
- // ip = IPAddress("10.0.0.1/24")
2210
- //
2211
- // ip.a?
2212
- // // => true
2213
- //
2214
- static is_class_a(my) {
2215
- // console.log("is_class_a:", my.to_string(), Crunchy.from_string("80000000", 16), my.is_ipv4());
2216
- return my.is_ipv4() && my.host_address.lt(crunchy_1.default.from_string("80000000", 16));
2217
- }
2218
- // Checks whether the ip address belongs to a
2219
- // RFC 791 CLASS B network, no matter
2220
- // what the subnet mask is.
2221
- //
2222
- // Example:
2223
- //
2224
- // ip = IPAddress("172.16.10.1/24")
2225
- //
2226
- // ip.b?
2227
- // // => true
2228
- //
2229
- static is_class_b(my) {
2230
- return my.is_ipv4() && crunchy_1.default.from_string("80000000", 16).lte(my.host_address) && my.host_address.lt(crunchy_1.default.from_string("c0000000", 16));
2231
- }
2232
- // Checks whether the ip address belongs to a
2233
- // RFC 791 CLASS C network, no matter
2234
- // what the subnet mask is.
2235
- //
2236
- // Example:
2237
- //
2238
- // ip = IPAddress("192.168.1.1/30")
2239
- //
2240
- // ip.c?
2241
- // // => true
2242
- //
2243
- static is_class_c(my) {
2244
- return my.is_ipv4() && crunchy_1.default.from_string("c0000000", 16).lte(my.host_address) && my.host_address.lt(crunchy_1.default.from_string("e0000000", 16));
2245
- }
2246
- // Creates a new IPv4 address object by parsing the
2247
- // address in a classful way.
2248
- //
2249
- // Classful addresses have a fixed netmask based on the
2250
- // class they belong to:
2251
- //
2252
- // * Class A, from 0.0.0.0 to 127.255.255.255
2253
- // * Class B, from 128.0.0.0 to 191.255.255.255
2254
- // * Class C, D and E, from 192.0.0.0 to 255.255.255.254
2255
- //
2256
- // Example:
2257
- //
2258
- // ip = IPAddress::IPv4.parse_classful "10.0.0.1"
2259
- //
2260
- // ip.netmask
2261
- // // => "255.0.0.0"
2262
- // ip.a?
2263
- // // => true
2264
- //
2265
- // Note that classes C, D and E will all have a default
2266
- // prefix of /24 or 255.255.255.0
2267
- //
2268
- static parse_classful(ip_si) {
2269
- if (!ipaddress_1.default.is_valid_ipv4(ip_si)) {
2270
- return null;
2271
- }
2272
- let o_ip = ipaddress_1.default.parse(ip_si);
2273
- if (o_ip === null) {
2274
- return o_ip;
2275
- }
2276
- let ip = o_ip;
2277
- if (Ipv4.is_class_a(ip)) {
2278
- ip.prefix = prefix32_1.default.create(8);
2279
- } else if (Ipv4.is_class_b(ip)) {
2280
- ip.prefix = prefix32_1.default.create(16);
2281
- } else if (Ipv4.is_class_c(ip)) {
2282
- ip.prefix = prefix32_1.default.create(24);
2283
- }
2284
- return ip;
2285
- }
2286
- }
2287
- Object.defineProperty(exports, "__esModule", { value: true });
2288
- exports.default = Ipv4;
2289
-
2290
- /***/ },
2291
- /* 8 */
2292
- /***/ function(module, exports, __webpack_require__) {
2293
-
2294
- "use strict";
2295
-
2296
- const prefix_1 = __webpack_require__(2);
2297
- const ip_bits_1 = __webpack_require__(3);
2298
- class Prefix32 {
2299
- static from(my, num) {
2300
- return Prefix32.create(num);
2301
- }
2302
- static create(num) {
2303
- if (0 <= num && num <= 32) {
2304
- //static _FROM: &'static (Fn(&::prefix::Prefix, usize) -> Result<::prefix::Prefix, String>) =
2305
- // &from;
2306
- //static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &to_ip_str;
2307
- let ip_bits = ip_bits_1.default.v4();
2308
- let bits = ip_bits.bits;
2309
- return new prefix_1.default({
2310
- num: num,
2311
- ip_bits: ip_bits,
2312
- net_mask: prefix_1.default.new_netmask(num, bits),
2313
- vt_from: Prefix32.from
2314
- });
2315
- }
2316
- return null;
2317
- }
2318
- }
2319
- Object.defineProperty(exports, "__esModule", { value: true });
2320
- exports.default = Prefix32;
2321
-
2322
- /***/ },
2323
- /* 9 */
2324
- /***/ function(module, exports, __webpack_require__) {
2325
-
2326
- "use strict";
2327
-
2328
- const prefix_1 = __webpack_require__(2);
2329
- const ip_bits_1 = __webpack_require__(3);
2330
- class Prefix128 {
2331
- // #[derive(Ord,PartialOrd,Eq,PartialEq,Debug,Copy,Clone)]
2332
- // pub struct Prefix128 {
2333
- // }
2334
- //
2335
- // impl Prefix128 {
2336
- //
2337
- // Creates a new prefix object for 128 bits IPv6 addresses
2338
- //
2339
- // prefix = IPAddressPrefix128.new 64
2340
- // // => 64
2341
- //
2342
- //#[allow(unused_comparisons)]
2343
- static create(num) {
2344
- if (num <= 128) {
2345
- //static _FROM: &'static (Fn(&Prefix, usize) -> Result<Prefix, String>) = &from;
2346
- //static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &Prefix128::to_ip_str;
2347
- let ip_bits = ip_bits_1.default.v6();
2348
- let bits = ip_bits.bits;
2349
- return new prefix_1.default({
2350
- num: num,
2351
- ip_bits: ip_bits,
2352
- net_mask: prefix_1.default.new_netmask(num, bits),
2353
- vt_from: Prefix128.from
2354
- });
2355
- }
2356
- return null;
2357
- }
2358
- static from(my, num) {
2359
- return Prefix128.create(num);
2360
- }
2361
- }
2362
- Object.defineProperty(exports, "__esModule", { value: true });
2363
- exports.default = Prefix128;
2364
-
2365
- /***/ },
2366
- /* 10 */
2367
- /***/ function(module, exports, __webpack_require__) {
2368
-
2369
- "use strict";
2370
-
2371
- const ip_bits_1 = __webpack_require__(3);
2372
- const ipaddress_1 = __webpack_require__(6);
2373
- const ipv4_1 = __webpack_require__(7);
2374
- const prefix128_1 = __webpack_require__(9);
2375
- const crunchy_1 = __webpack_require__(1);
2376
- class Ipv6 {
2377
- // =Name
2378
- //
2379
- // IPAddress::IPv6 - IP version 6 address manipulation library
2380
- //
2381
- // =Synopsis
2382
- //
2383
- // require 'ipaddress'
2384
- //
2385
- // =Description
2386
- //
2387
- // Class IPAddress::IPv6 is used to handle IPv6 type addresses.
2388
- //
2389
- // == IPv6 addresses
2390
- //
2391
- // IPv6 addresses are 128 bits long, in contrast with IPv4 addresses
2392
- // which are only 32 bits long. An IPv6 address is generally written as
2393
- // eight groups of four hexadecimal digits, each group representing 16
2394
- // bits or two octect. For example, the following is a valid IPv6
2395
- // address:
2396
- //
2397
- // 2001:0db8:0000:0000:0008:0800:200c:417a
2398
- //
2399
- // Letters in an IPv6 address are usually written downcase, as per
2400
- // RFC. You can create a new IPv6 object using uppercase letters, but
2401
- // they will be converted.
2402
- //
2403
- // === Compression
2404
- //
2405
- // Since IPv6 addresses are very long to write, there are some
2406
- // semplifications and compressions that you can use to shorten them.
2407
- //
2408
- // * Leading zeroes: all the leading zeroes within a group can be
2409
- // omitted: "0008" would become "8"
2410
- //
2411
- // * A string of consecutive zeroes can be replaced by the string
2412
- // "::". This can be only applied once.
2413
- //
2414
- // Using compression, the IPv6 address written above can be shorten into
2415
- // the following, equivalent, address
2416
- //
2417
- // 2001:db8::8:800:200c:417a
2418
- //
2419
- // This short version is often used in human representation.
2420
- //
2421
- // === Network Mask
2422
- //
2423
- // As we used to do with IPv4 addresses, an IPv6 address can be written
2424
- // using the prefix notation to specify the subnet mask:
2425
- //
2426
- // 2001:db8::8:800:200c:417a/64
2427
- //
2428
- // The /64 part means that the first 64 bits of the address are
2429
- // representing the network portion, and the last 64 bits are the host
2430
- // portion.
2431
- //
2432
- //
2433
- static from_str(str, radix, prefix) {
2434
- let num = crunchy_1.default.from_string(str, radix);
2435
- if (!num) {
2436
- return null;
2437
- }
2438
- return Ipv6.from_int(num, prefix);
2439
- }
2440
- static enhance_if_mapped(ip) {
2441
- // console.log("------A");
2442
- // println!("real mapped {:x} {:x}", &ip.host_address, ip.host_address.clone().shr(32));
2443
- if (ip.is_mapped()) {
2444
- // console.log("------B");
2445
- return ip;
2446
- }
2447
- // console.log("------C", ip);
2448
- let ipv6_top_96bit = ip.host_address.shr(32);
2449
- // console.log("------D", ip);
2450
- if (ipv6_top_96bit.eq(crunchy_1.default.from_number(0xffff))) {
2451
- // console.log("------E");
2452
- let num = ip.host_address.mod(crunchy_1.default.one().shl(32));
2453
- // console.log("------F");
2454
- if (num.eq(crunchy_1.default.zero())) {
2455
- return ip;
2456
- }
2457
- //println!("ip:{},{:x}", ip.to_string(), num);
2458
- let ipv4_bits = ip_bits_1.default.v4();
2459
- if (ipv4_bits.bits < ip.prefix.host_prefix()) {
2460
- //println!("enhance_if_mapped-2:{}:{}", ip.to_string(), ip.prefix.host_prefix());
2461
- return null;
2462
- }
2463
- // console.log("------G");
2464
- let mapped = ipv4_1.default.from_number(num, ipv4_bits.bits - ip.prefix.host_prefix());
2465
- // console.log("------H");
2466
- if (!mapped) {
2467
- // println!("enhance_if_mapped-3");
2468
- return mapped;
2469
- }
2470
- // println!("real mapped!!!!!={}", mapped.clone().to_string());
2471
- ip.mapped = mapped;
2472
- }
2473
- return ip;
2474
- }
2475
- static from_int(adr, prefix_num) {
2476
- let prefix = prefix128_1.default.create(prefix_num);
2477
- if (prefix === null) {
2478
- return null;
2479
- }
2480
- let ret = Ipv6.enhance_if_mapped(new ipaddress_1.default({
2481
- ip_bits: ip_bits_1.default.v6(),
2482
- host_address: adr.clone(),
2483
- prefix: prefix,
2484
- mapped: null,
2485
- vt_is_private: Ipv6.ipv6_is_private,
2486
- vt_is_loopback: Ipv6.ipv6_is_loopback,
2487
- vt_to_ipv6: Ipv6.to_ipv6
2488
- }));
2489
- //console.log("from_int:", adr, prefix, ret);
2490
- return ret;
2491
- }
2492
- // Creates a new IPv6 address object.
2493
- //
2494
- // An IPv6 address can be expressed in any of the following forms:
2495
- //
2496
- // * "2001:0db8:0000:0000:0008:0800:200C:417A": IPv6 address with no compression
2497
- // * "2001:db8:0:0:8:800:200C:417A": IPv6 address with leading zeros compression
2498
- // * "2001:db8::8:800:200C:417A": IPv6 address with full compression
2499
- //
2500
- // In all these 3 cases, a new IPv6 address object will be created, using the default
2501
- // subnet mask /128
2502
- //
2503
- // You can also specify the subnet mask as with IPv4 addresses:
2504
- //
2505
- // ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
2506
- //
2507
- static create(str) {
2508
- // console.log("1>>>>>>>>>", str);
2509
- let [ip, o_netmask] = ipaddress_1.default.split_at_slash(str);
2510
- // console.log("2>>>>>>>>>", str);
2511
- if (ipaddress_1.default.is_valid_ipv6(ip)) {
2512
- // console.log("3>>>>>>>>>", str);
2513
- let o_num = ipaddress_1.default.split_to_num(ip);
2514
- if (o_num === null) {
2515
- // console.log("ipv6_create-1", str);
2516
- return null;
2517
- }
2518
- // console.log("4>>>>>>>>>", str);
2519
- let netmask = 128;
2520
- if (o_netmask !== null) {
2521
- netmask = ipaddress_1.default.parse_dec_str(o_netmask);
2522
- if (netmask === null) {
2523
- // console.log("ipv6_create-2", str);
2524
- return null;
2525
- }
2526
- }
2527
- // console.log("5>>>>>>>>>", str);
2528
- let prefix = prefix128_1.default.create(netmask);
2529
- if (prefix === null) {
2530
- // console.log("ipv6_create-3", str);
2531
- return null;
2532
- }
2533
- //console.log("6>>>>>>>>>", str, prefix.num, o_netmask, netmask);
2534
- return Ipv6.enhance_if_mapped(new ipaddress_1.default({
2535
- ip_bits: ip_bits_1.default.v6(),
2536
- host_address: o_num.crunchy,
2537
- prefix: prefix,
2538
- mapped: null,
2539
- vt_is_private: Ipv6.ipv6_is_private,
2540
- vt_is_loopback: Ipv6.ipv6_is_loopback,
2541
- vt_to_ipv6: Ipv6.to_ipv6
2542
- }));
2543
- } else {
2544
- // console.log("ipv6_create-4", str);
2545
- return null;
2546
- }
2547
- } // pub fn initialize
2548
- static to_ipv6(ia) {
2549
- return ia.clone();
2550
- }
2551
- static ipv6_is_loopback(my) {
2552
- // console.log("*************", my.host_address, Crunchy.one());
2553
- return my.host_address.eq(crunchy_1.default.one());
2554
- }
2555
- static ipv6_is_private(my) {
2556
- return ipaddress_1.default.parse("fd00::/8").includes(my);
2557
- }
2558
- }
2559
- Object.defineProperty(exports, "__esModule", { value: true });
2560
- exports.default = Ipv6;
2561
-
2562
- /***/ },
2563
- /* 11 */
2564
- /***/ function(module, exports, __webpack_require__) {
2565
-
2566
- "use strict";
2567
-
2568
- const crunchy_1 = __webpack_require__(1);
2569
- const ip_bits_1 = __webpack_require__(3);
2570
- const ipaddress_1 = __webpack_require__(6);
2571
- class Ipv6Mapped {
2572
- // Ac
2573
- // It is usually identified as a IPv4 mapped IPv6 address, a particular
2574
- // IPv6 address which aids the transition from IPv4 to IPv6. The
2575
- // structure of the address is
2576
- //
2577
- // ::ffff:w.y.x.z
2578
- //
2579
- // where w.x.y.z is a normal IPv4 address. For example, the following is
2580
- // a mapped IPv6 address:
2581
- //
2582
- // ::ffff:192.168.100.1
2583
- //
2584
- // IPAddress is very powerful in handling mapped IPv6 addresses, as the
2585
- // IPv4 portion is stored internally as a normal IPv4 object. Let's have
2586
- // a look at some examples. To create a new mapped address, just use the
2587
- // class builder itself
2588
- //
2589
- // ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
2590
- //
2591
- // or just use the wrapper method
2592
- //
2593
- // ip6 = IPAddress "::ffff:172.16.10.1/128"
2594
- //
2595
- // Let's check it's really a mapped address:
2596
- //
2597
- // ip6.mapped?
2598
- // // => true
2599
- //
2600
- // ip6.to_string
2601
- // // => "::FFFF:172.16.10.1/128"
2602
- //
2603
- // Now with the +ipv4+ attribute, we can easily access the IPv4 portion
2604
- // of the mapped IPv6 address:
2605
- //
2606
- // ip6.ipv4.address
2607
- // // => "172.16.10.1"
2608
- //
2609
- // Internally, the IPv4 address is stored as two 16 bits
2610
- // groups. Therefore all the usual methods for an IPv6 address are
2611
- // working perfectly fine:
2612
- //
2613
- // ip6.to_hex
2614
- // // => "00000000000000000000ffffac100a01"
2615
- //
2616
- // ip6.address
2617
- // // => "0000:0000:0000:0000:0000:ffff:ac10:0a01"
2618
- //
2619
- // A mapped IPv6 can also be created just by specify the address in the
2620
- // following format:
2621
- //
2622
- // ip6 = IPAddress "::172.16.10.1"
2623
- //
2624
- // That is, two colons and the IPv4 address. However, as by RFC, the ffff
2625
- // group will be automatically added at the beginning
2626
- //
2627
- // ip6.to_string
2628
- // => "::ffff:172.16.10.1/128"
2629
- //
2630
- // making it a mapped IPv6 compatible address.
2631
- //
2632
- //
2633
- // Creates a new IPv6 IPv4-mapped address
2634
- //
2635
- // ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
2636
- //
2637
- // ipv6.ipv4.class
2638
- // // => IPAddress::IPv4
2639
- //
2640
- // An IPv6 IPv4-mapped address can also be created using the
2641
- // IPv6 only format of the address:
2642
- //
2643
- // ip6 = IPAddress::IPv6::Mapped.new "::0d01:4403"
2644
- //
2645
- // ip6.to_string
2646
- // // => "::ffff:13.1.68.3"
2647
- //
2648
- static create(str) {
2649
- // console.log("mapped-1");
2650
- let i = ipaddress_1.default.split_at_slash(str);
2651
- let ip = i[0];
2652
- let o_netmask = i[1];
2653
- let split_colon = ip.split(":");
2654
- if (split_colon.length <= 1) {
2655
- // console.log("mapped-2");
2656
- return null;
2657
- }
2658
- // if split_colon.get(0).unwrap().len() > 0 {
2659
- // // println!("---1a");
2660
- // return Err(format!("not mapped format-2: {}", &str));
2661
- // }
2662
- // let mapped: Option<IPAddress> = None;
2663
- let netmask = "";
2664
- if (o_netmask !== null) {
2665
- netmask = `/${ o_netmask }`;
2666
- }
2667
- let ipv4_str = split_colon[split_colon.length - 1];
2668
- if (ipaddress_1.default.is_valid_ipv4(ipv4_str)) {
2669
- let ipv4 = ipaddress_1.default.parse(`${ ipv4_str }${ netmask }`);
2670
- if (ipv4 === null) {
2671
- // console.log("mapped-3");
2672
- return ipv4;
2673
- }
2674
- //mapped = Some(ipv4.unwrap());
2675
- let addr = ipv4;
2676
- let ipv6_bits = ip_bits_1.default.v6();
2677
- let part_mod = crunchy_1.default.from_number(ipv6_bits.part_mod);
2678
- let up_addr = addr.host_address.clone();
2679
- let down_addr = addr.host_address.clone();
2680
- let rebuild_ipv6 = "";
2681
- let colon = "";
2682
- for (let i = 0; i < split_colon.length - 1; ++i) {
2683
- rebuild_ipv6 += colon;
2684
- rebuild_ipv6 += split_colon[i];
2685
- colon = ":";
2686
- }
2687
- rebuild_ipv6 += colon;
2688
- let high_part = up_addr.shr(ip_bits_1.default.v6().part_bits).mod(part_mod).toString(16);
2689
- let low_part = down_addr.mod(part_mod).toString(16);
2690
- let bits = ipv6_bits.bits - addr.prefix.host_prefix();
2691
- let rebuild_ipv4 = `${ high_part }:${ low_part }/${ bits }`;
2692
- rebuild_ipv6 += rebuild_ipv4;
2693
- // console.log("-----A", rebuild_ipv6, part_mod);
2694
- let r_ipv6 = ipaddress_1.default.parse(rebuild_ipv6);
2695
- if (r_ipv6 === null) {
2696
- // println!("---3|{}", &rebuild_ipv6);
2697
- // console.log("mapped-4");
2698
- return r_ipv6;
2699
- }
2700
- if (r_ipv6.is_mapped()) {
2701
- // console.log("mapped-5");
2702
- return r_ipv6;
2703
- }
2704
- let ipv6 = r_ipv6;
2705
- let p96bit = ipv6.host_address.shr(32);
2706
- if (!p96bit.eq(crunchy_1.default.zero())) {
2707
- // println!("---4|{}", &rebuild_ipv6);
2708
- //console.log("mapped-6",ipv6.host_address, p96bit, Crunchy.zero());
2709
- return null;
2710
- }
2711
- {
2712
- let r_ipv6 = ipaddress_1.default.parse(`::ffff:${ rebuild_ipv4 }`);
2713
- if (r_ipv6 === null) {
2714
- // println!("---3|{}", &rebuild_ipv6);
2715
- //console.log("mapped-7");
2716
- return null;
2717
- }
2718
- // console.log("mapped-8");
2719
- return r_ipv6;
2720
- }
2721
- }
2722
- // console.log("mapped-9");
2723
- return null;
2724
- }
2725
- }
2726
- Object.defineProperty(exports, "__esModule", { value: true });
2727
- exports.default = Ipv6Mapped;
2728
-
2729
- /***/ },
2730
- /* 12 */
2731
- /***/ function(module, exports, __webpack_require__) {
2732
-
2733
- "use strict";
2734
-
2735
- const ipv6_1 = __webpack_require__(10);
2736
- const crunchy_1 = __webpack_require__(1);
2737
- class Ipv6Loopback {
2738
- // The loopback address is a unicast localhost address. If an
2739
- // application in a host sends packets to this address, the IPv6 stack
2740
- // will loop these packets back on the same virtual interface.
2741
- //
2742
- // Loopback addresses are expressed in the following form:
2743
- //
2744
- // ::1
2745
- //
2746
- // or, with their appropriate prefix,
2747
- //
2748
- // ::1/128
2749
- //
2750
- // As for the unspecified addresses, IPv6 loopbacks can be created with
2751
- // IPAddress calling their own class:
2752
- //
2753
- // ip = IPAddress::IPv6::Loopback.new
2754
- //
2755
- // ip.to_string
2756
- // // => "::1/128"
2757
- //
2758
- // or by using the wrapper:
2759
- //
2760
- // ip = IPAddress "::1"
2761
- //
2762
- // ip.to_string
2763
- // // => "::1/128"
2764
- //
2765
- // Checking if an address is loopback is easy with the IPv6// loopback?
2766
- // method:
2767
- //
2768
- // ip.loopback?
2769
- // // => true
2770
- //
2771
- // The IPv6 loopback address corresponds to 127.0.0.1 in IPv4.
2772
- //
2773
- //
2774
- // Creates a new IPv6 unspecified address
2775
- //
2776
- // ip = IPAddress::IPv6::Loopback.new
2777
- //
2778
- // ip.to_string
2779
- // // => "::1/128"
2780
- //
2781
- static create() {
2782
- return ipv6_1.default.from_int(crunchy_1.default.one(), 128);
2783
- }
2784
- }
2785
- Object.defineProperty(exports, "__esModule", { value: true });
2786
- exports.default = Ipv6Loopback;
2787
-
2788
- /***/ },
2789
- /* 13 */
2790
- /***/ function(module, exports, __webpack_require__) {
2791
-
2792
- "use strict";
2793
-
2794
- const ipaddress_1 = __webpack_require__(6);
2795
- class Ipv6Unspec {
2796
- // The address with all zero bits is called the +unspecified+ address
2797
- // (corresponding to 0.0.0.0 in IPv4). It should be something like this:
2798
- //
2799
- // 0000:0000:0000:0000:0000:0000:0000:0000
2800
- //
2801
- // but, with the use of compression, it is usually written as just two
2802
- // colons:
2803
- //
2804
- // ::
2805
- //
2806
- // or, specifying the netmask:
2807
- //
2808
- // ::/128
2809
- //
2810
- // With IPAddress, create a new unspecified IPv6 address using its own
2811
- // subclass:
2812
- //
2813
- // ip = IPAddress::IPv6::Unspecified.new
2814
- //
2815
- // ip.to_s
2816
- // // => => "::/128"
2817
- //
2818
- // You can easily check if an IPv6 object is an unspecified address by
2819
- // using the IPv6// unspecified? method
2820
- //
2821
- // ip.unspecified?
2822
- // // => true
2823
- //
2824
- // An unspecified IPv6 address can also be created with the wrapper
2825
- // method, like we've seen before
2826
- //
2827
- // ip = IPAddress "::"
2828
- //
2829
- // ip.unspecified?
2830
- // // => true
2831
- //
2832
- // This address must never be assigned to an interface and is to be used
2833
- // only in software before the application has learned its host's source
2834
- // address appropriate for a pending connection. Routers must not forward
2835
- // packets with the unspecified address.
2836
- //
2837
- //
2838
- // Creates a new IPv6 unspecified address
2839
- //
2840
- // ip = IPAddress::IPv6::Unspecified.new
2841
- //
2842
- // ip.to_s
2843
- // // => => "::/128"
2844
- //
2845
- static create() {
2846
- return ipaddress_1.default.parse("::");
2847
- }
2848
- }
2849
- Object.defineProperty(exports, "__esModule", { value: true });
2850
- exports.default = Ipv6Unspec;
2851
-
2852
- /***/ }
2853
- /******/ ]);
2854
- //# sourceMappingURL=ipaddress.js.map