Skip to content

Commit

Permalink
way more work on the new pathfinder
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb James DeLisle committed Jan 6, 2014
1 parent e198d22 commit 4ed8871
Show file tree
Hide file tree
Showing 19 changed files with 570 additions and 215 deletions.
5 changes: 4 additions & 1 deletion contrib/c/makekeys.c
Expand Up @@ -15,6 +15,7 @@
#include "crypto/random/Random.h"
#include "memory/MallocAllocator.h"
#include "crypto/AddressCalc.h"
#include "util/AddrTools.h"
#include "util/Hex.h"

#include "crypto_scalarmult_curve25519.h"
Expand All @@ -30,13 +31,15 @@ int main(int argc, char** argv)
uint8_t publicKey[32];
uint8_t ip[16];
uint8_t hexPrivateKey[65];
uint8_t printedIp[40];

for (;;) {
Random_bytes(rand, privateKey, 32);
crypto_scalarmult_curve25519_base(publicKey, privateKey);
if (AddressCalc_addressForPublicKey(ip, publicKey)) {
Hex_encode(hexPrivateKey, 65, privateKey, 32);
printf("%s\n", hexPrivateKey);
AddrTools_printIp(printedIp, ip);
printf("%s %s\n", hexPrivateKey, printedIp);
}
}
return 0;
Expand Down
11 changes: 7 additions & 4 deletions contrib/nodejs/makesim.js
Expand Up @@ -16,9 +16,12 @@ var Fs = require('fs');

var makeNodes = function (keys) {
var out = {};
var ips = [];
for (var i = 0; i < keys.length; i++) {
out['node'+i] = {
privateKey: keys[i],
var kip = keys[i].split(' ');
ips.push(kip[1]);
out[kip[1]] = {
privateKey: kip[0],
peers: []
};
}
Expand All @@ -36,7 +39,7 @@ var makeNodes = function (keys) {
unlinked = x;

}
out['node'+unlinked].peers.push('node'+linked);
out[ips[unlinked]].peers.push(ips[linked]);
}
console.log(JSON.stringify({nodes: out}, null, ' '));
};
Expand All @@ -51,7 +54,7 @@ Fs.readFile(process.argv[process.argv.length-1], function (err, ret) {
if (err) { throw err; }
var keys = ret.toString('utf8').split('\n');
for (var i = keys.length-1; i >= 0; i--) {
if (!(/^[a-f0-9]{64}$/.test(keys[i]))) {
if (!(/^[a-f0-9]{64} [a-f0-9:]{39}$/.test(keys[i]))) {
keys.splice(i, 1);
}
}
Expand Down
74 changes: 28 additions & 46 deletions contrib/nodejs/tools/getLinks.js
Expand Up @@ -17,58 +17,29 @@ var Cjdns = require('../cjdnsadmin/cjdnsadmin');
var nThen = require('../cjdnsadmin/nthen');
var PublicToIp6 = require('./lib/publicToIp6');

var NodeStore_getNode = function(cjdns, state, addr, callback) {
if (typeof(state['NodeStore_getNode' + addr]) !== 'undefined') {
callback(state['NodeStore_getNode' + addr]);
var NodeStore_nodeForAddr = function(cjdns, state, addr, callback) {
if (typeof(state['NodeStore_nodeForAddr' + addr]) !== 'undefined') {
callback(state['NodeStore_nodeForAddr' + addr]);
} else {
//console.log('NodeStore_getNode(' + addr + ');');
cjdns.NodeStore_getNode(addr, function (err, ret) {
//console.log('NodeStore_nodeForAddr(' + addr + ');');
cjdns.NodeStore_nodeForAddr(addr, function (err, ret) {
if (err) { throw err; }
state['NodeStore_getNode' + addr] = ret;
callback(ret);
});
}
};

var NodeStore_getRouteLabel = function(cjdns, state, parentPath, childAddr, callback) {
var key = 'NodeStore_getRouteLabel' + parentPath + ', ' + childAddr;
if (typeof(state[key]) !== 'undefined') {
callback(state[key]);
} else {
//console.log('NodeStore_getRouteLabel(' + parentPath + ', ' + childAddr + ');');
cjdns.NodeStore_getRouteLabel(parentPath, childAddr, function (err, ret) {
if (err) { throw err; }
state[key] = ret;
callback(ret);
});
}
};

var NodeStore_getLink = function (cjdns, state, addr, num, callback) {
var key = 'NodeStore_getLink' + addr + ', ' + num;
if (typeof(state[key]) !== 'undefined') {
callback(state[key]);
} else {
//console.log('NodeStore_getLink(' + addr + ', ' + num + ');');
cjdns.NodeStore_getLink(addr, num, function (err, ret) {
if (err) { throw err; }
state[key] = ret;
state['NodeStore_nodeForAddr' + addr] = ret;
callback(ret);
});
}
};

var getNode = function (cjdns, next, output, state, parentPath, ipsByReach, nodes, callback) {

if (next.parent === next.child || nodes.indexOf(next.child) > -1) { process.nextTick(callback); return; }
nodes.push(next.child);
if (next.parent === next.child) { process.nextTick(callback); return; }

var getNodeRet;
var path = undefined;
var path;
nThen(function (waitFor) {

NodeStore_getNode(cjdns, state, next.child, waitFor(function (ret) {
//console.log('cjdns.NodeStore_getNode(' + next.child + '); --> ' + JSON.stringify(ret, null, ' '));
NodeStore_nodeForAddr(cjdns, state, next.child, waitFor(function (ret) {
//console.log('cjdns.NodeStore_nodeForAddr(' + next.child + '); --> ' + JSON.stringify(ret, null, ' '));
getNodeRet = ret;
}));

Expand All @@ -78,11 +49,13 @@ var getNode = function (cjdns, next, output, state, parentPath, ipsByReach, node
return;
}

NodeStore_getRouteLabel(cjdns, state, parentPath, next.child, waitFor(function (ret) {
cjdns.NodeStore_getRouteLabel(parentPath, next.cannonicalLabel, waitFor(function (err, ret) {
if (err) { throw err; }
if (ret.error !== 'none') {
throw new Error('cjdns.NodeStore_getRouteLabel(' + parentPath + ', ' + next.child +
'); --> ' + JSON.stringify([ret, parents], null, ' '));
throw new Error('cjdns.NodeStore_getRouteLabel(' + parentPath + ', ' + next.cannonicalLabel +
'); --> ' + JSON.stringify(ret, null, ' '));
}
//console.log('cjdns.NodeStore_getRouteLabel(' + parentPath + ', ' + next.pathParentToChild + ') --> ' + ret.result);
if (ret.result !== 'ffff.ffff.ffff.ffff') {
path = ret.result;
}
Expand All @@ -100,6 +73,7 @@ var getNode = function (cjdns, next, output, state, parentPath, ipsByReach, node
if (output.peers) { /* sanity check */ throw new Error(); }
output.addr = next.child;
output.cannonicalLabel = next.cannonicalLabel;
output.isOneHop = next.isOneHop;
output.fullPath = path;
output.peers = [];

Expand All @@ -109,15 +83,20 @@ var getNode = function (cjdns, next, output, state, parentPath, ipsByReach, node
nThen(function (waitFor) {

for (var i = 0; i < getNodeRet.result.linkCount; i++) {
NodeStore_getLink(cjdns, state, next.child, i, waitFor(function (ret) {
cjdns.NodeStore_getLink(next.child, i, waitFor(function (err, ret) {
if (err) { throw err; }
links.push(ret);
}));
}

}).nThen(function (waitFor) {

if (nodes.indexOf(next.child) > -1) { process.nextTick(callback); return; }
nodes.push(next.child);

//console.log(JSON.stringify(links, null, ' '));
links.sort(function (a,b) {
if (a.isOneHop !== b.isOneHop) { return a.isOneHop ? -1 : 1; }
return (ipsByReach.indexOf(a.result.child) < ipsByReach.indexOf(b.result.child)) ?
-1 : 1;
});
Expand Down Expand Up @@ -187,7 +166,7 @@ var ipsByReachDesc = function (cjdns, callback) {
var getTree = function (cjdns, callback) {
ipsByReachDesc(cjdns, function (ipsByReach) {

cjdns.NodeStore_getNode(undefined, function (err, ret) {
cjdns.NodeStore_nodeForAddr(undefined, function (err, ret) {
if (err) { throw err; }
var myIp6 = PublicToIp6.convert(ret['result']['key']);
var output = {};
Expand All @@ -208,14 +187,17 @@ var printTree = function (cjdns, tree, callback) {
cjdns.RouterModule_pingNode(tree.fullPath, waitFor(function (err, ret) {
if (err) { throw err; }
var resp = (ret.result !== 'pong') ? "[" + ret.error + "]" : (ret.ms + 'ms.');
if (resp == '[could not find node to ping]') { resp = '[notfound]'; }
process.stdout.write(' rp:' + resp);
}));
}).nThen(function (waitFor) {
cjdns.SwitchPinger_ping(tree.fullPath, waitFor(function (err, ret) {
if (err) { throw err; }
var resp = (ret.result !== 'pong') ? ret.error : (ret.ms + 'ms.');
process.stdout.write(' sp:' + resp + '\n');
var resp = (ret.result !== 'pong') ? ("["+ret.result+"]") : (ret.ms + 'ms.');
process.stdout.write(' sp:' + resp);
}));
}).nThen(function (waitFor) {
process.stdout.write(((tree.isOneHop === '1') ? ' oh' : '') + '\n');
}).nThen;

tree.peers.forEach(function (peer) {
Expand Down
73 changes: 73 additions & 0 deletions contrib/nodejs/tools/pingAll.js
@@ -0,0 +1,73 @@
#!/usr/bin/env node
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var Cjdns = require('../cjdnsadmin/cjdnsadmin');
var nThen = require('../cjdnsadmin/nthen');

Cjdns.connectWithAdminInfo(function (cjdns) {

var nodes = [];
var lags = [];
nThen(function (waitFor) {

var again = function (i) {
cjdns.NodeStore_dumpTable(i, waitFor(function (err, table) {
if (err) { throw err; }
var j;
for (j = 0; j < table.routingTable.length; j++) {
var r = table.routingTable[j];
nodes.push(r);
}
if (j) {
again(i+1);
}
}));
};
again(0);

}).nThen(function (waitFor) {

nodes.sort(function (a,b) { return (Number(a.link) < Number(b.link)) ? 1 : -1 });

var ips = [];
var uniques = [];
nodes.forEach(function (node) {
if (node.path !== '0000.0000.0000.0001' && ips.indexOf(node.ip) === -1) {
uniques.push(node);
ips.push(node.ip);
}
});

uniques.forEach(function (node) {
cjdns.RouterModule_pingNode(node.path, waitFor(function (err, ret) {
if (err) { throw err; }
if (ret.result === 'pong') {
console.log(ret.from + ' ' + ret.ms + 'ms linkq:' + node.link);
lags.push(Number(ret.ms));
} else {
console.log(ret.from + ' ' + ret.error);
lags.push(3000);
}
}));
});

}).nThen(function (waitFor) {
var totalLag = 0;
for (var i = 0; i < lags.length; i++) { totalLag += lags[i]; }
console.log("Average lag: " + (totalLag / lags.length));
cjdns.disconnect();
});

});
29 changes: 29 additions & 0 deletions dht/dhtcore/Janitor.c
Expand Up @@ -159,6 +159,30 @@ static void search(uint8_t target[16], struct Janitor* janitor, int searchType)
rp->userData = search;
}

static void checkPeers(struct Janitor* janitor, struct Node* n)
{
// Lets check for non-one-hop links at each node along the path between us and this node.
uint32_t i = 0;
for (;;i++) {
struct Node_Link* link =
NodeStore_getLinkOnPath(janitor->nodeStore, n->address.path, i);
if (!link) { return; }
int count = NodeStore_linkCount(link->child);
for (int j = 0; j < count; j++) {
struct Node_Link* l = NodeStore_getLink(link->child, j);
if (!Node_isOneHopLink(l)) {
RouterModule_getPeers(link->child,
l->cannonicalLabel,
0,
janitor->routerModule,
janitor->allocator);
// Only send max 1 getPeers req per second.
return;
}
}
}
}

static void maintanenceCycle(void* vcontext)
{
struct Janitor* const janitor = (struct Janitor*) vcontext;
Expand Down Expand Up @@ -207,6 +231,11 @@ static void maintanenceCycle(void* vcontext)

search(targetAddr.ip6.bytes, janitor, searchType);
return;

} else {
#ifdef EXPERIMENTAL_PATHFINDER
checkPeers(janitor, n);
#endif
}

#ifdef Log_DEBUG
Expand Down
27 changes: 27 additions & 0 deletions dht/dhtcore/Node.c
@@ -0,0 +1,27 @@
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "dht/dhtcore/Node.h"
#include "switch/EncodingScheme.h"
#include "util/Assert.h"
#include "util/Bits.h"

bool Node_isOneHopLink(struct Node_Link* link)
{
struct EncodingScheme* ps = link->parent->encodingScheme;
int num = EncodingScheme_getFormNum(ps, link->cannonicalLabel);
Assert_always(num > -1 && num < ps->count);
return EncodingScheme_formSize(&ps->forms[num]) == Bits_log2x64(link->cannonicalLabel);
}
11 changes: 11 additions & 0 deletions dht/dhtcore/Node.h
Expand Up @@ -20,6 +20,8 @@
#include "memory/Allocator.h"
#include "util/Assert.h"
#include "util/Identity.h"
#include "util/Linker.h"
Linker_require("dht/dhtcore/Node.c")

/** A network address for reaching a peer, in the format which is sent over the wire. */
struct Node
Expand Down Expand Up @@ -88,6 +90,12 @@ struct Node_Two
/** Used for freeing the links associated with this node. */
struct Node_Link* reversePeers;

/** The best link for getting to this node. */
struct Node_Link* bestParent;

/** The best known way to get to this node. */
uint64_t bestPath;

struct Allocator* alloc;

Identity
Expand Down Expand Up @@ -158,4 +166,7 @@ struct Node_Link
Identity
};


bool Node_isOneHopLink(struct Node_Link* link);

#endif

0 comments on commit 4ed8871

Please sign in to comment.