itns-sidechain/lib/utils/ip.js

1099 lines
20 KiB
JavaScript
Raw Normal View History

2016-05-25 14:38:35 -07:00
/*!
* ip.js - ip utils for bcoin
2016-12-19 02:28:56 -08:00
* Copyright (c) 2014-2015, Fedor Indutny (MIT License).
2017-02-03 22:47:26 -08:00
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
2016-06-09 16:18:50 -07:00
* https://github.com/bcoin-org/bcoin
2016-12-19 02:28:56 -08:00
*
* Parts of this software are based on node-ip.
* https://github.com/indutny/node-ip
2017-01-09 15:20:56 -08:00
* Copyright (c) 2012, Fedor Indutny (MIT License).
2016-05-25 14:38:35 -07:00
*/
2016-06-13 01:06:01 -07:00
'use strict';
2016-05-25 14:38:35 -07:00
var assert = require('assert');
var os = require('os');
2017-01-23 22:31:45 -08:00
var base32 = require('./base32');
2017-02-03 22:47:26 -08:00
/**
* @exports utils/ip
*/
2016-12-19 02:28:56 -08:00
var IP = exports;
/*
* Constants
*/
2017-01-23 19:13:47 -08:00
var ZERO_IP = new Buffer('00000000000000000000000000000000', 'hex');
var LOCAL_IP = new Buffer('00000000000000000000000000000001', 'hex');
var RFC6052 = new Buffer('0064ff9b0000000000000000', 'hex');
var RFC4862 = new Buffer('fe80000000000000', 'hex');
var RFC6145 = new Buffer('0000000000000000ffff0000', 'hex');
var TOR_ONION = new Buffer('fd87d87eeb43', 'hex');
var SHIFTED = new Buffer('00000000000000ffff', 'hex');
var IPV4_REGEX = /^(\d{1,3}\.){3}\d{1,3}$/;
var IPV6_REGEX =
2016-12-19 02:28:56 -08:00
/^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i;
2016-05-25 14:38:35 -07:00
2017-01-23 19:13:47 -08:00
/**
* IP address of all zeroes.
* @const {Buffer}
*/
IP.ZERO_IP = ZERO_IP;
2017-01-23 22:31:45 -08:00
/**
* Address types.
* @enum {Number}
*/
IP.types = {
DNS: -1,
IPV4: 4,
IPV6: 6,
2017-03-02 15:25:44 -08:00
ONION: 10,
TEREDO: 11
2017-01-23 22:31:45 -08:00
};
2016-05-25 14:38:35 -07:00
/**
* Parse a hostname.
* @param {String} addr
2016-12-16 13:43:23 -08:00
* @param {Number?} fallback - Fallback port.
2017-01-23 22:31:45 -08:00
* @returns {Object} Contains `host`, `port`, and `type`.
2016-05-25 14:38:35 -07:00
*/
2017-01-23 23:08:21 -08:00
IP.fromHostname = function fromHostname(addr, fallback) {
2017-01-23 22:31:45 -08:00
var parts, host, port, type, hostname, raw;
2016-05-25 14:38:35 -07:00
2016-12-16 13:43:23 -08:00
assert(typeof addr === 'string');
2016-05-25 14:38:35 -07:00
2016-12-19 00:43:42 -08:00
assert(addr.length > 0, 'Bad address.');
2016-05-25 14:38:35 -07:00
if (addr[0] === '[') {
2016-12-19 05:59:46 -08:00
if (addr[addr.length - 1] === ']') {
// Case:
// [::1]
host = addr.slice(1, -1);
port = null;
} else {
// Case:
// [::1]:80
addr = addr.slice(1);
parts = addr.split(']:');
assert(parts.length === 2, 'Bad IPv6 address.');
host = parts[0];
port = parts[1];
}
2016-05-25 14:38:35 -07:00
} else {
parts = addr.split(':');
2016-12-19 05:59:46 -08:00
switch (parts.length) {
case 2:
// Cases:
// 127.0.0.1:80
// localhost:80
host = parts[0];
port = parts[1];
break;
case 1:
// Cases:
// 127.0.0.1
// localhost
host = parts[0];
port = null;
break;
default:
// Case:
// ::1
2017-01-23 23:08:21 -08:00
assert(IP.isV6String(addr), 'Bad IPv6 address.');
2016-12-19 05:59:46 -08:00
host = addr;
port = null;
break;
2016-12-19 00:43:42 -08:00
}
2016-05-25 14:38:35 -07:00
}
2016-12-16 13:43:23 -08:00
assert(host.length > 0, 'Bad host.');
2016-12-19 05:59:46 -08:00
if (port != null) {
2016-12-19 06:31:48 -08:00
assert(port.length <= 5, 'Bad port.');
2016-12-16 13:43:23 -08:00
assert(/^\d+$/.test(port), 'Bad port.');
port = parseInt(port, 10);
2016-12-19 06:31:48 -08:00
assert(port <= 0xffff);
2016-12-19 05:59:46 -08:00
} else {
port = fallback || 0;
2016-12-16 13:43:23 -08:00
}
2017-01-23 23:08:21 -08:00
type = IP.getStringType(host);
2016-05-25 14:38:35 -07:00
2017-01-23 22:31:45 -08:00
if (type !== IP.types.DNS) {
2017-01-23 19:13:47 -08:00
raw = IP.toBuffer(host);
host = IP.toString(raw);
}
2016-05-25 14:38:35 -07:00
2016-12-20 15:40:36 -08:00
hostname = host;
2017-01-23 22:31:45 -08:00
if (type === IP.types.IPV6)
2016-12-20 15:40:36 -08:00
hostname = '[' + hostname + ']';
hostname += ':' + port;
2017-01-23 22:31:45 -08:00
return new Address(host, port, type, hostname, raw);
2016-05-25 14:38:35 -07:00
};
/**
* Concatenate a host and port.
* @param {String} host
* @param {Number} port
* @returns {String}
*/
2017-01-23 22:31:45 -08:00
IP.toHostname = function toHostname(host, port) {
var type;
2016-12-16 13:43:23 -08:00
assert(typeof host === 'string');
assert(host.length > 0);
assert(typeof port === 'number');
2016-12-19 06:31:48 -08:00
assert(port >= 0 && port <= 0xffff);
2016-12-16 13:43:23 -08:00
assert(!/[\[\]]/.test(host), 'Bad host.');
2017-01-23 23:08:21 -08:00
type = IP.getStringType(host);
2016-12-16 13:43:23 -08:00
2016-12-19 05:59:46 -08:00
if (host.indexOf(':') !== -1)
2017-01-23 22:31:45 -08:00
assert(type === IP.types.IPV6, 'Bad host.');
2016-12-19 05:59:46 -08:00
2017-01-23 22:31:45 -08:00
if (type !== IP.types.DNS)
2016-12-19 02:28:56 -08:00
host = IP.normalize(host);
2016-12-16 13:43:23 -08:00
2017-01-23 22:31:45 -08:00
if (type === IP.types.IPV6)
2016-05-25 14:38:35 -07:00
host = '[' + host + ']';
2016-12-16 13:43:23 -08:00
2016-05-25 14:38:35 -07:00
return host + ':' + port;
};
/**
2017-01-23 22:31:45 -08:00
* Get address type (-1=dns, 4=ipv4, 6=ipv6, 10=tor).
2016-12-19 02:28:56 -08:00
* @param {String?} str
2017-01-23 22:31:45 -08:00
* @returns {Number}
2016-05-25 14:38:35 -07:00
*/
2017-01-23 23:08:21 -08:00
IP.getStringType = function getStringType(str) {
if (IP.isV4String(str))
2017-01-23 22:31:45 -08:00
return IP.types.IPV4;
2016-05-25 14:38:35 -07:00
2017-01-23 23:08:21 -08:00
if (IP.isV6String(str))
2017-01-23 22:31:45 -08:00
return IP.types.IPV6;
2016-05-25 14:38:35 -07:00
2017-01-24 18:25:44 -08:00
if (IP.isOnionString(str))
return IP.types.ONION;
2017-01-23 22:31:45 -08:00
return IP.types.DNS;
2016-05-25 14:38:35 -07:00
};
2016-12-19 02:28:56 -08:00
/**
* Test whether a string is IPv4.
* @param {String?} str
* @returns {Boolean}
*/
2017-01-23 23:08:21 -08:00
IP.isV4String = function isV4String(str) {
2016-12-19 06:31:48 -08:00
assert(typeof str === 'string');
2016-12-19 04:08:35 -08:00
if (str.length < 7)
return false;
2016-12-19 05:59:46 -08:00
if (str.length > 15)
return false;
2017-01-23 19:13:47 -08:00
return IPV4_REGEX.test(str);
2016-12-19 02:28:56 -08:00
};
/**
* Test whether a string is IPv6.
* @param {String?} str
* @returns {Boolean}
*/
2017-01-23 23:08:21 -08:00
IP.isV6String = function isV6String(str) {
2016-12-19 06:31:48 -08:00
assert(typeof str === 'string');
2016-12-19 05:59:46 -08:00
if (str.length < 2)
return false;
if (str.length > 39)
return false;
2017-01-23 19:13:47 -08:00
return IPV6_REGEX.test(str);
2016-12-19 02:28:56 -08:00
};
2017-01-23 22:31:45 -08:00
/**
* Test whether a string is an onion address.
* @param {String?} str
* @returns {Boolean}
*/
2017-01-24 18:25:44 -08:00
IP.isOnionString = function isOnionString(str) {
2017-01-23 22:31:45 -08:00
assert(typeof str === 'string');
if (str.length < 7)
return false;
return str.slice(-6) === '.onion';
};
2016-05-25 14:38:35 -07:00
/**
* Test whether a buffer is an ipv4-mapped ipv6 address.
2017-01-23 19:13:47 -08:00
* @param {Buffer} raw
2016-05-25 14:38:35 -07:00
* @returns {Boolean}
*/
2017-01-23 19:13:47 -08:00
IP.isMapped = function isMapped(raw) {
assert(Buffer.isBuffer(raw));
assert(raw.length === 16);
return raw[0] === 0x00
&& raw[1] === 0x00
&& raw[2] === 0x00
&& raw[3] === 0x00
&& raw[4] === 0x00
&& raw[5] === 0x00
&& raw[6] === 0x00
&& raw[7] === 0x00
&& raw[8] === 0x00
&& raw[9] === 0x00
&& raw[10] === 0xff
&& raw[11] === 0xff;
2016-05-25 14:38:35 -07:00
};
/**
2017-01-23 19:13:47 -08:00
* Parse an IP string and return a buffer.
2016-12-19 02:28:56 -08:00
* @param {String} str
2016-05-25 14:38:35 -07:00
* @returns {Buffer}
*/
2016-12-27 14:29:46 -08:00
IP.toBuffer = function toBuffer(str) {
2017-01-23 19:13:47 -08:00
var raw = new Buffer(16);
2017-01-23 22:31:45 -08:00
var data;
2016-12-19 02:28:56 -08:00
assert(typeof str === 'string');
2017-01-23 23:08:21 -08:00
if (IP.isV4String(str)) {
2017-01-23 19:13:47 -08:00
raw.fill(0);
raw[10] = 0xff;
raw[11] = 0xff;
return IP.parseV4(str, raw, 12);
2016-12-19 02:28:56 -08:00
}
2017-01-24 18:25:44 -08:00
if (IP.isOnionString(str)) {
2017-01-23 22:31:45 -08:00
data = TOR_ONION;
data.copy(raw, 0);
data = base32.decode(str.slice(0, -6));
assert(data.length === 10, 'Invalid onion address.');
data.copy(raw, 6);
return raw;
}
2017-01-23 19:13:47 -08:00
return IP.parseV6(str, raw, 0);
2016-12-19 02:28:56 -08:00
};
/**
* Convert an IPv4 string to a buffer.
* @private
* @param {String} str
2017-01-23 19:13:47 -08:00
* @param {Buffer} raw
2016-12-19 02:28:56 -08:00
* @param {Number} offset
* @returns {Buffer}
*/
2016-05-25 14:38:35 -07:00
2017-01-23 19:13:47 -08:00
IP.parseV4 = function parseV4(str, raw, offset) {
2016-12-19 02:28:56 -08:00
var parts = str.split('.');
var i, ch;
2016-05-25 14:38:35 -07:00
2016-12-19 02:28:56 -08:00
assert(parts.length === 4);
2016-05-25 14:38:35 -07:00
2016-12-19 02:28:56 -08:00
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);
2017-01-23 19:13:47 -08:00
raw[offset++] = ch;
2016-05-25 14:38:35 -07:00
}
2017-01-23 19:13:47 -08:00
return raw;
2016-12-19 02:28:56 -08:00
};
/**
* Convert an IPv6 string to a buffer.
* @private
* @param {String} str
2017-01-23 19:13:47 -08:00
* @param {Buffer} raw
2016-12-19 02:28:56 -08:00
* @param {Number} offset
* @returns {Buffer}
*/
2017-01-23 19:13:47 -08:00
IP.parseV6 = function parseV6(str, raw, offset) {
2016-12-19 02:28:56 -08:00
var parts = str.split(':');
var missing = 8 - parts.length;
var start = offset;
2016-12-19 03:17:26 -08:00
var colon = false;
var i, word;
2016-12-19 02:28:56 -08:00
assert(parts.length >= 2, 'Not an IPv6 address.');
for (i = 0; i < parts.length; i++) {
word = parts[i];
2017-01-23 23:08:21 -08:00
if (IP.isV4String(word))
missing--;
}
2016-12-19 03:17:26 -08:00
for (i = 0; i < parts.length; i++) {
2016-12-19 02:28:56 -08:00
word = parts[i];
if (word.length === 0) {
2016-12-19 03:17:26 -08:00
assert(!colon, 'Overuse of double colon in IPv6 address.');
2016-12-19 02:28:56 -08:00
2016-12-19 03:17:26 -08:00
colon = true;
2016-12-19 02:28:56 -08:00
missing += 1;
// Eat extra colons.
// e.g. :::0
2016-12-19 03:17:26 -08:00
while (i + 1 < parts.length) {
2016-12-19 02:28:56 -08:00
word = parts[i + 1];
if (word.length !== 0)
break;
missing += 1;
i++;
}
while (missing > 0) {
2017-01-23 19:13:47 -08:00
raw[offset++] = 0;
raw[offset++] = 0;
2016-12-19 02:28:56 -08:00
missing--;
}
continue;
}
2017-01-23 23:08:21 -08:00
if (IP.isV4String(word)) {
2017-01-23 19:13:47 -08:00
IP.parseV4(word, raw, offset);
2016-12-19 02:28:56 -08:00
offset += 4;
continue;
}
assert(word.length <= 4);
word = parseInt(word, 16);
assert(word === word, 'Non-number in IPv6 address.');
2017-01-23 19:13:47 -08:00
raw[offset++] = (word >> 8) & 0xff;
raw[offset++] = word & 0xff;
2016-12-19 02:28:56 -08:00
}
assert(missing === 0, 'IPv6 address has missing sections.');
assert.equal(offset, start + 16);
2017-01-23 19:13:47 -08:00
return raw;
2016-05-25 14:38:35 -07:00
};
/**
* Convert a buffer to an ip string.
2017-01-23 19:13:47 -08:00
* @param {Buffer} raw
2016-05-25 14:38:35 -07:00
* @returns {String}
*/
2017-01-23 19:13:47 -08:00
IP.toString = function toString(raw) {
2017-01-23 22:31:45 -08:00
var host = '';
2016-12-19 02:28:56 -08:00
var i;
2017-01-23 19:13:47 -08:00
assert(Buffer.isBuffer(raw));
2016-05-25 14:38:35 -07:00
2017-01-23 19:13:47 -08:00
if (raw.length === 4) {
2017-01-23 22:31:45 -08:00
host += raw[0];
host += '.' + raw[1];
host += '.' + raw[2];
host += '.' + raw[3];
return host;
2016-05-25 14:38:35 -07:00
}
2017-01-23 19:13:47 -08:00
if (raw.length === 16) {
if (IP.isMapped(raw)) {
2017-01-23 22:31:45 -08:00
host += raw[12];
host += '.' + raw[13];
host += '.' + raw[14];
host += '.' + raw[15];
return host;
}
2017-01-24 18:25:44 -08:00
if (IP.isOnion(raw)) {
2017-01-23 22:31:45 -08:00
host = base32.encode(raw.slice(6));
return host + '.onion';
2016-12-19 02:28:56 -08:00
}
2017-01-23 22:31:45 -08:00
host += raw.readUInt16BE(0, true).toString(16);
2016-12-19 02:28:56 -08:00
for (i = 2; i < 16; i += 2) {
2017-01-23 22:31:45 -08:00
host += ':';
host += raw.readUInt16BE(i, true).toString(16);
2016-12-19 02:28:56 -08:00
}
2017-01-23 22:31:45 -08:00
host = host.replace(/(^|:)0(:0)*:0(:|$)/, '$1::$3');
host = host.replace(/:{3,4}/, '::');
2016-12-19 02:28:56 -08:00
2017-01-23 22:31:45 -08:00
return host;
2016-12-19 02:28:56 -08:00
}
2017-02-04 01:16:17 -08:00
throw new Error('Invalid IP address: ' + raw.toString('hex'));
2017-01-23 19:13:47 -08:00
};
2016-05-25 14:38:35 -07:00
/**
* Normalize an ip.
2016-12-19 02:28:56 -08:00
* @param {String} str
2016-05-25 14:38:35 -07:00
* @returns {String}
*/
2016-12-19 02:28:56 -08:00
IP.normalize = function normalize(str) {
return IP.toString(IP.toBuffer(str));
2016-05-25 14:38:35 -07:00
};
2016-12-27 14:29:46 -08:00
/**
2017-01-23 19:13:47 -08:00
* Test whether the address is IPv4.
2016-12-27 14:29:46 -08:00
* @returns {Boolean}
*/
2017-01-23 19:13:47 -08:00
IP.isIPv4 = function isIPv4(raw) {
return IP.isMapped(raw);
2016-12-27 14:29:46 -08:00
};
/**
2017-01-23 19:13:47 -08:00
* Test whether the address is IPv6.
2016-12-27 14:29:46 -08:00
* @returns {Boolean}
*/
2017-01-23 19:13:47 -08:00
IP.isIPv6 = function isIPv6(raw) {
2017-01-24 18:25:44 -08:00
return !IP.isMapped(raw) && !IP.isOnion(raw);
2016-12-27 14:29:46 -08:00
};
2017-01-23 23:08:21 -08:00
/**
* Get address type.
* @param {Buffer} raw
* @returns {Number}
*/
IP.getType = function getType(raw) {
if (IP.isIPv4(raw))
return IP.types.IPV4;
if (IP.isIPv6(raw))
return IP.types.IPV6;
2017-01-24 18:25:44 -08:00
if (IP.isOnion(raw))
return IP.types.ONION;
2017-01-23 23:08:21 -08:00
assert(false, 'Unknown type.');
};
2017-03-02 15:25:44 -08:00
/**
* Get addr network. Similar to
* type, but includes teredo.
* @param {Buffer} raw
* @returns {Number}
*/
IP.getNetwork = function getNetwork(raw) {
if (IP.isRFC4380(raw))
return IP.types.TEREDO;
return IP.getType(raw);
};
2016-12-27 14:29:46 -08:00
/**
2017-01-23 19:13:47 -08:00
* Test whether the host is null.
2016-12-27 14:29:46 -08:00
* @returns {Boolean}
*/
2017-01-23 19:13:47 -08:00
IP.isNull = function isNull(raw) {
if (IP.isIPv4(raw)) {
// 0.0.0.0
return raw[12] === 0
&& raw[13] === 0
&& raw[14] === 0
&& raw[15] === 0;
}
// ::
return IP.isEqual(raw, ZERO_IP);
2016-12-27 14:29:46 -08:00
};
/**
2017-01-23 19:13:47 -08:00
* Test whether the host is a broadcast address.
2016-12-27 14:29:46 -08:00
* @returns {Boolean}
*/
2017-01-23 19:13:47 -08:00
IP.isBroadcast = function isBroadcast(raw) {
if (IP.isIPv4(raw)) {
// 255.255.255.255
return raw[12] === 255
&& raw[13] === 255
&& raw[14] === 255
&& raw[15] === 255;
}
return false;
2016-12-27 14:29:46 -08:00
};
2016-12-19 02:28:56 -08:00
/**
2017-01-23 19:13:47 -08:00
* Test whether the ip is RFC 1918.
* @param {Buffer} raw
2016-12-19 02:28:56 -08:00
* @returns {Boolean}
*/
2017-01-23 19:13:47 -08:00
IP.isRFC1918 = function isRFC1918(raw) {
if (!IP.isIPv4(raw))
return false;
if (raw[12] === 10)
return true;
2016-12-19 06:31:48 -08:00
2017-01-23 19:13:47 -08:00
if (raw[12] === 192 && raw[13] === 168)
return true;
if (raw[12] === 172 && (raw[13] >= 16 && raw[13] <= 31))
return true;
return false;
2016-12-19 02:28:56 -08:00
};
/**
2017-01-23 19:13:47 -08:00
* Test whether the ip is RFC 2544.
* @param {Buffer} raw
2016-12-19 02:28:56 -08:00
* @returns {Boolean}
*/
2017-01-23 19:13:47 -08:00
IP.isRFC2544 = function isRFC2544(raw) {
if (!IP.isIPv4(raw))
return false;
if (raw[12] === 198 && (raw[13] === 18 || raw[13] === 19))
return true;
if (raw[12] === 169 && raw[13] === 254)
return true;
return false;
2016-12-19 02:28:56 -08:00
};
/**
2017-01-23 19:13:47 -08:00
* Test whether the ip is RFC 3927.
* @param {Buffer} raw
2016-12-19 02:28:56 -08:00
* @returns {Boolean}
*/
2017-01-23 19:13:47 -08:00
IP.isRFC3927 = function isRFC3927(raw) {
if (!IP.isIPv4(raw))
return false;
if (raw[12] === 169 && raw[13] === 254)
return true;
2016-12-19 06:31:48 -08:00
2017-01-23 19:13:47 -08:00
return false;
2016-12-19 02:28:56 -08:00
};
/**
2017-01-23 19:13:47 -08:00
* Test whether the ip is RFC 6598.
* @param {Buffer} raw
* @returns {Boolean}
2016-12-19 02:28:56 -08:00
*/
2017-01-23 19:13:47 -08:00
IP.isRFC6598 = function isRFC6598(raw) {
if (!IP.isIPv4(raw))
return false;
2016-12-19 02:28:56 -08:00
2017-01-23 19:13:47 -08:00
if (raw[12] === 100
&& (raw[13] >= 64 && raw[13] <= 127)) {
return true;
}
return false;
};
2016-12-19 02:28:56 -08:00
2017-01-23 19:13:47 -08:00
/**
* Test whether the ip is RFC 5737.
* @param {Buffer} raw
* @returns {Boolean}
*/
2016-12-19 02:28:56 -08:00
2017-01-23 19:13:47 -08:00
IP.isRFC5737 = function isRFC5737(raw) {
if (!IP.isIPv4(raw))
return false;
if (raw[12] === 192
&& (raw[13] === 0 && raw[14] === 2)) {
return true;
}
if (raw[12] === 198 && raw[13] === 51 && raw[14] === 100)
return true;
if (raw[12] === 203 && raw[13] === 0 && raw[14] === 113)
return true;
return false;
2016-12-19 02:28:56 -08:00
};
2016-12-16 13:43:23 -08:00
2017-01-23 19:13:47 -08:00
/**
* Test whether the ip is RFC 3849.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isRFC3849 = function isRFC3849(raw) {
if (raw[0] === 0x20 && raw[1] === 0x01
&& raw[2] === 0x0d && raw[3] === 0xb8) {
return true;
}
return false;
};
/**
* Test whether the ip is RFC 3964.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isRFC3964 = function isRFC3964(raw) {
if (raw[0] === 0x20 && raw[1] === 0x02)
return true;
return false;
};
/**
* Test whether the ip is RFC 6052.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isRFC6052 = function isRFC6052(raw) {
return IP.hasPrefix(raw, RFC6052);
};
/**
* Test whether the ip is RFC 4380.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isRFC4380 = function isRFC4380(raw) {
if (raw[0] === 0x20 && raw[1] === 0x01
&& raw[2] === 0x00 && raw[3] === 0x00) {
return true;
}
return false;
};
/**
* Test whether the ip is RFC 4862.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isRFC4862 = function isRFC4862(raw) {
return IP.hasPrefix(raw, RFC4862);
};
/**
* Test whether the ip is RFC 4193.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isRFC4193 = function isRFC4193(raw) {
if ((raw[0] & 0xfe) === 0xfc)
return true;
return false;
};
/**
* Test whether the ip is RFC 6145.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isRFC6145 = function isRFC6145(raw) {
return IP.hasPrefix(raw, RFC6145);
};
/**
* Test whether the ip is RFC 4843.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isRFC4843 = function isRFC4843(raw) {
if (raw[0] === 0x20 && raw[1] === 0x01
&& raw[2] === 0x00 && (raw[3] & 0xf0) === 0x10) {
return true;
}
return false;
};
/**
* Test whether the ip has a tor onion prefix.
* @param {Buffer} raw
* @returns {Boolean}
*/
2017-01-24 18:25:44 -08:00
IP.isOnion = function isOnion(raw) {
2017-01-23 19:13:47 -08:00
return IP.hasPrefix(raw, TOR_ONION);
};
/**
* Test whether the ip is local.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isLocal = function isLocal(raw) {
if (IP.isIPv4(raw)) {
if (raw[12] === 127 && raw[13] === 0)
return true;
return false;
}
if (IP.isEqual(raw, LOCAL_IP))
return true;
return false;
};
/**
* Test whether the ip is a multicast address.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isMulticast = function isMulticast(raw) {
if (IP.isIPv4(raw)) {
if ((raw[12] & 0xf0) === 0xe0)
return true;
return false;
}
return raw[0] === 0xff;
};
/**
* Test whether the ip is valid.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isValid = function isValid(raw) {
if (IP.hasPrefix(raw, SHIFTED))
return false;
if (IP.isNull(raw))
return false;
if (IP.isBroadcast(raw))
return false;
if (IP.isRFC3849(raw))
return false;
return true;
};
/**
* Test whether the ip is routable.
* @param {Buffer} raw
* @returns {Boolean}
*/
IP.isRoutable = function isRoutable(raw) {
if (!IP.isValid(raw))
return false;
if (IP.isRFC1918(raw))
return false;
if (IP.isRFC2544(raw))
return false;
if (IP.isRFC3927(raw))
return false;
if (IP.isRFC4862(raw))
return false;
if (IP.isRFC6598(raw))
return false;
if (IP.isRFC5737(raw))
return false;
2017-01-24 18:25:44 -08:00
if (IP.isRFC4193(raw) && !IP.isOnion(raw))
2017-01-23 19:13:47 -08:00
return false;
if (IP.isRFC4843(raw))
return false;
if (IP.isLocal(raw))
return false;
return true;
};
2017-03-02 15:25:44 -08:00
/**
* Calculate reachable score from source to destination.
* @param {Buffer} src
* @param {Buffer} dest
* @returns {Number} Ranges from 0-6.
*/
IP.getReachability = function getReachability(src, dest) {
var srcNet = IP.getNetwork(src);
var destNet = IP.getNetwork(dest);
var types = IP.types;
var UNREACHABLE = 0;
var DEFAULT = 1;
var TEREDO = 2;
var IPV6_WEAK = 3;
var IPV4 = 4;
var IPV6_STRONG = 5;
var PRIVATE = 6;
if (!IP.isRoutable(src))
return UNREACHABLE;
switch (destNet) {
case types.IPV4:
switch (srcNet) {
case types.IPV4:
return IPV4;
default:
return DEFAULT;
}
break;
case types.IPV6:
switch (srcNet) {
case types.TEREDO:
return TEREDO;
case types.IPV4:
return IPV4;
case types.IPV6:
if (IP.isRFC3964(src)
|| IP.isRFC6052(src)
|| IP.isRFC6145(src)) {
// tunnel
return IPV6_WEAK;
}
return IPV6_STRONG;
default:
return DEFAULT;
}
break;
case types.ONION:
switch (srcNet) {
case types.IPV4:
return IPV4;
case types.ONION:
return PRIVATE;
default:
return DEFAULT;
}
break;
case types.TEREDO:
switch (srcNet) {
case types.TEREDO:
return TEREDO;
case types.IPV6:
return IPV6_WEAK;
case types.IPV4:
return IPV4;
default:
return DEFAULT;
}
break;
default:
switch (srcNet) {
case types.TEREDO:
return TEREDO;
case types.IPV6:
return IPV6_WEAK;
case types.IPV4:
return IPV4;
case types.ONION:
return PRIVATE;
default:
return DEFAULT;
}
break;
}
};
2017-01-23 19:13:47 -08:00
/**
* Test whether an IP has a prefix.
* @param {Buffer} raw
* @param {Buffer} prefix
* @returns {Boolean}
*/
IP.hasPrefix = function hasPrefix(raw, prefix) {
var i;
assert(Buffer.isBuffer(raw));
assert(Buffer.isBuffer(prefix));
assert(raw.length >= prefix.length);
for (i = 0; i < prefix.length; i++) {
if (raw[i] !== prefix[i])
return false;
}
return true;
};
/**
* Test whether two IPs are equal.
* @param {Buffer} a
* @param {Buffer} b
* @returns {Boolean}
*/
IP.isEqual = function isEqual(a, b) {
var i;
assert(a.length === 16);
assert(b.length === 16);
if (a.compare)
return a.compare(b) === 0;
for (i = 0; i < a.length; i++) {
if (a[i] !== b[i])
return false;
}
return true;
};
/**
* Get IP address from network interfaces.
* @param {String?} name - `public` or `private`.
* @param {String?} family - IP family name.
* @returns {String}
*/
IP.getInterfaces = function _getInterfaces(name, family) {
var interfaces = getInterfaces();
var keys = Object.keys(interfaces);
var result = [];
var i, j, key, items, details, type, raw;
for (i = 0; i < keys.length; i++) {
key = keys[i];
items = interfaces[key];
for (j = 0; j < items.length; j++) {
details = items[j];
type = details.family.toLowerCase();
if (family && type !== family)
continue;
if (details.internal)
continue;
try {
raw = IP.toBuffer(details.address);
} catch (e) {
continue;
}
if (IP.isNull(raw))
continue;
if (IP.isLocal(raw))
continue;
if (name === 'public') {
if (!IP.isRoutable(raw))
continue;
} else if (name === 'private') {
if (IP.isRoutable(raw))
continue;
}
result.push(IP.toString(raw));
}
}
return result;
};
/**
* Get private IP from network interfaces.
* @param {String?} family - IP family name.
* @returns {String}
*/
IP.getPrivate = function getPrivate(family) {
return IP.getInterfaces('private', family);
};
/**
* Get public IP from network interfaces.
* @param {String?} family - IP family name.
* @returns {String}
*/
IP.getPublic = function getPublic(family) {
return IP.getInterfaces('public', family);
};
2017-01-23 19:13:47 -08:00
/**
* Represents a parsed address.
2017-02-03 22:47:26 -08:00
* @constructor
* @alias module:utils/ip.Address
2017-01-23 19:13:47 -08:00
* @param {String} host
* @param {Number} port
2017-01-23 22:31:45 -08:00
* @param {Number} type
2017-01-23 19:13:47 -08:00
* @param {String} hostname
2017-02-03 22:47:26 -08:00
* @param {Buffer|null} raw
* @property {String} host
* @property {Number} port
* @property {Number} type
* @property {String} hostname
* @property {Buffer} raw
2016-12-16 13:43:23 -08:00
*/
2017-01-23 22:31:45 -08:00
function Address(host, port, type, hostname, raw) {
2017-02-03 22:47:26 -08:00
this.host = host || '0.0.0.0';
this.port = port || 0;
this.type = type || IP.types.IPV4;
this.hostname = hostname || '0.0.0.0:0';
2017-01-23 19:13:47 -08:00
this.raw = raw || ZERO_IP;
2016-12-16 13:43:23 -08:00
}
2017-02-03 22:47:26 -08:00
/*
* Helpers
*/
function getInterfaces() {
if (os.unsupported)
return {};
return os.networkInterfaces();
}
2017-02-03 22:47:26 -08:00
/*
* Expose
*/
IP.Address = Address;