From b8324e0b09949f776c40413b3f59683a0e75ac5e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 23 Jan 2017 23:08:21 -0800 Subject: [PATCH] ip: rename some functions. --- lib/http/rpc.js | 4 +-- lib/net/bip150.js | 4 +-- lib/net/hostlist.js | 4 +-- lib/primitives/netaddress.js | 2 +- lib/utils/ip.js | 49 +++++++++++++++++++++++++----------- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 26e4f1d2..8767670c 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -429,7 +429,7 @@ RPC.prototype.disconnectnode = co(function* disconnectnode(args) { throw new RPCError('disconnectnode "node"'); addr = toString(args[0]); - addr = IP.parseHost(addr, this.network.port); + addr = IP.fromHostname(addr, this.network.port); peer = this.pool.peers.get(addr.hostname); @@ -448,7 +448,7 @@ RPC.prototype.getaddednodeinfo = co(function* getaddednodeinfo(args) { if (args.length === 2) { addr = toString(args[1]); - addr = IP.parseHost(addr, this.network.port); + addr = IP.fromHostname(addr, this.network.port); peer = this.pool.peers.get(addr.hostname); if (!peer) throw new RPCError('Node has not been added.'); diff --git a/lib/net/bip150.js b/lib/net/bip150.js index 61cfa984..206ada53 100644 --- a/lib/net/bip150.js +++ b/lib/net/bip150.js @@ -527,7 +527,7 @@ AuthDB.prototype.addKnown = function addKnown(host, key) { assert(Buffer.isBuffer(key) && key.length === 33, 'Invalid public key for known peer.'); - addr = IP.parseHost(host); + addr = IP.fromHostname(host); if (addr.type === IP.types.DNS) { // Defer this for resolution. @@ -596,7 +596,7 @@ AuthDB.prototype.getKnown = function getKnown(hostname) { if (known) return known; - addr = IP.parseHost(hostname); + addr = IP.fromHostname(hostname); return this.known[addr.host]; }; diff --git a/lib/net/hostlist.js b/lib/net/hostlist.js index 0f734e50..2894b862 100644 --- a/lib/net/hostlist.js +++ b/lib/net/hostlist.js @@ -662,7 +662,7 @@ HostList.prototype.toArray = function toArray() { */ HostList.prototype.addSeed = function addSeed(host) { - var addr = IP.parseHost(host, this.network.port); + var addr = IP.fromHostname(host, this.network.port); if (addr.type === IP.types.DNS) { // Defer for resolution. @@ -683,7 +683,7 @@ HostList.prototype.addSeed = function addSeed(host) { */ HostList.prototype.addNode = function addNode(host) { - var addr = IP.parseHost(host, this.network.port); + var addr = IP.fromHostname(host, this.network.port); if (addr.type === IP.types.DNS) { // Defer for resolution. diff --git a/lib/primitives/netaddress.js b/lib/primitives/netaddress.js index f927d960..fb2e0bf4 100644 --- a/lib/primitives/netaddress.js +++ b/lib/primitives/netaddress.js @@ -246,7 +246,7 @@ NetAddress.prototype.fromHostname = function fromHostname(hostname, network) { network = Network.get(network); - addr = IP.parseHost(hostname, network.port); + addr = IP.fromHostname(hostname, network.port); return this.fromHost(addr.host, addr.port, network); }; diff --git a/lib/utils/ip.js b/lib/utils/ip.js index 669c498c..17695caa 100644 --- a/lib/utils/ip.js +++ b/lib/utils/ip.js @@ -57,7 +57,7 @@ IP.types = { * @returns {Object} Contains `host`, `port`, and `type`. */ -IP.parseHost = function parseHost(addr, fallback) { +IP.fromHostname = function fromHostname(addr, fallback) { var parts, host, port, type, hostname, raw; assert(typeof addr === 'string'); @@ -99,7 +99,7 @@ IP.parseHost = function parseHost(addr, fallback) { default: // Case: // ::1 - assert(IP.isV6Format(addr), 'Bad IPv6 address.'); + assert(IP.isV6String(addr), 'Bad IPv6 address.'); host = addr; port = null; break; @@ -117,7 +117,7 @@ IP.parseHost = function parseHost(addr, fallback) { port = fallback || 0; } - type = IP.getType(host); + type = IP.getStringType(host); if (type !== IP.types.DNS) { raw = IP.toBuffer(host); @@ -151,7 +151,7 @@ IP.toHostname = function toHostname(host, port) { assert(!/[\[\]]/.test(host), 'Bad host.'); - type = IP.getType(host); + type = IP.getStringType(host); if (host.indexOf(':') !== -1) assert(type === IP.types.IPV6, 'Bad host.'); @@ -171,14 +171,14 @@ IP.toHostname = function toHostname(host, port) { * @returns {Number} */ -IP.getType = function getType(str) { - if (IP.isV4Format(str)) +IP.getStringType = function getStringType(str) { + if (IP.isV4String(str)) return IP.types.IPV4; - if (IP.isV6Format(str)) + if (IP.isV6String(str)) return IP.types.IPV6; - if (IP.isTorFormat(str)) + if (IP.isTorString(str)) return IP.types.TOR; return IP.types.DNS; @@ -190,7 +190,7 @@ IP.getType = function getType(str) { * @returns {Boolean} */ -IP.isV4Format = function isV4Format(str) { +IP.isV4String = function isV4String(str) { assert(typeof str === 'string'); if (str.length < 7) @@ -208,7 +208,7 @@ IP.isV4Format = function isV4Format(str) { * @returns {Boolean} */ -IP.isV6Format = function isV6Format(str) { +IP.isV6String = function isV6String(str) { assert(typeof str === 'string'); if (str.length < 2) @@ -226,7 +226,7 @@ IP.isV6Format = function isV6Format(str) { * @returns {Boolean} */ -IP.isTorFormat = function isTorFormat(str) { +IP.isTorString = function isTorString(str) { assert(typeof str === 'string'); if (str.length < 7) @@ -271,14 +271,14 @@ IP.toBuffer = function toBuffer(str) { assert(typeof str === 'string'); - if (IP.isV4Format(str)) { + if (IP.isV4String(str)) { raw.fill(0); raw[10] = 0xff; raw[11] = 0xff; return IP.parseV4(str, raw, 12); } - if (IP.isTorFormat(str)) { + if (IP.isTorString(str)) { data = TOR_ONION; data.copy(raw, 0); data = base32.decode(str.slice(0, -6)); @@ -337,7 +337,7 @@ IP.parseV6 = function parseV6(str, raw, offset) { for (i = 0; i < parts.length; i++) { word = parts[i]; - if (IP.isV4Format(word)) + if (IP.isV4String(word)) missing--; } @@ -369,7 +369,7 @@ IP.parseV6 = function parseV6(str, raw, offset) { continue; } - if (IP.isV4Format(word)) { + if (IP.isV4String(word)) { IP.parseV4(word, raw, offset); offset += 4; continue; @@ -469,6 +469,25 @@ IP.isIPv6 = function isIPv6(raw) { return !IP.isMapped(raw) && !IP.isTor(raw); }; +/** + * 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; + + if (IP.isTor(raw)) + return IP.types.TOR; + + assert(false, 'Unknown type.'); +}; + /** * Test whether the host is null. * @returns {Boolean}