ipaddress 0.2.5 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/crunchy.d.ts +130 -0
- package/dist/src/crunchy.d.ts.map +1 -0
- package/dist/src/crunchy.js +616 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +21 -0
- package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -7
- package/dist/src/ip_bits.d.ts.map +1 -0
- package/dist/src/ip_bits.js +133 -0
- package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
- package/dist/src/ip_version.d.ts.map +1 -0
- package/dist/src/ip_version.js +6 -0
- package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +38 -35
- package/dist/src/ipaddress.d.ts.map +1 -0
- package/dist/src/ipaddress.js +1243 -0
- package/dist/src/ipv4.d.ts +15 -0
- package/dist/src/ipv4.d.ts.map +1 -0
- package/dist/src/ipv4.js +187 -0
- package/dist/src/ipv6.d.ts +13 -0
- package/dist/src/ipv6.d.ts.map +1 -0
- package/dist/src/ipv6.js +190 -0
- package/dist/src/ipv6_loopback.d.ts +6 -0
- package/dist/src/ipv6_loopback.d.ts.map +1 -0
- package/dist/src/ipv6_loopback.js +51 -0
- package/dist/src/ipv6_mapped.d.ts +6 -0
- package/dist/src/ipv6_mapped.d.ts.map +1 -0
- package/dist/src/ipv6_mapped.js +159 -0
- package/dist/src/ipv6_unspec.d.ts +6 -0
- package/dist/src/ipv6_unspec.d.ts.map +1 -0
- package/dist/src/ipv6_unspec.js +57 -0
- package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
- package/dist/src/prefix.d.ts.map +1 -0
- package/dist/src/prefix.js +142 -0
- package/dist/src/prefix128.d.ts +7 -0
- package/dist/src/prefix128.d.ts.map +1 -0
- package/dist/src/prefix128.js +39 -0
- package/dist/src/prefix32.d.ts +7 -0
- package/dist/src/prefix32.d.ts.map +1 -0
- package/dist/src/prefix32.js +29 -0
- package/dist/src/rle.d.ts +19 -0
- package/dist/src/rle.d.ts.map +1 -0
- package/dist/src/rle.js +73 -0
- package/dist/test/test_crunchy_impls.d.ts +2 -0
- package/dist/test/test_crunchy_impls.d.ts.map +1 -0
- package/dist/test/test_crunchy_impls.js +150 -0
- package/dist/test/test_crunchy_with32bit.d.ts +2 -0
- package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
- package/dist/test/test_crunchy_with32bit.js +382 -0
- package/dist/test/test_ipaddress.d.ts +2 -0
- package/dist/test/test_ipaddress.d.ts.map +1 -0
- package/dist/test/test_ipaddress.js +182 -0
- package/dist/test/test_ipv4.d.ts +2 -0
- package/dist/test/test_ipv4.d.ts.map +1 -0
- package/dist/test/test_ipv4.js +561 -0
- package/dist/test/test_ipv6.d.ts +2 -0
- package/dist/test/test_ipv6.d.ts.map +1 -0
- package/dist/test/test_ipv6.js +313 -0
- package/dist/test/test_ipv6_loopback.d.ts +2 -0
- package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
- package/dist/test/test_ipv6_loopback.js +40 -0
- package/dist/test/test_ipv6_mapped.d.ts +2 -0
- package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
- package/dist/test/test_ipv6_mapped.js +89 -0
- package/dist/test/test_ipv6_unspec.d.ts +2 -0
- package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
- package/dist/test/test_ipv6_unspec.js +39 -0
- package/dist/test/test_prefix128.d.ts +2 -0
- package/dist/test/test_prefix128.d.ts.map +1 -0
- package/dist/test/test_prefix128.js +35 -0
- package/dist/test/test_prefix32.d.ts +2 -0
- package/dist/test/test_prefix32.d.ts.map +1 -0
- package/dist/test/test_prefix32.js +126 -0
- package/dist/test/test_rle.d.ts +2 -0
- package/dist/test/test_rle.d.ts.map +1 -0
- package/dist/test/test_rle.js +141 -0
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +11 -0
- package/package.json +41 -37
- package/src/crunchy.ts +731 -0
- package/src/index.ts +23 -0
- package/src/ip_bits.ts +148 -0
- package/src/ip_version.ts +6 -0
- package/src/ipaddress.ts +1347 -0
- package/src/ipv4.ts +203 -0
- package/src/ipv6.ts +202 -0
- package/src/ipv6_loopback.ts +55 -0
- package/src/ipv6_mapped.ts +163 -0
- package/src/ipv6_unspec.ts +59 -0
- package/src/prefix.ts +170 -0
- package/src/prefix128.ts +42 -0
- package/src/prefix32.ts +32 -0
- package/src/rle.ts +85 -0
- package/test/test_crunchy_impls.ts +179 -0
- package/test/test_crunchy_with32bit.ts +510 -0
- package/test/test_ipaddress.ts +208 -0
- package/test/test_ipv4.ts +596 -0
- package/test/test_ipv6.ts +325 -0
- package/test/test_ipv6_loopback.ts +52 -0
- package/test/test_ipv6_mapped.ts +101 -0
- package/test/test_ipv6_unspec.ts +51 -0
- package/test/test_prefix128.ts +39 -0
- package/test/test_prefix32.ts +130 -0
- package/test/test_rle.ts +144 -0
- package/LICENSE +0 -20
- package/README.md +0 -960
- package/crunchy.d.ts +0 -55
- package/index.d.ts +0 -12
- package/ipaddress.js +0 -2869
- package/ipaddress.js.map +0 -1
- package/ipv4.d.ts +0 -14
- package/ipv6.d.ts +0 -12
- package/ipv6_loopback.d.ts +0 -5
- package/ipv6_mapped.d.ts +0 -5
- package/ipv6_unspec.d.ts +0 -5
- package/prefix128.d.ts +0 -6
- package/prefix32.d.ts +0 -6
- package/rle.d.ts +0 -14
package/ipaddress.js
DELETED
|
@@ -1,2869 +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 this;
|
|
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
|
-
if (IpBits._v4) {
|
|
741
|
-
return IpBits._v4;
|
|
742
|
-
}
|
|
743
|
-
let my = new IpBits();
|
|
744
|
-
IpBits._v4 = my;
|
|
745
|
-
my.version = ip_version_1.default.V4;
|
|
746
|
-
my.vt_as_compressed_string = IpBits.ipv4_as_compressed;
|
|
747
|
-
my.vt_as_uncompressed_string = IpBits.ipv4_as_compressed;
|
|
748
|
-
my.bits = 32;
|
|
749
|
-
my.part_bits = 8;
|
|
750
|
-
my.dns_bits = 8;
|
|
751
|
-
my.rev_domain = "in-addr.arpa";
|
|
752
|
-
my.part_mod = 1 << 8;
|
|
753
|
-
my.host_ofs = crunchy_1.default.one();
|
|
754
|
-
return my;
|
|
755
|
-
}
|
|
756
|
-
static v6() {
|
|
757
|
-
if (IpBits._v6) {
|
|
758
|
-
return IpBits._v6;
|
|
759
|
-
}
|
|
760
|
-
let my = new IpBits();
|
|
761
|
-
IpBits._v6 = my;
|
|
762
|
-
my.version = ip_version_1.default.V6;
|
|
763
|
-
my.vt_as_compressed_string = IpBits.ipv6_as_compressed;
|
|
764
|
-
my.vt_as_uncompressed_string = IpBits.ipv6_as_uncompressed;
|
|
765
|
-
my.bits = 128;
|
|
766
|
-
my.part_bits = 16;
|
|
767
|
-
my.dns_bits = 4;
|
|
768
|
-
my.rev_domain = "ip6.arpa";
|
|
769
|
-
my.part_mod = 1 << 16;
|
|
770
|
-
my.host_ofs = crunchy_1.default.zero();
|
|
771
|
-
return my;
|
|
772
|
-
}
|
|
773
|
-
static ipv4_as_compressed(ip_bits, host_address) {
|
|
774
|
-
let ret = "";
|
|
775
|
-
let sep = "";
|
|
776
|
-
for (let part of ip_bits.parts(host_address)) {
|
|
777
|
-
ret += sep;
|
|
778
|
-
ret += `${ part }`;
|
|
779
|
-
sep = ".";
|
|
780
|
-
}
|
|
781
|
-
return ret;
|
|
782
|
-
}
|
|
783
|
-
static ipv6_as_compressed(ip_bits, host_address) {
|
|
784
|
-
//println!("ipv6_as_compressed:{}", host_address);
|
|
785
|
-
let ret = "";
|
|
786
|
-
let colon = "";
|
|
787
|
-
let done = false;
|
|
788
|
-
for (let rle of rle_1.default.code(ip_bits.parts(host_address))) {
|
|
789
|
-
// console.log(rle.toString());
|
|
790
|
-
for (let _ = 0; _ < rle.cnt; _++) {
|
|
791
|
-
if (done || !(rle.part == 0 && rle.max)) {
|
|
792
|
-
ret += `${ colon }${ rle.part.toString(16) }`;
|
|
793
|
-
colon = ":";
|
|
794
|
-
} else if (rle.part == 0 && rle.max) {
|
|
795
|
-
ret += "::";
|
|
796
|
-
colon = "";
|
|
797
|
-
done = true;
|
|
798
|
-
break;
|
|
799
|
-
}
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
return ret;
|
|
803
|
-
}
|
|
804
|
-
static ipv6_as_uncompressed(ip_bits, host_address) {
|
|
805
|
-
let ret = "";
|
|
806
|
-
let sep = "";
|
|
807
|
-
for (let part of ip_bits.parts(host_address)) {
|
|
808
|
-
ret += sep;
|
|
809
|
-
ret += (0x10000 + part).toString(16).slice(1);
|
|
810
|
-
sep = ":";
|
|
811
|
-
}
|
|
812
|
-
return ret;
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
IpBits._v4 = null;
|
|
816
|
-
IpBits._v6 = null;
|
|
817
|
-
exports.IpBits = IpBits;
|
|
818
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
819
|
-
exports.default = IpBits;
|
|
820
|
-
|
|
821
|
-
/***/ },
|
|
822
|
-
/* 4 */
|
|
823
|
-
/***/ function(module, exports) {
|
|
824
|
-
|
|
825
|
-
"use strict";
|
|
826
|
-
|
|
827
|
-
class Last {
|
|
828
|
-
constructor() {
|
|
829
|
-
this.val = null;
|
|
830
|
-
this.max_poses = {};
|
|
831
|
-
this.ret = [];
|
|
832
|
-
}
|
|
833
|
-
handle_last() {
|
|
834
|
-
if (null == this.val) {
|
|
835
|
-
return;
|
|
836
|
-
}
|
|
837
|
-
let _last = this.val;
|
|
838
|
-
let max_rles = this.max_poses[_last.part];
|
|
839
|
-
if (max_rles == null) {
|
|
840
|
-
max_rles = this.max_poses[_last.part] = [];
|
|
841
|
-
}
|
|
842
|
-
// console.log(_last.part, this.max_poses);
|
|
843
|
-
for (let idx in max_rles) {
|
|
844
|
-
let prev = this.ret[max_rles[idx]];
|
|
845
|
-
if (prev.cnt > _last.cnt) {
|
|
846
|
-
// console.log(`>>>>> last=${_last}->${idx}->prev=${prev}`);
|
|
847
|
-
_last.max = false;
|
|
848
|
-
} else if (prev.cnt == _last.cnt) {} else if (prev.cnt < _last.cnt) {
|
|
849
|
-
// console.log(`<<<<< last=${_last}->${idx}->prev=${prev}`);
|
|
850
|
-
prev.max = false;
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
//println!("push:{}:{:?}", self.ret.len(), _last);
|
|
854
|
-
max_rles.push(this.ret.length);
|
|
855
|
-
_last.pos = this.ret.length;
|
|
856
|
-
this.ret.push(_last);
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
class Rle {
|
|
860
|
-
constructor(obj) {
|
|
861
|
-
this.part = obj['part'];
|
|
862
|
-
this.pos = obj['pos'];
|
|
863
|
-
this.cnt = obj['cnt'];
|
|
864
|
-
this.max = obj['max'];
|
|
865
|
-
}
|
|
866
|
-
toString() {
|
|
867
|
-
return `<Rle@part:${ this.part },pos:${ this.pos },cnt:${ this.cnt },max:${ this.max }>`;
|
|
868
|
-
}
|
|
869
|
-
eq(other) {
|
|
870
|
-
return this.part == other.part && this.pos == other.pos && this.cnt == other.cnt && this.max == other.max;
|
|
871
|
-
}
|
|
872
|
-
ne(other) {
|
|
873
|
-
return !this.eq(other);
|
|
874
|
-
}
|
|
875
|
-
static code(parts) {
|
|
876
|
-
let last = new Last();
|
|
877
|
-
// println!("code");
|
|
878
|
-
for (let i = 0; i < parts.length; ++i) {
|
|
879
|
-
let part = parts[i];
|
|
880
|
-
// console.log(`part:${part}`);
|
|
881
|
-
if (last.val && last.val.part == part) {
|
|
882
|
-
last.val.cnt += 1;
|
|
883
|
-
} else {
|
|
884
|
-
last.handle_last();
|
|
885
|
-
last.val = new Rle({ part: part, pos: 0, cnt: 1, max: true });
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
last.handle_last();
|
|
889
|
-
return last.ret;
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
893
|
-
exports.default = Rle;
|
|
894
|
-
|
|
895
|
-
/***/ },
|
|
896
|
-
/* 5 */
|
|
897
|
-
/***/ function(module, exports) {
|
|
898
|
-
|
|
899
|
-
"use strict";
|
|
900
|
-
|
|
901
|
-
var IpVersion;
|
|
902
|
-
(function (IpVersion) {
|
|
903
|
-
IpVersion[IpVersion["V4"] = 4] = "V4";
|
|
904
|
-
IpVersion[IpVersion["V6"] = 6] = "V6";
|
|
905
|
-
})(IpVersion || (IpVersion = {}));
|
|
906
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
907
|
-
exports.default = IpVersion;
|
|
908
|
-
|
|
909
|
-
/***/ },
|
|
910
|
-
/* 6 */
|
|
911
|
-
/***/ function(module, exports, __webpack_require__) {
|
|
912
|
-
|
|
913
|
-
"use strict";
|
|
914
|
-
|
|
915
|
-
const ip_version_1 = __webpack_require__(5);
|
|
916
|
-
const ipv4_1 = __webpack_require__(7);
|
|
917
|
-
const ipv6_1 = __webpack_require__(10);
|
|
918
|
-
const ipv6_mapped_1 = __webpack_require__(11);
|
|
919
|
-
const crunchy_1 = __webpack_require__(1);
|
|
920
|
-
class ResultCrunchyParts {
|
|
921
|
-
constructor(crunchy, parts) {
|
|
922
|
-
this.crunchy = crunchy;
|
|
923
|
-
this.parts = parts;
|
|
924
|
-
// console.log("ResultCrunchyParts:", this);
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
exports.ResultCrunchyParts = ResultCrunchyParts;
|
|
928
|
-
class IPAddress {
|
|
929
|
-
constructor(obj) {
|
|
930
|
-
this.ip_bits = obj['ip_bits'];
|
|
931
|
-
this.host_address = obj['host_address'];
|
|
932
|
-
this.prefix = obj['prefix'];
|
|
933
|
-
this.mapped = obj['mapped'];
|
|
934
|
-
this.vt_is_private = obj['vt_is_private'];
|
|
935
|
-
this.vt_is_loopback = obj['vt_is_loopback'];
|
|
936
|
-
this.vt_to_ipv6 = obj['vt_to_ipv6'];
|
|
937
|
-
}
|
|
938
|
-
clone() {
|
|
939
|
-
let mapped = null;
|
|
940
|
-
if (this.mapped) {
|
|
941
|
-
mapped = this.mapped.clone();
|
|
942
|
-
}
|
|
943
|
-
return new IPAddress({
|
|
944
|
-
ip_bits: this.ip_bits.clone(),
|
|
945
|
-
host_address: this.host_address.clone(),
|
|
946
|
-
prefix: this.prefix.clone(),
|
|
947
|
-
mapped: mapped,
|
|
948
|
-
vt_is_private: this.vt_is_private,
|
|
949
|
-
vt_is_loopback: this.vt_is_loopback,
|
|
950
|
-
vt_to_ipv6: this.vt_to_ipv6
|
|
951
|
-
});
|
|
952
|
-
}
|
|
953
|
-
lt(oth) {
|
|
954
|
-
return this.cmp(oth) == -1;
|
|
955
|
-
}
|
|
956
|
-
lte(oth) {
|
|
957
|
-
return this.cmp(oth) <= 0;
|
|
958
|
-
}
|
|
959
|
-
gt(oth) {
|
|
960
|
-
return this.cmp(oth) == 1;
|
|
961
|
-
}
|
|
962
|
-
gte(oth) {
|
|
963
|
-
return this.cmp(oth) >= 0;
|
|
964
|
-
}
|
|
965
|
-
cmp(oth) {
|
|
966
|
-
if (this.ip_bits.version != oth.ip_bits.version) {
|
|
967
|
-
if (this.ip_bits.version == ip_version_1.default.V6) {
|
|
968
|
-
return 1;
|
|
969
|
-
}
|
|
970
|
-
return -1;
|
|
971
|
-
}
|
|
972
|
-
//let adr_diff = this.host_address - oth.host_address;
|
|
973
|
-
let hostCmp = this.host_address.compare(oth.host_address);
|
|
974
|
-
if (hostCmp != 0) {
|
|
975
|
-
return hostCmp;
|
|
976
|
-
}
|
|
977
|
-
return this.prefix.cmp(oth.prefix);
|
|
978
|
-
}
|
|
979
|
-
eq(other) {
|
|
980
|
-
// if (!!this.mapped != !!this.mapped) {
|
|
981
|
-
// return false;
|
|
982
|
-
// }
|
|
983
|
-
// if (this.mapped) {
|
|
984
|
-
// if (!this.mapped.eq(other.mapped)) {
|
|
985
|
-
// return false;
|
|
986
|
-
// }
|
|
987
|
-
// }
|
|
988
|
-
// console.log("************", this);
|
|
989
|
-
return this.ip_bits.version == other.ip_bits.version && this.prefix.eq(other.prefix) && this.host_address.eq(other.host_address);
|
|
990
|
-
}
|
|
991
|
-
ne(other) {
|
|
992
|
-
return !this.eq(other);
|
|
993
|
-
}
|
|
994
|
-
// Parse the argument string to create a new
|
|
995
|
-
// IPv4, IPv6 or Mapped IP object
|
|
996
|
-
//
|
|
997
|
-
// ip = IPAddress.parse "172.16.10.1/24"
|
|
998
|
-
// ip6 = IPAddress.parse "2001:db8.8:800:200c:417a/64"
|
|
999
|
-
// ip_mapped = IPAddress.parse ".ffff:172.16.10.1/128"
|
|
1000
|
-
//
|
|
1001
|
-
// All the object created will be instances of the
|
|
1002
|
-
// correct class:
|
|
1003
|
-
//
|
|
1004
|
-
// ip.class
|
|
1005
|
-
// //=> IPAddress.IPv4
|
|
1006
|
-
// ip6.class
|
|
1007
|
-
// //=> IPAddress.IPv6
|
|
1008
|
-
// ip_mapped.class
|
|
1009
|
-
// //=> IPAddress.IPv6.Mapped
|
|
1010
|
-
//
|
|
1011
|
-
static parse(str) {
|
|
1012
|
-
let re_mapped = new RegExp(":.+\\.");
|
|
1013
|
-
let re_ipv4 = new RegExp("\\.");
|
|
1014
|
-
let re_ipv6 = new RegExp(":");
|
|
1015
|
-
if (re_mapped.test(str)) {
|
|
1016
|
-
// console.log("mapped:", str);
|
|
1017
|
-
return ipv6_mapped_1.default.create(str);
|
|
1018
|
-
} else {
|
|
1019
|
-
if (re_ipv4.test(str)) {
|
|
1020
|
-
// console.log("ipv4:", str);
|
|
1021
|
-
return ipv4_1.default.create(str);
|
|
1022
|
-
} else if (re_ipv6.test(str)) {
|
|
1023
|
-
// console.log("ipv6:", str);
|
|
1024
|
-
return ipv6_1.default.create(str);
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
return null;
|
|
1028
|
-
}
|
|
1029
|
-
static split_at_slash(str) {
|
|
1030
|
-
let slash = str.trim().split("/");
|
|
1031
|
-
let addr = "";
|
|
1032
|
-
if (slash[0]) {
|
|
1033
|
-
addr += slash[0].trim();
|
|
1034
|
-
}
|
|
1035
|
-
if (slash[1]) {
|
|
1036
|
-
return [addr, slash[1].trim()];
|
|
1037
|
-
} else {
|
|
1038
|
-
return [addr, null];
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
from(addr, prefix) {
|
|
1042
|
-
let mapped = null;
|
|
1043
|
-
if (this.mapped) {
|
|
1044
|
-
mapped = this.mapped.clone();
|
|
1045
|
-
}
|
|
1046
|
-
return new IPAddress({
|
|
1047
|
-
ip_bits: this.ip_bits,
|
|
1048
|
-
host_address: addr.clone(),
|
|
1049
|
-
prefix: prefix.clone(),
|
|
1050
|
-
mapped: mapped,
|
|
1051
|
-
vt_is_private: this.vt_is_private,
|
|
1052
|
-
vt_is_loopback: this.vt_is_loopback,
|
|
1053
|
-
vt_to_ipv6: this.vt_to_ipv6
|
|
1054
|
-
});
|
|
1055
|
-
}
|
|
1056
|
-
// True if the object is an IPv4 address
|
|
1057
|
-
//
|
|
1058
|
-
// ip = IPAddress("192.168.10.100/24")
|
|
1059
|
-
//
|
|
1060
|
-
// ip.ipv4?
|
|
1061
|
-
// //-> true
|
|
1062
|
-
//
|
|
1063
|
-
is_ipv4() {
|
|
1064
|
-
return this.ip_bits.version == ip_version_1.default.V4;
|
|
1065
|
-
}
|
|
1066
|
-
// True if the object is an IPv6 address
|
|
1067
|
-
//
|
|
1068
|
-
// ip = IPAddress("192.168.10.100/24")
|
|
1069
|
-
//
|
|
1070
|
-
// ip.ipv6?
|
|
1071
|
-
// //-> false
|
|
1072
|
-
//
|
|
1073
|
-
is_ipv6() {
|
|
1074
|
-
return this.ip_bits.version == ip_version_1.default.V6;
|
|
1075
|
-
}
|
|
1076
|
-
// Checks if the given string is a valid IP address,
|
|
1077
|
-
// either IPv4 or IPv6
|
|
1078
|
-
//
|
|
1079
|
-
// Example:
|
|
1080
|
-
//
|
|
1081
|
-
// IPAddress.valid? "2002.1"
|
|
1082
|
-
// //=> true
|
|
1083
|
-
//
|
|
1084
|
-
// IPAddress.valid? "10.0.0.256"
|
|
1085
|
-
// //=> false
|
|
1086
|
-
//
|
|
1087
|
-
static is_valid(addr) {
|
|
1088
|
-
return IPAddress.is_valid_ipv4(addr) || IPAddress.is_valid_ipv6(addr);
|
|
1089
|
-
}
|
|
1090
|
-
static parse_dec_str(str) {
|
|
1091
|
-
let re_digit = new RegExp("^\\d+$");
|
|
1092
|
-
if (!re_digit.test(str)) {
|
|
1093
|
-
// console.log("parse_dec_str:-1:", str);
|
|
1094
|
-
return null;
|
|
1095
|
-
}
|
|
1096
|
-
let part = parseInt(str, 10);
|
|
1097
|
-
if (isNaN(part)) {
|
|
1098
|
-
// console.log("parse_dec_str:-2:", str, part);
|
|
1099
|
-
return null;
|
|
1100
|
-
}
|
|
1101
|
-
// console.log("parse_dec_str:-3:", str, part);
|
|
1102
|
-
return part;
|
|
1103
|
-
}
|
|
1104
|
-
static parse_hex_str(str) {
|
|
1105
|
-
let re_digit = new RegExp("^[0-9a-fA-F]+$");
|
|
1106
|
-
if (!re_digit.test(str)) {
|
|
1107
|
-
return null;
|
|
1108
|
-
}
|
|
1109
|
-
let part = parseInt(str, 16);
|
|
1110
|
-
if (isNaN(part)) {
|
|
1111
|
-
return null;
|
|
1112
|
-
}
|
|
1113
|
-
return part;
|
|
1114
|
-
}
|
|
1115
|
-
// Checks if the given string is a valid IPv4 address
|
|
1116
|
-
//
|
|
1117
|
-
// Example:
|
|
1118
|
-
//
|
|
1119
|
-
// IPAddress.valid_ipv4? "2002.1"
|
|
1120
|
-
// //=> false
|
|
1121
|
-
//
|
|
1122
|
-
// IPAddress.valid_ipv4? "172.16.10.1"
|
|
1123
|
-
// //=> true
|
|
1124
|
-
//
|
|
1125
|
-
static parse_ipv4_part(i) {
|
|
1126
|
-
let part = IPAddress.parse_dec_str(i);
|
|
1127
|
-
//console.log("i=", i, part);
|
|
1128
|
-
if (part === null || part >= 256) {
|
|
1129
|
-
return null;
|
|
1130
|
-
}
|
|
1131
|
-
return part;
|
|
1132
|
-
}
|
|
1133
|
-
static split_to_u32(addr) {
|
|
1134
|
-
let ip = crunchy_1.default.zero();
|
|
1135
|
-
let shift = 24;
|
|
1136
|
-
let split_addr = addr.split(".");
|
|
1137
|
-
if (split_addr.length > 4) {
|
|
1138
|
-
return null;
|
|
1139
|
-
}
|
|
1140
|
-
let split_addr_len = split_addr.length;
|
|
1141
|
-
if (split_addr_len < 4) {
|
|
1142
|
-
let part = IPAddress.parse_ipv4_part(split_addr[split_addr_len - 1]);
|
|
1143
|
-
if (part === null) {
|
|
1144
|
-
return null;
|
|
1145
|
-
}
|
|
1146
|
-
ip = crunchy_1.default.from_number(part);
|
|
1147
|
-
split_addr = split_addr.slice(0, split_addr_len - 1);
|
|
1148
|
-
}
|
|
1149
|
-
for (let i of split_addr) {
|
|
1150
|
-
let part = IPAddress.parse_ipv4_part(i);
|
|
1151
|
-
// console.log("u32-", addr, i, part);
|
|
1152
|
-
if (part === null) {
|
|
1153
|
-
return null;
|
|
1154
|
-
}
|
|
1155
|
-
//println!("{}-{}", part_num, shift);
|
|
1156
|
-
ip = ip.add(crunchy_1.default.from_number(part).shl(shift));
|
|
1157
|
-
shift -= 8;
|
|
1158
|
-
}
|
|
1159
|
-
return ip;
|
|
1160
|
-
}
|
|
1161
|
-
static is_valid_ipv4(addr) {
|
|
1162
|
-
return IPAddress.split_to_u32(addr) !== null;
|
|
1163
|
-
}
|
|
1164
|
-
// Checks if the given string is a valid IPv6 address
|
|
1165
|
-
//
|
|
1166
|
-
// Example:
|
|
1167
|
-
//
|
|
1168
|
-
// IPAddress.valid_ipv6? "2002.1"
|
|
1169
|
-
// //=> true
|
|
1170
|
-
//
|
|
1171
|
-
// IPAddress.valid_ipv6? "2002.DEAD.BEEF"
|
|
1172
|
-
// //=> false
|
|
1173
|
-
//
|
|
1174
|
-
static split_on_colon(addr) {
|
|
1175
|
-
let parts = addr.trim().split(":");
|
|
1176
|
-
let ip = crunchy_1.default.zero();
|
|
1177
|
-
if (parts.length == 1 && parts[0].length == 0) {
|
|
1178
|
-
return new ResultCrunchyParts(ip, 0);
|
|
1179
|
-
}
|
|
1180
|
-
let parts_len = parts.length;
|
|
1181
|
-
let shift = (parts_len - 1) * 16;
|
|
1182
|
-
for (let i of parts) {
|
|
1183
|
-
//println!("{}={}", addr, i);
|
|
1184
|
-
let part = IPAddress.parse_hex_str(i);
|
|
1185
|
-
if (part === null || part >= 65536) {
|
|
1186
|
-
return null;
|
|
1187
|
-
}
|
|
1188
|
-
ip = ip.add(crunchy_1.default.from_number(part).shl(shift));
|
|
1189
|
-
shift -= 16;
|
|
1190
|
-
}
|
|
1191
|
-
return new ResultCrunchyParts(ip, parts_len);
|
|
1192
|
-
}
|
|
1193
|
-
static split_to_num(addr) {
|
|
1194
|
-
//let ip = 0;
|
|
1195
|
-
let pre_post = addr.trim().split("::");
|
|
1196
|
-
if (pre_post.length > 2) {
|
|
1197
|
-
return null;
|
|
1198
|
-
}
|
|
1199
|
-
if (pre_post.length == 2) {
|
|
1200
|
-
//println!("{}=.={}", pre_post[0], pre_post[1]);
|
|
1201
|
-
let pre = IPAddress.split_on_colon(pre_post[0]);
|
|
1202
|
-
if (!pre) {
|
|
1203
|
-
return pre;
|
|
1204
|
-
}
|
|
1205
|
-
let post = IPAddress.split_on_colon(pre_post[1]);
|
|
1206
|
-
if (!post) {
|
|
1207
|
-
return post;
|
|
1208
|
-
}
|
|
1209
|
-
// println!("pre:{} post:{}", pre_parts, post_parts);
|
|
1210
|
-
return new ResultCrunchyParts(pre.crunchy.shl(128 - pre.parts * 16).add(post.crunchy), 128 / 16);
|
|
1211
|
-
}
|
|
1212
|
-
//println!("split_to_num:no double:{}", addr);
|
|
1213
|
-
let ret = IPAddress.split_on_colon(addr);
|
|
1214
|
-
if (ret == null || ret.parts != 128 / 16) {
|
|
1215
|
-
return null;
|
|
1216
|
-
}
|
|
1217
|
-
return ret;
|
|
1218
|
-
}
|
|
1219
|
-
static is_valid_ipv6(addr) {
|
|
1220
|
-
return IPAddress.split_to_num(addr) != null;
|
|
1221
|
-
}
|
|
1222
|
-
// private helper for summarize
|
|
1223
|
-
// assumes that networks is output from reduce_networks
|
|
1224
|
-
// means it should be sorted lowers first and uniq
|
|
1225
|
-
//
|
|
1226
|
-
static pos_to_idx(pos, len) {
|
|
1227
|
-
let ilen = len;
|
|
1228
|
-
// let ret = pos % ilen;
|
|
1229
|
-
let rem = (pos % ilen + ilen) % ilen;
|
|
1230
|
-
// println!("pos_to_idx:{}:{}=>{}:{}", pos, len, ret, rem);
|
|
1231
|
-
return rem;
|
|
1232
|
-
}
|
|
1233
|
-
static aggregate(networks) {
|
|
1234
|
-
if (networks.length == 0) {
|
|
1235
|
-
return [];
|
|
1236
|
-
}
|
|
1237
|
-
if (networks.length == 1) {
|
|
1238
|
-
// console.log("aggregate:", networks[0], networks[0].network());
|
|
1239
|
-
return [networks[0].network()];
|
|
1240
|
-
}
|
|
1241
|
-
let stack = networks.map(i => i.network()).sort((a, b) => a.cmp(b));
|
|
1242
|
-
// console.log(IPAddress.to_string_vec(stack));
|
|
1243
|
-
// for i in 0..networks.length {
|
|
1244
|
-
// println!("{}==={}", &networks[i].to_string_uncompressed(),
|
|
1245
|
-
// &stack[i].to_string_uncompressed());
|
|
1246
|
-
// }
|
|
1247
|
-
for (let pos = 0; true;) {
|
|
1248
|
-
if (pos < 0) {
|
|
1249
|
-
pos = 0;
|
|
1250
|
-
}
|
|
1251
|
-
let stack_len = stack.length; // borrow checker
|
|
1252
|
-
// println!("loop:{}:{}", pos, stack_len);
|
|
1253
|
-
// if stack_len == 1 {
|
|
1254
|
-
// println!("exit 1");
|
|
1255
|
-
// break;
|
|
1256
|
-
// }
|
|
1257
|
-
if (pos >= stack_len) {
|
|
1258
|
-
// println!("exit first:{}:{}", stack_len, pos);
|
|
1259
|
-
break;
|
|
1260
|
-
}
|
|
1261
|
-
let first = IPAddress.pos_to_idx(pos, stack_len);
|
|
1262
|
-
pos = pos + 1;
|
|
1263
|
-
if (pos >= stack_len) {
|
|
1264
|
-
// println!("exit second:{}:{}", stack_len, pos);
|
|
1265
|
-
break;
|
|
1266
|
-
}
|
|
1267
|
-
let second = IPAddress.pos_to_idx(pos, stack_len);
|
|
1268
|
-
pos = pos + 1;
|
|
1269
|
-
//let firstUnwrap = first;
|
|
1270
|
-
if (stack[first].includes(stack[second])) {
|
|
1271
|
-
pos = pos - 2;
|
|
1272
|
-
// println!("remove:1:{}:{}:{}=>{}", first, second, stack_len, pos + 1);
|
|
1273
|
-
let pidx = IPAddress.pos_to_idx(pos + 1, stack_len);
|
|
1274
|
-
stack = stack.slice(0, pidx).concat(stack.slice(pidx + 1));
|
|
1275
|
-
} else {
|
|
1276
|
-
stack[first].prefix = stack[first].prefix.sub(1);
|
|
1277
|
-
// println!("complex:{}:{}:{}:{}:P1:{}:P2:{}", pos, stack_len,
|
|
1278
|
-
// first, second,
|
|
1279
|
-
// stack[first].to_string(), stack[second].to_string());
|
|
1280
|
-
if (stack[first].prefix.num + 1 == stack[second].prefix.num && stack[first].includes(stack[second])) {
|
|
1281
|
-
pos = pos - 2;
|
|
1282
|
-
let idx = IPAddress.pos_to_idx(pos, stack_len);
|
|
1283
|
-
stack[idx] = stack[first].clone(); // kaputt
|
|
1284
|
-
let pidx = IPAddress.pos_to_idx(pos + 1, stack_len);
|
|
1285
|
-
stack = stack.slice(0, pidx).concat(stack.slice(pidx + 1));
|
|
1286
|
-
// println!("remove-2:{}:{}", pos + 1, stack_len);
|
|
1287
|
-
pos = pos - 1; // backtrack
|
|
1288
|
-
} else {
|
|
1289
|
-
stack[first].prefix = stack[first].prefix.add(1); //reset prefix
|
|
1290
|
-
// println!("easy:{}:{}=>{}", pos, stack_len, stack[first].to_string());
|
|
1291
|
-
pos = pos - 1; // do it with second as first
|
|
1292
|
-
}
|
|
1293
|
-
}
|
|
1294
|
-
}
|
|
1295
|
-
// println!("agg={}:{}", pos, stack.length);
|
|
1296
|
-
return stack.slice(0, stack.length);
|
|
1297
|
-
}
|
|
1298
|
-
parts() {
|
|
1299
|
-
return this.ip_bits.parts(this.host_address);
|
|
1300
|
-
}
|
|
1301
|
-
parts_hex_str() {
|
|
1302
|
-
let ret = [];
|
|
1303
|
-
let leading = 1 << this.ip_bits.part_bits;
|
|
1304
|
-
for (let i of this.parts()) {
|
|
1305
|
-
ret.push((leading + i).toString(16).slice(1));
|
|
1306
|
-
}
|
|
1307
|
-
return ret;
|
|
1308
|
-
}
|
|
1309
|
-
// Returns the IP address in in-addr.arpa format
|
|
1310
|
-
// for DNS Domain definition entries like SOA Records
|
|
1311
|
-
//
|
|
1312
|
-
// ip = IPAddress("172.17.100.50/15")
|
|
1313
|
-
//
|
|
1314
|
-
// ip.dns_rev_domains
|
|
1315
|
-
// // => ["16.172.in-addr.arpa","17.172.in-addr.arpa"]
|
|
1316
|
-
//
|
|
1317
|
-
dns_rev_domains() {
|
|
1318
|
-
let ret = [];
|
|
1319
|
-
for (let net of this.dns_networks()) {
|
|
1320
|
-
// console.log("dns_rev_domains:", this.to_string(), net.to_string());
|
|
1321
|
-
ret.push(net.dns_reverse());
|
|
1322
|
-
}
|
|
1323
|
-
return ret;
|
|
1324
|
-
}
|
|
1325
|
-
dns_reverse() {
|
|
1326
|
-
let ret = "";
|
|
1327
|
-
let dot = "";
|
|
1328
|
-
let dns_parts = this.dns_parts();
|
|
1329
|
-
for (let i = ~~((this.prefix.host_prefix() + (this.ip_bits.dns_bits - 1)) / this.ip_bits.dns_bits); i < this.dns_parts().length; ++i) {
|
|
1330
|
-
// console.log("dns_r", i);
|
|
1331
|
-
ret += dot;
|
|
1332
|
-
ret += this.ip_bits.dns_part_format(dns_parts[i]);
|
|
1333
|
-
dot = ".";
|
|
1334
|
-
}
|
|
1335
|
-
ret += dot;
|
|
1336
|
-
ret += this.ip_bits.rev_domain;
|
|
1337
|
-
return ret;
|
|
1338
|
-
}
|
|
1339
|
-
dns_parts() {
|
|
1340
|
-
let ret = [];
|
|
1341
|
-
let num = this.host_address.clone();
|
|
1342
|
-
let mask = crunchy_1.default.one().shl(this.ip_bits.dns_bits);
|
|
1343
|
-
for (let _ = 0; _ < this.ip_bits.bits / this.ip_bits.dns_bits; _++) {
|
|
1344
|
-
let part = +num.clone().mod(mask).toString();
|
|
1345
|
-
num = num.shr(this.ip_bits.dns_bits);
|
|
1346
|
-
ret.push(part);
|
|
1347
|
-
}
|
|
1348
|
-
return ret;
|
|
1349
|
-
}
|
|
1350
|
-
dns_networks() {
|
|
1351
|
-
// +this.ip_bits.dns_bits-1
|
|
1352
|
-
let next_bit_mask = this.ip_bits.bits - ~~(this.prefix.host_prefix() / this.ip_bits.dns_bits) * this.ip_bits.dns_bits;
|
|
1353
|
-
// console.log("dns_networks-1", this.to_string(), this.prefix.host_prefix();j
|
|
1354
|
-
// this.ip_bits.dns_bits, next_bit_mask);
|
|
1355
|
-
if (next_bit_mask <= 0) {
|
|
1356
|
-
return [this.network()];
|
|
1357
|
-
}
|
|
1358
|
-
// println!("dns_networks:{}:{}", this.to_string(), next_bit_mask);
|
|
1359
|
-
// dns_bits
|
|
1360
|
-
let step_bit_net = crunchy_1.default.one().shl(this.ip_bits.bits - next_bit_mask);
|
|
1361
|
-
if (step_bit_net.eq(crunchy_1.default.zero())) {
|
|
1362
|
-
// console.log("dns_networks-2", this.to_string());
|
|
1363
|
-
return [this.network()];
|
|
1364
|
-
}
|
|
1365
|
-
let ret = [];
|
|
1366
|
-
let step = this.network().host_address;
|
|
1367
|
-
let prefix = this.prefix.from(next_bit_mask);
|
|
1368
|
-
while (step.lte(this.broadcast().host_address)) {
|
|
1369
|
-
// console.log("dns_networks-3", this.to_string(), step.toString(), next_bit_mask, step_bit_net.toString());
|
|
1370
|
-
ret.push(this.from(step, prefix));
|
|
1371
|
-
step = step.add(step_bit_net);
|
|
1372
|
-
}
|
|
1373
|
-
return ret;
|
|
1374
|
-
}
|
|
1375
|
-
// Summarization (or aggregation) is the process when two or more
|
|
1376
|
-
// networks are taken together to check if a supernet, including all
|
|
1377
|
-
// and only these networks, exists. If it exists then this supernet
|
|
1378
|
-
// is called the summarized (or aggregated) network.
|
|
1379
|
-
//
|
|
1380
|
-
// It is very important to understand that summarization can only
|
|
1381
|
-
// occur if there are no holes in the aggregated network, or, in other
|
|
1382
|
-
// words, if the given networks fill completely the address space
|
|
1383
|
-
// of the supernet. So the two rules are:
|
|
1384
|
-
//
|
|
1385
|
-
// 1) The aggregate network must contain +all+ the IP addresses of the
|
|
1386
|
-
// original networks;
|
|
1387
|
-
// 2) The aggregate network must contain +only+ the IP addresses of the
|
|
1388
|
-
// original networks;
|
|
1389
|
-
//
|
|
1390
|
-
// A few examples will help clarify the above. Let's consider for
|
|
1391
|
-
// instance the following two networks:
|
|
1392
|
-
//
|
|
1393
|
-
// ip1 = IPAddress("172.16.10.0/24")
|
|
1394
|
-
// ip2 = IPAddress("172.16.11.0/24")
|
|
1395
|
-
//
|
|
1396
|
-
// These two networks can be expressed using only one IP address
|
|
1397
|
-
// network if we change the prefix. Let Ruby do the work:
|
|
1398
|
-
//
|
|
1399
|
-
// IPAddress.IPv4.summarize(ip1,ip2).to_s
|
|
1400
|
-
// //=> "172.16.10.0/23"
|
|
1401
|
-
//
|
|
1402
|
-
// We note how the network "172.16.10.0/23" includes all the addresses
|
|
1403
|
-
// specified in the above networks, and (more important) includes
|
|
1404
|
-
// ONLY those addresses.
|
|
1405
|
-
//
|
|
1406
|
-
// If we summarized +ip1+ and +ip2+ with the following network:
|
|
1407
|
-
//
|
|
1408
|
-
// "172.16.0.0/16"
|
|
1409
|
-
//
|
|
1410
|
-
// we would have satisfied rule //1 above, but not rule //2. So "172.16.0.0/16"
|
|
1411
|
-
// is not an aggregate network for +ip1+ and +ip2+.
|
|
1412
|
-
//
|
|
1413
|
-
// If it's not possible to compute a single aggregated network for all the
|
|
1414
|
-
// original networks, the method returns an array with all the aggregate
|
|
1415
|
-
// networks found. For example, the following four networks can be
|
|
1416
|
-
// aggregated in a single /22:
|
|
1417
|
-
//
|
|
1418
|
-
// ip1 = IPAddress("10.0.0.1/24")
|
|
1419
|
-
// ip2 = IPAddress("10.0.1.1/24")
|
|
1420
|
-
// ip3 = IPAddress("10.0.2.1/24")
|
|
1421
|
-
// ip4 = IPAddress("10.0.3.1/24")
|
|
1422
|
-
//
|
|
1423
|
-
// IPAddress.IPv4.summarize(ip1,ip2,ip3,ip4).to_string
|
|
1424
|
-
// //=> "10.0.0.0/22",
|
|
1425
|
-
//
|
|
1426
|
-
// But the following networks can't be summarized in a single network:
|
|
1427
|
-
//
|
|
1428
|
-
// ip1 = IPAddress("10.0.1.1/24")
|
|
1429
|
-
// ip2 = IPAddress("10.0.2.1/24")
|
|
1430
|
-
// ip3 = IPAddress("10.0.3.1/24")
|
|
1431
|
-
// ip4 = IPAddress("10.0.4.1/24")
|
|
1432
|
-
//
|
|
1433
|
-
// IPAddress.IPv4.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
|
|
1434
|
-
// //=> ["10.0.1.0/24","10.0.2.0/23","10.0.4.0/24"]
|
|
1435
|
-
//
|
|
1436
|
-
//
|
|
1437
|
-
// Summarization (or aggregation) is the process when two or more
|
|
1438
|
-
// networks are taken together to check if a supernet, including all
|
|
1439
|
-
// and only these networks, exists. If it exists then this supernet
|
|
1440
|
-
// is called the summarized (or aggregated) network.
|
|
1441
|
-
//
|
|
1442
|
-
// It is very important to understand that summarization can only
|
|
1443
|
-
// occur if there are no holes in the aggregated network, or, in other
|
|
1444
|
-
// words, if the given networks fill completely the address space
|
|
1445
|
-
// of the supernet. So the two rules are:
|
|
1446
|
-
//
|
|
1447
|
-
// 1) The aggregate network must contain +all+ the IP addresses of the
|
|
1448
|
-
// original networks;
|
|
1449
|
-
// 2) The aggregate network must contain +only+ the IP addresses of the
|
|
1450
|
-
// original networks;
|
|
1451
|
-
//
|
|
1452
|
-
// A few examples will help clarify the above. Let's consider for
|
|
1453
|
-
// instance the following two networks:
|
|
1454
|
-
//
|
|
1455
|
-
// ip1 = IPAddress("2000:0.4/32")
|
|
1456
|
-
// ip2 = IPAddress("2000:1.6/32")
|
|
1457
|
-
//
|
|
1458
|
-
// These two networks can be expressed using only one IP address
|
|
1459
|
-
// network if we change the prefix. Let Ruby do the work:
|
|
1460
|
-
//
|
|
1461
|
-
// IPAddress.IPv6.summarize(ip1,ip2).to_s
|
|
1462
|
-
// // => "2000:0./31"
|
|
1463
|
-
//
|
|
1464
|
-
// We note how the network "2000:0./31" includes all the addresses
|
|
1465
|
-
// specified in the above networks, and (more important) includes
|
|
1466
|
-
// ONLY those addresses.
|
|
1467
|
-
//
|
|
1468
|
-
// If we summarized +ip1+ and +ip2+ with the following network:
|
|
1469
|
-
//
|
|
1470
|
-
// "2000./16"
|
|
1471
|
-
//
|
|
1472
|
-
// we would have satisfied rule // 1 above, but not rule // 2. So "2000./16"
|
|
1473
|
-
// is not an aggregate network for +ip1+ and +ip2+.
|
|
1474
|
-
//
|
|
1475
|
-
// If it's not possible to compute a single aggregated network for all the
|
|
1476
|
-
// original networks, the method returns an array with all the aggregate
|
|
1477
|
-
// networks found. For example, the following four networks can be
|
|
1478
|
-
// aggregated in a single /22:
|
|
1479
|
-
//
|
|
1480
|
-
// ip1 = IPAddress("2000:0./32")
|
|
1481
|
-
// ip2 = IPAddress("2000:1./32")
|
|
1482
|
-
// ip3 = IPAddress("2000:2./32")
|
|
1483
|
-
// ip4 = IPAddress("2000:3./32")
|
|
1484
|
-
//
|
|
1485
|
-
// IPAddress.IPv6.summarize(ip1,ip2,ip3,ip4).to_string
|
|
1486
|
-
// // => ""2000:3./30",
|
|
1487
|
-
//
|
|
1488
|
-
// But the following networks can't be summarized in a single network:
|
|
1489
|
-
//
|
|
1490
|
-
// ip1 = IPAddress("2000:1./32")
|
|
1491
|
-
// ip2 = IPAddress("2000:2./32")
|
|
1492
|
-
// ip3 = IPAddress("2000:3./32")
|
|
1493
|
-
// ip4 = IPAddress("2000:4./32")
|
|
1494
|
-
//
|
|
1495
|
-
// IPAddress.IPv4.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
|
|
1496
|
-
// // => ["2000:1./32","2000:2./31","2000:4./32"]
|
|
1497
|
-
//
|
|
1498
|
-
static summarize(networks) {
|
|
1499
|
-
return IPAddress.aggregate(networks);
|
|
1500
|
-
}
|
|
1501
|
-
static summarize_str(netstr) {
|
|
1502
|
-
let vec = IPAddress.to_ipaddress_vec(netstr);
|
|
1503
|
-
// console.log(netstr, vec);
|
|
1504
|
-
if (!vec) {
|
|
1505
|
-
return vec;
|
|
1506
|
-
}
|
|
1507
|
-
return IPAddress.aggregate(vec);
|
|
1508
|
-
}
|
|
1509
|
-
ip_same_kind(oth) {
|
|
1510
|
-
return this.ip_bits.version == oth.ip_bits.version;
|
|
1511
|
-
}
|
|
1512
|
-
// Returns true if the address is an unspecified address
|
|
1513
|
-
//
|
|
1514
|
-
// See IPAddress.IPv6.Unspecified for more information
|
|
1515
|
-
//
|
|
1516
|
-
is_unspecified() {
|
|
1517
|
-
return this.host_address.eq(crunchy_1.default.zero());
|
|
1518
|
-
}
|
|
1519
|
-
// Returns true if the address is a loopback address
|
|
1520
|
-
//
|
|
1521
|
-
// See IPAddress.IPv6.Loopback for more information
|
|
1522
|
-
//
|
|
1523
|
-
is_loopback() {
|
|
1524
|
-
return this.vt_is_loopback(this);
|
|
1525
|
-
}
|
|
1526
|
-
// Returns true if the address is a mapped address
|
|
1527
|
-
//
|
|
1528
|
-
// See IPAddress.IPv6.Mapped for more information
|
|
1529
|
-
//
|
|
1530
|
-
is_mapped() {
|
|
1531
|
-
let ret = this.mapped !== null && this.host_address.shr(32).eq(crunchy_1.default.one().shl(16).sub(crunchy_1.default.one()));
|
|
1532
|
-
// console.log("+++++++++++", this.mapped, ret);
|
|
1533
|
-
return ret;
|
|
1534
|
-
}
|
|
1535
|
-
// Returns the prefix portion of the IPv4 object
|
|
1536
|
-
// as a IPAddress.Prefix32 object
|
|
1537
|
-
//
|
|
1538
|
-
// ip = IPAddress("172.16.100.4/22")
|
|
1539
|
-
//
|
|
1540
|
-
// ip.prefix
|
|
1541
|
-
// // => 22
|
|
1542
|
-
//
|
|
1543
|
-
// ip.prefix.class
|
|
1544
|
-
// // => IPAddress.Prefix32
|
|
1545
|
-
//
|
|
1546
|
-
// public prefix(): Prefix {
|
|
1547
|
-
// return this.prefix;
|
|
1548
|
-
// }
|
|
1549
|
-
// Checks if the argument is a valid IPv4 netmask
|
|
1550
|
-
// expressed in dotted decimal format.
|
|
1551
|
-
//
|
|
1552
|
-
// IPAddress.valid_ipv4_netmask? "255.255.0.0"
|
|
1553
|
-
// //=> true
|
|
1554
|
-
//
|
|
1555
|
-
static is_valid_netmask(addr) {
|
|
1556
|
-
return IPAddress.parse_netmask_to_prefix(addr) !== null;
|
|
1557
|
-
}
|
|
1558
|
-
static netmask_to_prefix(nm, bits) {
|
|
1559
|
-
let prefix = 0;
|
|
1560
|
-
let addr = nm.clone();
|
|
1561
|
-
let in_host_part = true;
|
|
1562
|
-
// let two = Crunchy.two();
|
|
1563
|
-
for (let _ = 0; _ < bits; _++) {
|
|
1564
|
-
let bit = addr.mds(2);
|
|
1565
|
-
// console.log(">>>", bits, bit, addr, nm);
|
|
1566
|
-
if (in_host_part && bit == 0) {
|
|
1567
|
-
prefix = prefix + 1;
|
|
1568
|
-
} else if (in_host_part && bit == 1) {
|
|
1569
|
-
in_host_part = false;
|
|
1570
|
-
} else if (!in_host_part && bit == 0) {
|
|
1571
|
-
return null;
|
|
1572
|
-
}
|
|
1573
|
-
addr = addr.shr(1);
|
|
1574
|
-
}
|
|
1575
|
-
return bits - prefix;
|
|
1576
|
-
}
|
|
1577
|
-
static parse_netmask_to_prefix(netmask) {
|
|
1578
|
-
// console.log("--1", netmask);
|
|
1579
|
-
let is_number = IPAddress.parse_dec_str(netmask);
|
|
1580
|
-
if (is_number !== null) {
|
|
1581
|
-
// console.log("--2", netmask, is_number);
|
|
1582
|
-
return is_number;
|
|
1583
|
-
}
|
|
1584
|
-
let my = IPAddress.parse(netmask);
|
|
1585
|
-
// console.log("--3", netmask, my);
|
|
1586
|
-
if (!my) {
|
|
1587
|
-
// console.log("--4", netmask, my);
|
|
1588
|
-
return null;
|
|
1589
|
-
}
|
|
1590
|
-
// console.log("--5", netmask, my);
|
|
1591
|
-
let my_ip = my;
|
|
1592
|
-
return IPAddress.netmask_to_prefix(my_ip.host_address, my_ip.ip_bits.bits);
|
|
1593
|
-
}
|
|
1594
|
-
// Set a new prefix number for the object
|
|
1595
|
-
//
|
|
1596
|
-
// This is useful if you want to change the prefix
|
|
1597
|
-
// to an object created with IPv4.parse_u32 or
|
|
1598
|
-
// if the object was created using the classful
|
|
1599
|
-
// mask.
|
|
1600
|
-
//
|
|
1601
|
-
// ip = IPAddress("172.16.100.4")
|
|
1602
|
-
//
|
|
1603
|
-
// puts ip
|
|
1604
|
-
// // => 172.16.100.4/16
|
|
1605
|
-
//
|
|
1606
|
-
// ip.prefix = 22
|
|
1607
|
-
//
|
|
1608
|
-
// puts ip
|
|
1609
|
-
// // => 172.16.100.4/22
|
|
1610
|
-
//
|
|
1611
|
-
change_prefix(num) {
|
|
1612
|
-
let prefix = this.prefix.from(num);
|
|
1613
|
-
if (!prefix) {
|
|
1614
|
-
return null;
|
|
1615
|
-
}
|
|
1616
|
-
return this.from(this.host_address, prefix);
|
|
1617
|
-
}
|
|
1618
|
-
change_netmask(str) {
|
|
1619
|
-
let nm = IPAddress.parse_netmask_to_prefix(str);
|
|
1620
|
-
if (!nm) {
|
|
1621
|
-
return null;
|
|
1622
|
-
}
|
|
1623
|
-
return this.change_prefix(nm);
|
|
1624
|
-
}
|
|
1625
|
-
// Returns a string with the IP address in canonical
|
|
1626
|
-
// form.
|
|
1627
|
-
//
|
|
1628
|
-
// ip = IPAddress("172.16.100.4/22")
|
|
1629
|
-
//
|
|
1630
|
-
// ip.to_string
|
|
1631
|
-
// // => "172.16.100.4/22"
|
|
1632
|
-
//
|
|
1633
|
-
to_string() {
|
|
1634
|
-
let ret = "";
|
|
1635
|
-
ret += this.to_s();
|
|
1636
|
-
ret += "/";
|
|
1637
|
-
ret += this.prefix.to_s();
|
|
1638
|
-
return ret;
|
|
1639
|
-
}
|
|
1640
|
-
to_s() {
|
|
1641
|
-
return this.ip_bits.as_compressed_string(this.host_address);
|
|
1642
|
-
}
|
|
1643
|
-
to_string_uncompressed() {
|
|
1644
|
-
let ret = "";
|
|
1645
|
-
ret += this.to_s_uncompressed();
|
|
1646
|
-
ret += "/";
|
|
1647
|
-
ret += this.prefix.to_s();
|
|
1648
|
-
return ret;
|
|
1649
|
-
}
|
|
1650
|
-
to_s_uncompressed() {
|
|
1651
|
-
return this.ip_bits.as_uncompressed_string(this.host_address);
|
|
1652
|
-
}
|
|
1653
|
-
to_s_mapped() {
|
|
1654
|
-
if (this.is_mapped()) {
|
|
1655
|
-
return `::ffff:${ this.mapped.to_s() }`;
|
|
1656
|
-
}
|
|
1657
|
-
return this.to_s();
|
|
1658
|
-
}
|
|
1659
|
-
to_string_mapped() {
|
|
1660
|
-
if (this.is_mapped()) {
|
|
1661
|
-
let mapped = this.mapped.clone();
|
|
1662
|
-
return `${ this.to_s_mapped() }/${ mapped.prefix.num }`;
|
|
1663
|
-
}
|
|
1664
|
-
return this.to_string();
|
|
1665
|
-
}
|
|
1666
|
-
// Returns the address portion of an IP in binary format,
|
|
1667
|
-
// as a string containing a sequence of 0 and 1
|
|
1668
|
-
//
|
|
1669
|
-
// ip = IPAddress("127.0.0.1")
|
|
1670
|
-
//
|
|
1671
|
-
// ip.bits
|
|
1672
|
-
// // => "01111111000000000000000000000001"
|
|
1673
|
-
//
|
|
1674
|
-
bits() {
|
|
1675
|
-
let num = this.host_address.toString(2);
|
|
1676
|
-
let ret = "";
|
|
1677
|
-
for (let _ = num.length; _ < this.ip_bits.bits; _++) {
|
|
1678
|
-
ret += "0";
|
|
1679
|
-
}
|
|
1680
|
-
ret += num;
|
|
1681
|
-
return ret;
|
|
1682
|
-
}
|
|
1683
|
-
to_hex() {
|
|
1684
|
-
return this.host_address.toString(16);
|
|
1685
|
-
}
|
|
1686
|
-
netmask() {
|
|
1687
|
-
return this.from(this.prefix.netmask(), this.prefix);
|
|
1688
|
-
}
|
|
1689
|
-
// Returns the broadcast address for the given IP.
|
|
1690
|
-
//
|
|
1691
|
-
// ip = IPAddress("172.16.10.64/24")
|
|
1692
|
-
//
|
|
1693
|
-
// ip.broadcast.to_s
|
|
1694
|
-
// // => "172.16.10.255"
|
|
1695
|
-
//
|
|
1696
|
-
broadcast() {
|
|
1697
|
-
return this.from(this.network().host_address.add(this.size().sub(crunchy_1.default.one())), this.prefix);
|
|
1698
|
-
// IPv4.parse_u32(this.broadcast_u32, this.prefix)
|
|
1699
|
-
}
|
|
1700
|
-
// Checks if the IP address is actually a network
|
|
1701
|
-
//
|
|
1702
|
-
// ip = IPAddress("172.16.10.64/24")
|
|
1703
|
-
//
|
|
1704
|
-
// ip.network?
|
|
1705
|
-
// // => false
|
|
1706
|
-
//
|
|
1707
|
-
// ip = IPAddress("172.16.10.64/26")
|
|
1708
|
-
//
|
|
1709
|
-
// ip.network?
|
|
1710
|
-
// // => true
|
|
1711
|
-
//
|
|
1712
|
-
is_network() {
|
|
1713
|
-
return this.prefix.num != this.ip_bits.bits && this.host_address.eq(this.network().host_address);
|
|
1714
|
-
}
|
|
1715
|
-
// Returns a new IPv4 object with the network number
|
|
1716
|
-
// for the given IP.
|
|
1717
|
-
//
|
|
1718
|
-
// ip = IPAddress("172.16.10.64/24")
|
|
1719
|
-
//
|
|
1720
|
-
// ip.network.to_s
|
|
1721
|
-
// // => "172.16.10.0"
|
|
1722
|
-
//
|
|
1723
|
-
network() {
|
|
1724
|
-
return this.from(IPAddress.to_network(this.host_address, this.prefix.host_prefix()), this.prefix);
|
|
1725
|
-
}
|
|
1726
|
-
static to_network(adr, host_prefix) {
|
|
1727
|
-
return adr.shr(host_prefix).shl(host_prefix);
|
|
1728
|
-
}
|
|
1729
|
-
sub(other) {
|
|
1730
|
-
if (this.host_address.gt(other.host_address)) {
|
|
1731
|
-
return this.host_address.clone().sub(other.host_address);
|
|
1732
|
-
}
|
|
1733
|
-
return other.host_address.clone().sub(this.host_address);
|
|
1734
|
-
}
|
|
1735
|
-
add(other) {
|
|
1736
|
-
return IPAddress.aggregate([this.clone(), other.clone()]);
|
|
1737
|
-
}
|
|
1738
|
-
static to_s_vec(vec) {
|
|
1739
|
-
let ret = [];
|
|
1740
|
-
for (let i of vec) {
|
|
1741
|
-
ret.push(i.to_s());
|
|
1742
|
-
}
|
|
1743
|
-
return ret;
|
|
1744
|
-
}
|
|
1745
|
-
static to_string_vec(vec) {
|
|
1746
|
-
let ret = [];
|
|
1747
|
-
for (let i of vec) {
|
|
1748
|
-
ret.push(i.to_string());
|
|
1749
|
-
}
|
|
1750
|
-
return ret;
|
|
1751
|
-
}
|
|
1752
|
-
static to_ipaddress_vec(vec) {
|
|
1753
|
-
let ret = [];
|
|
1754
|
-
for (let ipstr of vec) {
|
|
1755
|
-
let ipa = IPAddress.parse(ipstr);
|
|
1756
|
-
if (!ipa) {
|
|
1757
|
-
return null;
|
|
1758
|
-
}
|
|
1759
|
-
ret.push(ipa);
|
|
1760
|
-
}
|
|
1761
|
-
return ret;
|
|
1762
|
-
}
|
|
1763
|
-
// Returns a new IPv4 object with the
|
|
1764
|
-
// first host IP address in the range.
|
|
1765
|
-
//
|
|
1766
|
-
// Example: given the 192.168.100.0/24 network, the first
|
|
1767
|
-
// host IP address is 192.168.100.1.
|
|
1768
|
-
//
|
|
1769
|
-
// ip = IPAddress("192.168.100.0/24")
|
|
1770
|
-
//
|
|
1771
|
-
// ip.first.to_s
|
|
1772
|
-
// // => "192.168.100.1"
|
|
1773
|
-
//
|
|
1774
|
-
// The object IP doesn't need to be a network: the method
|
|
1775
|
-
// automatically gets the network number from it
|
|
1776
|
-
//
|
|
1777
|
-
// ip = IPAddress("192.168.100.50/24")
|
|
1778
|
-
//
|
|
1779
|
-
// ip.first.to_s
|
|
1780
|
-
// // => "192.168.100.1"
|
|
1781
|
-
//
|
|
1782
|
-
first() {
|
|
1783
|
-
return this.from(this.network().host_address.add(this.ip_bits.host_ofs), this.prefix);
|
|
1784
|
-
}
|
|
1785
|
-
// Like its sibling method IPv4// first, this method
|
|
1786
|
-
// returns a new IPv4 object with the
|
|
1787
|
-
// last host IP address in the range.
|
|
1788
|
-
//
|
|
1789
|
-
// Example: given the 192.168.100.0/24 network, the last
|
|
1790
|
-
// host IP address is 192.168.100.254
|
|
1791
|
-
//
|
|
1792
|
-
// ip = IPAddress("192.168.100.0/24")
|
|
1793
|
-
//
|
|
1794
|
-
// ip.last.to_s
|
|
1795
|
-
// // => "192.168.100.254"
|
|
1796
|
-
//
|
|
1797
|
-
// The object IP doesn't need to be a network: the method
|
|
1798
|
-
// automatically gets the network number from it
|
|
1799
|
-
//
|
|
1800
|
-
// ip = IPAddress("192.168.100.50/24")
|
|
1801
|
-
//
|
|
1802
|
-
// ip.last.to_s
|
|
1803
|
-
// // => "192.168.100.254"
|
|
1804
|
-
//
|
|
1805
|
-
last() {
|
|
1806
|
-
return this.from(this.broadcast().host_address.sub(this.ip_bits.host_ofs), this.prefix);
|
|
1807
|
-
}
|
|
1808
|
-
// Iterates over all the hosts IP addresses for the given
|
|
1809
|
-
// network (or IP address).
|
|
1810
|
-
//
|
|
1811
|
-
// ip = IPAddress("10.0.0.1/29")
|
|
1812
|
-
//
|
|
1813
|
-
// ip.each_host do |i|
|
|
1814
|
-
// p i.to_s
|
|
1815
|
-
// end
|
|
1816
|
-
// // => "10.0.0.1"
|
|
1817
|
-
// // => "10.0.0.2"
|
|
1818
|
-
// // => "10.0.0.3"
|
|
1819
|
-
// // => "10.0.0.4"
|
|
1820
|
-
// // => "10.0.0.5"
|
|
1821
|
-
// // => "10.0.0.6"
|
|
1822
|
-
//
|
|
1823
|
-
each_host(func) {
|
|
1824
|
-
let i = this.first().host_address;
|
|
1825
|
-
while (i.lte(this.last().host_address)) {
|
|
1826
|
-
func(this.from(i, this.prefix));
|
|
1827
|
-
i = i.add(crunchy_1.default.one());
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
inc() {
|
|
1831
|
-
let ret = this.clone();
|
|
1832
|
-
ret.host_address = ret.host_address.add(crunchy_1.default.one());
|
|
1833
|
-
if (ret.lte(this.last())) {
|
|
1834
|
-
return ret;
|
|
1835
|
-
}
|
|
1836
|
-
return null;
|
|
1837
|
-
}
|
|
1838
|
-
dec() {
|
|
1839
|
-
let ret = this.clone();
|
|
1840
|
-
ret.host_address = ret.host_address.sub(crunchy_1.default.one());
|
|
1841
|
-
if (ret.gte(this.first())) {
|
|
1842
|
-
return ret;
|
|
1843
|
-
}
|
|
1844
|
-
return null;
|
|
1845
|
-
}
|
|
1846
|
-
// Iterates over all the IP addresses for the given
|
|
1847
|
-
// network (or IP address).
|
|
1848
|
-
//
|
|
1849
|
-
// The object yielded is a new IPv4 object created
|
|
1850
|
-
// from the iteration.
|
|
1851
|
-
//
|
|
1852
|
-
// ip = IPAddress("10.0.0.1/29")
|
|
1853
|
-
//
|
|
1854
|
-
// ip.each do |i|
|
|
1855
|
-
// p i.address
|
|
1856
|
-
// end
|
|
1857
|
-
// // => "10.0.0.0"
|
|
1858
|
-
// // => "10.0.0.1"
|
|
1859
|
-
// // => "10.0.0.2"
|
|
1860
|
-
// // => "10.0.0.3"
|
|
1861
|
-
// // => "10.0.0.4"
|
|
1862
|
-
// // => "10.0.0.5"
|
|
1863
|
-
// // => "10.0.0.6"
|
|
1864
|
-
// // => "10.0.0.7"
|
|
1865
|
-
//
|
|
1866
|
-
each(func) {
|
|
1867
|
-
let i = this.network().host_address;
|
|
1868
|
-
while (i <= this.broadcast().host_address) {
|
|
1869
|
-
func(this.from(i, this.prefix));
|
|
1870
|
-
i = i.add(crunchy_1.default.one());
|
|
1871
|
-
}
|
|
1872
|
-
}
|
|
1873
|
-
// Spaceship operator to compare IPv4 objects
|
|
1874
|
-
//
|
|
1875
|
-
// Comparing IPv4 addresses is useful to ordinate
|
|
1876
|
-
// them into lists that match our intuitive
|
|
1877
|
-
// perception of ordered IP addresses.
|
|
1878
|
-
//
|
|
1879
|
-
// The first comparison criteria is the u32 value.
|
|
1880
|
-
// For example, 10.100.100.1 will be considered
|
|
1881
|
-
// to be less than 172.16.0.1, because, in a ordered list,
|
|
1882
|
-
// we expect 10.100.100.1 to come before 172.16.0.1.
|
|
1883
|
-
//
|
|
1884
|
-
// The second criteria, in case two IPv4 objects
|
|
1885
|
-
// have identical addresses, is the prefix. An higher
|
|
1886
|
-
// prefix will be considered greater than a lower
|
|
1887
|
-
// prefix. This is because we expect to see
|
|
1888
|
-
// 10.100.100.0/24 come before 10.100.100.0/25.
|
|
1889
|
-
//
|
|
1890
|
-
// Example:
|
|
1891
|
-
//
|
|
1892
|
-
// ip1 = IPAddress "10.100.100.1/8"
|
|
1893
|
-
// ip2 = IPAddress "172.16.0.1/16"
|
|
1894
|
-
// ip3 = IPAddress "10.100.100.1/16"
|
|
1895
|
-
//
|
|
1896
|
-
// ip1 < ip2
|
|
1897
|
-
// // => true
|
|
1898
|
-
// ip1 > ip3
|
|
1899
|
-
// // => false
|
|
1900
|
-
//
|
|
1901
|
-
// [ip1,ip2,ip3].sort.map{|i| i.to_string}
|
|
1902
|
-
// // => ["10.100.100.1/8","10.100.100.1/16","172.16.0.1/16"]
|
|
1903
|
-
//
|
|
1904
|
-
// Returns the number of IP addresses included
|
|
1905
|
-
// in the network. It also counts the network
|
|
1906
|
-
// address and the broadcast address.
|
|
1907
|
-
//
|
|
1908
|
-
// ip = IPAddress("10.0.0.1/29")
|
|
1909
|
-
//
|
|
1910
|
-
// ip.size
|
|
1911
|
-
// // => 8
|
|
1912
|
-
//
|
|
1913
|
-
size() {
|
|
1914
|
-
return crunchy_1.default.one().shl(this.prefix.host_prefix());
|
|
1915
|
-
}
|
|
1916
|
-
is_same_kind(oth) {
|
|
1917
|
-
return this.is_ipv4() == oth.is_ipv4() && this.is_ipv6() == oth.is_ipv6();
|
|
1918
|
-
}
|
|
1919
|
-
// Checks whether a subnet includes the given IP address.
|
|
1920
|
-
//
|
|
1921
|
-
// Accepts an IPAddress.IPv4 object.
|
|
1922
|
-
//
|
|
1923
|
-
// ip = IPAddress("192.168.10.100/24")
|
|
1924
|
-
//
|
|
1925
|
-
// addr = IPAddress("192.168.10.102/24")
|
|
1926
|
-
//
|
|
1927
|
-
// ip.include? addr
|
|
1928
|
-
// // => true
|
|
1929
|
-
//
|
|
1930
|
-
// ip.include? IPAddress("172.16.0.48/16")
|
|
1931
|
-
// // => false
|
|
1932
|
-
//
|
|
1933
|
-
includes(oth) {
|
|
1934
|
-
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()));
|
|
1935
|
-
// println!("includes:{}=={}=>{}", this.to_string(), oth.to_string(), ret);
|
|
1936
|
-
return ret;
|
|
1937
|
-
}
|
|
1938
|
-
// Checks whether a subnet includes all the
|
|
1939
|
-
// given IPv4 objects.
|
|
1940
|
-
//
|
|
1941
|
-
// ip = IPAddress("192.168.10.100/24")
|
|
1942
|
-
//
|
|
1943
|
-
// addr1 = IPAddress("192.168.10.102/24")
|
|
1944
|
-
// addr2 = IPAddress("192.168.10.103/24")
|
|
1945
|
-
//
|
|
1946
|
-
// ip.include_all?(addr1,addr2)
|
|
1947
|
-
// // => true
|
|
1948
|
-
//
|
|
1949
|
-
includes_all(oths) {
|
|
1950
|
-
for (let oth of oths) {
|
|
1951
|
-
if (!this.includes(oth)) {
|
|
1952
|
-
return false;
|
|
1953
|
-
}
|
|
1954
|
-
}
|
|
1955
|
-
return true;
|
|
1956
|
-
}
|
|
1957
|
-
// Checks if an IPv4 address objects belongs
|
|
1958
|
-
// to a private network RFC1918
|
|
1959
|
-
//
|
|
1960
|
-
// Example:
|
|
1961
|
-
//
|
|
1962
|
-
// ip = IPAddress "10.1.1.1/24"
|
|
1963
|
-
// ip.private?
|
|
1964
|
-
// // => true
|
|
1965
|
-
//
|
|
1966
|
-
is_private() {
|
|
1967
|
-
return this.vt_is_private(this);
|
|
1968
|
-
}
|
|
1969
|
-
// Splits a network into different subnets
|
|
1970
|
-
//
|
|
1971
|
-
// If the IP Address is a network, it can be divided into
|
|
1972
|
-
// multiple networks. If +self+ is not a network, this
|
|
1973
|
-
// method will calculate the network from the IP and then
|
|
1974
|
-
// subnet it.
|
|
1975
|
-
//
|
|
1976
|
-
// If +subnets+ is an power of two number, the resulting
|
|
1977
|
-
// networks will be divided evenly from the supernet.
|
|
1978
|
-
//
|
|
1979
|
-
// network = IPAddress("172.16.10.0/24")
|
|
1980
|
-
//
|
|
1981
|
-
// network / 4 // implies map{|i| i.to_string}
|
|
1982
|
-
// // => ["172.16.10.0/26",
|
|
1983
|
-
// "172.16.10.64/26",
|
|
1984
|
-
// "172.16.10.128/26",
|
|
1985
|
-
// "172.16.10.192/26"]
|
|
1986
|
-
//
|
|
1987
|
-
// If +num+ is any other number, the supernet will be
|
|
1988
|
-
// divided into some networks with a even number of hosts and
|
|
1989
|
-
// other networks with the remaining addresses.
|
|
1990
|
-
//
|
|
1991
|
-
// network = IPAddress("172.16.10.0/24")
|
|
1992
|
-
//
|
|
1993
|
-
// network / 3 // implies map{|i| i.to_string}
|
|
1994
|
-
// // => ["172.16.10.0/26",
|
|
1995
|
-
// "172.16.10.64/26",
|
|
1996
|
-
// "172.16.10.128/25"]
|
|
1997
|
-
//
|
|
1998
|
-
// Returns an array of IPv4 objects
|
|
1999
|
-
//
|
|
2000
|
-
sum_first_found(arr) {
|
|
2001
|
-
let dup = arr.slice();
|
|
2002
|
-
if (dup.length < 2) {
|
|
2003
|
-
return dup;
|
|
2004
|
-
}
|
|
2005
|
-
for (let i = dup.length - 2; i >= 0; --i) {
|
|
2006
|
-
// console.log("sum_first_found:", dup[i], dup[i + 1]);
|
|
2007
|
-
let a = IPAddress.summarize([dup[i], dup[i + 1]]);
|
|
2008
|
-
// println!("dup:{}:{}:{}", dup.length, i, a.length);
|
|
2009
|
-
if (a.length == 1) {
|
|
2010
|
-
dup[i] = a[0];
|
|
2011
|
-
dup = dup.slice(0, i + 1).concat(dup.slice(i + 2));
|
|
2012
|
-
break;
|
|
2013
|
-
}
|
|
2014
|
-
}
|
|
2015
|
-
return dup;
|
|
2016
|
-
}
|
|
2017
|
-
split(subnets) {
|
|
2018
|
-
if (subnets == 0 || 1 << this.prefix.host_prefix() <= subnets) {
|
|
2019
|
-
return null;
|
|
2020
|
-
}
|
|
2021
|
-
let networks = this.subnet(this.newprefix(subnets).num);
|
|
2022
|
-
if (!networks) {
|
|
2023
|
-
return networks;
|
|
2024
|
-
}
|
|
2025
|
-
let net = networks;
|
|
2026
|
-
while (net.length != subnets) {
|
|
2027
|
-
net = this.sum_first_found(net);
|
|
2028
|
-
}
|
|
2029
|
-
return net;
|
|
2030
|
-
}
|
|
2031
|
-
// alias_method :/, :split
|
|
2032
|
-
// Returns a new IPv4 object from the supernetting
|
|
2033
|
-
// of the instance network.
|
|
2034
|
-
//
|
|
2035
|
-
// Supernetting is similar to subnetting, except
|
|
2036
|
-
// that you getting as a result a network with a
|
|
2037
|
-
// smaller prefix (bigger host space). For example,
|
|
2038
|
-
// given the network
|
|
2039
|
-
//
|
|
2040
|
-
// ip = IPAddress("172.16.10.0/24")
|
|
2041
|
-
//
|
|
2042
|
-
// you can supernet it with a new /23 prefix
|
|
2043
|
-
//
|
|
2044
|
-
// ip.supernet(23).to_string
|
|
2045
|
-
// // => "172.16.10.0/23"
|
|
2046
|
-
//
|
|
2047
|
-
// However if you supernet it with a /22 prefix, the
|
|
2048
|
-
// network address will change:
|
|
2049
|
-
//
|
|
2050
|
-
// ip.supernet(22).to_string
|
|
2051
|
-
// // => "172.16.8.0/22"
|
|
2052
|
-
//
|
|
2053
|
-
// If +new_prefix+ is less than 1, returns 0.0.0.0/0
|
|
2054
|
-
//
|
|
2055
|
-
supernet(new_prefix) {
|
|
2056
|
-
if (new_prefix >= this.prefix.num) {
|
|
2057
|
-
return null;
|
|
2058
|
-
}
|
|
2059
|
-
// let new_ip = this.host_address.clone();
|
|
2060
|
-
// for _ in new_prefix..this.prefix.num {
|
|
2061
|
-
// new_ip = new_ip << 1;
|
|
2062
|
-
// }
|
|
2063
|
-
return this.from(this.host_address, this.prefix.from(new_prefix)).network();
|
|
2064
|
-
}
|
|
2065
|
-
// This method implements the subnetting function
|
|
2066
|
-
// similar to the one described in RFC3531.
|
|
2067
|
-
//
|
|
2068
|
-
// By specifying a new prefix, the method calculates
|
|
2069
|
-
// the network number for the given IPv4 object
|
|
2070
|
-
// and calculates the subnets associated to the new
|
|
2071
|
-
// prefix.
|
|
2072
|
-
//
|
|
2073
|
-
// For example, given the following network:
|
|
2074
|
-
//
|
|
2075
|
-
// ip = IPAddress "172.16.10.0/24"
|
|
2076
|
-
//
|
|
2077
|
-
// we can calculate the subnets with a /26 prefix
|
|
2078
|
-
//
|
|
2079
|
-
// ip.subnets(26).map(:to_string)
|
|
2080
|
-
// // => ["172.16.10.0/26", "172.16.10.64/26",
|
|
2081
|
-
// "172.16.10.128/26", "172.16.10.192/26"]
|
|
2082
|
-
//
|
|
2083
|
-
// The resulting number of subnets will of course always be
|
|
2084
|
-
// a power of two.
|
|
2085
|
-
//
|
|
2086
|
-
subnet(subprefix) {
|
|
2087
|
-
if (subprefix < this.prefix.num || this.ip_bits.bits < subprefix) {
|
|
2088
|
-
return null;
|
|
2089
|
-
}
|
|
2090
|
-
let ret = [];
|
|
2091
|
-
let net = this.network();
|
|
2092
|
-
net.prefix = net.prefix.from(subprefix);
|
|
2093
|
-
for (let _ = 0; _ < 1 << subprefix - this.prefix.num; _++) {
|
|
2094
|
-
ret.push(net.clone());
|
|
2095
|
-
net = net.from(net.host_address, net.prefix);
|
|
2096
|
-
let size = net.size();
|
|
2097
|
-
net.host_address = net.host_address.add(size);
|
|
2098
|
-
}
|
|
2099
|
-
return ret;
|
|
2100
|
-
}
|
|
2101
|
-
// Return the ip address in a format compatible
|
|
2102
|
-
// with the IPv6 Mapped IPv4 addresses
|
|
2103
|
-
//
|
|
2104
|
-
// Example:
|
|
2105
|
-
//
|
|
2106
|
-
// ip = IPAddress("172.16.10.1/24")
|
|
2107
|
-
//
|
|
2108
|
-
// ip.to_ipv6
|
|
2109
|
-
// // => "ac10:0a01"
|
|
2110
|
-
//
|
|
2111
|
-
to_ipv6() {
|
|
2112
|
-
return this.vt_to_ipv6(this);
|
|
2113
|
-
}
|
|
2114
|
-
newprefix(num) {
|
|
2115
|
-
for (let i = num; i < this.ip_bits.bits; ++i) {
|
|
2116
|
-
let a = ~~Math.log2(i);
|
|
2117
|
-
if (a == Math.log2(i)) {
|
|
2118
|
-
return this.prefix.add(a);
|
|
2119
|
-
}
|
|
2120
|
-
}
|
|
2121
|
-
return null;
|
|
2122
|
-
}
|
|
2123
|
-
}
|
|
2124
|
-
exports.IPAddress = IPAddress;
|
|
2125
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2126
|
-
exports.default = IPAddress;
|
|
2127
|
-
|
|
2128
|
-
/***/ },
|
|
2129
|
-
/* 7 */
|
|
2130
|
-
/***/ function(module, exports, __webpack_require__) {
|
|
2131
|
-
|
|
2132
|
-
"use strict";
|
|
2133
|
-
|
|
2134
|
-
const crunchy_1 = __webpack_require__(1);
|
|
2135
|
-
const prefix32_1 = __webpack_require__(8);
|
|
2136
|
-
const ipaddress_1 = __webpack_require__(6);
|
|
2137
|
-
const ip_bits_1 = __webpack_require__(3);
|
|
2138
|
-
const prefix128_1 = __webpack_require__(9);
|
|
2139
|
-
const ipv6_1 = __webpack_require__(10);
|
|
2140
|
-
class Ipv4 {
|
|
2141
|
-
static from_number(addr, prefix_num) {
|
|
2142
|
-
let prefix = prefix32_1.default.create(prefix_num);
|
|
2143
|
-
if (!prefix) {
|
|
2144
|
-
return null;
|
|
2145
|
-
}
|
|
2146
|
-
return new ipaddress_1.default({
|
|
2147
|
-
ip_bits: ip_bits_1.default.v4(),
|
|
2148
|
-
host_address: addr.clone(),
|
|
2149
|
-
prefix: prefix,
|
|
2150
|
-
mapped: null,
|
|
2151
|
-
vt_is_private: Ipv4.ipv4_is_private,
|
|
2152
|
-
vt_is_loopback: Ipv4.ipv4_is_loopback,
|
|
2153
|
-
vt_to_ipv6: Ipv4.to_ipv6
|
|
2154
|
-
});
|
|
2155
|
-
}
|
|
2156
|
-
static create(str) {
|
|
2157
|
-
// console.log("create:v4:", str);
|
|
2158
|
-
// let enable = str == "0.0.0.0/0";
|
|
2159
|
-
let tmp = ipaddress_1.default.split_at_slash(str);
|
|
2160
|
-
let ip = tmp[0];
|
|
2161
|
-
let netmask = tmp[1];
|
|
2162
|
-
if (!ipaddress_1.default.is_valid_ipv4(ip)) {
|
|
2163
|
-
// enable && console.log("xx1");
|
|
2164
|
-
return null;
|
|
2165
|
-
}
|
|
2166
|
-
let ip_prefix_num = 32;
|
|
2167
|
-
if (netmask) {
|
|
2168
|
-
// netmask is defined
|
|
2169
|
-
ip_prefix_num = ipaddress_1.default.parse_netmask_to_prefix(netmask);
|
|
2170
|
-
if (ip_prefix_num === null) {
|
|
2171
|
-
// enable && console.log("xx2");
|
|
2172
|
-
return null;
|
|
2173
|
-
}
|
|
2174
|
-
}
|
|
2175
|
-
let ip_prefix = prefix32_1.default.create(ip_prefix_num);
|
|
2176
|
-
if (ip_prefix === null) {
|
|
2177
|
-
// enable && console.log("xx3");
|
|
2178
|
-
return null;
|
|
2179
|
-
}
|
|
2180
|
-
let split_number = ipaddress_1.default.split_to_u32(ip);
|
|
2181
|
-
if (split_number === null) {
|
|
2182
|
-
// enable && console.log("xx4");
|
|
2183
|
-
return null;
|
|
2184
|
-
}
|
|
2185
|
-
// console.log(">>>>>>>", ip, ip_prefix);
|
|
2186
|
-
return new ipaddress_1.default({
|
|
2187
|
-
ip_bits: ip_bits_1.default.v4(),
|
|
2188
|
-
host_address: split_number,
|
|
2189
|
-
prefix: ip_prefix,
|
|
2190
|
-
mapped: null,
|
|
2191
|
-
vt_is_private: Ipv4.ipv4_is_private,
|
|
2192
|
-
vt_is_loopback: Ipv4.ipv4_is_loopback,
|
|
2193
|
-
vt_to_ipv6: Ipv4.to_ipv6
|
|
2194
|
-
});
|
|
2195
|
-
}
|
|
2196
|
-
static ipv4_is_private(my) {
|
|
2197
|
-
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;
|
|
2198
|
-
}
|
|
2199
|
-
static ipv4_is_loopback(my) {
|
|
2200
|
-
return ipaddress_1.default.parse("127.0.0.0/8").includes(my);
|
|
2201
|
-
}
|
|
2202
|
-
static to_ipv6(ia) {
|
|
2203
|
-
return new ipaddress_1.default({
|
|
2204
|
-
ip_bits: ip_bits_1.default.v6(),
|
|
2205
|
-
host_address: ia.host_address.clone(),
|
|
2206
|
-
prefix: prefix128_1.default.create(ia.prefix.num),
|
|
2207
|
-
mapped: null,
|
|
2208
|
-
vt_is_private: ipv6_1.default.ipv6_is_private,
|
|
2209
|
-
vt_is_loopback: ipv6_1.default.ipv6_is_loopback,
|
|
2210
|
-
vt_to_ipv6: ipv6_1.default.to_ipv6
|
|
2211
|
-
});
|
|
2212
|
-
}
|
|
2213
|
-
// Checks whether the ip address belongs to a
|
|
2214
|
-
// RFC 791 CLASS A network, no matter
|
|
2215
|
-
// what the subnet mask is.
|
|
2216
|
-
//
|
|
2217
|
-
// Example:
|
|
2218
|
-
//
|
|
2219
|
-
// ip = IPAddress("10.0.0.1/24")
|
|
2220
|
-
//
|
|
2221
|
-
// ip.a?
|
|
2222
|
-
// // => true
|
|
2223
|
-
//
|
|
2224
|
-
static is_class_a(my) {
|
|
2225
|
-
// console.log("is_class_a:", my.to_string(), Crunchy.from_string("80000000", 16), my.is_ipv4());
|
|
2226
|
-
return my.is_ipv4() && my.host_address.lt(crunchy_1.default.from_string("80000000", 16));
|
|
2227
|
-
}
|
|
2228
|
-
// Checks whether the ip address belongs to a
|
|
2229
|
-
// RFC 791 CLASS B network, no matter
|
|
2230
|
-
// what the subnet mask is.
|
|
2231
|
-
//
|
|
2232
|
-
// Example:
|
|
2233
|
-
//
|
|
2234
|
-
// ip = IPAddress("172.16.10.1/24")
|
|
2235
|
-
//
|
|
2236
|
-
// ip.b?
|
|
2237
|
-
// // => true
|
|
2238
|
-
//
|
|
2239
|
-
static is_class_b(my) {
|
|
2240
|
-
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));
|
|
2241
|
-
}
|
|
2242
|
-
// Checks whether the ip address belongs to a
|
|
2243
|
-
// RFC 791 CLASS C network, no matter
|
|
2244
|
-
// what the subnet mask is.
|
|
2245
|
-
//
|
|
2246
|
-
// Example:
|
|
2247
|
-
//
|
|
2248
|
-
// ip = IPAddress("192.168.1.1/30")
|
|
2249
|
-
//
|
|
2250
|
-
// ip.c?
|
|
2251
|
-
// // => true
|
|
2252
|
-
//
|
|
2253
|
-
static is_class_c(my) {
|
|
2254
|
-
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));
|
|
2255
|
-
}
|
|
2256
|
-
// Creates a new IPv4 address object by parsing the
|
|
2257
|
-
// address in a classful way.
|
|
2258
|
-
//
|
|
2259
|
-
// Classful addresses have a fixed netmask based on the
|
|
2260
|
-
// class they belong to:
|
|
2261
|
-
//
|
|
2262
|
-
// * Class A, from 0.0.0.0 to 127.255.255.255
|
|
2263
|
-
// * Class B, from 128.0.0.0 to 191.255.255.255
|
|
2264
|
-
// * Class C, D and E, from 192.0.0.0 to 255.255.255.254
|
|
2265
|
-
//
|
|
2266
|
-
// Example:
|
|
2267
|
-
//
|
|
2268
|
-
// ip = IPAddress::IPv4.parse_classful "10.0.0.1"
|
|
2269
|
-
//
|
|
2270
|
-
// ip.netmask
|
|
2271
|
-
// // => "255.0.0.0"
|
|
2272
|
-
// ip.a?
|
|
2273
|
-
// // => true
|
|
2274
|
-
//
|
|
2275
|
-
// Note that classes C, D and E will all have a default
|
|
2276
|
-
// prefix of /24 or 255.255.255.0
|
|
2277
|
-
//
|
|
2278
|
-
static parse_classful(ip_si) {
|
|
2279
|
-
if (!ipaddress_1.default.is_valid_ipv4(ip_si)) {
|
|
2280
|
-
return null;
|
|
2281
|
-
}
|
|
2282
|
-
let o_ip = ipaddress_1.default.parse(ip_si);
|
|
2283
|
-
if (o_ip === null) {
|
|
2284
|
-
return o_ip;
|
|
2285
|
-
}
|
|
2286
|
-
let ip = o_ip;
|
|
2287
|
-
if (Ipv4.is_class_a(ip)) {
|
|
2288
|
-
ip.prefix = prefix32_1.default.create(8);
|
|
2289
|
-
} else if (Ipv4.is_class_b(ip)) {
|
|
2290
|
-
ip.prefix = prefix32_1.default.create(16);
|
|
2291
|
-
} else if (Ipv4.is_class_c(ip)) {
|
|
2292
|
-
ip.prefix = prefix32_1.default.create(24);
|
|
2293
|
-
}
|
|
2294
|
-
return ip;
|
|
2295
|
-
}
|
|
2296
|
-
}
|
|
2297
|
-
exports.Ipv4 = Ipv4;
|
|
2298
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2299
|
-
exports.default = Ipv4;
|
|
2300
|
-
|
|
2301
|
-
/***/ },
|
|
2302
|
-
/* 8 */
|
|
2303
|
-
/***/ function(module, exports, __webpack_require__) {
|
|
2304
|
-
|
|
2305
|
-
"use strict";
|
|
2306
|
-
|
|
2307
|
-
const prefix_1 = __webpack_require__(2);
|
|
2308
|
-
const ip_bits_1 = __webpack_require__(3);
|
|
2309
|
-
class Prefix32 {
|
|
2310
|
-
static from(my, num) {
|
|
2311
|
-
return Prefix32.create(num);
|
|
2312
|
-
}
|
|
2313
|
-
static create(num) {
|
|
2314
|
-
if (0 <= num && num <= 32) {
|
|
2315
|
-
//static _FROM: &'static (Fn(&::prefix::Prefix, usize) -> Result<::prefix::Prefix, String>) =
|
|
2316
|
-
// &from;
|
|
2317
|
-
//static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &to_ip_str;
|
|
2318
|
-
let ip_bits = ip_bits_1.default.v4();
|
|
2319
|
-
let bits = ip_bits.bits;
|
|
2320
|
-
return new prefix_1.default({
|
|
2321
|
-
num: num,
|
|
2322
|
-
ip_bits: ip_bits,
|
|
2323
|
-
net_mask: prefix_1.default.new_netmask(num, bits),
|
|
2324
|
-
vt_from: Prefix32.from
|
|
2325
|
-
});
|
|
2326
|
-
}
|
|
2327
|
-
return null;
|
|
2328
|
-
}
|
|
2329
|
-
}
|
|
2330
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2331
|
-
exports.default = Prefix32;
|
|
2332
|
-
|
|
2333
|
-
/***/ },
|
|
2334
|
-
/* 9 */
|
|
2335
|
-
/***/ function(module, exports, __webpack_require__) {
|
|
2336
|
-
|
|
2337
|
-
"use strict";
|
|
2338
|
-
|
|
2339
|
-
const prefix_1 = __webpack_require__(2);
|
|
2340
|
-
const ip_bits_1 = __webpack_require__(3);
|
|
2341
|
-
class Prefix128 {
|
|
2342
|
-
// #[derive(Ord,PartialOrd,Eq,PartialEq,Debug,Copy,Clone)]
|
|
2343
|
-
// pub struct Prefix128 {
|
|
2344
|
-
// }
|
|
2345
|
-
//
|
|
2346
|
-
// impl Prefix128 {
|
|
2347
|
-
//
|
|
2348
|
-
// Creates a new prefix object for 128 bits IPv6 addresses
|
|
2349
|
-
//
|
|
2350
|
-
// prefix = IPAddressPrefix128.new 64
|
|
2351
|
-
// // => 64
|
|
2352
|
-
//
|
|
2353
|
-
//#[allow(unused_comparisons)]
|
|
2354
|
-
static create(num) {
|
|
2355
|
-
if (num <= 128) {
|
|
2356
|
-
//static _FROM: &'static (Fn(&Prefix, usize) -> Result<Prefix, String>) = &from;
|
|
2357
|
-
//static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &Prefix128::to_ip_str;
|
|
2358
|
-
let ip_bits = ip_bits_1.default.v6();
|
|
2359
|
-
let bits = ip_bits.bits;
|
|
2360
|
-
return new prefix_1.default({
|
|
2361
|
-
num: num,
|
|
2362
|
-
ip_bits: ip_bits,
|
|
2363
|
-
net_mask: prefix_1.default.new_netmask(num, bits),
|
|
2364
|
-
vt_from: Prefix128.from
|
|
2365
|
-
});
|
|
2366
|
-
}
|
|
2367
|
-
return null;
|
|
2368
|
-
}
|
|
2369
|
-
static from(my, num) {
|
|
2370
|
-
return Prefix128.create(num);
|
|
2371
|
-
}
|
|
2372
|
-
}
|
|
2373
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2374
|
-
exports.default = Prefix128;
|
|
2375
|
-
|
|
2376
|
-
/***/ },
|
|
2377
|
-
/* 10 */
|
|
2378
|
-
/***/ function(module, exports, __webpack_require__) {
|
|
2379
|
-
|
|
2380
|
-
"use strict";
|
|
2381
|
-
|
|
2382
|
-
const ip_bits_1 = __webpack_require__(3);
|
|
2383
|
-
const ipaddress_1 = __webpack_require__(6);
|
|
2384
|
-
const ipv4_1 = __webpack_require__(7);
|
|
2385
|
-
const prefix128_1 = __webpack_require__(9);
|
|
2386
|
-
const crunchy_1 = __webpack_require__(1);
|
|
2387
|
-
class Ipv6 {
|
|
2388
|
-
// =Name
|
|
2389
|
-
//
|
|
2390
|
-
// IPAddress::IPv6 - IP version 6 address manipulation library
|
|
2391
|
-
//
|
|
2392
|
-
// =Synopsis
|
|
2393
|
-
//
|
|
2394
|
-
// require 'ipaddress'
|
|
2395
|
-
//
|
|
2396
|
-
// =Description
|
|
2397
|
-
//
|
|
2398
|
-
// Class IPAddress::IPv6 is used to handle IPv6 type addresses.
|
|
2399
|
-
//
|
|
2400
|
-
// == IPv6 addresses
|
|
2401
|
-
//
|
|
2402
|
-
// IPv6 addresses are 128 bits long, in contrast with IPv4 addresses
|
|
2403
|
-
// which are only 32 bits long. An IPv6 address is generally written as
|
|
2404
|
-
// eight groups of four hexadecimal digits, each group representing 16
|
|
2405
|
-
// bits or two octect. For example, the following is a valid IPv6
|
|
2406
|
-
// address:
|
|
2407
|
-
//
|
|
2408
|
-
// 2001:0db8:0000:0000:0008:0800:200c:417a
|
|
2409
|
-
//
|
|
2410
|
-
// Letters in an IPv6 address are usually written downcase, as per
|
|
2411
|
-
// RFC. You can create a new IPv6 object using uppercase letters, but
|
|
2412
|
-
// they will be converted.
|
|
2413
|
-
//
|
|
2414
|
-
// === Compression
|
|
2415
|
-
//
|
|
2416
|
-
// Since IPv6 addresses are very long to write, there are some
|
|
2417
|
-
// semplifications and compressions that you can use to shorten them.
|
|
2418
|
-
//
|
|
2419
|
-
// * Leading zeroes: all the leading zeroes within a group can be
|
|
2420
|
-
// omitted: "0008" would become "8"
|
|
2421
|
-
//
|
|
2422
|
-
// * A string of consecutive zeroes can be replaced by the string
|
|
2423
|
-
// "::". This can be only applied once.
|
|
2424
|
-
//
|
|
2425
|
-
// Using compression, the IPv6 address written above can be shorten into
|
|
2426
|
-
// the following, equivalent, address
|
|
2427
|
-
//
|
|
2428
|
-
// 2001:db8::8:800:200c:417a
|
|
2429
|
-
//
|
|
2430
|
-
// This short version is often used in human representation.
|
|
2431
|
-
//
|
|
2432
|
-
// === Network Mask
|
|
2433
|
-
//
|
|
2434
|
-
// As we used to do with IPv4 addresses, an IPv6 address can be written
|
|
2435
|
-
// using the prefix notation to specify the subnet mask:
|
|
2436
|
-
//
|
|
2437
|
-
// 2001:db8::8:800:200c:417a/64
|
|
2438
|
-
//
|
|
2439
|
-
// The /64 part means that the first 64 bits of the address are
|
|
2440
|
-
// representing the network portion, and the last 64 bits are the host
|
|
2441
|
-
// portion.
|
|
2442
|
-
//
|
|
2443
|
-
//
|
|
2444
|
-
static from_str(str, radix, prefix) {
|
|
2445
|
-
let num = crunchy_1.default.from_string(str, radix);
|
|
2446
|
-
if (!num) {
|
|
2447
|
-
return null;
|
|
2448
|
-
}
|
|
2449
|
-
return Ipv6.from_int(num, prefix);
|
|
2450
|
-
}
|
|
2451
|
-
static enhance_if_mapped(ip) {
|
|
2452
|
-
// console.log("------A");
|
|
2453
|
-
// println!("real mapped {:x} {:x}", &ip.host_address, ip.host_address.clone().shr(32));
|
|
2454
|
-
if (ip.is_mapped()) {
|
|
2455
|
-
// console.log("------B");
|
|
2456
|
-
return ip;
|
|
2457
|
-
}
|
|
2458
|
-
// console.log("------C", ip);
|
|
2459
|
-
let ipv6_top_96bit = ip.host_address.shr(32);
|
|
2460
|
-
// console.log("------D", ip);
|
|
2461
|
-
if (ipv6_top_96bit.eq(crunchy_1.default.from_number(0xffff))) {
|
|
2462
|
-
// console.log("------E");
|
|
2463
|
-
let num = ip.host_address.mod(crunchy_1.default.one().shl(32));
|
|
2464
|
-
// console.log("------F");
|
|
2465
|
-
if (num.eq(crunchy_1.default.zero())) {
|
|
2466
|
-
return ip;
|
|
2467
|
-
}
|
|
2468
|
-
//println!("ip:{},{:x}", ip.to_string(), num);
|
|
2469
|
-
let ipv4_bits = ip_bits_1.default.v4();
|
|
2470
|
-
if (ipv4_bits.bits < ip.prefix.host_prefix()) {
|
|
2471
|
-
//println!("enhance_if_mapped-2:{}:{}", ip.to_string(), ip.prefix.host_prefix());
|
|
2472
|
-
return null;
|
|
2473
|
-
}
|
|
2474
|
-
// console.log("------G");
|
|
2475
|
-
let mapped = ipv4_1.default.from_number(num, ipv4_bits.bits - ip.prefix.host_prefix());
|
|
2476
|
-
// console.log("------H");
|
|
2477
|
-
if (!mapped) {
|
|
2478
|
-
// println!("enhance_if_mapped-3");
|
|
2479
|
-
return mapped;
|
|
2480
|
-
}
|
|
2481
|
-
// println!("real mapped!!!!!={}", mapped.clone().to_string());
|
|
2482
|
-
ip.mapped = mapped;
|
|
2483
|
-
}
|
|
2484
|
-
return ip;
|
|
2485
|
-
}
|
|
2486
|
-
static from_int(adr, prefix_num) {
|
|
2487
|
-
let prefix = prefix128_1.default.create(prefix_num);
|
|
2488
|
-
if (prefix === null) {
|
|
2489
|
-
return null;
|
|
2490
|
-
}
|
|
2491
|
-
let ret = Ipv6.enhance_if_mapped(new ipaddress_1.default({
|
|
2492
|
-
ip_bits: ip_bits_1.default.v6(),
|
|
2493
|
-
host_address: adr.clone(),
|
|
2494
|
-
prefix: prefix,
|
|
2495
|
-
mapped: null,
|
|
2496
|
-
vt_is_private: Ipv6.ipv6_is_private,
|
|
2497
|
-
vt_is_loopback: Ipv6.ipv6_is_loopback,
|
|
2498
|
-
vt_to_ipv6: Ipv6.to_ipv6
|
|
2499
|
-
}));
|
|
2500
|
-
//console.log("from_int:", adr, prefix, ret);
|
|
2501
|
-
return ret;
|
|
2502
|
-
}
|
|
2503
|
-
// Creates a new IPv6 address object.
|
|
2504
|
-
//
|
|
2505
|
-
// An IPv6 address can be expressed in any of the following forms:
|
|
2506
|
-
//
|
|
2507
|
-
// * "2001:0db8:0000:0000:0008:0800:200C:417A": IPv6 address with no compression
|
|
2508
|
-
// * "2001:db8:0:0:8:800:200C:417A": IPv6 address with leading zeros compression
|
|
2509
|
-
// * "2001:db8::8:800:200C:417A": IPv6 address with full compression
|
|
2510
|
-
//
|
|
2511
|
-
// In all these 3 cases, a new IPv6 address object will be created, using the default
|
|
2512
|
-
// subnet mask /128
|
|
2513
|
-
//
|
|
2514
|
-
// You can also specify the subnet mask as with IPv4 addresses:
|
|
2515
|
-
//
|
|
2516
|
-
// ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
|
|
2517
|
-
//
|
|
2518
|
-
static create(str) {
|
|
2519
|
-
// console.log("1>>>>>>>>>", str);
|
|
2520
|
-
let [ip, o_netmask] = ipaddress_1.default.split_at_slash(str);
|
|
2521
|
-
// console.log("2>>>>>>>>>", str);
|
|
2522
|
-
if (ipaddress_1.default.is_valid_ipv6(ip)) {
|
|
2523
|
-
// console.log("3>>>>>>>>>", str);
|
|
2524
|
-
let o_num = ipaddress_1.default.split_to_num(ip);
|
|
2525
|
-
if (o_num === null) {
|
|
2526
|
-
// console.log("ipv6_create-1", str);
|
|
2527
|
-
return null;
|
|
2528
|
-
}
|
|
2529
|
-
// console.log("4>>>>>>>>>", str);
|
|
2530
|
-
let netmask = 128;
|
|
2531
|
-
if (o_netmask !== null) {
|
|
2532
|
-
netmask = ipaddress_1.default.parse_dec_str(o_netmask);
|
|
2533
|
-
if (netmask === null) {
|
|
2534
|
-
// console.log("ipv6_create-2", str);
|
|
2535
|
-
return null;
|
|
2536
|
-
}
|
|
2537
|
-
}
|
|
2538
|
-
// console.log("5>>>>>>>>>", str);
|
|
2539
|
-
let prefix = prefix128_1.default.create(netmask);
|
|
2540
|
-
if (prefix === null) {
|
|
2541
|
-
// console.log("ipv6_create-3", str);
|
|
2542
|
-
return null;
|
|
2543
|
-
}
|
|
2544
|
-
//console.log("6>>>>>>>>>", str, prefix.num, o_netmask, netmask);
|
|
2545
|
-
return Ipv6.enhance_if_mapped(new ipaddress_1.default({
|
|
2546
|
-
ip_bits: ip_bits_1.default.v6(),
|
|
2547
|
-
host_address: o_num.crunchy,
|
|
2548
|
-
prefix: prefix,
|
|
2549
|
-
mapped: null,
|
|
2550
|
-
vt_is_private: Ipv6.ipv6_is_private,
|
|
2551
|
-
vt_is_loopback: Ipv6.ipv6_is_loopback,
|
|
2552
|
-
vt_to_ipv6: Ipv6.to_ipv6
|
|
2553
|
-
}));
|
|
2554
|
-
} else {
|
|
2555
|
-
// console.log("ipv6_create-4", str);
|
|
2556
|
-
return null;
|
|
2557
|
-
}
|
|
2558
|
-
} // pub fn initialize
|
|
2559
|
-
static to_ipv6(ia) {
|
|
2560
|
-
return ia.clone();
|
|
2561
|
-
}
|
|
2562
|
-
static ipv6_is_loopback(my) {
|
|
2563
|
-
// console.log("*************", my.host_address, Crunchy.one());
|
|
2564
|
-
return my.host_address.eq(crunchy_1.default.one());
|
|
2565
|
-
}
|
|
2566
|
-
static ipv6_is_private(my) {
|
|
2567
|
-
return ipaddress_1.default.parse("fd00::/8").includes(my);
|
|
2568
|
-
}
|
|
2569
|
-
}
|
|
2570
|
-
exports.Ipv6 = Ipv6;
|
|
2571
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2572
|
-
exports.default = Ipv6;
|
|
2573
|
-
|
|
2574
|
-
/***/ },
|
|
2575
|
-
/* 11 */
|
|
2576
|
-
/***/ function(module, exports, __webpack_require__) {
|
|
2577
|
-
|
|
2578
|
-
"use strict";
|
|
2579
|
-
|
|
2580
|
-
const crunchy_1 = __webpack_require__(1);
|
|
2581
|
-
const ip_bits_1 = __webpack_require__(3);
|
|
2582
|
-
const ipaddress_1 = __webpack_require__(6);
|
|
2583
|
-
class Ipv6Mapped {
|
|
2584
|
-
// Ac
|
|
2585
|
-
// It is usually identified as a IPv4 mapped IPv6 address, a particular
|
|
2586
|
-
// IPv6 address which aids the transition from IPv4 to IPv6. The
|
|
2587
|
-
// structure of the address is
|
|
2588
|
-
//
|
|
2589
|
-
// ::ffff:w.y.x.z
|
|
2590
|
-
//
|
|
2591
|
-
// where w.x.y.z is a normal IPv4 address. For example, the following is
|
|
2592
|
-
// a mapped IPv6 address:
|
|
2593
|
-
//
|
|
2594
|
-
// ::ffff:192.168.100.1
|
|
2595
|
-
//
|
|
2596
|
-
// IPAddress is very powerful in handling mapped IPv6 addresses, as the
|
|
2597
|
-
// IPv4 portion is stored internally as a normal IPv4 object. Let's have
|
|
2598
|
-
// a look at some examples. To create a new mapped address, just use the
|
|
2599
|
-
// class builder itself
|
|
2600
|
-
//
|
|
2601
|
-
// ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
|
|
2602
|
-
//
|
|
2603
|
-
// or just use the wrapper method
|
|
2604
|
-
//
|
|
2605
|
-
// ip6 = IPAddress "::ffff:172.16.10.1/128"
|
|
2606
|
-
//
|
|
2607
|
-
// Let's check it's really a mapped address:
|
|
2608
|
-
//
|
|
2609
|
-
// ip6.mapped?
|
|
2610
|
-
// // => true
|
|
2611
|
-
//
|
|
2612
|
-
// ip6.to_string
|
|
2613
|
-
// // => "::FFFF:172.16.10.1/128"
|
|
2614
|
-
//
|
|
2615
|
-
// Now with the +ipv4+ attribute, we can easily access the IPv4 portion
|
|
2616
|
-
// of the mapped IPv6 address:
|
|
2617
|
-
//
|
|
2618
|
-
// ip6.ipv4.address
|
|
2619
|
-
// // => "172.16.10.1"
|
|
2620
|
-
//
|
|
2621
|
-
// Internally, the IPv4 address is stored as two 16 bits
|
|
2622
|
-
// groups. Therefore all the usual methods for an IPv6 address are
|
|
2623
|
-
// working perfectly fine:
|
|
2624
|
-
//
|
|
2625
|
-
// ip6.to_hex
|
|
2626
|
-
// // => "00000000000000000000ffffac100a01"
|
|
2627
|
-
//
|
|
2628
|
-
// ip6.address
|
|
2629
|
-
// // => "0000:0000:0000:0000:0000:ffff:ac10:0a01"
|
|
2630
|
-
//
|
|
2631
|
-
// A mapped IPv6 can also be created just by specify the address in the
|
|
2632
|
-
// following format:
|
|
2633
|
-
//
|
|
2634
|
-
// ip6 = IPAddress "::172.16.10.1"
|
|
2635
|
-
//
|
|
2636
|
-
// That is, two colons and the IPv4 address. However, as by RFC, the ffff
|
|
2637
|
-
// group will be automatically added at the beginning
|
|
2638
|
-
//
|
|
2639
|
-
// ip6.to_string
|
|
2640
|
-
// => "::ffff:172.16.10.1/128"
|
|
2641
|
-
//
|
|
2642
|
-
// making it a mapped IPv6 compatible address.
|
|
2643
|
-
//
|
|
2644
|
-
//
|
|
2645
|
-
// Creates a new IPv6 IPv4-mapped address
|
|
2646
|
-
//
|
|
2647
|
-
// ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
|
|
2648
|
-
//
|
|
2649
|
-
// ipv6.ipv4.class
|
|
2650
|
-
// // => IPAddress::IPv4
|
|
2651
|
-
//
|
|
2652
|
-
// An IPv6 IPv4-mapped address can also be created using the
|
|
2653
|
-
// IPv6 only format of the address:
|
|
2654
|
-
//
|
|
2655
|
-
// ip6 = IPAddress::IPv6::Mapped.new "::0d01:4403"
|
|
2656
|
-
//
|
|
2657
|
-
// ip6.to_string
|
|
2658
|
-
// // => "::ffff:13.1.68.3"
|
|
2659
|
-
//
|
|
2660
|
-
static create(str) {
|
|
2661
|
-
// console.log("mapped-1");
|
|
2662
|
-
let i = ipaddress_1.default.split_at_slash(str);
|
|
2663
|
-
let ip = i[0];
|
|
2664
|
-
let o_netmask = i[1];
|
|
2665
|
-
let split_colon = ip.split(":");
|
|
2666
|
-
if (split_colon.length <= 1) {
|
|
2667
|
-
// console.log("mapped-2");
|
|
2668
|
-
return null;
|
|
2669
|
-
}
|
|
2670
|
-
// if split_colon.get(0).unwrap().len() > 0 {
|
|
2671
|
-
// // println!("---1a");
|
|
2672
|
-
// return Err(format!("not mapped format-2: {}", &str));
|
|
2673
|
-
// }
|
|
2674
|
-
// let mapped: Option<IPAddress> = None;
|
|
2675
|
-
let netmask = "";
|
|
2676
|
-
if (o_netmask !== null) {
|
|
2677
|
-
netmask = `/${ o_netmask }`;
|
|
2678
|
-
}
|
|
2679
|
-
let ipv4_str = split_colon[split_colon.length - 1];
|
|
2680
|
-
if (ipaddress_1.default.is_valid_ipv4(ipv4_str)) {
|
|
2681
|
-
let ipv4 = ipaddress_1.default.parse(`${ ipv4_str }${ netmask }`);
|
|
2682
|
-
if (ipv4 === null) {
|
|
2683
|
-
// console.log("mapped-3");
|
|
2684
|
-
return ipv4;
|
|
2685
|
-
}
|
|
2686
|
-
//mapped = Some(ipv4.unwrap());
|
|
2687
|
-
let addr = ipv4;
|
|
2688
|
-
let ipv6_bits = ip_bits_1.default.v6();
|
|
2689
|
-
let part_mod = crunchy_1.default.from_number(ipv6_bits.part_mod);
|
|
2690
|
-
let up_addr = addr.host_address.clone();
|
|
2691
|
-
let down_addr = addr.host_address.clone();
|
|
2692
|
-
let rebuild_ipv6 = "";
|
|
2693
|
-
let colon = "";
|
|
2694
|
-
for (let i = 0; i < split_colon.length - 1; ++i) {
|
|
2695
|
-
rebuild_ipv6 += colon;
|
|
2696
|
-
rebuild_ipv6 += split_colon[i];
|
|
2697
|
-
colon = ":";
|
|
2698
|
-
}
|
|
2699
|
-
rebuild_ipv6 += colon;
|
|
2700
|
-
let high_part = up_addr.shr(ip_bits_1.default.v6().part_bits).mod(part_mod).toString(16);
|
|
2701
|
-
let low_part = down_addr.mod(part_mod).toString(16);
|
|
2702
|
-
let bits = ipv6_bits.bits - addr.prefix.host_prefix();
|
|
2703
|
-
let rebuild_ipv4 = `${ high_part }:${ low_part }/${ bits }`;
|
|
2704
|
-
rebuild_ipv6 += rebuild_ipv4;
|
|
2705
|
-
// console.log("-----A", rebuild_ipv6, part_mod);
|
|
2706
|
-
let r_ipv6 = ipaddress_1.default.parse(rebuild_ipv6);
|
|
2707
|
-
if (r_ipv6 === null) {
|
|
2708
|
-
// println!("---3|{}", &rebuild_ipv6);
|
|
2709
|
-
// console.log("mapped-4");
|
|
2710
|
-
return r_ipv6;
|
|
2711
|
-
}
|
|
2712
|
-
if (r_ipv6.is_mapped()) {
|
|
2713
|
-
// console.log("mapped-5");
|
|
2714
|
-
return r_ipv6;
|
|
2715
|
-
}
|
|
2716
|
-
let ipv6 = r_ipv6;
|
|
2717
|
-
let p96bit = ipv6.host_address.shr(32);
|
|
2718
|
-
if (!p96bit.eq(crunchy_1.default.zero())) {
|
|
2719
|
-
// println!("---4|{}", &rebuild_ipv6);
|
|
2720
|
-
//console.log("mapped-6",ipv6.host_address, p96bit, Crunchy.zero());
|
|
2721
|
-
return null;
|
|
2722
|
-
}
|
|
2723
|
-
{
|
|
2724
|
-
let r_ipv6 = ipaddress_1.default.parse(`::ffff:${ rebuild_ipv4 }`);
|
|
2725
|
-
if (r_ipv6 === null) {
|
|
2726
|
-
// println!("---3|{}", &rebuild_ipv6);
|
|
2727
|
-
//console.log("mapped-7");
|
|
2728
|
-
return null;
|
|
2729
|
-
}
|
|
2730
|
-
// console.log("mapped-8");
|
|
2731
|
-
return r_ipv6;
|
|
2732
|
-
}
|
|
2733
|
-
}
|
|
2734
|
-
// console.log("mapped-9");
|
|
2735
|
-
return null;
|
|
2736
|
-
}
|
|
2737
|
-
}
|
|
2738
|
-
exports.Ipv6Mapped = Ipv6Mapped;
|
|
2739
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2740
|
-
exports.default = Ipv6Mapped;
|
|
2741
|
-
|
|
2742
|
-
/***/ },
|
|
2743
|
-
/* 12 */
|
|
2744
|
-
/***/ function(module, exports, __webpack_require__) {
|
|
2745
|
-
|
|
2746
|
-
"use strict";
|
|
2747
|
-
|
|
2748
|
-
const ipv6_1 = __webpack_require__(10);
|
|
2749
|
-
const crunchy_1 = __webpack_require__(1);
|
|
2750
|
-
class Ipv6Loopback {
|
|
2751
|
-
// The loopback address is a unicast localhost address. If an
|
|
2752
|
-
// application in a host sends packets to this address, the IPv6 stack
|
|
2753
|
-
// will loop these packets back on the same virtual interface.
|
|
2754
|
-
//
|
|
2755
|
-
// Loopback addresses are expressed in the following form:
|
|
2756
|
-
//
|
|
2757
|
-
// ::1
|
|
2758
|
-
//
|
|
2759
|
-
// or, with their appropriate prefix,
|
|
2760
|
-
//
|
|
2761
|
-
// ::1/128
|
|
2762
|
-
//
|
|
2763
|
-
// As for the unspecified addresses, IPv6 loopbacks can be created with
|
|
2764
|
-
// IPAddress calling their own class:
|
|
2765
|
-
//
|
|
2766
|
-
// ip = IPAddress::IPv6::Loopback.new
|
|
2767
|
-
//
|
|
2768
|
-
// ip.to_string
|
|
2769
|
-
// // => "::1/128"
|
|
2770
|
-
//
|
|
2771
|
-
// or by using the wrapper:
|
|
2772
|
-
//
|
|
2773
|
-
// ip = IPAddress "::1"
|
|
2774
|
-
//
|
|
2775
|
-
// ip.to_string
|
|
2776
|
-
// // => "::1/128"
|
|
2777
|
-
//
|
|
2778
|
-
// Checking if an address is loopback is easy with the IPv6// loopback?
|
|
2779
|
-
// method:
|
|
2780
|
-
//
|
|
2781
|
-
// ip.loopback?
|
|
2782
|
-
// // => true
|
|
2783
|
-
//
|
|
2784
|
-
// The IPv6 loopback address corresponds to 127.0.0.1 in IPv4.
|
|
2785
|
-
//
|
|
2786
|
-
//
|
|
2787
|
-
// Creates a new IPv6 unspecified address
|
|
2788
|
-
//
|
|
2789
|
-
// ip = IPAddress::IPv6::Loopback.new
|
|
2790
|
-
//
|
|
2791
|
-
// ip.to_string
|
|
2792
|
-
// // => "::1/128"
|
|
2793
|
-
//
|
|
2794
|
-
static create() {
|
|
2795
|
-
return ipv6_1.default.from_int(crunchy_1.default.one(), 128);
|
|
2796
|
-
}
|
|
2797
|
-
}
|
|
2798
|
-
exports.Ipv6Loopback = Ipv6Loopback;
|
|
2799
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2800
|
-
exports.default = Ipv6Loopback;
|
|
2801
|
-
|
|
2802
|
-
/***/ },
|
|
2803
|
-
/* 13 */
|
|
2804
|
-
/***/ function(module, exports, __webpack_require__) {
|
|
2805
|
-
|
|
2806
|
-
"use strict";
|
|
2807
|
-
|
|
2808
|
-
const ipaddress_1 = __webpack_require__(6);
|
|
2809
|
-
class Ipv6Unspec {
|
|
2810
|
-
// The address with all zero bits is called the +unspecified+ address
|
|
2811
|
-
// (corresponding to 0.0.0.0 in IPv4). It should be something like this:
|
|
2812
|
-
//
|
|
2813
|
-
// 0000:0000:0000:0000:0000:0000:0000:0000
|
|
2814
|
-
//
|
|
2815
|
-
// but, with the use of compression, it is usually written as just two
|
|
2816
|
-
// colons:
|
|
2817
|
-
//
|
|
2818
|
-
// ::
|
|
2819
|
-
//
|
|
2820
|
-
// or, specifying the netmask:
|
|
2821
|
-
//
|
|
2822
|
-
// ::/128
|
|
2823
|
-
//
|
|
2824
|
-
// With IPAddress, create a new unspecified IPv6 address using its own
|
|
2825
|
-
// subclass:
|
|
2826
|
-
//
|
|
2827
|
-
// ip = IPAddress::IPv6::Unspecified.new
|
|
2828
|
-
//
|
|
2829
|
-
// ip.to_s
|
|
2830
|
-
// // => => "::/128"
|
|
2831
|
-
//
|
|
2832
|
-
// You can easily check if an IPv6 object is an unspecified address by
|
|
2833
|
-
// using the IPv6// unspecified? method
|
|
2834
|
-
//
|
|
2835
|
-
// ip.unspecified?
|
|
2836
|
-
// // => true
|
|
2837
|
-
//
|
|
2838
|
-
// An unspecified IPv6 address can also be created with the wrapper
|
|
2839
|
-
// method, like we've seen before
|
|
2840
|
-
//
|
|
2841
|
-
// ip = IPAddress "::"
|
|
2842
|
-
//
|
|
2843
|
-
// ip.unspecified?
|
|
2844
|
-
// // => true
|
|
2845
|
-
//
|
|
2846
|
-
// This address must never be assigned to an interface and is to be used
|
|
2847
|
-
// only in software before the application has learned its host's source
|
|
2848
|
-
// address appropriate for a pending connection. Routers must not forward
|
|
2849
|
-
// packets with the unspecified address.
|
|
2850
|
-
//
|
|
2851
|
-
//
|
|
2852
|
-
// Creates a new IPv6 unspecified address
|
|
2853
|
-
//
|
|
2854
|
-
// ip = IPAddress::IPv6::Unspecified.new
|
|
2855
|
-
//
|
|
2856
|
-
// ip.to_s
|
|
2857
|
-
// // => => "::/128"
|
|
2858
|
-
//
|
|
2859
|
-
static create() {
|
|
2860
|
-
return ipaddress_1.default.parse("::");
|
|
2861
|
-
}
|
|
2862
|
-
}
|
|
2863
|
-
exports.Ipv6Unspec = Ipv6Unspec;
|
|
2864
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2865
|
-
exports.default = Ipv6Unspec;
|
|
2866
|
-
|
|
2867
|
-
/***/ }
|
|
2868
|
-
/******/ ]);
|
|
2869
|
-
//# sourceMappingURL=ipaddress.js.map
|