2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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-06-21 13:56:33 +02:00
|
|
|
//! Network and general IO module.
|
|
|
|
//!
|
2016-02-01 15:22:42 +01:00
|
|
|
//! Example usage for craeting a network service and adding an IO handler:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2016-08-05 10:32:04 +02:00
|
|
|
//! extern crate ethcore_network as net;
|
|
|
|
//! use net::*;
|
|
|
|
//! use std::sync::Arc;
|
2016-02-01 15:22:42 +01:00
|
|
|
//!
|
|
|
|
//! struct MyHandler;
|
|
|
|
//!
|
2016-07-11 17:02:42 +02:00
|
|
|
//! impl NetworkProtocolHandler for MyHandler {
|
2017-07-14 20:40:28 +02:00
|
|
|
//! fn initialize(&self, io: &NetworkContext, _host_info: &HostInfo) {
|
2016-02-01 15:22:42 +01:00
|
|
|
//! io.register_timer(0, 1000);
|
|
|
|
//! }
|
|
|
|
//!
|
2016-07-11 17:02:42 +02:00
|
|
|
//! fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
2016-02-01 15:22:42 +01:00
|
|
|
//! println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
|
|
|
|
//! }
|
|
|
|
//!
|
2016-07-11 17:02:42 +02:00
|
|
|
//! fn connected(&self, io: &NetworkContext, peer: &PeerId) {
|
2016-02-01 15:22:42 +01:00
|
|
|
//! println!("Connected {}", peer);
|
|
|
|
//! }
|
|
|
|
//!
|
2016-07-11 17:02:42 +02:00
|
|
|
//! fn disconnected(&self, io: &NetworkContext, peer: &PeerId) {
|
2016-02-01 15:22:42 +01:00
|
|
|
//! println!("Disconnected {}", peer);
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn main () {
|
2017-08-29 14:38:01 +02:00
|
|
|
//! let mut service = NetworkService::new(NetworkConfiguration::new_local(), None).expect("Error creating network service");
|
2016-06-17 18:26:54 +02:00
|
|
|
//! service.start().expect("Error starting service");
|
2016-12-22 16:15:26 +01:00
|
|
|
//! service.register_protocol(Arc::new(MyHandler), *b"myp", 1, &[1u8]);
|
2016-02-01 15:22:42 +01:00
|
|
|
//!
|
|
|
|
//! // Wait for quit condition
|
|
|
|
//! // ...
|
|
|
|
//! // Drop the service
|
|
|
|
//! }
|
|
|
|
//! ```
|
2016-08-05 10:32:04 +02:00
|
|
|
|
2017-06-05 20:40:40 +02:00
|
|
|
//TODO: use Poll from mio
|
|
|
|
#![allow(deprecated)]
|
|
|
|
|
2016-08-05 10:32:04 +02:00
|
|
|
extern crate ethcore_io as io;
|
|
|
|
extern crate ethcore_util as util;
|
2017-09-04 16:36:49 +02:00
|
|
|
extern crate ethcore_bigint as bigint;
|
2017-09-06 20:47:45 +02:00
|
|
|
extern crate ethcore_bytes;
|
2016-08-05 10:32:04 +02:00
|
|
|
extern crate parking_lot;
|
|
|
|
extern crate mio;
|
|
|
|
extern crate tiny_keccak;
|
|
|
|
extern crate crypto as rcrypto;
|
|
|
|
extern crate rand;
|
|
|
|
extern crate time;
|
|
|
|
extern crate ansi_term; //TODO: remove this
|
2017-07-06 11:26:14 +02:00
|
|
|
extern crate rustc_hex;
|
2016-08-05 10:32:04 +02:00
|
|
|
extern crate igd;
|
|
|
|
extern crate libc;
|
|
|
|
extern crate slab;
|
2016-08-24 18:35:21 +02:00
|
|
|
extern crate ethkey;
|
|
|
|
extern crate ethcrypto as crypto;
|
2016-09-01 14:49:12 +02:00
|
|
|
extern crate rlp;
|
2016-10-30 09:56:34 +01:00
|
|
|
extern crate bytes;
|
2017-03-22 06:23:40 +01:00
|
|
|
extern crate path;
|
|
|
|
extern crate ethcore_logger;
|
2017-07-28 19:06:39 +02:00
|
|
|
extern crate ipnetwork;
|
2017-08-30 17:14:52 +02:00
|
|
|
extern crate hash;
|
2017-10-11 09:34:23 +02:00
|
|
|
extern crate serde_json;
|
2016-09-01 14:49:12 +02:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
2016-08-05 10:32:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate ethcore_devtools as devtools;
|
|
|
|
|
2015-12-02 12:07:46 +01:00
|
|
|
mod host;
|
|
|
|
mod connection;
|
|
|
|
mod handshake;
|
|
|
|
mod session;
|
2015-12-03 15:11:40 +01:00
|
|
|
mod discovery;
|
2015-12-17 11:42:30 +01:00
|
|
|
mod service;
|
2016-01-13 11:31:37 +01:00
|
|
|
mod error;
|
2016-02-12 09:52:32 +01:00
|
|
|
mod node_table;
|
2016-01-24 18:53:54 +01:00
|
|
|
mod stats;
|
2016-02-16 02:05:36 +01:00
|
|
|
mod ip_utils;
|
2017-08-29 14:38:01 +02:00
|
|
|
mod connection_filter;
|
2016-01-24 18:53:54 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2015-12-17 11:42:30 +01:00
|
|
|
|
2017-07-14 20:40:28 +02:00
|
|
|
pub use host::{HostInfo, PeerId, PacketId, ProtocolId, NetworkContext, NetworkIoMessage, NetworkConfiguration};
|
2016-08-05 10:32:04 +02:00
|
|
|
pub use service::NetworkService;
|
|
|
|
pub use error::NetworkError;
|
|
|
|
pub use stats::NetworkStats;
|
2016-10-12 20:18:59 +02:00
|
|
|
pub use session::SessionInfo;
|
2017-08-29 14:38:01 +02:00
|
|
|
pub use connection_filter::{ConnectionFilter, ConnectionDirection};
|
2016-01-13 11:31:37 +01:00
|
|
|
|
2017-07-14 20:40:28 +02:00
|
|
|
pub use io::TimerToken;
|
2016-12-10 16:51:50 +01:00
|
|
|
pub use node_table::{is_valid_node_url, NodeId};
|
2017-07-28 19:06:39 +02:00
|
|
|
use ipnetwork::{IpNetwork, IpNetworkError};
|
|
|
|
use std::str::FromStr;
|
2015-12-17 11:42:30 +01:00
|
|
|
|
2016-02-13 22:57:39 +01:00
|
|
|
const PROTOCOL_VERSION: u32 = 4;
|
|
|
|
|
2015-12-30 12:23:36 +01:00
|
|
|
/// Network IO protocol handler. This needs to be implemented for each new subprotocol.
|
|
|
|
/// All the handler function are called from within IO event loop.
|
2016-01-13 11:31:37 +01:00
|
|
|
/// `Message` is the type for message data.
|
2016-07-11 17:02:42 +02:00
|
|
|
pub trait NetworkProtocolHandler: Sync + Send {
|
2016-01-13 13:56:48 +01:00
|
|
|
/// Initialize the handler
|
2017-07-14 20:40:28 +02:00
|
|
|
fn initialize(&self, _io: &NetworkContext, _host_info: &HostInfo) {}
|
2015-12-30 12:23:36 +01:00
|
|
|
/// Called when new network packet received.
|
2016-07-11 17:02:42 +02:00
|
|
|
fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]);
|
2015-12-30 12:23:36 +01:00
|
|
|
/// Called when new peer is connected. Only called when peer supports the same protocol.
|
2016-07-11 17:02:42 +02:00
|
|
|
fn connected(&self, io: &NetworkContext, peer: &PeerId);
|
2015-12-30 12:23:36 +01:00
|
|
|
/// Called when a previously connected peer disconnects.
|
2016-07-11 17:02:42 +02:00
|
|
|
fn disconnected(&self, io: &NetworkContext, peer: &PeerId);
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Timer function called after a timeout created with `NetworkContext::timeout`.
|
2016-07-11 17:02:42 +02:00
|
|
|
fn timeout(&self, _io: &NetworkContext, _timer: TimerToken) {}
|
2015-12-02 12:07:46 +01:00
|
|
|
}
|
2015-12-17 11:42:30 +01:00
|
|
|
|
2016-06-21 13:56:33 +02:00
|
|
|
/// Non-reserved peer modes.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum NonReservedPeerMode {
|
|
|
|
/// Accept them. This is the default.
|
|
|
|
Accept,
|
|
|
|
/// Deny them.
|
|
|
|
Deny,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NonReservedPeerMode {
|
|
|
|
/// Attempt to parse the peer mode from a string.
|
|
|
|
pub fn parse(s: &str) -> Option<Self> {
|
|
|
|
match s {
|
|
|
|
"accept" => Some(NonReservedPeerMode::Accept),
|
|
|
|
"deny" => Some(NonReservedPeerMode::Deny),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2016-07-11 17:02:42 +02:00
|
|
|
}
|
2016-10-24 18:25:27 +02:00
|
|
|
|
2017-07-28 19:06:39 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "ipc", binary)]
|
|
|
|
pub struct IpFilter {
|
|
|
|
pub predefined: AllowIP,
|
|
|
|
pub custom_allow: Vec<IpNetwork>,
|
|
|
|
pub custom_block: Vec<IpNetwork>,
|
2017-08-30 17:14:52 +02:00
|
|
|
}
|
2017-07-28 19:06:39 +02:00
|
|
|
|
|
|
|
impl Default for IpFilter {
|
|
|
|
fn default() -> Self {
|
|
|
|
IpFilter {
|
|
|
|
predefined: AllowIP::All,
|
|
|
|
custom_allow: vec![],
|
|
|
|
custom_block: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IpFilter {
|
|
|
|
/// Attempt to parse the peer mode from a string.
|
|
|
|
pub fn parse(s: &str) -> Result<IpFilter, IpNetworkError> {
|
|
|
|
let mut filter = IpFilter::default();
|
|
|
|
for f in s.split_whitespace() {
|
|
|
|
match f {
|
|
|
|
"all" => filter.predefined = AllowIP::All,
|
|
|
|
"private" => filter.predefined = AllowIP::Private,
|
|
|
|
"public" => filter.predefined = AllowIP::Public,
|
|
|
|
"none" => filter.predefined = AllowIP::None,
|
|
|
|
custom => {
|
|
|
|
if custom.starts_with("-") {
|
|
|
|
filter.custom_block.push(IpNetwork::from_str(&custom.to_owned().split_off(1))?)
|
|
|
|
} else {
|
|
|
|
filter.custom_allow.push(IpNetwork::from_str(custom)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 18:25:27 +02:00
|
|
|
/// IP fiter
|
2017-07-28 19:06:39 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2016-10-24 18:25:27 +02:00
|
|
|
pub enum AllowIP {
|
|
|
|
/// Connect to any address
|
|
|
|
All,
|
|
|
|
/// Connect to private network only
|
|
|
|
Private,
|
|
|
|
/// Connect to public network only
|
|
|
|
Public,
|
2017-07-28 19:06:39 +02:00
|
|
|
/// Block all addresses
|
|
|
|
None,
|
2016-10-24 18:25:27 +02:00
|
|
|
}
|
|
|
|
|