diff --git a/Cargo.lock b/Cargo.lock index 2eb248e71..e826ea22b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -700,6 +700,7 @@ dependencies = [ "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "snappy 0.1.0", "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)", "tiny-keccak 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/util/network/Cargo.toml b/util/network/Cargo.toml index 0960715bf..e989fb599 100644 --- a/util/network/Cargo.toml +++ b/util/network/Cargo.toml @@ -33,6 +33,7 @@ path = { path = "../path" } ethcore-logger = { path ="../../logger" } ipnetwork = "0.12.6" hash = { path = "../hash" } +snappy = { path = "../snappy" } serde_json = "1.0" [features] diff --git a/util/network/src/connection.rs b/util/network/src/connection.rs index fd61b4b38..726952648 100644 --- a/util/network/src/connection.rs +++ b/util/network/src/connection.rs @@ -40,6 +40,7 @@ use crypto; const ENCRYPTED_HEADER_LEN: usize = 32; const RECIEVE_PAYLOAD_TIMEOUT: u64 = 30000; +pub const MAX_PAYLOAD_SIZE: usize = (1 << 24) - 1; pub trait GenericSocket : Read + Write { } @@ -345,7 +346,7 @@ impl EncryptedConnection { ingress_mac: ingress_mac, read_state: EncryptedConnectionState::Header, protocol_id: 0, - payload_len: 0 + payload_len: 0, }; enc.connection.expect(ENCRYPTED_HEADER_LEN); Ok(enc) @@ -355,7 +356,7 @@ impl EncryptedConnection { pub fn send_packet(&mut self, io: &IoContext, payload: &[u8]) -> Result<(), NetworkError> where Message: Send + Clone + Sync + 'static { let mut header = RlpStream::new(); let len = payload.len(); - if len >= (1 << 24) { + if len > MAX_PAYLOAD_SIZE { return Err(NetworkError::OversizedPacket); } header.append_raw(&[(len >> 16) as u8, (len >> 8) as u8, len as u8], 1); diff --git a/util/network/src/error.rs b/util/network/src/error.rs index 54773d573..96fc1ff23 100644 --- a/util/network/src/error.rs +++ b/util/network/src/error.rs @@ -19,6 +19,7 @@ use rlp::*; use std::fmt; use ethkey::Error as KeyError; use crypto::Error as CryptoError; +use snappy; #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum DisconnectReason @@ -107,6 +108,8 @@ pub enum NetworkError { StdIo(::std::io::Error), /// Packet size is over the protocol limit. OversizedPacket, + /// Decompression error. + Decompression(snappy::InvalidInput), } impl fmt::Display for NetworkError { @@ -126,6 +129,7 @@ impl fmt::Display for NetworkError { StdIo(ref err) => format!("{}", err), InvalidNodeId => "Invalid node id".into(), OversizedPacket => "Packet is too large".into(), + Decompression(ref err) => format!("Error decompressing packet: {}", err), }; f.write_fmt(format_args!("Network error ({})", msg)) @@ -162,6 +166,12 @@ impl From for NetworkError { } } +impl From for NetworkError { + fn from(err: snappy::InvalidInput) -> NetworkError { + NetworkError::Decompression(err) + } +} + impl From<::std::net::AddrParseError> for NetworkError { fn from(err: ::std::net::AddrParseError) -> NetworkError { NetworkError::AddressParse(err) diff --git a/util/network/src/host.rs b/util/network/src/host.rs index 13e5f74a3..3d21bf5fe 100644 --- a/util/network/src/host.rs +++ b/util/network/src/host.rs @@ -256,7 +256,7 @@ impl<'s> NetworkContext<'s> { pub fn send_protocol(&self, protocol: ProtocolId, peer: PeerId, packet_id: PacketId, data: Vec) -> Result<(), NetworkError> { let session = self.resolve_session(peer); if let Some(session) = session { - session.lock().send_packet(self.io, protocol, packet_id as u8, &data)?; + session.lock().send_packet(self.io, Some(protocol), packet_id as u8, &data)?; } else { trace!(target: "network", "Send: Peer no longer exist") } @@ -938,7 +938,7 @@ impl Host { for (p, packet_id, data) in packet_data { let reserved = self.reserved_nodes.read(); if let Some(h) = handlers.get(&p).clone() { - h.read(&NetworkContext::new(io, p, Some(session.clone()), self.sessions.clone(), &reserved), &token, packet_id, &data[1..]); + h.read(&NetworkContext::new(io, p, Some(session.clone()), self.sessions.clone(), &reserved), &token, packet_id, &data); } } } diff --git a/util/network/src/lib.rs b/util/network/src/lib.rs index 6257118b6..8f2ccb8d4 100644 --- a/util/network/src/lib.rs +++ b/util/network/src/lib.rs @@ -81,6 +81,7 @@ extern crate ethcore_logger; extern crate ipnetwork; extern crate hash; extern crate serde_json; +extern crate snappy; #[macro_use] extern crate log; @@ -115,7 +116,7 @@ pub use node_table::{is_valid_node_url, NodeId}; use ipnetwork::{IpNetwork, IpNetworkError}; use std::str::FromStr; -const PROTOCOL_VERSION: u32 = 4; +const PROTOCOL_VERSION: u32 = 5; /// Network IO protocol handler. This needs to be implemented for each new subprotocol. /// All the handler function are called from within IO event loop. diff --git a/util/network/src/session.rs b/util/network/src/session.rs index 992081237..cf6c196e3 100644 --- a/util/network/src/session.rs +++ b/util/network/src/session.rs @@ -25,7 +25,7 @@ use mio::deprecated::{Handler, EventLoop}; use mio::tcp::*; use bigint::hash::*; use rlp::*; -use connection::{EncryptedConnection, Packet, Connection}; +use connection::{EncryptedConnection, Packet, Connection, MAX_PAYLOAD_SIZE}; use handshake::Handshake; use io::{IoContext, StreamToken}; use error::{NetworkError, DisconnectReason}; @@ -33,10 +33,13 @@ use host::*; use node_table::NodeId; use stats::NetworkStats; use time; +use snappy; // Timeout must be less than (interval - 1). const PING_TIMEOUT_SEC: u64 = 60; const PING_INTERVAL_SEC: u64 = 120; +const MIN_PROTOCOL_VERSION: u32 = 4; +const MIN_COMPRESSION_PROTOCOL_VERSION: u32 = 5; #[derive(Debug, Clone)] enum ProtocolState { @@ -61,6 +64,7 @@ pub struct Session { state: State, // Protocol states -- accumulates pending packets until signaled as ready. protocol_states: HashMap, + compression: bool, } enum State { @@ -198,6 +202,7 @@ impl Session { pong_time_ns: None, expired: false, protocol_states: HashMap::new(), + compression: false, }) } @@ -211,7 +216,6 @@ impl Session { }; self.state = State::Session(connection); self.write_hello(io, host)?; - self.send_ping(io)?; Ok(()) } @@ -326,28 +330,43 @@ impl Session { } /// Send a protocol packet to peer. - pub fn send_packet(&mut self, io: &IoContext, protocol: [u8; 3], packet_id: u8, data: &[u8]) -> Result<(), NetworkError> + pub fn send_packet(&mut self, io: &IoContext, protocol: Option<[u8; 3]>, packet_id: u8, data: &[u8]) -> Result<(), NetworkError> where Message: Send + Sync + Clone { - if self.info.capabilities.is_empty() || !self.had_hello { - debug!(target: "network", "Sending to unconfirmed session {}, protocol: {}, packet: {}", self.token(), str::from_utf8(&protocol[..]).unwrap_or("??"), packet_id); + if protocol.is_some() && (self.info.capabilities.is_empty() || !self.had_hello) { + debug!(target: "network", "Sending to unconfirmed session {}, protocol: {:?}, packet: {}", self.token(), protocol.as_ref().map(|p| str::from_utf8(&p[..]).unwrap_or("??")), packet_id); return Err(From::from(NetworkError::BadProtocol)); } if self.expired() { return Err(From::from(NetworkError::Expired)); } 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); - return Ok(()) - } - } - let pid = self.info.capabilities[i].id_offset + packet_id; + let pid = match protocol { + Some(protocol) => { + while protocol != self.info.capabilities[i].protocol { + i += 1; + if i == self.info.capabilities.len() { + debug!(target: "network", "Unknown protocol: {:?}", protocol); + return Ok(()) + } + } + self.info.capabilities[i].id_offset + packet_id + }, + None => packet_id + }; let mut rlp = RlpStream::new(); rlp.append(&(pid as u32)); - rlp.append_raw(data, 1); - self.send(io, rlp) + let mut compressed = Vec::new(); + let mut payload = data; // create a reference with local lifetime + if self.compression { + if payload.len() > MAX_PAYLOAD_SIZE { + return Err(NetworkError::OversizedPacket); + } + let len = snappy::compress_into(&payload, &mut compressed); + trace!(target: "network", "compressed {} to {}", payload.len(), len); + payload = &compressed[0..len]; + } + rlp.append_raw(payload, 1); + self.send(io, &rlp.drain()) } /// Keep this session alive. Returns false if ping timeout happened @@ -396,14 +415,23 @@ impl Session { if packet_id != PACKET_HELLO && packet_id != PACKET_DISCONNECT && !self.had_hello { return Err(From::from(NetworkError::BadProtocol)); } + let data = if self.compression { + let compressed = &packet.data[1..]; + if snappy::decompressed_len(&compressed)? > MAX_PAYLOAD_SIZE { + return Err(NetworkError::OversizedPacket); + } + snappy::decompress(&compressed)? + } else { + packet.data[1..].to_owned() + }; match packet_id { PACKET_HELLO => { - let rlp = UntrustedRlp::new(&packet.data[1..]); //TODO: validate rlp expected size + let rlp = UntrustedRlp::new(&data); //TODO: validate rlp expected size self.read_hello(io, &rlp, host)?; Ok(SessionData::Ready) }, PACKET_DISCONNECT => { - let rlp = UntrustedRlp::new(&packet.data[1..]); + let rlp = UntrustedRlp::new(&data); let reason: u8 = rlp.val_at(0)?; if self.had_hello { debug!(target:"network", "Disconnected: {}: {:?}", self.token(), DisconnectReason::from_u8(reason)); @@ -439,11 +467,11 @@ impl Session { match *self.protocol_states.entry(protocol).or_insert_with(|| ProtocolState::Pending(Vec::new())) { ProtocolState::Connected => { trace!(target: "network", "Packet {} mapped to {:?}:{}, i={}, capabilities={:?}", packet_id, protocol, protocol_packet_id, i, self.info.capabilities); - Ok(SessionData::Packet { data: packet.data, protocol: protocol, packet_id: protocol_packet_id } ) + Ok(SessionData::Packet { data: data, protocol: protocol, packet_id: protocol_packet_id } ) } ProtocolState::Pending(ref mut pending) => { trace!(target: "network", "Packet {} deferred until protocol connection event completion", packet_id); - pending.push((packet.data, protocol_packet_id)); + pending.push((data, protocol_packet_id)); Ok(SessionData::Continue) } @@ -465,7 +493,7 @@ impl Session { .append_list(&host.capabilities) .append(&host.local_endpoint.address.port()) .append(host.id()); - self.send(io, rlp) + self.send(io, &rlp.drain()) } fn read_hello(&mut self, io: &IoContext, rlp: &UntrustedRlp, host: &HostInfo) -> Result<(), NetworkError> @@ -494,8 +522,7 @@ impl Session { while i < caps.len() { if caps.iter().any(|c| c.protocol == caps[i].protocol && c.version > caps[i].version) { caps.remove(i); - } - else { + } else { i += 1; } } @@ -520,52 +547,46 @@ impl Session { trace!(target: "network", "No common capabilities with peer."); return Err(From::from(self.disconnect(io, DisconnectReason::UselessPeer))); } - if protocol != host.protocol_version { + if protocol < MIN_PROTOCOL_VERSION { trace!(target: "network", "Peer protocol version mismatch: {}", protocol); return Err(From::from(self.disconnect(io, DisconnectReason::UselessPeer))); } + self.compression = protocol >= MIN_COMPRESSION_PROTOCOL_VERSION; + self.send_ping(io)?; self.had_hello = true; Ok(()) } /// Senf ping packet pub fn send_ping(&mut self, io: &IoContext) -> Result<(), NetworkError> where Message: Send + Sync + Clone { - self.send(io, Session::prepare(PACKET_PING)?)?; + self.send_packet(io, None, PACKET_PING, &EMPTY_LIST_RLP)?; self.ping_time_ns = time::precise_time_ns(); self.pong_time_ns = None; Ok(()) } fn send_pong(&mut self, io: &IoContext) -> Result<(), NetworkError> where Message: Send + Sync + Clone { - self.send(io, Session::prepare(PACKET_PONG)?) + self.send_packet(io, None, PACKET_PONG, &EMPTY_LIST_RLP) } /// Disconnect this session pub fn disconnect(&mut self, io: &IoContext, 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(); + self.send_packet(io, None, PACKET_DISCONNECT, &rlp.drain()).ok(); } NetworkError::Disconnect(reason) } - fn prepare(packet_id: u8) -> Result { - let mut rlp = RlpStream::new(); - rlp.append(&(packet_id as u32)); - rlp.begin_list(0); - Ok(rlp) - } - - fn send(&mut self, io: &IoContext, rlp: RlpStream) -> Result<(), NetworkError> where Message: Send + Sync + Clone { + fn send(&mut self, io: &IoContext, data: &[u8]) -> Result<(), NetworkError> where Message: Send + Sync + Clone { match self.state { State::Handshake(_) => { warn!(target:"network", "Unexpected send request"); }, State::Session(ref mut s) => { - s.send_packet(io, &rlp.out())? + s.send_packet(io, data)? }, } Ok(())