diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-01-22 01:30:50 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-01-22 01:30:50 -0800 |
commit | ef8ea37d26716869cd4572152ffedc047700f747 (patch) | |
tree | 07ab323d523775e98ada04078c72c5b4f8d4a108 | |
parent | e547fa64ce684211f8eb76738f133adfad296eaa (diff) | |
download | geniza-ef8ea37d26716869cd4572152ffedc047700f747.tar.gz geniza-ef8ea37d26716869cd4572152ffedc047700f747.zip |
allow passing local_id to datconnect (and peer)
-rw-r--r-- | src/bin/geniza-net.rs | 2 | ||||
-rw-r--r-- | src/peer.rs | 20 | ||||
-rw-r--r-- | src/protocol.rs | 20 |
3 files changed, 34 insertions, 8 deletions
diff --git a/src/bin/geniza-net.rs b/src/bin/geniza-net.rs index b3a55ba..aeeef85 100644 --- a/src/bin/geniza-net.rs +++ b/src/bin/geniza-net.rs @@ -62,7 +62,7 @@ fn run() -> Result<()> { let dat_key = subm.value_of("dat_key").unwrap(); let key_bytes = parse_dat_address(&dat_key)?; let key = Key::from_slice(&key_bytes).unwrap(); - DatConnection::connect(host_port, &key, false)?; + DatConnection::connect(host_port, &key, false, None)?; println!("Done!"); } ("discovery-key", Some(subm)) => { diff --git a/src/peer.rs b/src/peer.rs index 3310a13..c6f8c6d 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -85,14 +85,30 @@ fn worker_thread(mut dc: DatConnection, handle: u64, outbound_chan: chan::Receiv impl DatPeerThread { - pub fn connect<A: ToSocketAddrs + Display>(addr: A, feed_key: Key, handle: u64, is_live: bool, unified_chan: chan::Sender<Result<PeerMsg>>) -> Result<DatPeerThread> { + pub fn connect<A: ToSocketAddrs + Display>(addr: A, feed_key: Key, handle: u64, is_live: bool, local_id: Option<&[u8]>, unified_chan: chan::Sender<Result<PeerMsg>>) -> Result<DatPeerThread> { let addr = addr.to_socket_addrs().unwrap().nth(0).unwrap(); let (outbound_chan, tx_chan) = chan::async(); let feed_key2 = feed_key.clone(); + + // Do an ugly little dance to copy local_id across thread barrier + let mut id_buf = [0; 32]; + let local_id = match local_id { + None => None, + Some(val) => { + id_buf.copy_from_slice(val); + Some(id_buf) + }, + }; thread::spawn(move || { - let dc = match DatConnection::connect(addr, &feed_key, is_live) { + let local_id = match local_id { + None => None, + Some(val) => { + Some(&id_buf[..]) + }, + }; + let dc = match DatConnection::connect(addr, &feed_key, is_live, local_id) { Ok(c) => c, Err(e) => { // TODO: error chain! diff --git a/src/protocol.rs b/src/protocol.rs index 84a5793..a3ecda8 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -195,23 +195,33 @@ impl Clone for DatConnection { } impl DatConnection { - pub fn connect<A: ToSocketAddrs + Display>(addr: A, key: &Key, live: bool) -> Result<DatConnection> { + pub fn connect<A: ToSocketAddrs + Display>(addr: A, key: &Key, live: bool, local_id: Option<&[u8]>) -> Result<DatConnection> { // Connect to server info!("Connecting to {}", addr); // TODO: timeout on connect (socketaddr iterator dance) let tcp = TcpStream::connect(addr)?; - DatConnection::from_tcp(tcp, key, live) + DatConnection::from_tcp(tcp, key, live, local_id) } // It's sort of a hack, but this should be usable from an accept() as well as a connect() - pub fn from_tcp(tcp: TcpStream, key: &Key, live: bool) -> Result<DatConnection> { + pub fn from_tcp(tcp: TcpStream, key: &Key, live: bool, local_id: Option<&[u8]>) -> Result<DatConnection> { let tx_nonce = gen_nonce(); - let mut local_id = [0; 32]; let mut rng = OsRng::new()?; - rng.fill_bytes(&mut local_id); + let mut local_id = match local_id { + Some(val) => { + let mut buf = [0; 32]; + buf.copy_from_slice(val); + buf + }, + None => { + let mut buf = [0; 32]; + rng.fill_bytes(&mut buf); + buf + }, + }; let mut dk = [0; 32]; dk.copy_from_slice(&make_discovery_key(&key[0..32])[0..32]); |