2015-02-01 21:34:47 +01:00
|
|
|
/**
|
2015-02-10 13:57:55 +01:00
|
|
|
* Networking base module. Defines base transport interface, useful for
|
|
|
|
* implementing new transports. End-users should import some transport
|
|
|
|
* instead in order to connect/accept connections to/from other nodes.
|
|
|
|
* **NOTE**: `BaseTransport` is exported as a module.
|
2015-02-14 11:53:24 +01:00
|
|
|
* @example var BaseTransport = require("bitmessage/lib/net/base");
|
2015-02-14 12:00:27 +01:00
|
|
|
* @module bitmessage/net/base
|
2015-02-01 21:34:47 +01:00
|
|
|
*/
|
|
|
|
// TODO(Kagami): Write some sort of tutorial.
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var inherits = require("inherits");
|
|
|
|
var EventEmitter = require("events").EventEmitter;
|
2015-02-24 17:38:03 +01:00
|
|
|
var bufferEqual = require("buffer-equal");
|
2015-02-23 21:45:32 +01:00
|
|
|
var util = require("../_util");
|
2015-02-02 16:30:05 +01:00
|
|
|
var PPromise = require("../platform").Promise;
|
2015-02-09 22:56:55 +01:00
|
|
|
var structs = require("../structs");
|
2015-02-23 21:45:32 +01:00
|
|
|
var messages = require("../messages");
|
2015-02-01 21:34:47 +01:00
|
|
|
|
2015-02-24 09:47:56 +01:00
|
|
|
var ServicesBitfield = structs.ServicesBitfield;
|
|
|
|
|
2015-02-01 21:34:47 +01:00
|
|
|
/**
|
2015-02-10 13:57:55 +01:00
|
|
|
* Base transport class. Allows to use single class for both client and
|
|
|
|
* server modes (as separate instances).
|
2015-02-01 21:34:47 +01:00
|
|
|
* @constructor
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function BaseTransport() {
|
|
|
|
BaseTransport.super_.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(BaseTransport, EventEmitter);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do the transport-specific bootstrap process and return promise that
|
2015-02-10 13:57:55 +01:00
|
|
|
* contains discovered nodes when fulfilled (both modes).
|
|
|
|
* NOTE: Do not use nodes received by this method in `addr` messages!
|
|
|
|
* This is meaningless.
|
|
|
|
* @return {Promise.<Array>}
|
2015-02-05 19:38:36 +01:00
|
|
|
* @abstract
|
2015-02-01 21:34:47 +01:00
|
|
|
*/
|
|
|
|
BaseTransport.prototype.bootstrap = function() {
|
2015-02-05 19:38:36 +01:00
|
|
|
return PPromise.reject(new Error("Not implemented"));
|
2015-02-01 21:34:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-02-10 13:57:55 +01:00
|
|
|
* Connect to the transport-specific address. Enters client mode. Should
|
|
|
|
* emit `open` event after successful connect and `established` event
|
|
|
|
* after `verack` messages exchange.
|
2015-02-01 21:34:47 +01:00
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
BaseTransport.prototype.connect = function() {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
2015-02-05 19:38:36 +01:00
|
|
|
/**
|
2015-02-10 13:57:55 +01:00
|
|
|
* Listen for the transport-specific incoming connections. Enters server
|
|
|
|
* mode. Should emit `connection` event with a transport instance for
|
|
|
|
* each new connection.
|
2015-02-05 19:38:36 +01:00
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
BaseTransport.prototype.listen = function() {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
2015-02-01 21:34:47 +01:00
|
|
|
/**
|
|
|
|
* Send [message]{@link module:bitmessage/structs.message} over the
|
2015-02-10 13:57:55 +01:00
|
|
|
* wire (client mode only).
|
2015-02-09 15:38:10 +01:00
|
|
|
* @param {(Buffer|string)} msg - Encoded message or command string
|
2015-02-10 19:23:20 +01:00
|
|
|
* @param {Buffer=} payload - Message payload (used if the first
|
2015-02-09 15:38:10 +01:00
|
|
|
* argument is a string)
|
2015-02-01 21:34:47 +01:00
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
BaseTransport.prototype.send = function() {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-02-05 19:38:36 +01:00
|
|
|
* Send [message]{@link module:bitmessage/structs.message} to all
|
2015-02-10 13:57:55 +01:00
|
|
|
* connected clients (server mode only).
|
2015-02-09 15:38:10 +01:00
|
|
|
* @param {(Buffer|string)} msg - Encoded message or command string
|
2015-02-10 19:23:20 +01:00
|
|
|
* @param {Buffer=} payload - Message payload (used if the first
|
2015-02-09 15:38:10 +01:00
|
|
|
* argument is a string)
|
2015-02-01 21:34:47 +01:00
|
|
|
* @abstract
|
|
|
|
*/
|
2015-02-05 19:38:36 +01:00
|
|
|
BaseTransport.prototype.broadcast = function() {
|
2015-02-01 21:34:47 +01:00
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-02-10 13:57:55 +01:00
|
|
|
* Close connection(s) and/or stop listening (both modes).
|
2015-02-01 21:34:47 +01:00
|
|
|
* @abstract
|
|
|
|
*/
|
2015-02-05 19:38:36 +01:00
|
|
|
BaseTransport.prototype.close = function() {
|
2015-02-01 21:34:47 +01:00
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
2015-02-23 21:45:32 +01:00
|
|
|
// Private helpers.
|
2015-02-09 22:56:55 +01:00
|
|
|
|
2015-02-23 21:45:32 +01:00
|
|
|
// Make a message from variable number of arguments.
|
2015-02-09 22:56:55 +01:00
|
|
|
BaseTransport._getmsg = function(args) {
|
|
|
|
if (typeof args[0] === "string") {
|
|
|
|
return structs.message.encode(args[0], args[1]);
|
|
|
|
} else {
|
|
|
|
return args[0];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Unmap IPv4-mapped IPv6 address.
|
|
|
|
BaseTransport._unmap = function(addr) {
|
|
|
|
if (addr.slice(0, 7) === "::ffff:") {
|
|
|
|
return addr.slice(7);
|
|
|
|
} else {
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-23 21:45:32 +01:00
|
|
|
// Check whether two given arrays intersect.
|
|
|
|
// NOTE(Kagami): It has O(n*m) complexity in the worst case but:
|
|
|
|
// * Max length of stream list = 160,000
|
|
|
|
// * One of the arrays (our streams) should have reasonable length
|
|
|
|
function intersects(a, b) {
|
|
|
|
var alen = a.length;
|
|
|
|
var blen = b.length;
|
|
|
|
if (!alen || !blen) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var i, j;
|
|
|
|
for (i = 0; i < alen; ++i) {
|
|
|
|
for (j = 0; j < blen; ++j) {
|
|
|
|
if (a[i] === b[j]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 09:47:56 +01:00
|
|
|
return false;
|
2015-02-23 21:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode and validate version message.
|
2015-02-24 09:47:56 +01:00
|
|
|
BaseTransport.prototype._decodeVersion = function(payload, opts) {
|
|
|
|
opts = opts || {};
|
2015-02-23 21:45:32 +01:00
|
|
|
var version;
|
|
|
|
try {
|
|
|
|
version = messages.version.decodePayload(payload);
|
|
|
|
} catch(err) {
|
|
|
|
throw new Error("Version decode error: " + err.message);
|
|
|
|
}
|
|
|
|
if (version.version < util.PROTOCOL_VERSION) {
|
|
|
|
throw new Error("Peer uses old protocol v" + version.version);
|
|
|
|
}
|
|
|
|
// TODO(Kagami): We may want to send error message describing the time
|
|
|
|
// offset problem to this node as PyBitmessage.
|
|
|
|
var delta = (version.time.getTime() - new Date().getTime()) / 1000;
|
|
|
|
if (delta > 3600) {
|
|
|
|
throw new Error("Peer's time is too far in the future: +" + delta + "s");
|
|
|
|
}
|
|
|
|
if (delta < -3600) {
|
|
|
|
throw new Error("Peer's time is too far in the past: " + delta + "s");
|
|
|
|
}
|
2015-02-24 17:38:03 +01:00
|
|
|
if (bufferEqual(version.nonce, messages.version.randomNonce)) {
|
|
|
|
throw new Error("Connection to self");
|
|
|
|
}
|
2015-02-24 16:42:57 +01:00
|
|
|
if (!intersects(this.streams, version.streams)) {
|
2015-02-23 21:45:32 +01:00
|
|
|
throw new Error(
|
|
|
|
"Peer isn't interested in our streams; " +
|
2015-02-24 16:42:57 +01:00
|
|
|
"first 10 peer's streams: " + version.streams.slice(0, 10)
|
2015-02-23 21:45:32 +01:00
|
|
|
);
|
|
|
|
}
|
2015-02-24 09:47:56 +01:00
|
|
|
if (opts.network && !version.services.get(ServicesBitfield.NODE_NETWORK)) {
|
|
|
|
throw new Error("Not a normal network node: " + version.services);
|
|
|
|
}
|
|
|
|
if (opts.gateway && !version.services.get(ServicesBitfield.NODE_GATEWAY)) {
|
|
|
|
throw new Error("Not a gateway node: " + version.services);
|
|
|
|
}
|
|
|
|
if (opts.mobile && !version.services.get(ServicesBitfield.NODE_MOBILE)) {
|
|
|
|
throw new Error("Not a mobile node: " + version.services);
|
|
|
|
}
|
2015-02-23 21:45:32 +01:00
|
|
|
return version;
|
|
|
|
};
|
|
|
|
|
2015-02-06 10:47:40 +01:00
|
|
|
module.exports = BaseTransport;
|