2 var expandIPv6, ipaddr, ipv4Part, ipv4Regexes, ipv6Part, ipv6Regexes, matchCIDR, root;
8 if ((typeof module !== "undefined" && module !== null) && module.exports) {
9 module.exports = ipaddr;
11 root['ipaddr'] = ipaddr;
14 matchCIDR = function(first, second, partSize, cidrBits) {
16 if (first.length !== second.length) {
17 throw new Error("ipaddr: cannot match CIDR for objects with different lengths");
20 while (cidrBits > 0) {
21 shift = partSize - cidrBits;
25 if (first[part] >> shift !== second[part] >> shift) {
34 ipaddr.subnetMatch = function(address, rangeList, defaultName) {
35 var rangeName, rangeSubnets, subnet, _i, _len;
36 if (defaultName == null) {
37 defaultName = 'unicast';
39 for (rangeName in rangeList) {
40 rangeSubnets = rangeList[rangeName];
41 if (rangeSubnets[0] && !(rangeSubnets[0] instanceof Array)) {
42 rangeSubnets = [rangeSubnets];
44 for (_i = 0, _len = rangeSubnets.length; _i < _len; _i++) {
45 subnet = rangeSubnets[_i];
46 if (address.match.apply(address, subnet)) {
54 ipaddr.IPv4 = (function() {
55 function IPv4(octets) {
57 if (octets.length !== 4) {
58 throw new Error("ipaddr: ipv4 octet count should be 4");
60 for (_i = 0, _len = octets.length; _i < _len; _i++) {
62 if (!((0 <= octet && octet <= 255))) {
63 throw new Error("ipaddr: ipv4 octet should fit in 8 bits");
69 IPv4.prototype.kind = function() {
73 IPv4.prototype.toString = function() {
74 return this.octets.join(".");
77 IPv4.prototype.toByteArray = function() {
78 return this.octets.slice(0);
81 IPv4.prototype.match = function(other, cidrRange) {
83 if (cidrRange === void 0) {
84 _ref = other, other = _ref[0], cidrRange = _ref[1];
86 if (other.kind() !== 'ipv4') {
87 throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");
89 return matchCIDR(this.octets, other.octets, 8, cidrRange);
92 IPv4.prototype.SpecialRanges = {
93 unspecified: [[new IPv4([0, 0, 0, 0]), 8]],
94 broadcast: [[new IPv4([255, 255, 255, 255]), 32]],
95 multicast: [[new IPv4([224, 0, 0, 0]), 4]],
96 linkLocal: [[new IPv4([169, 254, 0, 0]), 16]],
97 loopback: [[new IPv4([127, 0, 0, 0]), 8]],
98 "private": [[new IPv4([10, 0, 0, 0]), 8], [new IPv4([172, 16, 0, 0]), 12], [new IPv4([192, 168, 0, 0]), 16]],
99 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]]
102 IPv4.prototype.range = function() {
103 return ipaddr.subnetMatch(this, this.SpecialRanges);
106 IPv4.prototype.toIPv4MappedAddress = function() {
107 return ipaddr.IPv6.parse("::ffff:" + (this.toString()));
110 IPv4.prototype.prefixLengthFromSubnetMask = function() {
111 var cidr, i, octet, stop, zeros, zerotable, _i;
125 for (i = _i = 3; _i >= 0; i = _i += -1) {
126 octet = this.octets[i];
127 if (octet in zerotable) {
128 zeros = zerotable[octet];
129 if (stop && zeros !== 0) {
147 ipv4Part = "(0?\\d+|0x[a-f0-9]+)";
150 fourOctet: new RegExp("^" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$", 'i'),
151 longValue: new RegExp("^" + ipv4Part + "$", 'i')
154 ipaddr.IPv4.parser = function(string) {
155 var match, parseIntAuto, part, shift, value;
156 parseIntAuto = function(string) {
157 if (string[0] === "0" && string[1] !== "x") {
158 return parseInt(string, 8);
160 return parseInt(string);
163 if (match = string.match(ipv4Regexes.fourOctet)) {
165 var _i, _len, _ref, _results;
166 _ref = match.slice(1, 6);
168 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
170 _results.push(parseIntAuto(part));
174 } else if (match = string.match(ipv4Regexes.longValue)) {
175 value = parseIntAuto(match[1]);
176 if (value > 0xffffffff || value < 0) {
177 throw new Error("ipaddr: address outside defined range");
179 return ((function() {
182 for (shift = _i = 0; _i <= 24; shift = _i += 8) {
183 _results.push((value >> shift) & 0xff);
192 ipaddr.IPv6 = (function() {
193 function IPv6(parts) {
194 var i, part, _i, _j, _len, _ref;
195 if (parts.length === 16) {
197 for (i = _i = 0; _i <= 14; i = _i += 2) {
198 this.parts.push((parts[i] << 8) | parts[i + 1]);
200 } else if (parts.length === 8) {
203 throw new Error("ipaddr: ipv6 part count should be 8 or 16");
206 for (_j = 0, _len = _ref.length; _j < _len; _j++) {
208 if (!((0 <= part && part <= 0xffff))) {
209 throw new Error("ipaddr: ipv6 part should fit in 16 bits");
214 IPv6.prototype.kind = function() {
218 IPv6.prototype.toString = function() {
219 var compactStringParts, part, pushPart, state, stringParts, _i, _len;
220 stringParts = (function() {
221 var _i, _len, _ref, _results;
224 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
226 _results.push(part.toString(16));
230 compactStringParts = [];
231 pushPart = function(part) {
232 return compactStringParts.push(part);
235 for (_i = 0, _len = stringParts.length; _i < _len; _i++) {
236 part = stringParts[_i];
268 return compactStringParts.join(":");
271 IPv6.prototype.toByteArray = function() {
272 var bytes, part, _i, _len, _ref;
275 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
277 bytes.push(part >> 8);
278 bytes.push(part & 0xff);
283 IPv6.prototype.toNormalizedString = function() {
285 return ((function() {
286 var _i, _len, _ref, _results;
289 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
291 _results.push(part.toString(16));
294 }).call(this)).join(":");
297 IPv6.prototype.match = function(other, cidrRange) {
299 if (cidrRange === void 0) {
300 _ref = other, other = _ref[0], cidrRange = _ref[1];
302 if (other.kind() !== 'ipv6') {
303 throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");
305 return matchCIDR(this.parts, other.parts, 16, cidrRange);
308 IPv6.prototype.SpecialRanges = {
309 unspecified: [new IPv6([0, 0, 0, 0, 0, 0, 0, 0]), 128],
310 linkLocal: [new IPv6([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 10],
311 multicast: [new IPv6([0xff00, 0, 0, 0, 0, 0, 0, 0]), 8],
312 loopback: [new IPv6([0, 0, 0, 0, 0, 0, 0, 1]), 128],
313 uniqueLocal: [new IPv6([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7],
314 ipv4Mapped: [new IPv6([0, 0, 0, 0, 0, 0xffff, 0, 0]), 96],
315 rfc6145: [new IPv6([0, 0, 0, 0, 0xffff, 0, 0, 0]), 96],
316 rfc6052: [new IPv6([0x64, 0xff9b, 0, 0, 0, 0, 0, 0]), 96],
317 '6to4': [new IPv6([0x2002, 0, 0, 0, 0, 0, 0, 0]), 16],
318 teredo: [new IPv6([0x2001, 0, 0, 0, 0, 0, 0, 0]), 32],
319 reserved: [[new IPv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0]), 32]]
322 IPv6.prototype.range = function() {
323 return ipaddr.subnetMatch(this, this.SpecialRanges);
326 IPv6.prototype.isIPv4MappedAddress = function() {
327 return this.range() === 'ipv4Mapped';
330 IPv6.prototype.toIPv4Address = function() {
332 if (!this.isIPv4MappedAddress()) {
333 throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");
335 _ref = this.parts.slice(-2), high = _ref[0], low = _ref[1];
336 return new ipaddr.IPv4([high >> 8, high & 0xff, low >> 8, low & 0xff]);
343 ipv6Part = "(?:[0-9a-f]+::?)+";
346 "native": new RegExp("^(::)?(" + ipv6Part + ")?([0-9a-f]+)?(::)?$", 'i'),
347 transitional: new RegExp(("^((?:" + ipv6Part + ")|(?:::)(?:" + ipv6Part + ")?)") + ("" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$"), 'i')
350 expandIPv6 = function(string, parts) {
351 var colonCount, lastColon, part, replacement, replacementCount;
352 if (string.indexOf('::') !== string.lastIndexOf('::')) {
357 while ((lastColon = string.indexOf(':', lastColon + 1)) >= 0) {
360 if (string.substr(0, 2) === '::') {
363 if (string.substr(-2, 2) === '::') {
366 if (colonCount > parts) {
369 replacementCount = parts - colonCount;
371 while (replacementCount--) {
374 string = string.replace('::', replacement);
375 if (string[0] === ':') {
376 string = string.slice(1);
378 if (string[string.length - 1] === ':') {
379 string = string.slice(0, -1);
382 var _i, _len, _ref, _results;
383 _ref = string.split(":");
385 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
387 _results.push(parseInt(part, 16));
393 ipaddr.IPv6.parser = function(string) {
394 var match, octet, octets, parts, _i, _len;
395 if (string.match(ipv6Regexes['native'])) {
396 return expandIPv6(string, 8);
397 } else if (match = string.match(ipv6Regexes['transitional'])) {
398 parts = expandIPv6(match[1].slice(0, -1), 6);
400 octets = [parseInt(match[2]), parseInt(match[3]), parseInt(match[4]), parseInt(match[5])];
401 for (_i = 0, _len = octets.length; _i < _len; _i++) {
403 if (!((0 <= octet && octet <= 255))) {
407 parts.push(octets[0] << 8 | octets[1]);
408 parts.push(octets[2] << 8 | octets[3]);
415 ipaddr.IPv4.isIPv4 = ipaddr.IPv6.isIPv6 = function(string) {
416 return this.parser(string) !== null;
419 ipaddr.IPv4.isValid = function(string) {
422 new this(this.parser(string));
430 ipaddr.IPv6.isValid = function(string) {
432 if (typeof string === "string" && string.indexOf(":") === -1) {
436 new this(this.parser(string));
444 ipaddr.IPv4.parse = ipaddr.IPv6.parse = function(string) {
446 parts = this.parser(string);
447 if (parts === null) {
448 throw new Error("ipaddr: string is not formatted like ip address");
450 return new this(parts);
453 ipaddr.IPv4.parseCIDR = function(string) {
454 var maskLength, match;
455 if (match = string.match(/^(.+)\/(\d+)$/)) {
456 maskLength = parseInt(match[2]);
457 if (maskLength >= 0 && maskLength <= 32) {
458 return [this.parse(match[1]), maskLength];
461 throw new Error("ipaddr: string is not formatted like an IPv4 CIDR range");
464 ipaddr.IPv6.parseCIDR = function(string) {
465 var maskLength, match;
466 if (match = string.match(/^(.+)\/(\d+)$/)) {
467 maskLength = parseInt(match[2]);
468 if (maskLength >= 0 && maskLength <= 128) {
469 return [this.parse(match[1]), maskLength];
472 throw new Error("ipaddr: string is not formatted like an IPv6 CIDR range");
475 ipaddr.isValid = function(string) {
476 return ipaddr.IPv6.isValid(string) || ipaddr.IPv4.isValid(string);
479 ipaddr.parse = function(string) {
480 if (ipaddr.IPv6.isValid(string)) {
481 return ipaddr.IPv6.parse(string);
482 } else if (ipaddr.IPv4.isValid(string)) {
483 return ipaddr.IPv4.parse(string);
485 throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format");
489 ipaddr.parseCIDR = function(string) {
492 return ipaddr.IPv6.parseCIDR(string);
496 return ipaddr.IPv4.parseCIDR(string);
499 throw new Error("ipaddr: the address has neither IPv6 nor IPv4 CIDR format");
504 ipaddr.fromByteArray = function(bytes) {
506 length = bytes.length;
508 return new ipaddr.IPv4(bytes);
509 } else if (length === 16) {
510 return new ipaddr.IPv6(bytes);
512 throw new Error("ipaddr: the binary input is neither an IPv6 nor IPv4 address");
516 ipaddr.process = function(string) {
518 addr = this.parse(string);
519 if (addr.kind() === 'ipv6' && addr.isIPv4MappedAddress()) {
520 return addr.toIPv4Address();