Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / express / node_modules / proxy-addr / node_modules / ipaddr.js / lib / ipaddr.js
1 (function() {
2   var expandIPv6, ipaddr, ipv4Part, ipv4Regexes, ipv6Part, ipv6Regexes, matchCIDR, root;
3
4   ipaddr = {};
5
6   root = this;
7
8   if ((typeof module !== "undefined" && module !== null) && module.exports) {
9     module.exports = ipaddr;
10   } else {
11     root['ipaddr'] = ipaddr;
12   }
13
14   matchCIDR = function(first, second, partSize, cidrBits) {
15     var part, shift;
16     if (first.length !== second.length) {
17       throw new Error("ipaddr: cannot match CIDR for objects with different lengths");
18     }
19     part = 0;
20     while (cidrBits > 0) {
21       shift = partSize - cidrBits;
22       if (shift < 0) {
23         shift = 0;
24       }
25       if (first[part] >> shift !== second[part] >> shift) {
26         return false;
27       }
28       cidrBits -= partSize;
29       part += 1;
30     }
31     return true;
32   };
33
34   ipaddr.subnetMatch = function(address, rangeList, defaultName) {
35     var rangeName, rangeSubnets, subnet, _i, _len;
36     if (defaultName == null) {
37       defaultName = 'unicast';
38     }
39     for (rangeName in rangeList) {
40       rangeSubnets = rangeList[rangeName];
41       if (toString.call(rangeSubnets[0]) !== '[object Array]') {
42         rangeSubnets = [rangeSubnets];
43       }
44       for (_i = 0, _len = rangeSubnets.length; _i < _len; _i++) {
45         subnet = rangeSubnets[_i];
46         if (address.match.apply(address, subnet)) {
47           return rangeName;
48         }
49       }
50     }
51     return defaultName;
52   };
53
54   ipaddr.IPv4 = (function() {
55     function IPv4(octets) {
56       var octet, _i, _len;
57       if (octets.length !== 4) {
58         throw new Error("ipaddr: ipv4 octet count should be 4");
59       }
60       for (_i = 0, _len = octets.length; _i < _len; _i++) {
61         octet = octets[_i];
62         if (!((0 <= octet && octet <= 255))) {
63           throw new Error("ipaddr: ipv4 octet is a byte");
64         }
65       }
66       this.octets = octets;
67     }
68
69     IPv4.prototype.kind = function() {
70       return 'ipv4';
71     };
72
73     IPv4.prototype.toString = function() {
74       return this.octets.join(".");
75     };
76
77     IPv4.prototype.toByteArray = function() {
78       return this.octets.slice(0);
79     };
80
81     IPv4.prototype.match = function(other, cidrRange) {
82       if (other.kind() !== 'ipv4') {
83         throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");
84       }
85       return matchCIDR(this.octets, other.octets, 8, cidrRange);
86     };
87
88     IPv4.prototype.SpecialRanges = {
89       unspecified: [[new IPv4([0, 0, 0, 0]), 8]],
90       broadcast: [[new IPv4([255, 255, 255, 255]), 32]],
91       multicast: [[new IPv4([224, 0, 0, 0]), 4]],
92       linkLocal: [[new IPv4([169, 254, 0, 0]), 16]],
93       loopback: [[new IPv4([127, 0, 0, 0]), 8]],
94       "private": [[new IPv4([10, 0, 0, 0]), 8], [new IPv4([172, 16, 0, 0]), 12], [new IPv4([192, 168, 0, 0]), 16]],
95       reserved: [[new IPv4([192, 0, 0, 0]), 24], [new IPv4([192, 0, 2, 0]), 24], [new IPv4([192, 88, 99, 0]), 24], [new IPv4([198, 51, 100, 0]), 24], [new IPv4([203, 0, 113, 0]), 24], [new IPv4([240, 0, 0, 0]), 4]]
96     };
97
98     IPv4.prototype.range = function() {
99       return ipaddr.subnetMatch(this, this.SpecialRanges);
100     };
101
102     IPv4.prototype.toIPv4MappedAddress = function() {
103       return ipaddr.IPv6.parse("::ffff:" + (this.toString()));
104     };
105
106     return IPv4;
107
108   })();
109
110   ipv4Part = "(0?\\d+|0x[a-f0-9]+)";
111
112   ipv4Regexes = {
113     fourOctet: new RegExp("^" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$", 'i'),
114     longValue: new RegExp("^" + ipv4Part + "$", 'i')
115   };
116
117   ipaddr.IPv4.parser = function(string) {
118     var match, parseIntAuto, part, shift, value;
119     parseIntAuto = function(string) {
120       if (string[0] === "0" && string[1] !== "x") {
121         return parseInt(string, 8);
122       } else {
123         return parseInt(string);
124       }
125     };
126     if (match = string.match(ipv4Regexes.fourOctet)) {
127       return (function() {
128         var _i, _len, _ref, _results;
129         _ref = match.slice(1, 6);
130         _results = [];
131         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
132           part = _ref[_i];
133           _results.push(parseIntAuto(part));
134         }
135         return _results;
136       })();
137     } else if (match = string.match(ipv4Regexes.longValue)) {
138       value = parseIntAuto(match[1]);
139       if (value > 0xffffffff || value < 0) {
140         throw new Error("ipaddr: address outside defined range");
141       }
142       return ((function() {
143         var _i, _results;
144         _results = [];
145         for (shift = _i = 0; _i <= 24; shift = _i += 8) {
146           _results.push((value >> shift) & 0xff);
147         }
148         return _results;
149       })()).reverse();
150     } else {
151       return null;
152     }
153   };
154
155   ipaddr.IPv6 = (function() {
156     function IPv6(parts) {
157       var part, _i, _len;
158       if (parts.length !== 8) {
159         throw new Error("ipaddr: ipv6 part count should be 8");
160       }
161       for (_i = 0, _len = parts.length; _i < _len; _i++) {
162         part = parts[_i];
163         if (!((0 <= part && part <= 0xffff))) {
164           throw new Error("ipaddr: ipv6 part should fit to two octets");
165         }
166       }
167       this.parts = parts;
168     }
169
170     IPv6.prototype.kind = function() {
171       return 'ipv6';
172     };
173
174     IPv6.prototype.toString = function() {
175       var compactStringParts, part, pushPart, state, stringParts, _i, _len;
176       stringParts = (function() {
177         var _i, _len, _ref, _results;
178         _ref = this.parts;
179         _results = [];
180         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
181           part = _ref[_i];
182           _results.push(part.toString(16));
183         }
184         return _results;
185       }).call(this);
186       compactStringParts = [];
187       pushPart = function(part) {
188         return compactStringParts.push(part);
189       };
190       state = 0;
191       for (_i = 0, _len = stringParts.length; _i < _len; _i++) {
192         part = stringParts[_i];
193         switch (state) {
194           case 0:
195             if (part === '0') {
196               pushPart('');
197             } else {
198               pushPart(part);
199             }
200             state = 1;
201             break;
202           case 1:
203             if (part === '0') {
204               state = 2;
205             } else {
206               pushPart(part);
207             }
208             break;
209           case 2:
210             if (part !== '0') {
211               pushPart('');
212               pushPart(part);
213               state = 3;
214             }
215             break;
216           case 3:
217             pushPart(part);
218         }
219       }
220       if (state === 2) {
221         pushPart('');
222         pushPart('');
223       }
224       return compactStringParts.join(":");
225     };
226
227     IPv6.prototype.toByteArray = function() {
228       var bytes, part, _i, _len, _ref;
229       bytes = [];
230       _ref = this.parts;
231       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
232         part = _ref[_i];
233         bytes.push(part >> 8);
234         bytes.push(part & 0xff);
235       }
236       return bytes;
237     };
238
239     IPv6.prototype.toNormalizedString = function() {
240       var part;
241       return ((function() {
242         var _i, _len, _ref, _results;
243         _ref = this.parts;
244         _results = [];
245         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
246           part = _ref[_i];
247           _results.push(part.toString(16));
248         }
249         return _results;
250       }).call(this)).join(":");
251     };
252
253     IPv6.prototype.match = function(other, cidrRange) {
254       if (other.kind() !== 'ipv6') {
255         throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");
256       }
257       return matchCIDR(this.parts, other.parts, 16, cidrRange);
258     };
259
260     IPv6.prototype.SpecialRanges = {
261       unspecified: [new IPv6([0, 0, 0, 0, 0, 0, 0, 0]), 128],
262       linkLocal: [new IPv6([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 10],
263       multicast: [new IPv6([0xff00, 0, 0, 0, 0, 0, 0, 0]), 8],
264       loopback: [new IPv6([0, 0, 0, 0, 0, 0, 0, 1]), 128],
265       uniqueLocal: [new IPv6([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7],
266       ipv4Mapped: [new IPv6([0, 0, 0, 0, 0, 0xffff, 0, 0]), 96],
267       rfc6145: [new IPv6([0, 0, 0, 0, 0xffff, 0, 0, 0]), 96],
268       rfc6052: [new IPv6([0x64, 0xff9b, 0, 0, 0, 0, 0, 0]), 96],
269       '6to4': [new IPv6([0x2002, 0, 0, 0, 0, 0, 0, 0]), 16],
270       teredo: [new IPv6([0x2001, 0, 0, 0, 0, 0, 0, 0]), 32],
271       reserved: [[new IPv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0]), 32]]
272     };
273
274     IPv6.prototype.range = function() {
275       return ipaddr.subnetMatch(this, this.SpecialRanges);
276     };
277
278     IPv6.prototype.isIPv4MappedAddress = function() {
279       return this.range() === 'ipv4Mapped';
280     };
281
282     IPv6.prototype.toIPv4Address = function() {
283       var high, low, _ref;
284       if (!this.isIPv4MappedAddress()) {
285         throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");
286       }
287       _ref = this.parts.slice(-2), high = _ref[0], low = _ref[1];
288       return new ipaddr.IPv4([high >> 8, high & 0xff, low >> 8, low & 0xff]);
289     };
290
291     return IPv6;
292
293   })();
294
295   ipv6Part = "(?:[0-9a-f]+::?)+";
296
297   ipv6Regexes = {
298     "native": new RegExp("^(::)?(" + ipv6Part + ")?([0-9a-f]+)?(::)?$", 'i'),
299     transitional: new RegExp(("^((?:" + ipv6Part + ")|(?:::)(?:" + ipv6Part + ")?)") + ("" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$"), 'i')
300   };
301
302   expandIPv6 = function(string, parts) {
303     var colonCount, lastColon, part, replacement, replacementCount;
304     if (string.indexOf('::') !== string.lastIndexOf('::')) {
305       return null;
306     }
307     colonCount = 0;
308     lastColon = -1;
309     while ((lastColon = string.indexOf(':', lastColon + 1)) >= 0) {
310       colonCount++;
311     }
312     if (string[0] === ':') {
313       colonCount--;
314     }
315     if (string[string.length - 1] === ':') {
316       colonCount--;
317     }
318     replacementCount = parts - colonCount;
319     replacement = ':';
320     while (replacementCount--) {
321       replacement += '0:';
322     }
323     string = string.replace('::', replacement);
324     if (string[0] === ':') {
325       string = string.slice(1);
326     }
327     if (string[string.length - 1] === ':') {
328       string = string.slice(0, -1);
329     }
330     return (function() {
331       var _i, _len, _ref, _results;
332       _ref = string.split(":");
333       _results = [];
334       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
335         part = _ref[_i];
336         _results.push(parseInt(part, 16));
337       }
338       return _results;
339     })();
340   };
341
342   ipaddr.IPv6.parser = function(string) {
343     var match, parts;
344     if (string.match(ipv6Regexes['native'])) {
345       return expandIPv6(string, 8);
346     } else if (match = string.match(ipv6Regexes['transitional'])) {
347       parts = expandIPv6(match[1].slice(0, -1), 6);
348       if (parts) {
349         parts.push(parseInt(match[2]) << 8 | parseInt(match[3]));
350         parts.push(parseInt(match[4]) << 8 | parseInt(match[5]));
351         return parts;
352       }
353     }
354     return null;
355   };
356
357   ipaddr.IPv4.isIPv4 = ipaddr.IPv6.isIPv6 = function(string) {
358     return this.parser(string) !== null;
359   };
360
361   ipaddr.IPv4.isValid = ipaddr.IPv6.isValid = function(string) {
362     var e;
363     try {
364       new this(this.parser(string));
365       return true;
366     } catch (_error) {
367       e = _error;
368       return false;
369     }
370   };
371
372   ipaddr.IPv4.parse = ipaddr.IPv6.parse = function(string) {
373     var parts;
374     parts = this.parser(string);
375     if (parts === null) {
376       throw new Error("ipaddr: string is not formatted like ip address");
377     }
378     return new this(parts);
379   };
380
381   ipaddr.isValid = function(string) {
382     return ipaddr.IPv6.isValid(string) || ipaddr.IPv4.isValid(string);
383   };
384
385   ipaddr.parse = function(string) {
386     if (ipaddr.IPv6.isValid(string)) {
387       return ipaddr.IPv6.parse(string);
388     } else if (ipaddr.IPv4.isValid(string)) {
389       return ipaddr.IPv4.parse(string);
390     } else {
391       throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format");
392     }
393   };
394
395   ipaddr.process = function(string) {
396     var addr;
397     addr = this.parse(string);
398     if (addr.kind() === 'ipv6' && addr.isIPv4MappedAddress()) {
399       return addr.toIPv4Address();
400     } else {
401       return addr;
402     }
403   };
404
405 }).call(this);