From ef8ea37d26716869cd4572152ffedc047700f747 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 22 Jan 2018 01:30:50 -0800 Subject: allow passing local_id to datconnect (and peer) --- src/bin/geniza-net.rs | 2 +- src/peer.rs | 20 ++++++++++++++++++-- src/protocol.rs | 20 +++++++++++++++----- 3 files changed, 34 insertions(+), 8 deletions(-) (limited to 'src') 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(addr: A, feed_key: Key, handle: u64, is_live: bool, unified_chan: chan::Sender>) -> Result { + pub fn connect(addr: A, feed_key: Key, handle: u64, is_live: bool, local_id: Option<&[u8]>, unified_chan: chan::Sender>) -> Result { 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(addr: A, key: &Key, live: bool) -> Result { + pub fn connect(addr: A, key: &Key, live: bool, local_id: Option<&[u8]>) -> Result { // 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 { + pub fn from_tcp(tcp: TcpStream, key: &Key, live: bool, local_id: Option<&[u8]>) -> Result { 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]); -- cgit v1.2.3