utils: rewrite ip module.

This commit is contained in:
Christopher Jeffrey 2016-12-19 02:28:56 -08:00
parent 1836d8b314
commit b51554d765
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 273 additions and 518 deletions

View File

@ -1,25 +1,35 @@
/*!
* ip.js - ip utils for bcoin
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* Copyright (c) 2014-2015, Fedor Indutny (MIT License).
* Copyright (c) 2014-2016, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*
* Parts of this software are based on node-ip.
* https://github.com/indutny/node-ip
* Copyright (c) 202, Fedor Indutny (MIT License).
*/
'use strict';
var IP = require('../../vendor/ip');
var assert = require('assert');
var IP = exports;
/*
* Constants
*/
var ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
var ipv6Regex =
/^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i;
/**
* Parse a hostname.
* @example
* IP.parseHost('127.0.0.1:3000');
* @param {String} addr
* @param {Number?} fallback - Fallback port.
* @returns {Object} Contains `host` and `port`.
* @returns {Object} Contains `host`, `port`, and `version`.
*/
exports.parseHost = function parseHost(addr, fallback) {
IP.parseHost = function parseHost(addr, fallback) {
var port = fallback || 0;
var parts, host, version;
@ -54,10 +64,10 @@ exports.parseHost = function parseHost(addr, fallback) {
port = parseInt(port, 10);
}
version = exports.version(host);
version = IP.version(host);
if (version !== -1)
host = exports.normalize(host);
host = IP.normalize(host);
return new Address(host, port, version);
};
@ -69,7 +79,7 @@ exports.parseHost = function parseHost(addr, fallback) {
* @returns {String}
*/
exports.hostname = function hostname(host, port) {
IP.hostname = function hostname(host, port) {
var version;
assert(typeof host === 'string');
@ -78,10 +88,10 @@ exports.hostname = function hostname(host, port) {
assert(!/[\[\]]/.test(host), 'Bad host.');
version = exports.version(host);
version = IP.version(host);
if (version !== -1)
host = exports.normalize(host);
host = IP.normalize(host);
if (version === 6)
host = '[' + host + ']';
@ -91,104 +101,310 @@ exports.hostname = function hostname(host, port) {
/**
* Test whether a string is an IP address.
* @param {String?} ip
* @param {String?} str
* @returns {Number} IP version (4 or 6).
*/
exports.version = function version(ip) {
assert(typeof ip === 'string');
IP.version = function version(str) {
assert(typeof str === 'string');
if (IP.isV4Format(ip))
if (IP.isV4Format(str))
return 4;
if (IP.isV6Format(ip))
if (IP.isV6Format(str))
return 6;
return -1;
};
/**
* Test whether a buffer is an ipv4-mapped ipv6 address.
* @param {Buffer} ip
* Test whether a string is IPv4.
* @param {String?} str
* @returns {Boolean}
*/
exports.isMapped = function isMapped(ip) {
IP.isV4Format = function(str) {
return ipv4Regex.test(str);
};
/**
* Test whether a string is IPv6.
* @param {String?} str
* @returns {Boolean}
*/
IP.isV6Format = function(str) {
return ipv6Regex.test(str);
};
/**
* Test whether a buffer is an ipv4-mapped ipv6 address.
* @param {Buffer} buf
* @returns {Boolean}
*/
IP.isMapped = function isMapped(buf) {
var i;
assert(Buffer.isBuffer(ip));
assert(ip.length === 16);
assert(Buffer.isBuffer(buf));
assert(buf.length === 16);
for (i = 0; i < ip.length - 6; i++) {
if (ip[i] !== 0)
if (buf[10] !== 0xff && buf[11] !== 0xff)
return false;
for (i = 0; i < 10; i++) {
if (buf[i] !== 0)
return false;
}
if (ip[ip.length - 6] !== 0xff && ip[ip.length - 5] !== 0xff)
return false;
return true;
};
/**
* Convert an IP string to a buffer.
* @param {String} ip
* @param {String} str
* @returns {Buffer}
*/
exports.toBuffer = function toBuffer(ip) {
var out;
IP.toBuffer = function(str) {
var buf = new Buffer(16);
assert(typeof ip === 'string');
assert(exports.version(ip) !== -1);
assert(typeof str === 'string');
ip = IP.toBuffer(ip);
if (ip.length === 4) {
out = new Buffer(16);
out.fill(0);
out[10] = 0xff;
out[11] = 0xff;
out[12] = ip[0];
out[13] = ip[1];
out[14] = ip[2];
out[15] = ip[3];
return out;
if (IP.isV4Format(str)) {
buf.fill(0);
buf[10] = 0xff;
buf[11] = 0xff;
return IP.parseV4(str, buf, 12);
}
return ip;
if (IP.isV6Format(str))
return IP.parseV6(str, buf, 0);
throw Error('Invalid IP address: ' + str);
};
/**
* Convert an IPv4 string to a buffer.
* @private
* @param {String} str
* @param {Buffer} buf
* @param {Number} offset
* @returns {Buffer}
*/
IP.parseV4 = function parseV4(str, buf, offset) {
var parts = str.split('.');
var i, ch;
assert(parts.length === 4);
for (i = 0; i < parts.length; i++) {
ch = parts[i];
assert(ch.length > 0);
assert(ch.length <= 3);
ch = parseInt(ch, 10);
assert(ch >= 0 && ch <= 255);
buf[offset++] = ch;
}
return buf;
};
/**
* Convert an IPv6 string to a buffer.
* @private
* @param {String} str
* @param {Buffer} buf
* @param {Number} offset
* @returns {Buffer}
*/
IP.parseV6 = function parseV6(str, buf, offset) {
var parts = str.split(':');
var missing = 8 - parts.length;
var start = offset;
var len = parts.length;
var i = 0;
var word;
assert(missing >= 0, 'IPv6 address is too long.');
assert(parts.length >= 2, 'Not an IPv6 address.');
if (parts[0].length === 0) {
assert(parts[1].length === 0,
'Empty leading colon in IPv6 address.');
missing += 1;
i++;
} else if (parts[len - 1].length === 0) {
assert(parts[len - 2].length === 0,
'Empty trailing colon in IPv6 address.');
missing += 1;
len--;
}
for (; i < len; i++) {
word = parts[i];
if (word.length === 0) {
assert(missing > 0, 'Overuse of double colon in IPv6 address.');
missing += 1;
// Eat extra colons.
// e.g. :::0
while (i + 1 < len) {
word = parts[i + 1];
if (word.length !== 0)
break;
missing += 1;
i++;
}
while (missing > 0) {
buf[offset++] = 0;
buf[offset++] = 0;
missing--;
}
continue;
}
if (IP.isV4Format(word)) {
IP.parseV4(word, buf, offset);
offset += 4;
continue;
}
assert(word.length <= 4);
word = parseInt(word, 16);
assert(word === word, 'Non-number in IPv6 address.');
buf[offset++] = (word >> 8) & 0xff;
buf[offset++] = word & 0xff;
}
assert(missing === 0, 'IPv6 address has missing sections.');
assert.equal(offset, start + 16);
return buf;
};
/**
* Convert a buffer to an ip string.
* @param {Buffer} ip
* @param {Buffer} buf
* @returns {String}
*/
exports.toString = function toString(ip) {
assert(Buffer.isBuffer(ip));
assert(ip.length === 16);
IP.toString = function(buf) {
var str = '';
var i;
if (exports.isMapped(ip)) {
return ip[ip.length - 4]
+ '.' + ip[ip.length - 3]
+ '.' + ip[ip.length - 2]
+ '.' + ip[ip.length - 1];
assert(Buffer.isBuffer(buf));
if (buf.length === 4) {
str += buf[0];
str += '.' + buf[1];
str += '.' + buf[2];
str += '.' + buf[3];
return str;
}
return IP.toString(ip);
if (buf.length === 16) {
if (IP.isMapped(buf)) {
str += buf[12];
str += '.' + buf[13];
str += '.' + buf[14];
str += '.' + buf[15];
return str;
}
str += buf.readUInt16BE(0, true).toString(16);
for (i = 2; i < 16; i += 2) {
str += ':';
str += buf.readUInt16BE(i, true).toString(16);
}
str = str.replace(/(^|:)0(:0)*:0(:|$)/, '$1::$3');
str = str.replace(/:{3,4}/, '::');
return str;
}
throw Error('Invalid IP address: ' + buf.toString('hex'));
};
/**
* Normalize an ip.
* @param {String} ip
* @param {String} str
* @returns {String}
*/
exports.normalize = function normalize(ip) {
return exports.toString(exports.toBuffer(ip));
IP.normalize = function normalize(str) {
return IP.toString(IP.toBuffer(str));
};
/**
* Test whether a string is a private address.
* @param {String} str
* @returns {Boolean}
*/
IP.isPrivate = function(str) {
return /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(str)
|| /^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(str)
|| /^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(str)
|| /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(str)
|| /^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(str)
|| /^f[cd][0-9a-f]{2}:/i.test(str)
|| /^fe80:/i.test(str)
|| /^::1$/.test(str)
|| /^::$/.test(str);
};
/**
* Test whether a string is a public address.
* @param {String} str
* @returns {Boolean}
*/
IP.isPublic = function(str) {
return !IP.isPrivate(str);
};
/**
* Test whether a string is a loopback address.
* @param {String} str
* @returns {Boolean}
*/
IP.isLoopback = function(str) {
return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test(str)
|| /^fe80::1$/.test(str)
|| /^::1$/.test(str)
|| /^::$/.test(str);
};
/**
* Get loopback address for ip family.
* @param {String} family - ipv4 or ipv6.
* @returns {String}
*/
IP.loopback = function(family) {
if (!family)
family = 'ipv4';
family = family.toLowerCase();
if (family !== 'ipv4' && family !== 'ipv6')
throw new Error('Family must be ipv4 or ipv6.');
return family === 'ipv4' ? '127.0.0.1' : 'fe80::1';
};
/*
* Helpers
@ -199,25 +415,3 @@ function Address(host, port, version) {
this.port = port;
this.version = version;
}
/*
* Expose IP functions.
*/
exports.isV4Format = IP.isV4Format;
exports.isV6Format = IP.isV6Format;
exports.fromPrefixLen = IP.fromPrefixLen;
exports.mask = IP.mask;
exports.cidr = IP.cidr;
exports.subnet = IP.subnet;
exports.cidrSubnet = IP.cidrSubnet;
exports.not = IP.not;
exports.or = IP.or;
exports.isEqual = IP.isEqual;
exports.isPrivate = IP.isPrivate;
exports.isPublic = IP.isPublic;
exports.isLoopback = IP.isLoopback;
exports.loopback = IP.loopback;
exports.address = IP.address;
exports.toLong = IP.toLong;
exports.fromLong = IP.fromLong;

439
vendor/ip.js vendored
View File

@ -1,439 +0,0 @@
'use strict';
/*!
* https://github.com/indutny/node-ip
*
* This software is licensed under the MIT License.
*
* Copyright Fedor Indutny, 2012.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
var os = require('os');
var ip = exports;
ip.toBuffer = function(ip, buff, offset) {
offset = ~~offset;
var result;
if (this.isV4Format(ip)) {
result = buff || new Buffer(offset + 4);
ip.split(/\./g).map(function(byte) {
result[offset++] = parseInt(byte, 10) & 0xff;
});
} else if (this.isV6Format(ip)) {
var sections = ip.split(':', 8);
var i;
for (i = 0; i < sections.length; i++) {
var isv4 = this.isV4Format(sections[i]);
var v4Buffer;
if (isv4) {
v4Buffer = this.toBuffer(sections[i]);
sections[i] = v4Buffer.slice(0, 2).toString('hex');
}
if (v4Buffer && ++i < 8) {
sections.splice(i, 0, v4Buffer.slice(2, 4).toString('hex'));
}
}
if (sections[0] === '') {
while (sections.length < 8) sections.unshift('0');
} else if (sections[sections.length - 1] === '') {
while (sections.length < 8) sections.push('0');
} else if (sections.length < 8) {
for (i = 0; i < sections.length && sections[i] !== ''; i++);
var argv = [ i, 1 ];
for (i = 9 - sections.length; i > 0; i--) {
argv.push('0');
}
sections.splice.apply(sections, argv);
}
result = buff || new Buffer(offset + 16);
for (i = 0; i < sections.length; i++) {
var word = parseInt(sections[i], 16);
result[offset++] = (word >> 8) & 0xff;
result[offset++] = word & 0xff;
}
}
if (!result) {
throw Error('Invalid ip address: ' + ip);
}
return result;
};
ip.toString = function(buff, offset, length) {
offset = ~~offset;
length = length || (buff.length - offset);
var result = [];
if (length === 4) {
// IPv4
for (var i = 0; i < length; i++) {
result.push(buff[offset + i]);
}
result = result.join('.');
} else if (length === 16) {
// IPv6
for (var i = 0; i < length; i += 2) {
result.push(buff.readUInt16BE(offset + i).toString(16));
}
result = result.join(':');
result = result.replace(/(^|:)0(:0)*:0(:|$)/, '$1::$3');
result = result.replace(/:{3,4}/, '::');
}
return result;
};
var ipv4Regex = /^(\d{1,3}\.){3,3}\d{1,3}$/;
var ipv6Regex =
/^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i;
ip.isV4Format = function(ip) {
return ipv4Regex.test(ip);
};
ip.isV6Format = function(ip) {
return ipv6Regex.test(ip);
};
function _normalizeFamily(family) {
return family ? family.toLowerCase() : 'ipv4';
}
ip.fromPrefixLen = function(prefixlen, family) {
if (prefixlen > 32) {
family = 'ipv6';
} else {
family = _normalizeFamily(family);
}
var len = 4;
if (family === 'ipv6') {
len = 16;
}
var buff = new Buffer(len);
for (var i = 0, n = buff.length; i < n; ++i) {
var bits = 8;
if (prefixlen < 8) {
bits = prefixlen;
}
prefixlen -= bits;
buff[i] = ~(0xff >> bits);
}
return ip.toString(buff);
};
ip.mask = function(addr, mask) {
addr = ip.toBuffer(addr);
mask = ip.toBuffer(mask);
var result = new Buffer(Math.max(addr.length, mask.length));
// Same protocol - do bitwise and
if (addr.length === mask.length) {
for (var i = 0; i < addr.length; i++) {
result[i] = addr[i] & mask[i];
}
} else if (mask.length === 4) {
// IPv6 address and IPv4 mask
// (Mask low bits)
for (var i = 0; i < mask.length; i++) {
result[i] = addr[addr.length - 4 + i] & mask[i];
}
} else {
// IPv6 mask and IPv4 addr
for (var i = 0; i < result.length - 6; i++) {
result[i] = 0;
}
// ::ffff:ipv4
result[10] = 0xff;
result[11] = 0xff;
for (var i = 0; i < addr.length; i++) {
result[i + 12] = addr[i] & mask[i + 12];
}
}
return ip.toString(result);
};
ip.cidr = function(cidrString) {
var cidrParts = cidrString.split('/');
var addr = cidrParts[0];
if (cidrParts.length !== 2)
throw new Error('invalid CIDR subnet: ' + addr);
var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));
return ip.mask(addr, mask);
};
ip.subnet = function(addr, mask) {
var networkAddress = ip.toLong(ip.mask(addr, mask));
// Calculate the mask's length.
var maskBuffer = ip.toBuffer(mask);
var maskLength = 0;
for (var i = 0; i < maskBuffer.length; i++) {
if (maskBuffer[i] === 0xff) {
maskLength += 8;
} else {
var octet = maskBuffer[i] & 0xff;
while (octet) {
octet = (octet << 1) & 0xff;
maskLength++;
}
}
}
var numberOfAddresses = Math.pow(2, 32 - maskLength);
return {
networkAddress: ip.fromLong(networkAddress),
firstAddress: numberOfAddresses <= 2 ?
ip.fromLong(networkAddress) :
ip.fromLong(networkAddress + 1),
lastAddress: numberOfAddresses <= 2 ?
ip.fromLong(networkAddress + numberOfAddresses - 1) :
ip.fromLong(networkAddress + numberOfAddresses - 2),
broadcastAddress: ip.fromLong(networkAddress + numberOfAddresses - 1),
subnetMask: mask,
subnetMaskLength: maskLength,
numHosts: numberOfAddresses <= 2 ?
numberOfAddresses : numberOfAddresses - 2,
length: numberOfAddresses,
contains: function(other) {
return networkAddress === ip.toLong(ip.mask(other, mask));
}
};
};
ip.cidrSubnet = function(cidrString) {
var cidrParts = cidrString.split('/');
var addr = cidrParts[0];
if (cidrParts.length !== 2)
throw new Error('invalid CIDR subnet: ' + addr);
var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10));
return ip.subnet(addr, mask);
};
ip.not = function(addr) {
var buff = ip.toBuffer(addr);
for (var i = 0; i < buff.length; i++) {
buff[i] = 0xff ^ buff[i];
}
return ip.toString(buff);
};
ip.or = function(a, b) {
a = ip.toBuffer(a);
b = ip.toBuffer(b);
// same protocol
if (a.length === b.length) {
for (var i = 0; i < a.length; ++i) {
a[i] |= b[i];
}
return ip.toString(a);
// mixed protocols
} else {
var buff = a;
var other = b;
if (b.length > a.length) {
buff = b;
other = a;
}
var offset = buff.length - other.length;
for (var i = offset; i < buff.length; ++i) {
buff[i] |= other[i - offset];
}
return ip.toString(buff);
}
};
ip.isEqual = function(a, b) {
a = ip.toBuffer(a);
b = ip.toBuffer(b);
// Same protocol
if (a.length === b.length) {
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
// Swap
if (b.length === 4) {
var t = b;
b = a;
a = t;
}
// a - IPv4, b - IPv6
for (var i = 0; i < 10; i++) {
if (b[i] !== 0) return false;
}
var word = b.readUInt16BE(10);
if (word !== 0 && word !== 0xffff) return false;
for (var i = 0; i < 4; i++) {
if (a[i] !== b[i + 12]) return false;
}
return true;
};
ip.isPrivate = function(addr) {
return /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i
.test(addr) ||
/^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
/^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/i
.test(addr) ||
/^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
/^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
/^f[cd][0-9a-f]{2}:/i.test(addr) ||
/^fe80:/i.test(addr) ||
/^::1$/.test(addr) ||
/^::$/.test(addr);
};
ip.isPublic = function(addr) {
return !ip.isPrivate(addr);
};
ip.isLoopback = function(addr) {
return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/
.test(addr) ||
/^fe80::1$/.test(addr) ||
/^::1$/.test(addr) ||
/^::$/.test(addr);
};
ip.loopback = function(family) {
//
// Default to `ipv4`
//
family = _normalizeFamily(family);
if (family !== 'ipv4' && family !== 'ipv6') {
throw new Error('family must be ipv4 or ipv6');
}
return family === 'ipv4' ? '127.0.0.1' : 'fe80::1';
};
//
// ### function address (name, family)
// #### @name {string|'public'|'private'} **Optional** Name or security
// of the network interface.
// #### @family {ipv4|ipv6} **Optional** IP family of the address (defaults
// to ipv4).
//
// Returns the address for the network interface on the current system with
// the specified `name`:
// * String: First `family` address of the interface.
// If not found see `undefined`.
// * 'public': the first public ip address of family.
// * 'private': the first private ip address of family.
// * undefined: First address with `ipv4` or loopback address `127.0.0.1`.
//
ip.address = function(name, family) {
if (os.unsupported)
return '127.0.0.1';
var interfaces = os.networkInterfaces();
//
// Default to `ipv4`
//
family = _normalizeFamily(family);
//
// If a specific network interface has been named,
// return the address.
//
if (name && name !== 'private' && name !== 'public') {
var res = interfaces[name].filter(function(details) {
var itemFamily = details.family.toLowerCase();
return itemFamily === family;
});
if (res.length === 0)
return undefined;
return res[0].address;
}
var all = Object.keys(interfaces).map(function (nic) {
//
// Note: name will only be `public` or `private`
// when this is called.
//
var addresses = interfaces[nic].filter(function (details) {
details.family = details.family.toLowerCase();
if (details.family !== family || ip.isLoopback(details.address)) {
return false;
} else if (!name) {
return true;
}
return name === 'public' ? ip.isPrivate(details.address) :
ip.isPublic(details.address);
});
return addresses.length ? addresses[0].address : undefined;
}).filter(Boolean);
return !all.length ? ip.loopback(family) : all[0];
};
ip.toLong = function(ip) {
var ipl = 0;
ip.split('.').forEach(function(octet) {
ipl <<= 8;
ipl += parseInt(octet);
});
return(ipl >>> 0);
};
ip.fromLong = function(ipl) {
return ((ipl >>> 24) + '.' +
(ipl >> 16 & 255) + '.' +
(ipl >> 8 & 255) + '.' +
(ipl & 255) );
};