Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/IPAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ exports.parseIPv6 = function parseIPv6(ip) {
}
return v6Address.parsedAddress;
};

exports.parseIPv4MappedIPv6 = function parseIPv4MappedIPv6(ip){
var v6Address = new IPv6.Address(ip);
if (!v6Address.isValid() || !v6Address.is4()){
throw new Error("Invalid IPv4-mapped IPv6 address");
}
return v6Address.tov4().parsedAddress
}
8 changes: 7 additions & 1 deletion lib/IPParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module.exports = IPParser;

function IPParser(ip) {
if (ip.indexOf('.') !== -1) {
return IPParser.parseIPv4(ip);
if (ip.indexOf(':') !== -1){
return IPParser.parseIPv4MappedIPv6(ip)
} else return IPParser.parseIPv4(ip);
} else {
return IPParser.parseIPv6(ip);
}
Expand All @@ -20,6 +22,10 @@ IPParser.parseIPv6 = function parseIPv6(ip) {
return ipv6Buffer(IPAddress.parseIPv6(ip));
};

IPParser.parseIPv4MappedIPv6 = function parseIPv4MappedIPv6(ip){
return ipv4Buffer(IPAddress.parseIPv4MappedIPv6(ip));
}

function ipv4Buffer(groups) {
var arr = new Buffer(4);
arr.fill(0);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"dependencies": {
"big-integer": ">=1.1.5",
"ip-address": "4.0.0"
"ip-address": "4.2.0"
},
"devDependencies": {
"istanbul": "^0.3.13",
Expand Down
19 changes: 17 additions & 2 deletions test/test-ipparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,24 @@ ipParseTest('IPv6', function run(it) {
t.end();
});

it('should throw exception on invalid ipv4 formats', function should(t) {
it('should throw exception on invalid ipv6 formats', function should(t) {
t.verifyParseThrows('myipaddress');
t.verifyParseThrows('::ffff:192.0.2.128');
t.verifyParseThrows('2001:0db8:85a3:00000000:8a2e:0370:7334');
t.verifyParseThrows('2001:db8:85a3:8a2e:370:7334');
t.end();
});
});

ipParseTest('IPv4-Mapped IPv6', function run(it) {
it('should successfully parse correct formats', function should(t) {
t.verifyParse('::ffff:192.0.2.128', new Buffer([0xc0, 0x00, 0x02, 0x80]));
t.verifyParse('0:0:0:0:0:FFFF:222.1.41.90', new Buffer([0xde, 0x01, 0x29, 0x5a]));
t.end();
});

it('should throw exception on invalid IPv6 address with Embedded IPv4 address', function should(t) {
t.verifyParseThrows(':192.0.2.128');
t.verifyParseThrows('::ffff:192.0.2.128:');
t.end();
});
});