listening for incomming connections and -connect works

This commit is contained in:
debris 2016-10-03 15:44:35 +02:00
parent b365ec90ff
commit c1184e4a9b
12 changed files with 275 additions and 155 deletions

2
Cargo.lock generated
View File

@ -5,11 +5,13 @@ dependencies = [
"bitcrypto 0.1.0",
"chain 0.1.0",
"clap 2.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"keys 0.1.0",
"net 0.1.0",
"p2p 0.1.0",
"primitives 0.1.0",
"script 0.1.0",
"tokio-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View File

@ -7,6 +7,8 @@ description = "Parity bitcoin client."
[dependencies]
clap = { version = "2", features = ["yaml"] }
tokio-core = "0.1"
futures = "0.1"
bitcrypto = { path = "crypto" }
chain = { path = "chain" }

View File

@ -33,6 +33,20 @@ impl Magic {
_ => Err(Error::InvalidMagic),
}
}
pub fn port(&self) -> u16 {
match *self {
Magic::Mainnet => 8333,
Magic::Testnet => 18333,
}
}
pub fn rpc_port(&self) -> u16 {
match *self {
Magic::Mainnet => 8332,
Magic::Testnet => 18332,
}
}
}
impl Serializable for Magic {
@ -61,4 +75,16 @@ mod tests {
assert_eq!(Magic::from_u32(MAGIC_TESTNET).unwrap(), Magic::Testnet);
assert_eq!(Magic::from_u32(0).unwrap_err(), Error::InvalidMagic);
}
#[test]
fn test_network_port() {
assert_eq!(Magic::Mainnet.port(), 8333);
assert_eq!(Magic::Testnet.port(), 18333);
}
#[test]
fn test_network_rpc_port() {
assert_eq!(Magic::Mainnet.rpc_port(), 8332);
assert_eq!(Magic::Testnet.rpc_port(), 18332);
}
}

View File

@ -1,4 +1,3 @@
use crypto::checksum;
use ser::{
Serializable, Stream,
Deserializable, Reader, Error as ReaderError

View File

@ -2,11 +2,9 @@ use std::{net, io};
use futures::{Future, Poll, Async};
use futures::stream::Stream;
use tokio_core::reactor::Handle;
use tokio_core::net::TcpListener;
use tokio_core::io::IoStream;
use net::common::{Magic, ServiceFlags, NetAddress};
use net::messages::{Version, Simple, V106, V70001};
use stream::{TcpStream, TcpStreamNew};
use stream::{TcpStream, TcpStreamNew, TcpListener, IoStream};
use io::{handshake, Handshake, HandshakeResult, Error, accept_handshake, AcceptHandshake, VERSION};
use util::time::{Time, RealTime};
use util::nonce::{NonceGenerator, RandomNonce};
@ -66,8 +64,8 @@ pub fn connect(address: &net::SocketAddr, handle: &Handle, config: &Config) -> C
pub fn listen(handle: &Handle, config: Config) -> Result<Listen, Error> {
let listener = try!(TcpListener::bind(&config.local_address, handle));
let listen = Listen {
incoming: listener.incoming()
.map(move |(stream, address)| accept_connection(stream.into(), &config, address))
inner: listener.incoming()
.and_then(move |(stream, address)| accept_connection(stream.into(), &config, address))
.boxed(),
};
Ok(listen)
@ -89,13 +87,8 @@ pub struct Connect {
}
pub struct Listen {
incoming: IoStream<AcceptConnection<TcpStream>>,
}
impl Listen {
pub fn incoming(self) -> IoStream<AcceptConnection<TcpStream>> {
self.incoming
}
//inner: Incoming,
inner: IoStream<Connection<TcpStream>>,
}
impl Future for Connect {
@ -161,3 +154,12 @@ impl<A> Future for AcceptConnection<A> where A: io::Read + io::Write {
Ok(connection.into())
}
}
impl Stream for Listen {
type Item = Connection<TcpStream>;
type Error = Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
self.inner.poll()
}
}

View File

@ -15,3 +15,4 @@ pub mod util;
pub mod stream;
pub use primitives::bytes;

View File

@ -1,5 +1,6 @@
use std::{ops, net, io};
use futures::{Future, Poll, Async};
use futures::stream::{Stream, BoxStream};
use tokio_core::{net as tnet};
use tokio_core::reactor::Handle;
use io::Error;
@ -53,3 +54,43 @@ impl TcpStream {
TcpStreamNew(tnet::TcpStream::connect(addr, handle))
}
}
pub struct Incoming(tnet::Incoming);
impl Stream for Incoming {
type Item = (TcpStream, net::SocketAddr);
type Error = Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match try_ready!(self.0.poll()) {
Some((stream, addr)) => {
let stream: TcpStream = stream.into();
Ok(Some((stream, addr)).into())
},
None => Ok(None.into()),
}
}
}
pub struct TcpListener(tnet::TcpListener);
impl ops::Deref for TcpListener {
type Target = tnet::TcpListener;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl TcpListener {
pub fn bind(addr: &net::SocketAddr, handle: &Handle) -> Result<TcpListener, Error> {
let listener = try!(tnet::TcpListener::bind(addr, handle));
Ok(TcpListener(listener))
}
pub fn incoming(self) -> Incoming {
Incoming(self.0.incoming())
}
}
pub type IoStream<T> = BoxStream<T, Error>;

View File

@ -23,4 +23,7 @@ args:
value_name: PORT
help: Listen for connections on PORT
takes_value: true
- printtoconsole:
long: printtoconsole
help: Send trace/debug info to console instead of debug.log file

View File

@ -1,14 +1,17 @@
use std::net;
use clap;
use net::common::Magic;
pub struct Config<'a> {
pub struct Config {
pub magic: Magic,
pub port: u16,
pub connect: Option<&'a str>,
pub seednode: Option<&'a str>,
pub connect: Option<net::IpAddr>,
pub seednode: Option<net::IpAddr>,
pub print_to_console: bool,
}
pub fn parse<'a>(matches: &'a clap::ArgMatches<'a>) -> Result<Config<'a>, String> {
pub fn parse(matches: &clap::ArgMatches) -> Result<Config, String> {
let print_to_console = matches.is_present("printtoconsole");
let magic = match matches.is_present("testnet") {
true => Magic::Testnet,
false => Magic::Mainnet,
@ -16,17 +19,25 @@ pub fn parse<'a>(matches: &'a clap::ArgMatches<'a>) -> Result<Config<'a>, String
let port = match matches.value_of("port") {
Some(port) => try!(port.parse().map_err(|_| "Invalid port".to_owned())),
None => match magic {
Magic::Mainnet => 8333,
Magic::Testnet => 18333,
},
None => magic.port(),
};
let connect = match matches.value_of("connect") {
Some(s) => Some(try!(s.parse().map_err(|_| "Invalid connect".to_owned()))),
None => None,
};
let seednode = match matches.value_of("seednode") {
Some(s) => Some(try!(s.parse().map_err(|_| "Invalid seednode".to_owned()))),
None => None,
};
let config = Config {
print_to_console: print_to_console,
magic: magic,
port: port,
connect: matches.value_of("connect"),
seednode: matches.value_of("seednode"),
connect: connect,
seednode: seednode,
};
Ok(config)

View File

@ -2,6 +2,8 @@
#[macro_use]
extern crate clap;
extern crate tokio_core;
extern crate futures;
extern crate bitcrypto as crypto;
extern crate chain;
@ -13,6 +15,14 @@ extern crate p2p;
mod config;
use futures::stream::Stream;
use std::net::SocketAddr;
use p2p::connect::{Config as P2PConfig, self};
pub fn event_loop() -> tokio_core::reactor::Core {
tokio_core::reactor::Core::new().unwrap()
}
fn main() {
match run() {
Err(err) => println!("{}", err),
@ -25,10 +35,30 @@ fn run() -> Result<(), String> {
let matches = clap::App::from_yaml(yaml).get_matches();
let cfg = try!(config::parse(&matches));
if let Some(_connect) = cfg.connect {
let mut el = event_loop();
let handle = el.handle();
let p2p_cfg = P2PConfig {
magic: cfg.magic,
local_address: SocketAddr::new("127.0.0.1".parse().unwrap(), cfg.port),
services: Default::default(),
user_agent: "pbtc".into(),
start_height: 0,
relay: false,
};
if let Some(connect) = cfg.connect {
let c = connect::connect(&SocketAddr::new(connect, cfg.magic.port()), &handle, &p2p_cfg);
let connection = try!(el.run(c).map_err(|_| format!("Connect to {} failed", connect)));
}
let listen = try!(connect::listen(&handle, p2p_cfg).map_err(|_| "Cannot start listening".to_owned()));
let server = listen.for_each(|connection| {
println!("new connection: {:?}", connection.handshake_result);
Ok(())
});
el.run(server).unwrap();
Ok(())
}

View File

@ -3,51 +3,51 @@ digraph dependencies {
N1[label="bitcrypto",shape=box];
N2[label="chain",shape=box];
N3[label="clap",shape=box];
N4[label="keys",shape=box];
N5[label="p2p",shape=box];
N6[label="primitives",shape=box];
N7[label="script",shape=box];
N8[label="ansi_term",shape=box];
N9[label="arrayvec",shape=box];
N10[label="nodrop",shape=box];
N11[label="odds",shape=box];
N12[label="base58",shape=box];
N13[label="rust-crypto",shape=box];
N14[label="bitflags v0.4.0",shape=box];
N15[label="bitflags v0.7.0",shape=box];
N16[label="byteorder",shape=box];
N17[label="cfg-if",shape=box];
N18[label="rustc-serialize",shape=box];
N19[label="serialization",shape=box];
N20[label="libc",shape=box];
N21[label="strsim",shape=box];
N22[label="term_size",shape=box];
N23[label="unicode-segmentation",shape=box];
N24[label="unicode-width",shape=box];
N25[label="vec_map",shape=box];
N26[label="yaml-rust",shape=box];
N27[label="eth-secp256k1",shape=box];
N28[label="gcc",shape=box];
N29[label="rand",shape=box];
N30[label="futures",shape=box];
N31[label="log",shape=box];
N32[label="kernel32-sys",shape=box];
N33[label="winapi",shape=box];
N34[label="winapi-build",shape=box];
N35[label="lazy_static",shape=box];
N36[label="lazycell",shape=box];
N37[label="mio",shape=box];
N38[label="miow",shape=box];
N39[label="net2",shape=box];
N40[label="nix",shape=box];
N41[label="slab",shape=box];
N42[label="ws2_32-sys",shape=box];
N43[label="net",shape=box];
N44[label="rustc_version",shape=box];
N45[label="semver",shape=box];
N46[label="void",shape=box];
N47[label="time",shape=box];
N48[label="tokio-core",shape=box];
N4[label="futures",shape=box];
N5[label="keys",shape=box];
N6[label="net",shape=box];
N7[label="p2p",shape=box];
N8[label="primitives",shape=box];
N9[label="script",shape=box];
N10[label="tokio-core",shape=box];
N11[label="ansi_term",shape=box];
N12[label="arrayvec",shape=box];
N13[label="nodrop",shape=box];
N14[label="odds",shape=box];
N15[label="base58",shape=box];
N16[label="rust-crypto",shape=box];
N17[label="bitflags v0.4.0",shape=box];
N18[label="bitflags v0.7.0",shape=box];
N19[label="byteorder",shape=box];
N20[label="cfg-if",shape=box];
N21[label="rustc-serialize",shape=box];
N22[label="serialization",shape=box];
N23[label="libc",shape=box];
N24[label="strsim",shape=box];
N25[label="term_size",shape=box];
N26[label="unicode-segmentation",shape=box];
N27[label="unicode-width",shape=box];
N28[label="vec_map",shape=box];
N29[label="yaml-rust",shape=box];
N30[label="eth-secp256k1",shape=box];
N31[label="gcc",shape=box];
N32[label="rand",shape=box];
N33[label="log",shape=box];
N34[label="kernel32-sys",shape=box];
N35[label="winapi",shape=box];
N36[label="winapi-build",shape=box];
N37[label="lazy_static",shape=box];
N38[label="lazycell",shape=box];
N39[label="mio",shape=box];
N40[label="miow",shape=box];
N41[label="net2",shape=box];
N42[label="nix",shape=box];
N43[label="slab",shape=box];
N44[label="ws2_32-sys",shape=box];
N45[label="rustc_version",shape=box];
N46[label="semver",shape=box];
N47[label="void",shape=box];
N48[label="time",shape=box];
N49[label="scoped-tls",shape=box];
N0 -> N1[label="",style=dashed];
N0 -> N2[label="",style=dashed];
@ -56,101 +56,104 @@ digraph dependencies {
N0 -> N5[label="",style=dashed];
N0 -> N6[label="",style=dashed];
N0 -> N7[label="",style=dashed];
N1 -> N6[label="",style=dashed];
N1 -> N13[label="",style=dashed];
N0 -> N8[label="",style=dashed];
N0 -> N9[label="",style=dashed];
N0 -> N10[label="",style=dashed];
N1 -> N8[label="",style=dashed];
N1 -> N16[label="",style=dashed];
N2 -> N1[label="",style=dashed];
N2 -> N6[label="",style=dashed];
N2 -> N18[label="",style=dashed];
N2 -> N19[label="",style=dashed];
N3 -> N8[label="",style=dashed];
N3 -> N15[label="",style=dashed];
N3 -> N20[label="",style=dashed];
N3 -> N21[label="",style=dashed];
N3 -> N22[label="",style=dashed];
N2 -> N8[label="",style=dashed];
N2 -> N21[label="",style=dashed];
N2 -> N22[label="",style=dashed];
N3 -> N11[label="",style=dashed];
N3 -> N18[label="",style=dashed];
N3 -> N23[label="",style=dashed];
N3 -> N24[label="",style=dashed];
N3 -> N25[label="",style=dashed];
N3 -> N26[label="",style=dashed];
N4 -> N1[label="",style=dashed];
N4 -> N6[label="",style=dashed];
N4 -> N12[label="",style=dashed];
N4 -> N18[label="",style=dashed];
N4 -> N27[label="",style=dashed];
N4 -> N29[label="",style=dashed];
N4 -> N35[label="",style=dashed];
N3 -> N27[label="",style=dashed];
N3 -> N28[label="",style=dashed];
N3 -> N29[label="",style=dashed];
N4 -> N33[label="",style=dashed];
N5 -> N1[label="",style=dashed];
N5 -> N6[label="",style=dashed];
N5 -> N19[label="",style=dashed];
N5 -> N29[label="",style=dashed];
N5 -> N8[label="",style=dashed];
N5 -> N15[label="",style=dashed];
N5 -> N21[label="",style=dashed];
N5 -> N30[label="",style=dashed];
N5 -> N43[label="",style=dashed];
N5 -> N47[label="",style=dashed];
N5 -> N48[label="",style=dashed];
N6 -> N18[label="",style=dashed];
N5 -> N32[label="",style=dashed];
N5 -> N37[label="",style=dashed];
N6 -> N1[label="",style=dashed];
N6 -> N8[label="",style=dashed];
N6 -> N19[label="",style=dashed];
N6 -> N22[label="",style=dashed];
N7 -> N1[label="",style=dashed];
N7 -> N2[label="",style=dashed];
N7 -> N4[label="",style=dashed];
N7 -> N6[label="",style=dashed];
N7 -> N19[label="",style=dashed];
N9 -> N10[label=""];
N9 -> N11[label=""];
N10 -> N11[label=""];
N13 -> N18[label="",style=dashed];
N13 -> N20[label="",style=dashed];
N13 -> N28[label="",style=dashed];
N13 -> N29[label="",style=dashed];
N13 -> N47[label="",style=dashed];
N19 -> N6[label="",style=dashed];
N19 -> N16[label="",style=dashed];
N22 -> N20[label="",style=dashed];
N22 -> N32[label="",style=dashed];
N22 -> N33[label="",style=dashed];
N27 -> N9[label="",style=dashed];
N27 -> N18[label="",style=dashed];
N27 -> N20[label="",style=dashed];
N27 -> N28[label="",style=dashed];
N27 -> N29[label="",style=dashed];
N29 -> N20[label="",style=dashed];
N7 -> N8[label="",style=dashed];
N7 -> N10[label="",style=dashed];
N7 -> N22[label="",style=dashed];
N7 -> N32[label="",style=dashed];
N7 -> N48[label="",style=dashed];
N8 -> N21[label="",style=dashed];
N9 -> N1[label="",style=dashed];
N9 -> N2[label="",style=dashed];
N9 -> N5[label="",style=dashed];
N9 -> N8[label="",style=dashed];
N9 -> N22[label="",style=dashed];
N10 -> N4[label="",style=dashed];
N10 -> N33[label="",style=dashed];
N10 -> N39[label="",style=dashed];
N10 -> N43[label="",style=dashed];
N10 -> N49[label="",style=dashed];
N12 -> N13[label=""];
N12 -> N14[label=""];
N13 -> N14[label=""];
N16 -> N21[label="",style=dashed];
N16 -> N23[label="",style=dashed];
N16 -> N31[label="",style=dashed];
N16 -> N32[label="",style=dashed];
N16 -> N48[label="",style=dashed];
N22 -> N8[label="",style=dashed];
N22 -> N19[label="",style=dashed];
N25 -> N23[label="",style=dashed];
N25 -> N34[label="",style=dashed];
N25 -> N35[label="",style=dashed];
N30 -> N12[label="",style=dashed];
N30 -> N21[label="",style=dashed];
N30 -> N23[label="",style=dashed];
N30 -> N31[label="",style=dashed];
N32 -> N33[label="",style=dashed];
N32 -> N34[label="",style=dashed];
N37 -> N20[label="",style=dashed];
N37 -> N31[label="",style=dashed];
N37 -> N32[label="",style=dashed];
N37 -> N33[label="",style=dashed];
N37 -> N36[label=""];
N37 -> N38[label=""];
N37 -> N39[label=""];
N37 -> N40[label=""];
N37 -> N41[label="",style=dashed];
N38 -> N32[label=""];
N38 -> N33[label=""];
N38 -> N39[label=""];
N38 -> N42[label=""];
N39 -> N17[label=""];
N39 -> N20[label=""];
N39 -> N32[label=""];
N39 -> N33[label=""];
N39 -> N42[label=""];
N40 -> N14[label=""];
N40 -> N17[label=""];
N40 -> N20[label=""];
N40 -> N44[label=""];
N40 -> N45[label=""];
N40 -> N46[label=""];
N42 -> N33[label=""];
N42 -> N34[label=""];
N43 -> N1[label="",style=dashed];
N43 -> N6[label="",style=dashed];
N43 -> N16[label="",style=dashed];
N43 -> N19[label="",style=dashed];
N44 -> N45[label=""];
N47 -> N20[label="",style=dashed];
N47 -> N32[label="",style=dashed];
N47 -> N33[label="",style=dashed];
N48 -> N30[label="",style=dashed];
N48 -> N31[label="",style=dashed];
N48 -> N37[label="",style=dashed];
N48 -> N41[label="",style=dashed];
N48 -> N49[label="",style=dashed];
N30 -> N32[label="",style=dashed];
N32 -> N23[label="",style=dashed];
N34 -> N35[label="",style=dashed];
N34 -> N36[label="",style=dashed];
N39 -> N23[label="",style=dashed];
N39 -> N33[label="",style=dashed];
N39 -> N34[label="",style=dashed];
N39 -> N35[label="",style=dashed];
N39 -> N38[label="",style=dashed];
N39 -> N40[label="",style=dashed];
N39 -> N41[label="",style=dashed];
N39 -> N42[label="",style=dashed];
N39 -> N43[label="",style=dashed];
N40 -> N34[label="",style=dashed];
N40 -> N35[label="",style=dashed];
N40 -> N41[label="",style=dashed];
N40 -> N44[label="",style=dashed];
N41 -> N20[label="",style=dashed];
N41 -> N23[label="",style=dashed];
N41 -> N34[label="",style=dashed];
N41 -> N35[label="",style=dashed];
N41 -> N44[label="",style=dashed];
N42 -> N17[label="",style=dashed];
N42 -> N20[label="",style=dashed];
N42 -> N23[label="",style=dashed];
N42 -> N45[label="",style=dashed];
N42 -> N46[label="",style=dashed];
N42 -> N47[label="",style=dashed];
N44 -> N35[label="",style=dashed];
N44 -> N36[label="",style=dashed];
N45 -> N46[label="",style=dashed];
N48 -> N23[label="",style=dashed];
N48 -> N34[label="",style=dashed];
N48 -> N35[label="",style=dashed];
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 KiB

After

Width:  |  Height:  |  Size: 260 KiB