2016-10-26 08:06:13 +03:00
|
|
|
// Copyright 2016 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
use std::sync::RwLock;
|
|
|
|
|
|
|
|
use rand::Rng;
|
|
|
|
use rand::os::OsRng;
|
2016-10-29 22:36:45 +03:00
|
|
|
use mioco::tcp::{TcpStream, Shutdown};
|
2016-10-26 08:06:13 +03:00
|
|
|
|
|
|
|
use core::ser::{serialize, deserialize, Error};
|
|
|
|
use msg::*;
|
|
|
|
use types::*;
|
|
|
|
use protocol::ProtocolV1;
|
|
|
|
|
|
|
|
const NONCES_CAP: usize = 100;
|
|
|
|
|
|
|
|
/// Handles the handshake negotiation when two peers connect and decides on
|
|
|
|
/// protocol.
|
|
|
|
pub struct Handshake {
|
|
|
|
/// Ring buffer of nonces sent to detect self connections without requiring
|
|
|
|
/// a node id.
|
|
|
|
nonces: RwLock<VecDeque<u64>>,
|
|
|
|
}
|
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
unsafe impl Sync for Handshake {}
|
|
|
|
unsafe impl Send for Handshake {}
|
2016-10-26 08:06:13 +03:00
|
|
|
|
|
|
|
impl Handshake {
|
|
|
|
/// Creates a new handshake handler
|
|
|
|
pub fn new() -> Handshake {
|
|
|
|
Handshake { nonces: RwLock::new(VecDeque::with_capacity(NONCES_CAP)) }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles connecting to a new remote peer, starting the version handshake.
|
2016-10-29 22:36:45 +03:00
|
|
|
pub fn connect(&self, mut conn: TcpStream) -> Result<(Box<Protocol>, PeerInfo), Error> {
|
2016-10-28 00:28:02 +03:00
|
|
|
// get a new nonce that can be used on handshake to detect self-connection
|
2016-10-26 08:06:13 +03:00
|
|
|
let nonce = self.next_nonce();
|
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
// send the first part of the handshake
|
2016-10-29 22:36:45 +03:00
|
|
|
let sender_addr = conn.local_addr().unwrap();
|
|
|
|
let receiver_addr = conn.peer_addr().unwrap();
|
2016-11-01 05:03:12 +03:00
|
|
|
try!(serialize(&mut conn,
|
|
|
|
&Hand {
|
|
|
|
version: PROTOCOL_VERSION,
|
|
|
|
capabilities: FULL_SYNC,
|
|
|
|
nonce: nonce,
|
|
|
|
sender_addr: SockAddr(sender_addr),
|
|
|
|
receiver_addr: SockAddr(receiver_addr),
|
|
|
|
user_agent: USER_AGENT.to_string(),
|
|
|
|
}));
|
2016-10-28 00:28:02 +03:00
|
|
|
|
|
|
|
// deserialize the handshake response and do version negotiation
|
2016-10-29 22:36:45 +03:00
|
|
|
let shake = try!(deserialize::<Shake>(&mut conn));
|
2016-10-26 08:06:13 +03:00
|
|
|
if shake.version != 1 {
|
2016-10-29 22:36:45 +03:00
|
|
|
self.close(&mut conn,
|
2016-10-28 00:28:02 +03:00
|
|
|
ErrCodes::UnsupportedVersion as u32,
|
2016-10-26 08:06:13 +03:00
|
|
|
format!("Unsupported version: {}, ours: {})",
|
|
|
|
shake.version,
|
|
|
|
PROTOCOL_VERSION));
|
2016-10-28 00:28:02 +03:00
|
|
|
return Err(Error::UnexpectedData {
|
|
|
|
expected: vec![PROTOCOL_VERSION as u8],
|
|
|
|
received: vec![shake.version as u8],
|
|
|
|
});
|
2016-10-26 08:06:13 +03:00
|
|
|
}
|
2016-10-28 00:28:02 +03:00
|
|
|
|
2016-10-31 04:23:52 +03:00
|
|
|
let peer_info = PeerInfo {
|
|
|
|
capabilities: shake.capabilities,
|
|
|
|
user_agent: shake.user_agent,
|
|
|
|
addr: receiver_addr,
|
|
|
|
version: shake.version,
|
|
|
|
};
|
2016-10-29 22:36:45 +03:00
|
|
|
|
|
|
|
info!("Connected to peer {:?}", peer_info);
|
2016-10-26 08:06:13 +03:00
|
|
|
// when more than one protocol version is supported, choosing should go here
|
2016-10-29 22:36:45 +03:00
|
|
|
Ok((Box::new(ProtocolV1::new(conn)), peer_info))
|
2016-10-26 08:06:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles receiving a connection from a new remote peer that started the
|
|
|
|
/// version handshake.
|
2016-10-29 22:36:45 +03:00
|
|
|
pub fn handshake(&self, mut conn: TcpStream) -> Result<(Box<Protocol>, PeerInfo), Error> {
|
2016-10-31 04:23:52 +03:00
|
|
|
// deserialize first part of handshake sent to us and do version negotiation
|
|
|
|
let hand = try!(deserialize::<Hand>(&mut conn));
|
|
|
|
if hand.version != 1 {
|
|
|
|
self.close(&mut conn,
|
|
|
|
ErrCodes::UnsupportedVersion as u32,
|
|
|
|
format!("Unsupported version: {}, ours: {})",
|
|
|
|
hand.version,
|
|
|
|
PROTOCOL_VERSION));
|
|
|
|
return Err(Error::UnexpectedData {
|
|
|
|
expected: vec![PROTOCOL_VERSION as u8],
|
|
|
|
received: vec![hand.version as u8],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// check the nonce to see if we could be trying to connect to ourselves
|
|
|
|
let nonces = self.nonces.read().unwrap();
|
|
|
|
if nonces.contains(&hand.nonce) {
|
|
|
|
return Err(Error::UnexpectedData {
|
|
|
|
expected: vec![],
|
|
|
|
received: vec![],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all good, keep peer info
|
|
|
|
let peer_info = PeerInfo {
|
|
|
|
capabilities: hand.capabilities,
|
|
|
|
user_agent: hand.user_agent,
|
|
|
|
addr: conn.peer_addr().unwrap(),
|
|
|
|
version: hand.version,
|
|
|
|
};
|
|
|
|
|
|
|
|
// send our reply with our info
|
2016-11-01 05:03:12 +03:00
|
|
|
try!(serialize(&mut conn,
|
|
|
|
&Shake {
|
|
|
|
version: PROTOCOL_VERSION,
|
|
|
|
capabilities: FULL_SYNC,
|
|
|
|
user_agent: USER_AGENT.to_string(),
|
|
|
|
}));
|
2016-10-31 04:23:52 +03:00
|
|
|
|
|
|
|
info!("Received connection from peer {:?}", peer_info);
|
|
|
|
// when more than one protocol version is supported, choosing should go here
|
2016-10-29 22:36:45 +03:00
|
|
|
Ok((Box::new(ProtocolV1::new(conn)), peer_info))
|
2016-10-31 04:23:52 +03:00
|
|
|
}
|
2016-10-26 08:06:13 +03:00
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
/// Generate a new random nonce and store it in our ring buffer
|
2016-10-26 08:06:13 +03:00
|
|
|
fn next_nonce(&self) -> u64 {
|
|
|
|
let mut rng = OsRng::new().unwrap();
|
|
|
|
let nonce = rng.next_u64();
|
|
|
|
|
|
|
|
let mut nonces = self.nonces.write().unwrap();
|
|
|
|
nonces.push_back(nonce);
|
|
|
|
if nonces.len() >= NONCES_CAP {
|
|
|
|
nonces.pop_front();
|
|
|
|
}
|
|
|
|
nonce
|
|
|
|
}
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
fn close(&self, conn: &mut TcpStream, err_code: u32, explanation: String) {
|
|
|
|
serialize(conn,
|
2016-10-26 08:06:13 +03:00
|
|
|
&PeerError {
|
|
|
|
code: err_code,
|
|
|
|
message: explanation,
|
|
|
|
});
|
2016-10-29 22:36:45 +03:00
|
|
|
conn.shutdown(Shutdown::Both);
|
2016-10-26 08:06:13 +03:00
|
|
|
}
|
|
|
|
}
|