openethereum/util/network/src/session.rs

481 lines
15 KiB
Rust
Raw Normal View History

2016-02-05 13:40:41 +01:00
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it 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.
// Parity 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 Parity. If not, see <http://www.gnu.org/licenses/>.
2016-02-15 19:54:27 +01:00
use std::net::SocketAddr;
use std::io;
use std::sync::*;
2015-12-02 12:07:46 +01:00
use mio::*;
use mio::tcp::*;
use util::rlp::*;
use util::hash::*;
use connection::{EncryptedConnection, Packet, Connection};
use handshake::Handshake;
2016-02-02 20:58:12 +01:00
use io::{IoContext, StreamToken};
use error::{NetworkError, DisconnectReason};
use host::*;
use node_table::NodeId;
use stats::NetworkStats;
2016-02-02 20:58:12 +01:00
use time;
const PING_TIMEOUT_SEC: u64 = 30;
const PING_INTERVAL_SEC: u64 = 30;
2015-12-02 12:07:46 +01:00
2016-01-10 22:42:27 +01:00
/// Peer session over encrypted connection.
/// When created waits for Hello packet exchange and signals ready state.
/// Sends and receives protocol packets and handles basic packes such as ping/pong and disconnect.
2015-12-02 12:07:46 +01:00
pub struct Session {
2016-01-10 22:42:27 +01:00
/// Shared session information
2015-12-02 20:11:13 +01:00
pub info: SessionInfo,
2016-01-10 22:42:27 +01:00
/// Session ready flag. Set after successfull Hello packet exchange
2015-12-02 20:11:13 +01:00
had_hello: bool,
2016-02-20 01:10:27 +01:00
/// Session is no longer active flag.
expired: bool,
2016-02-02 20:58:12 +01:00
ping_time_ns: u64,
pong_time_ns: Option<u64>,
state: State,
}
enum State {
Handshake(Handshake),
Session(EncryptedConnection),
2015-12-02 20:11:13 +01:00
}
2016-01-10 22:42:27 +01:00
/// Structure used to report various session events.
2015-12-17 11:42:30 +01:00
pub enum SessionData {
None,
2016-01-10 22:42:27 +01:00
/// Session is ready to send/receive packets.
2015-12-17 11:42:30 +01:00
Ready,
2016-01-10 22:42:27 +01:00
/// A packet has been received
2015-12-17 11:42:30 +01:00
Packet {
2016-01-10 22:42:27 +01:00
/// Packet data
2015-12-17 11:42:30 +01:00
data: Vec<u8>,
2016-01-10 22:42:27 +01:00
/// Packet protocol ID
2015-12-17 11:42:30 +01:00
protocol: &'static str,
2016-05-23 11:46:01 +02:00
/// Zero based packet ID
2015-12-17 11:42:30 +01:00
packet_id: u8,
},
/// Session has more data to be read
Continue,
2015-12-17 11:42:30 +01:00
}
2016-01-10 22:42:27 +01:00
/// Shared session information
2015-12-02 20:11:13 +01:00
pub struct SessionInfo {
2016-01-10 22:42:27 +01:00
/// Peer public key
pub id: Option<NodeId>,
2016-01-10 22:42:27 +01:00
/// Peer client ID
2015-12-02 20:11:13 +01:00
pub client_version: String,
2016-01-10 22:42:27 +01:00
/// Peer RLPx protocol version
2015-12-02 20:11:13 +01:00
pub protocol_version: u32,
2016-01-10 22:42:27 +01:00
/// Peer protocol capabilities
capabilities: Vec<SessionCapabilityInfo>,
2016-02-02 20:58:12 +01:00
/// Peer ping delay in milliseconds
pub ping_ms: Option<u64>,
/// True if this session was originated by us.
pub originated: bool,
2015-12-17 11:42:30 +01:00
}
#[derive(Debug, PartialEq, Eq)]
pub struct PeerCapabilityInfo {
pub protocol: String,
pub version: u8,
}
impl Decodable for PeerCapabilityInfo {
2015-12-17 14:05:13 +01:00
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
2016-01-29 13:59:29 +01:00
let c = decoder.as_rlp();
2015-12-17 11:42:30 +01:00
Ok(PeerCapabilityInfo {
2016-01-29 13:59:29 +01:00
protocol: try!(c.val_at(0)),
version: try!(c.val_at(1))
2015-12-17 11:42:30 +01:00
})
}
}
#[derive(Debug)]
2016-01-10 22:42:27 +01:00
struct SessionCapabilityInfo {
2015-12-17 11:42:30 +01:00
pub protocol: &'static str,
pub version: u8,
pub packet_count: u8,
pub id_offset: u8,
2015-12-02 12:07:46 +01:00
}
2015-12-02 20:11:13 +01:00
const PACKET_HELLO: u8 = 0x80;
const PACKET_DISCONNECT: u8 = 0x01;
const PACKET_PING: u8 = 0x02;
const PACKET_PONG: u8 = 0x03;
const PACKET_GET_PEERS: u8 = 0x04;
const PACKET_PEERS: u8 = 0x05;
const PACKET_USER: u8 = 0x10;
const PACKET_LAST: u8 = 0x7f;
impl Session {
2016-05-23 11:46:01 +02:00
/// Create a new session out of comepleted handshake. This clones the handshake connection object
2016-02-21 16:52:25 +01:00
/// and leaves the handhsake in limbo to be deregistered from the event loop.
pub fn new<Message>(io: &IoContext<Message>, socket: TcpStream, token: StreamToken, id: Option<&NodeId>,
nonce: &H256, stats: Arc<NetworkStats>, host: &HostInfo) -> Result<Session, NetworkError>
where Message: Send + Clone {
let originated = id.is_some();
let mut handshake = Handshake::new(token, id, socket, nonce, stats).expect("Can't create handshake");
try!(handshake.start(io, host, originated));
Ok(Session {
state: State::Handshake(handshake),
2015-12-02 20:11:13 +01:00
had_hello: false,
info: SessionInfo {
id: id.cloned(),
2015-12-02 20:11:13 +01:00
client_version: String::new(),
protocol_version: 0,
capabilities: Vec::new(),
2016-02-02 20:58:12 +01:00
ping_ms: None,
originated: originated,
2015-12-02 20:11:13 +01:00
},
2016-02-02 20:58:12 +01:00
ping_time_ns: 0,
pong_time_ns: None,
2016-02-20 01:10:27 +01:00
expired: false,
})
}
fn complete_handshake<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo) -> Result<(), NetworkError> where Message: Send + Sync + Clone {
let connection = if let State::Handshake(ref mut h) = self.state {
self.info.id = Some(h.id.clone());
try!(EncryptedConnection::new(h))
} else {
panic!("Unexpected state");
2015-12-02 20:11:13 +01:00
};
self.state = State::Session(connection);
try!(self.write_hello(io, host));
try!(self.send_ping(io));
Ok(())
}
fn connection(&self) -> &Connection {
match self.state {
State::Handshake(ref h) => &h.connection,
State::Session(ref s) => &s.connection,
}
2015-12-02 12:07:46 +01:00
}
2015-12-02 20:11:13 +01:00
2016-02-12 09:52:32 +01:00
/// Get id of the remote peer
pub fn id(&self) -> Option<&NodeId> {
self.info.id.as_ref()
2016-02-12 09:52:32 +01:00
}
2016-01-10 22:42:27 +01:00
/// Check if session is ready to send/receive data
2016-01-10 14:02:01 +01:00
pub fn is_ready(&self) -> bool {
self.had_hello
}
2016-02-20 01:10:27 +01:00
/// Mark this session as inactive to be deleted lated.
pub fn set_expired(&mut self) {
self.expired = true;
}
/// Check if this session is expired.
pub fn expired(&self) -> bool {
match self.state {
State::Handshake(ref h) => h.expired(),
_ => self.expired,
}
2016-02-20 01:10:27 +01:00
}
/// Check if this session is over and there is nothing to be sent.
pub fn done(&self) -> bool {
self.expired() && !self.connection().is_sending()
2016-02-14 12:11:18 +01:00
}
2016-02-15 19:54:27 +01:00
/// Get remote peer address
pub fn remote_addr(&self) -> io::Result<SocketAddr> {
self.connection().remote_addr()
2016-02-15 19:54:27 +01:00
}
2016-01-10 22:42:27 +01:00
/// Readable IO handler. Returns packet data if available.
pub fn readable<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo) -> Result<SessionData, NetworkError> where Message: Send + Sync + Clone {
2016-02-20 01:10:27 +01:00
if self.expired() {
2016-05-23 11:46:01 +02:00
return Ok(SessionData::None)
2016-02-20 01:10:27 +01:00
}
let mut create_session = false;
let mut packet_data = None;
match self.state {
State::Handshake(ref mut h) => {
try!(h.readable(io, host));
if h.done() {
create_session = true;
}
}
State::Session(ref mut c) => {
match try!(c.readable(io)) {
data @ Some(_) => packet_data = data,
None => return Ok(SessionData::None)
}
}
}
if let Some(data) = packet_data {
return Ok(try!(self.read_packet(io, data, host)));
}
if create_session {
try!(self.complete_handshake(io, host));
io.update_registration(self.token()).unwrap_or_else(|e| debug!(target: "network", "Token registration error: {:?}", e));
2015-12-17 11:42:30 +01:00
}
Ok(SessionData::None)
2015-12-02 12:07:46 +01:00
}
2015-12-02 20:11:13 +01:00
2016-01-10 22:42:27 +01:00
/// Writable IO handler. Sends pending packets.
pub fn writable<Message>(&mut self, io: &IoContext<Message>, _host: &HostInfo) -> Result<(), NetworkError> where Message: Send + Sync + Clone {
match self.state {
State::Handshake(ref mut h) => h.writable(io),
State::Session(ref mut s) => s.writable(io),
}
2015-12-02 12:07:46 +01:00
}
2015-12-02 20:11:13 +01:00
2016-01-10 22:42:27 +01:00
/// Checks if peer supports given capability
2015-12-17 11:42:30 +01:00
pub fn have_capability(&self, protocol: &str) -> bool {
self.info.capabilities.iter().any(|c| c.protocol == protocol)
}
/// Register the session socket with the event loop
pub fn register_socket<Host:Handler<Timeout = Token>>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> Result<(), NetworkError> {
2016-02-20 01:10:27 +01:00
if self.expired() {
return Ok(());
}
try!(self.connection().register_socket(reg, event_loop));
Ok(())
}
2016-01-10 22:42:27 +01:00
/// Update registration with the event loop. Should be called at the end of the IO handler.
pub fn update_socket<Host:Handler>(&self, reg:Token, event_loop: &mut EventLoop<Host>) -> Result<(), NetworkError> {
try!(self.connection().update_socket(reg, event_loop));
Ok(())
2016-01-10 14:02:01 +01:00
}
2016-01-22 18:13:59 +01:00
/// Delete registration
pub fn deregister_socket<Host:Handler>(&self, event_loop: &mut EventLoop<Host>) -> Result<(), NetworkError> {
try!(self.connection().deregister_socket(event_loop));
Ok(())
2016-01-22 18:13:59 +01:00
}
2016-01-10 22:42:27 +01:00
/// Send a protocol packet to peer.
pub fn send_packet<Message>(&mut self, io: &IoContext<Message>, protocol: &str, packet_id: u8, data: &[u8]) -> Result<(), NetworkError>
where Message: Send + Sync + Clone {
2016-03-15 11:20:19 +01:00
if self.info.capabilities.is_empty() || !self.had_hello {
debug!(target: "network", "Sending to unconfirmed session {}, protocol: {}, packet: {}", self.token(), protocol, packet_id);
return Err(From::from(NetworkError::BadProtocol));
}
2016-03-05 23:09:51 +01:00
if self.expired() {
return Err(From::from(NetworkError::Expired));
}
2015-12-17 11:42:30 +01:00
let mut i = 0usize;
while protocol != self.info.capabilities[i].protocol {
i += 1;
if i == self.info.capabilities.len() {
debug!(target: "network", "Unknown protocol: {:?}", protocol);
2015-12-17 11:42:30 +01:00
return Ok(())
}
}
let pid = self.info.capabilities[i].id_offset + packet_id;
let mut rlp = RlpStream::new();
rlp.append(&(pid as u32));
rlp.append_raw(data, 1);
self.send(io, rlp)
2015-12-17 11:42:30 +01:00
}
2016-02-02 20:58:12 +01:00
/// Keep this session alive. Returns false if ping timeout happened
2016-02-11 21:10:41 +01:00
pub fn keep_alive<Message>(&mut self, io: &IoContext<Message>) -> bool where Message: Send + Sync + Clone {
if let State::Handshake(_) = self.state {
return true;
}
2016-02-02 20:58:12 +01:00
let timed_out = if let Some(pong) = self.pong_time_ns {
pong - self.ping_time_ns > PING_TIMEOUT_SEC * 1000_000_000
} else {
time::precise_time_ns() - self.ping_time_ns > PING_TIMEOUT_SEC * 1000_000_000
};
if !timed_out && time::precise_time_ns() - self.ping_time_ns > PING_INTERVAL_SEC * 1000_000_000 {
if let Err(e) = self.send_ping(io) {
2016-02-02 20:58:12 +01:00
debug!("Error sending ping message: {:?}", e);
}
}
!timed_out
}
pub fn token(&self) -> StreamToken {
self.connection().token()
2016-02-02 20:58:12 +01:00
}
fn read_packet<Message>(&mut self, io: &IoContext<Message>, packet: Packet, host: &HostInfo) -> Result<SessionData, NetworkError>
where Message: Send + Sync + Clone {
2015-12-17 11:42:30 +01:00
if packet.data.len() < 2 {
return Err(From::from(NetworkError::BadProtocol));
2015-12-02 20:11:13 +01:00
}
2015-12-17 11:42:30 +01:00
let packet_id = packet.data[0];
2015-12-02 20:11:13 +01:00
if packet_id != PACKET_HELLO && packet_id != PACKET_DISCONNECT && !self.had_hello {
return Err(From::from(NetworkError::BadProtocol));
2015-12-02 20:11:13 +01:00
}
match packet_id {
2015-12-17 11:42:30 +01:00
PACKET_HELLO => {
2016-01-08 15:52:43 +01:00
let rlp = UntrustedRlp::new(&packet.data[1..]); //TODO: validate rlp expected size
try!(self.read_hello(io, &rlp, host));
2015-12-17 11:42:30 +01:00
Ok(SessionData::Ready)
2016-01-08 15:52:43 +01:00
},
2016-02-17 14:07:26 +01:00
PACKET_DISCONNECT => {
let rlp = UntrustedRlp::new(&packet.data[1..]);
let reason: u8 = try!(rlp.val_at(0));
if self.had_hello {
debug!("Disconnected: {}: {:?}", self.token(), DisconnectReason::from_u8(reason));
}
2016-02-17 14:07:26 +01:00
Err(From::from(NetworkError::Disconnect(DisconnectReason::from_u8(reason))))
}
2015-12-17 11:42:30 +01:00
PACKET_PING => {
try!(self.send_pong(io));
Ok(SessionData::Continue)
2016-02-02 20:58:12 +01:00
},
PACKET_PONG => {
self.pong_time_ns = Some(time::precise_time_ns());
self.info.ping_ms = Some((self.pong_time_ns.unwrap() - self.ping_time_ns) / 1000_000);
Ok(SessionData::Continue)
2016-01-08 15:52:43 +01:00
},
2015-12-17 11:42:30 +01:00
PACKET_GET_PEERS => Ok(SessionData::None), //TODO;
PACKET_PEERS => Ok(SessionData::None),
2015-12-02 20:11:13 +01:00
PACKET_USER ... PACKET_LAST => {
2015-12-17 11:42:30 +01:00
let mut i = 0usize;
while packet_id < self.info.capabilities[i].id_offset {
i += 1;
if i == self.info.capabilities.len() {
debug!(target: "network", "Unknown packet: {:?}", packet_id);
return Ok(SessionData::Continue)
2015-12-17 11:42:30 +01:00
}
}
// map to protocol
let protocol = self.info.capabilities[i].protocol;
let pid = packet_id - self.info.capabilities[i].id_offset;
2016-01-19 12:14:29 +01:00
Ok(SessionData::Packet { data: packet.data, protocol: protocol, packet_id: pid } )
2015-12-02 20:11:13 +01:00
},
_ => {
debug!(target: "network", "Unknown packet: {:?}", packet_id);
Ok(SessionData::Continue)
2015-12-02 20:11:13 +01:00
}
}
}
fn write_hello<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo) -> Result<(), NetworkError> where Message: Send + Sync + Clone {
2015-12-02 20:11:13 +01:00
let mut rlp = RlpStream::new();
2016-01-10 14:02:01 +01:00
rlp.append_raw(&[PACKET_HELLO as u8], 0);
2016-01-27 12:14:57 +01:00
rlp.begin_list(5)
2015-12-02 20:11:13 +01:00
.append(&host.protocol_version)
.append(&host.client_version)
2016-01-28 20:13:05 +01:00
.append(&host.capabilities)
2016-03-20 11:35:46 +01:00
.append(&host.local_endpoint.address.port())
2015-12-02 20:11:13 +01:00
.append(host.id());
self.send(io, rlp)
2015-12-02 20:11:13 +01:00
}
fn read_hello<Message>(&mut self, io: &IoContext<Message>, rlp: &UntrustedRlp, host: &HostInfo) -> Result<(), NetworkError>
where Message: Send + Sync + Clone {
2015-12-17 14:05:13 +01:00
let protocol = try!(rlp.val_at::<u32>(0));
let client_version = try!(rlp.val_at::<String>(1));
let peer_caps = try!(rlp.val_at::<Vec<PeerCapabilityInfo>>(2));
let id = try!(rlp.val_at::<NodeId>(4));
2015-12-02 20:11:13 +01:00
// Intersect with host capabilities
// Leave only highset mutually supported capability version
2015-12-17 11:42:30 +01:00
let mut caps: Vec<SessionCapabilityInfo> = Vec::new();
2016-01-19 12:14:29 +01:00
for hc in &host.capabilities {
2015-12-17 11:42:30 +01:00
if peer_caps.iter().any(|c| c.protocol == hc.protocol && c.version == hc.version) {
caps.push(SessionCapabilityInfo {
protocol: hc.protocol,
version: hc.version,
id_offset: 0,
packet_count: hc.packet_count,
});
}
}
caps.retain(|c| host.capabilities.iter().any(|hc| hc.protocol == c.protocol && hc.version == c.version));
2015-12-02 20:11:13 +01:00
let mut i = 0;
while i < caps.len() {
if caps.iter().any(|c| c.protocol == caps[i].protocol && c.version > caps[i].version) {
caps.remove(i);
}
else {
i += 1;
}
}
2015-12-17 11:42:30 +01:00
i = 0;
let mut offset: u8 = PACKET_USER;
while i < caps.len() {
caps[i].id_offset = offset;
offset += caps[i].packet_count;
i += 1;
}
2016-03-05 23:09:51 +01:00
trace!(target: "network", "Hello: {} v{} {} {:?}", client_version, protocol, id, caps);
2016-01-15 00:50:48 +01:00
self.info.client_version = client_version;
2015-12-17 11:42:30 +01:00
self.info.capabilities = caps;
2016-02-16 19:08:58 +01:00
if self.info.capabilities.is_empty() {
2016-03-05 23:09:51 +01:00
trace!(target: "network", "No common capabilities with peer.");
return Err(From::from(self.disconnect(io, DisconnectReason::UselessPeer)));
2016-02-16 19:08:58 +01:00
}
2015-12-02 20:11:13 +01:00
if protocol != host.protocol_version {
2016-03-05 23:09:51 +01:00
trace!(target: "network", "Peer protocol version mismatch: {}", protocol);
return Err(From::from(self.disconnect(io, DisconnectReason::UselessPeer)));
2015-12-02 20:11:13 +01:00
}
self.had_hello = true;
Ok(())
}
2016-02-02 20:58:12 +01:00
/// Senf ping packet
pub fn send_ping<Message>(&mut self, io: &IoContext<Message>) -> Result<(), NetworkError> where Message: Send + Sync + Clone {
try!(self.send(io, try!(Session::prepare(PACKET_PING))));
2016-02-02 20:58:12 +01:00
self.ping_time_ns = time::precise_time_ns();
self.pong_time_ns = None;
Ok(())
2015-12-02 20:11:13 +01:00
}
fn send_pong<Message>(&mut self, io: &IoContext<Message>) -> Result<(), NetworkError> where Message: Send + Sync + Clone {
self.send(io, try!(Session::prepare(PACKET_PONG)))
2015-12-02 20:11:13 +01:00
}
2016-02-02 14:54:46 +01:00
/// Disconnect this session
pub fn disconnect<Message>(&mut self, io: &IoContext<Message>, reason: DisconnectReason) -> NetworkError where Message: Send + Sync + Clone {
if let State::Session(_) = self.state {
let mut rlp = RlpStream::new();
rlp.append(&(PACKET_DISCONNECT as u32));
rlp.begin_list(1);
rlp.append(&(reason as u32));
self.send(io, rlp).ok();
}
NetworkError::Disconnect(reason)
2015-12-02 20:11:13 +01:00
}
fn prepare(packet_id: u8) -> Result<RlpStream, NetworkError> {
2016-01-10 14:02:01 +01:00
let mut rlp = RlpStream::new();
2015-12-02 20:11:13 +01:00
rlp.append(&(packet_id as u32));
2016-01-27 12:14:57 +01:00
rlp.begin_list(0);
2015-12-02 20:11:13 +01:00
Ok(rlp)
}
fn send<Message>(&mut self, io: &IoContext<Message>, rlp: RlpStream) -> Result<(), NetworkError> where Message: Send + Sync + Clone {
match self.state {
State::Handshake(_) => {
warn!(target:"network", "Unexpected send request");
},
State::Session(ref mut s) => {
try!(s.send_packet(io, &rlp.out()))
},
}
Ok(())
2015-12-02 20:11:13 +01:00
}
2015-12-02 12:07:46 +01:00
}