From 6cb40890eca206be4ff9db95c09b85679721ae9e Mon Sep 17 00:00:00 2001 From: "James M. Greene" Date: Mon, 21 Aug 2017 14:32:24 -0500 Subject: [PATCH] Fix zoneIndex parsing for IPv4-mapped IPv6 addresses Fixes #79 --- ipaddr.min.js | 2 +- lib/ipaddr.js | 5 +++-- src/ipaddr.coffee | 15 ++++++++------- test/ipaddr.test.coffee | 6 ++++++ 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/ipaddr.min.js b/ipaddr.min.js index 4931324..34f345d 100644 --- a/ipaddr.min.js +++ b/ipaddr.min.js @@ -1 +1 @@ -(function(){var r,t,n,e,i,o,a,s,p;t={},s=this,"undefined"!=typeof module&&null!==module&&module.exports?module.exports=t:s.ipaddr=t,a=function(r,t,n,e){var i,o;if(r.length!==t.length)throw new Error("ipaddr: cannot match CIDR for objects with different lengths");for(i=0;e>0;){if((o=n-e)<0&&(o=0),r[i]>>o!=t[i]>>o)return!1;e-=n,i+=1}return!0},t.subnetMatch=function(r,t,n){var e,i,o,a,s;null==n&&(n="unicast");for(o in t)for(!(a=t[o])[0]||a[0]instanceof Array||(a=[a]),e=0,i=a.length;e=0;t=n+=-1){if(!((e=this.octets[t])in a))return null;if(o=a[e],i&&0!==o)return null;8!==o&&(i=!0),r+=o}return 32-r},r}(),n="(0?\\d+|0x[a-f0-9]+)",e={fourOctet:new RegExp("^"+n+"\\."+n+"\\."+n+"\\."+n+"$","i"),longValue:new RegExp("^"+n+"$","i")},t.IPv4.parser=function(r){var t,n,i,o,a;if(n=function(r){return"0"===r[0]&&"x"!==r[1]?parseInt(r,8):parseInt(r)},t=r.match(e.fourOctet))return function(){var r,e,o,a;for(a=[],r=0,e=(o=t.slice(1,6)).length;r4294967295||a<0)throw new Error("ipaddr: address outside defined range");return function(){var r,t;for(t=[],o=r=0;r<=24;o=r+=8)t.push(a>>o&255);return t}().reverse()}return null},t.IPv6=function(){function r(r,t){var n,e,i,o,a,s;if(16===r.length)for(this.parts=[],n=e=0;e<=14;n=e+=2)this.parts.push(r[n]<<8|r[n+1]);else{if(8!==r.length)throw new Error("ipaddr: ipv6 part count should be 8 or 16");this.parts=r}for(i=0,o=(s=this.parts).length;i>8),r.push(255&e);return r},r.prototype.toNormalizedString=function(){var r,t,n;return r=function(){var r,n,e,i;for(i=[],r=0,n=(e=this.parts).length;r>8,255&r,n>>8,255&n])},r.prototype.prefixLengthFromSubnetMask=function(){var r,t,n,e,i,o,a;for(a={0:16,32768:15,49152:14,57344:13,61440:12,63488:11,64512:10,65024:9,65280:8,65408:7,65472:6,65504:5,65520:4,65528:3,65532:2,65534:1,65535:0},r=0,i=!1,t=n=7;n>=0;t=n+=-1){if(!((e=this.parts[t])in a))return null;if(o=a[e],i&&0!==o)return null;16!==o&&(i=!0),r+=o}return 128-r},r}(),i="(?:[0-9a-f]+::?)+",p="%[0-9a-z]{1,}",o={zoneIndex:new RegExp(p,"i"),native:new RegExp("^(::)?("+i+")?([0-9a-f]+)?(::)?("+p+")?$","i"),transitional:new RegExp("^((?:"+i+")|(?:::)(?:"+i+")?)"+n+"\\."+n+"\\."+n+"\\."+n+"(%[0-9a-z]{1,})?$","i")},r=function(r,t){var n,e,i,o,a,s;if(r.indexOf("::")!==r.lastIndexOf("::"))return null;for((s=(r.match(p)||[])[0])&&(s=s.substring(1),r=r.replace(/%.+$/,"")),n=0,e=-1;(e=r.indexOf(":",e+1))>=0;)n++;if("::"===r.substr(0,2)&&n--,"::"===r.substr(-2,2)&&n--,n>t)return null;for(a=t-n,o=":";a--;)o+="0:";return":"===(r=r.replace("::",o))[0]&&(r=r.slice(1)),":"===r[r.length-1]&&(r=r.slice(0,-1)),t=function(){var t,n,e,o;for(o=[],t=0,n=(e=r.split(":")).length;t=0&&t<=32)return[this.parse(n[1]),t];throw new Error("ipaddr: string is not formatted like an IPv4 CIDR range")},t.IPv4.subnetMaskFromPrefixLength=function(r){var t,n,e;if((r=parseInt(r))<0||r>32)throw new Error("ipaddr: invalid IPv4 prefix length");for(e=[0,0,0,0],n=0,t=Math.floor(r/8);n=0&&t<=128)return[this.parse(n[1]),t];throw new Error("ipaddr: string is not formatted like an IPv6 CIDR range")},t.isValid=function(r){return t.IPv6.isValid(r)||t.IPv4.isValid(r)},t.parse=function(r){if(t.IPv6.isValid(r))return t.IPv6.parse(r);if(t.IPv4.isValid(r))return t.IPv4.parse(r);throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format")},t.parseCIDR=function(r){try{return t.IPv6.parseCIDR(r)}catch(n){n;try{return t.IPv4.parseCIDR(r)}catch(r){throw r,new Error("ipaddr: the address has neither IPv6 nor IPv4 CIDR format")}}},t.fromByteArray=function(r){var n;if(4===(n=r.length))return new t.IPv4(r);if(16===n)return new t.IPv6(r);throw new Error("ipaddr: the binary input is neither an IPv6 nor IPv4 address")},t.process=function(r){var t;return"ipv6"===(t=this.parse(r)).kind()&&t.isIPv4MappedAddress()?t.toIPv4Address():t}}).call(this); \ No newline at end of file +(function(){var r,t,n,e,i,o,a,s,p;t={},s=this,"undefined"!=typeof module&&null!==module&&module.exports?module.exports=t:s.ipaddr=t,a=function(r,t,n,e){var i,o;if(r.length!==t.length)throw new Error("ipaddr: cannot match CIDR for objects with different lengths");for(i=0;e>0;){if((o=n-e)<0&&(o=0),r[i]>>o!=t[i]>>o)return!1;e-=n,i+=1}return!0},t.subnetMatch=function(r,t,n){var e,i,o,a,s;null==n&&(n="unicast");for(o in t)for(!(a=t[o])[0]||a[0]instanceof Array||(a=[a]),e=0,i=a.length;e=0;t=n+=-1){if(!((e=this.octets[t])in a))return null;if(o=a[e],i&&0!==o)return null;8!==o&&(i=!0),r+=o}return 32-r},r}(),n="(0?\\d+|0x[a-f0-9]+)",e={fourOctet:new RegExp("^"+n+"\\."+n+"\\."+n+"\\."+n+"$","i"),longValue:new RegExp("^"+n+"$","i")},t.IPv4.parser=function(r){var t,n,i,o,a;if(n=function(r){return"0"===r[0]&&"x"!==r[1]?parseInt(r,8):parseInt(r)},t=r.match(e.fourOctet))return function(){var r,e,o,a;for(a=[],r=0,e=(o=t.slice(1,6)).length;r4294967295||a<0)throw new Error("ipaddr: address outside defined range");return function(){var r,t;for(t=[],o=r=0;r<=24;o=r+=8)t.push(a>>o&255);return t}().reverse()}return null},t.IPv6=function(){function r(r,t){var n,e,i,o,a,s;if(16===r.length)for(this.parts=[],n=e=0;e<=14;n=e+=2)this.parts.push(r[n]<<8|r[n+1]);else{if(8!==r.length)throw new Error("ipaddr: ipv6 part count should be 8 or 16");this.parts=r}for(i=0,o=(s=this.parts).length;i>8),r.push(255&e);return r},r.prototype.toNormalizedString=function(){var r,t,n;return r=function(){var r,n,e,i;for(i=[],r=0,n=(e=this.parts).length;r>8,255&r,n>>8,255&n])},r.prototype.prefixLengthFromSubnetMask=function(){var r,t,n,e,i,o,a;for(a={0:16,32768:15,49152:14,57344:13,61440:12,63488:11,64512:10,65024:9,65280:8,65408:7,65472:6,65504:5,65520:4,65528:3,65532:2,65534:1,65535:0},r=0,i=!1,t=n=7;n>=0;t=n+=-1){if(!((e=this.parts[t])in a))return null;if(o=a[e],i&&0!==o)return null;16!==o&&(i=!0),r+=o}return 128-r},r}(),i="(?:[0-9a-f]+::?)+",p="%[0-9a-z]{1,}",o={zoneIndex:new RegExp(p,"i"),native:new RegExp("^(::)?("+i+")?([0-9a-f]+)?(::)?("+p+")?$","i"),transitional:new RegExp("^((?:"+i+")|(?:::)(?:"+i+")?)"+n+"\\."+n+"\\."+n+"\\."+n+"(%[0-9a-z]{1,})?$","i")},r=function(r,t){var n,e,i,o,a,s;if(r.indexOf("::")!==r.lastIndexOf("::"))return null;for((s=(r.match(p)||[])[0])&&(s=s.substring(1),r=r.replace(/%.+$/,"")),n=0,e=-1;(e=r.indexOf(":",e+1))>=0;)n++;if("::"===r.substr(0,2)&&n--,"::"===r.substr(-2,2)&&n--,n>t)return null;for(a=t-n,o=":";a--;)o+="0:";return":"===(r=r.replace("::",o))[0]&&(r=r.slice(1)),":"===r[r.length-1]&&(r=r.slice(0,-1)),t=function(){var t,n,e,o;for(o=[],t=0,n=(e=r.split(":")).length;t=0&&t<=32)return[this.parse(n[1]),t];throw new Error("ipaddr: string is not formatted like an IPv4 CIDR range")},t.IPv4.subnetMaskFromPrefixLength=function(r){var t,n,e;if((r=parseInt(r))<0||r>32)throw new Error("ipaddr: invalid IPv4 prefix length");for(e=[0,0,0,0],n=0,t=Math.floor(r/8);n=0&&t<=128)return[this.parse(n[1]),t];throw new Error("ipaddr: string is not formatted like an IPv6 CIDR range")},t.isValid=function(r){return t.IPv6.isValid(r)||t.IPv4.isValid(r)},t.parse=function(r){if(t.IPv6.isValid(r))return t.IPv6.parse(r);if(t.IPv4.isValid(r))return t.IPv4.parse(r);throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format")},t.parseCIDR=function(r){try{return t.IPv6.parseCIDR(r)}catch(n){n;try{return t.IPv4.parseCIDR(r)}catch(r){throw r,new Error("ipaddr: the address has neither IPv6 nor IPv4 CIDR format")}}},t.fromByteArray=function(r){var n;if(4===(n=r.length))return new t.IPv4(r);if(16===n)return new t.IPv6(r);throw new Error("ipaddr: the binary input is neither an IPv6 nor IPv4 address")},t.process=function(r){var t;return"ipv6"===(t=this.parse(r)).kind()&&t.isIPv4MappedAddress()?t.toIPv4Address():t}}).call(this); \ No newline at end of file diff --git a/lib/ipaddr.js b/lib/ipaddr.js index c464209..7033046 100644 --- a/lib/ipaddr.js +++ b/lib/ipaddr.js @@ -464,11 +464,12 @@ }; ipaddr.IPv6.parser = function(string) { - var addr, k, len, match, octet, octets; + var addr, k, len, match, octet, octets, zoneId; if (ipv6Regexes['native'].test(string)) { return expandIPv6(string, 8); } else if (match = string.match(ipv6Regexes['transitional'])) { - addr = expandIPv6(match[1].slice(0, -1), 6); + zoneId = match[6] || ''; + addr = expandIPv6(match[1].slice(0, -1) + zoneId, 6); if (addr.parts) { octets = [parseInt(match[2]), parseInt(match[3]), parseInt(match[4]), parseInt(match[5])]; for (k = 0, len = octets.length; k < len; k++) { diff --git a/src/ipaddr.coffee b/src/ipaddr.coffee index f03a98b..8b28c92 100644 --- a/src/ipaddr.coffee +++ b/src/ipaddr.coffee @@ -381,10 +381,10 @@ expandIPv6 = (string, parts) -> return null # Remove zone index and save it for later - zoneId = (string.match(zoneIndex) || [])[0]; + zoneId = (string.match(zoneIndex) || [])[0] if zoneId - zoneId = zoneId.substring(1); - string = string.replace(/%.+$/, ''); + zoneId = zoneId.substring(1) + string = string.replace(/%.+$/, '') # How many parts do we already have? colonCount = 0 @@ -415,7 +415,7 @@ expandIPv6 = (string, parts) -> string = string[0..-2] if string[string.length-1] == ':' parts = (parseInt(part, 16) for part in string.split(":")) - return {parts: parts, zoneId: zoneId} + return { parts: parts, zoneId: zoneId } # Parse an IPv6 address. ipaddr.IPv6.parser = (string) -> @@ -423,7 +423,8 @@ ipaddr.IPv6.parser = (string) -> return expandIPv6(string, 8) else if match = string.match(ipv6Regexes['transitional']) - addr = expandIPv6(match[1][0..-2], 6) + zoneId = match[6] || '' + addr = expandIPv6(match[1][0..-2] + zoneId, 6) if addr.parts octets = [parseInt(match[2]), parseInt(match[3]), parseInt(match[4]), parseInt(match[5])] @@ -433,7 +434,7 @@ ipaddr.IPv6.parser = (string) -> addr.parts.push(octets[0] << 8 | octets[1]) addr.parts.push(octets[2] << 8 | octets[3]) - return {parts: addr.parts, zoneId: addr.zoneId} + return { parts: addr.parts, zoneId: addr.zoneId } return null @@ -470,7 +471,7 @@ ipaddr.IPv6.isValid = (string) -> # Tries to parse and validate a string with IPv4/IPv6 address. # Throws an error if it fails. -ipaddr.IPv4.parse= (string) -> +ipaddr.IPv4.parse = (string) -> parts = @parser(string) if parts == null throw new Error "ipaddr: string is not formatted like ip address" diff --git a/test/ipaddr.test.coffee b/test/ipaddr.test.coffee index b42387d..7aed7fb 100644 --- a/test/ipaddr.test.coffee +++ b/test/ipaddr.test.coffee @@ -149,6 +149,12 @@ module.exports = test.equal(addr.toString(), '2001:db8:f53a::1%utun0') test.done() + 'returns IPv6 zoneIndex for IPv4-mapped IPv6 addresses': (test) -> + addr = ipaddr.parse('::ffff:192.168.1.1%eth0') + test.equal(addr.toNormalizedString(), '0:0:0:0:0:ffff:c0a8:101%eth0') + test.equal(addr.toString(), '::ffff:c0a8:101%eth0') + test.done() + 'returns correct kind for IPv6': (test) -> addr = new ipaddr.IPv6([0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 1]) test.equal(addr.kind(), 'ipv6')