2015-02-12 11:44:15 +01:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>JSDoc: Source: net/base.js</title>
|
|
|
|
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
|
|
<!--[if lt IE 9]>
|
|
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
|
|
<![endif]-->
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div id="main">
|
|
|
|
|
|
|
|
<h1 class="page-title">Source: net/base.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<article>
|
|
|
|
<pre class="prettyprint source linenums"><code>/**
|
|
|
|
* 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:54:39 +01:00
|
|
|
* @example var BaseTransport = require("bitmessage/lib/net/base");
|
2015-02-14 12:00:58 +01:00
|
|
|
* @module bitmessage/net/base
|
2015-02-12 11:44:15 +01:00
|
|
|
*/
|
|
|
|
// TODO(Kagami): Write some sort of tutorial.
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var inherits = require("inherits");
|
|
|
|
var EventEmitter = require("events").EventEmitter;
|
2015-02-24 18:23:55 +01:00
|
|
|
var bufferEqual = require("buffer-equal");
|
|
|
|
var util = require("../_util");
|
2015-02-12 11:44:15 +01:00
|
|
|
var PPromise = require("../platform").Promise;
|
|
|
|
var structs = require("../structs");
|
2015-02-24 18:23:55 +01:00
|
|
|
var messages = require("../messages");
|
|
|
|
|
|
|
|
var ServicesBitfield = structs.ServicesBitfield;
|
2015-02-12 11:44:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base transport class. Allows to use single class for both client and
|
|
|
|
* server modes (as separate instances).
|
|
|
|
* @constructor
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function BaseTransport() {
|
|
|
|
BaseTransport.super_.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(BaseTransport, EventEmitter);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do the transport-specific bootstrap process and return promise that
|
|
|
|
* 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>}
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
BaseTransport.prototype.bootstrap = function() {
|
|
|
|
return PPromise.reject(new Error("Not implemented"));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to the transport-specific address. Enters client mode. Should
|
|
|
|
* emit `open` event after successful connect and `established` event
|
|
|
|
* after `verack` messages exchange.
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
BaseTransport.prototype.connect = function() {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for the transport-specific incoming connections. Enters server
|
|
|
|
* mode. Should emit `connection` event with a transport instance for
|
|
|
|
* each new connection.
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
BaseTransport.prototype.listen = function() {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send [message]{@link module:bitmessage/structs.message} over the
|
|
|
|
* wire (client mode only).
|
|
|
|
* @param {(Buffer|string)} msg - Encoded message or command string
|
|
|
|
* @param {Buffer=} payload - Message payload (used if the first
|
|
|
|
* argument is a string)
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
BaseTransport.prototype.send = function() {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send [message]{@link module:bitmessage/structs.message} to all
|
|
|
|
* connected clients (server mode only).
|
|
|
|
* @param {(Buffer|string)} msg - Encoded message or command string
|
|
|
|
* @param {Buffer=} payload - Message payload (used if the first
|
|
|
|
* argument is a string)
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
BaseTransport.prototype.broadcast = function() {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close connection(s) and/or stop listening (both modes).
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
BaseTransport.prototype.close = function() {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
};
|
|
|
|
|
2015-02-24 18:23:55 +01:00
|
|
|
// Private helpers.
|
2015-02-12 11:44:15 +01:00
|
|
|
|
2015-02-24 18:23:55 +01:00
|
|
|
// Make a message from variable number of arguments.
|
2015-02-12 11:44:15 +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-24 18:23:55 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode and validate version message.
|
|
|
|
BaseTransport.prototype._decodeVersion = function(payload, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
if (bufferEqual(version.nonce, messages.version.randomNonce)) {
|
|
|
|
throw new Error("Connection to self");
|
|
|
|
}
|
|
|
|
if (!intersects(this.streams, version.streams)) {
|
|
|
|
throw new Error(
|
|
|
|
"Peer isn't interested in our streams; " +
|
|
|
|
"first 10 peer's streams: " + version.streams.slice(0, 10)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
};
|
|
|
|
|
2015-02-12 11:44:15 +01:00
|
|
|
module.exports = BaseTransport;
|
|
|
|
</code></pre>
|
|
|
|
</article>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<nav>
|
2015-02-14 12:00:58 +01:00
|
|
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bitmessage.html">bitmessage</a></li><li><a href="module-bitmessage_address.html">bitmessage/address</a></li><li><a href="module-bitmessage_crypto.html">bitmessage/crypto</a></li><li><a href="module-bitmessage_messages.html">bitmessage/messages</a></li><li><a href="module-bitmessage_net_base.html">bitmessage/net/base</a></li><li><a href="module-bitmessage_net_tcp.html">bitmessage/net/tcp</a></li><li><a href="module-bitmessage_net_ws.html">bitmessage/net/ws</a></li><li><a href="module-bitmessage_objects.html">bitmessage/objects</a></li><li><a href="module-bitmessage_pow.html">bitmessage/pow</a></li><li><a href="module-bitmessage_structs.html">bitmessage/structs</a></li><li><a href="module-bitmessage_user-agent.html">bitmessage/user-agent</a></li><li><a href="module-bitmessage_wif.html">bitmessage/wif</a></li></ul><h3>Classes</h3><ul><li><a href="module-bitmessage_address.Address.html">Address</a></li><li><a href="module-bitmessage_net_base.BaseTransport.html">BaseTransport</a></li><li><a href="module-bitmessage_net_tcp.TcpTransport.html">TcpTransport</a></li><li><a href="module-bitmessage_net_ws.WsTransport.html">WsTransport</a></li><li><a href="module-bitmessage_structs.PubkeyBitfield.html">PubkeyBitfield</a></li><li><a href="module-bitmessage_structs.ServicesBitfield.html">ServicesBitfield</a></li></ul><h3>Namespaces</h3><ul><li><a href="module-bitmessage_messages.addr.html">addr</a></li><li><a href="module-bitmessage_messages.error.html">error</a></li><li><a href="module-bitmessage_messages.getdata.html">getdata</a></li><li><a href="module-bitmessage_messages.inv.html">inv</a></li><li><a href="module-bitmessage_messages.version.html">version</a></li><li><a href="module-bitmessage_objects.broadcast.html">broadcast</a></li><li><a href="module-bitmessage_objects.getpubkey.html">getpubkey</a></li><li><a href="module-bitmessage_objects.msg.html">msg</a></li><li><a href="module-bitmessage_objects.pubkey.html">pubkey</a></li><li><a href="module-bitmessage_structs.encrypted.html">encrypted</a></li><li><a href="module-bitmessage_structs.inv_vect.html">inv_vect</a></li><li><a href="module-bitmessage_structs.message.html">message</a></li><li><a href="module-bitmessage_structs.net_addr.html">net_addr</a></li><li><a href="module-bitmessage_structs.object.html">object</a></li><li><a href="module-bitmessage_structs.var_int.html">var_int</a></li><li><a href="module-bitmessage_structs.var_int_list.html">var_int_list</a></li><li><a href="module-bitmessage_structs.var_str.html">var_str</a></li></ul>
|
2015-02-12 11:44:15 +01:00
|
|
|
</nav>
|
|
|
|
|
|
|
|
<br class="clear">
|
|
|
|
|
|
|
|
<footer>
|
2015-02-24 18:23:55 +01:00
|
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0-dev</a> on Tue Feb 24 2015 20:23:33 GMT+0300 (MSK)
|
2015-02-12 11:44:15 +01:00
|
|
|
</footer>
|
|
|
|
|
|
|
|
<script> prettyPrint(); </script>
|
|
|
|
<script src="scripts/linenumber.js"> </script>
|
|
|
|
</body>
|
|
|
|
</html>
|