Skip to content

Commit

Permalink
convert to new hashname interface and link handshakes
Browse files Browse the repository at this point in the history
  • Loading branch information
quartzjer committed Mar 7, 2015
1 parent a41c257 commit 67406b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
5 changes: 3 additions & 2 deletions ext/peer.js
Expand Up @@ -37,9 +37,10 @@ exports.mesh = function(mesh, cbExt)
Object.keys(mesh.keys).forEach(function(csid){
if(link.csid && link.csid != csid) return; // if we know the csid, only send that key
var json = {type:'peer',peer:link.hashname,c:router.x.cid()};
var body = lob.encode(hashname.toPacket(mesh.keys,csid));
var body = lob.encode(hashname.intermediates(mesh.keys), hashname.key(csid, mesh.keys));
var attach = lob.encode({type:'link', csid:csid}, body);
log.debug('sending peer key to',router.hashname,json,csid);
router.x.send({json:json,body:body});
router.x.send({json:json,body:attach});
});
return;
}
Expand Down
35 changes: 25 additions & 10 deletions lib/mesh.js
Expand Up @@ -333,7 +333,7 @@ exports.mesh = function(args, cbMesh)
log.debug('handshake sync',sync,atOld,atNew);

// always send handshake back if not in sync
if(!sync) link.x.sending(link.x.handshake(),pipe);
if(!sync) link.x.sending(link.handshake(),pipe);

// new outgoing sync
if(atNew > atOld)
Expand Down Expand Up @@ -410,20 +410,25 @@ exports.mesh = function(args, cbMesh)
log.debug('invalid link handshake attachment',handshake.body);
return false;
}
console.log('XXX',handshake.json,attach.json,attach.body)

// make sure key info is at least usable
if(handshake.json.csid != hashname.match(mesh.keys,attach.json))
// make sure key info is at least correct
var keys = {};
keys[handshake.json.csid] = attach.body;
var csid = hashname.match(mesh.keys, keys, null, attach.json);
if(handshake.json.csid != csid)
{
log.debug('invalid key handshake, unsupported csid',attach.json);
log.debug('invalid key handshake, unsupported csid',attach.json, keys);
return false;
}
handshake.json.hashname = hashname.fromPacket(attach);
handshake.json.hashname = hashname.fromKeys(keys, attach.json);
if(!handshake.json.hashname)
{
log.debug('invalid key handshake, no hashname',attach.json);
log.debug('invalid key handshake, no hashname',attach.json, keys);
return false;
}
// save key bytes in handshake

// hashname is valid now, so stash key bytes in handshake
handshake.body = attach.body;
}

Expand Down Expand Up @@ -649,7 +654,7 @@ exports.mesh = function(args, cbMesh)

// send most recent handshake if it's not seen
// IMPORTANT, always call pipe.send even w/ empty packets to signal intent to transport
if(!see) pipe.send(link.x && link.x.handshake(), link, function(){});
if(!see) pipe.send(link.handshake(), link, function(){});
}

var seen = link.seen[pipe.uid];
Expand Down Expand Up @@ -677,7 +682,7 @@ exports.mesh = function(args, cbMesh)
}

// added pipe that hasn't been seen since a sync, send most recent handshake again
if(!see && seen && seen < link.syncedAt) link.x.sending(link.x.handshake(),pipe);
if(!see && seen && seen < link.syncedAt) link.x.sending(link.handshake(),pipe);
}

// make sure the path is in the json
Expand Down Expand Up @@ -707,6 +712,16 @@ exports.mesh = function(args, cbMesh)
});
});
}

// generate a current handshake message
link.handshake = function()
{
if(!link.x) return ;
var json = {type:'link'};
var ims = hashname.intermediates(mesh.keys);
delete ims[link.csid];
return link.x.handshake({json:json, body:lob.encode(ims,hashname.key(link.csid, mesh.keys))});
}

// sync all pipes, try to create/init exchange if we have key info
link.sync = function()
Expand All @@ -715,7 +730,7 @@ exports.mesh = function(args, cbMesh)
log.debug('link sync keepalive',link.hashname,link.x?true:false);
if(!link.x) return false;
link.x.at(link.x.at()+1); // forces new
var handshake = link.x.handshake();
var handshake = link.handshake();
// track sync time for per-pipe latency on responses
link.syncedAt = Date.now();
link.pipes.forEach(function(pipe){
Expand Down

0 comments on commit 67406b2

Please sign in to comment.