seednodes dns lookup (#34)

* fixed compile error after merging 29 & 30

* dns lookup

* connect to seednode
This commit is contained in:
Marek Kotewicz 2016-10-25 09:55:43 +02:00 committed by GitHub
parent fa4cd03713
commit 6ac07659b6
6 changed files with 87 additions and 6 deletions

43
Cargo.lock generated
View File

@ -14,6 +14,16 @@ dependencies = [
"verification 0.1.0",
]
[[package]]
name = "abstract-ns"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"futures 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aho-corasick"
version = "0.5.3"
@ -124,6 +134,17 @@ dependencies = [
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "domain"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "elastic-array"
version = "0.5.0"
@ -328,6 +349,17 @@ dependencies = [
"odds 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ns-dns-tokio"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"abstract-ns 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"domain 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num_cpus"
version = "0.2.13"
@ -358,12 +390,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "p2p"
version = "0.1.0"
dependencies = [
"abstract-ns 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"bitcrypto 0.1.0",
"chain 0.1.0",
"futures 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"futures-cpupool 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"message 0.1.0",
"ns-dns-tokio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"primitives 0.1.0",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
@ -400,6 +434,11 @@ dependencies = [
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quick-error"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.3.14"
@ -659,6 +698,7 @@ version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum abstract-ns 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "179c25175ddfb957808aab46c48dd7d0c66a366bff79363813b527447c9f620c"
"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
"checksum arrayvec 0.3.20 (registry+https://github.com/rust-lang/crates.io-index)" = "d89f1b0e242270b5b797778af0c8d182a1a2ccac5d8d6fadf414223cc0fab096"
@ -670,6 +710,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum clap 2.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5fa304b03c49ccbb005784fc26e985b5d2310b1d37f2c311ce90dbcd18ea5fde"
"checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97"
"checksum deque 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1614659040e711785ed8ea24219140654da1729f3ec8a47a9719d041112fe7bf"
"checksum domain 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "725459994103308a8476a95d8115280b1359dccc06ca14291df75f37459a9e30"
"checksum elastic-array 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4bc9250a632e7c001b741eb0ec6cee93c9a5b6d5f1879696a4b94d62b012210a"
"checksum env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f"
"checksum eth-secp256k1 0.5.6 (git+https://github.com/ethcore/rust-secp256k1)" = "<none>"
@ -689,12 +730,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2"
"checksum nix 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a7bb1da2be7da3cbffda73fc681d509ffd9e665af478d2bee1907cee0bc64b2"
"checksum nodrop 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0dbbadd3f4c98dea0bd3d9b4be4c0cdaf1ab57035cb2e41fce3983db5add7cc5"
"checksum ns-dns-tokio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "43330aab5077c311b390b62147feb44316cb5b754b97d28c92210e6c6b7baff7"
"checksum num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "cee7e88156f3f9e19bdd598f8d6c9db7bf4078f99f8381f43a55b09648d1a6e3"
"checksum num_cpus 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8890e6084723d57d0df8d2720b0d60c6ee67d6c93e7169630e4371e88765dcad"
"checksum odds 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)" = "e04630a62b3f1cc8c58b4d8f2555a40136f02b420e158242936ef286a72d33a0"
"checksum owning_ref 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8d91377085359426407a287ab16884a0111ba473aa6844ff01d4ec20ce3d75e7"
"checksum parking_lot 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e1435e7a2a00dfebededd6c6bdbd54008001e94b4a2aadd6aef0dc4c56317621"
"checksum parking_lot_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fb1b97670a2ffadce7c397fb80a3d687c4f3060140b885621ef1653d0e5d5068"
"checksum quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0aad603e8d7fb67da22dbdf1f4b826ce8829e406124109e73cf1b2454b93a71c"
"checksum rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2791d88c6defac799c3f20d74f094ca33b9332612d9aef9078519c82e4fe04a5"
"checksum rayon 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "655df67c314c30fa3055a365eae276eb88aa4f3413a352a1ab32c1320eda41ea"
"checksum regex 0.1.77 (registry+https://github.com/rust-lang/crates.io-index)" = "64b03446c466d35b42f2a8b203c8e03ed8b91c0f17b56e1f84f7210a257aa665"

View File

@ -11,6 +11,8 @@ futures-cpupool = "0.1"
time = "0.1"
rand = "0.3"
log = "0.3"
abstract-ns = "0.2.1"
ns-dns-tokio = "0.1.0"
primitives = { path = "../primitives" }
bitcrypto = { path = "../crypto" }

View File

@ -18,5 +18,5 @@ pub struct Config {
/// Connect only ot these nodes.
pub peers: Vec<IpAddr>,
/// Connect to these nodes to retrieve peer addresses, and disconnect.
pub seeds: Vec<IpAddr>,
pub seeds: Vec<String>,
}

View File

@ -7,6 +7,8 @@ extern crate tokio_core;
extern crate parking_lot;
#[macro_use]
extern crate log;
extern crate abstract_ns;
extern crate ns_dns_tokio;
extern crate chain;
extern crate bitcrypto as crypto;

View File

@ -1,4 +1,4 @@
use std::{io, net};
use std::{io, net, error};
use std::sync::Arc;
use parking_lot::RwLock;
use futures::{Future, finished, failed, BoxFuture};
@ -6,6 +6,8 @@ use futures::stream::Stream;
use futures_cpupool::CpuPool;
use tokio_core::io::IoFuture;
use tokio_core::reactor::{Handle, Remote};
use abstract_ns::Resolver;
use ns_dns_tokio::DnsResolver;
use message::{Payload, MessageResult};
use protocol::Direction;
use net::{connect, listen, Connections, Channel, Config as NetConfig};
@ -235,13 +237,14 @@ impl P2P {
}
}
pub fn run(&self) -> Result<(), io::Error> {
pub fn run(&self) -> Result<(), Box<error::Error>> {
for peer in self.config.peers.iter() {
self.connect::<NormalSessionFactory>(*peer);
}
let resolver = try!(DnsResolver::system_config(&self.event_loop_handle));
for seed in self.config.seeds.iter() {
self.connect::<SeednodeSessionFactory>(*seed);
self.connect_to_seednode(&resolver, seed);
}
try!(self.listen());
@ -255,7 +258,38 @@ impl P2P {
self.event_loop_handle.spawn(pool_work);
}
fn listen(&self) -> Result<(), io::Error> {
pub fn connect_to_seednode(&self, resolver: &Resolver, seednode: &str) {
let owned_seednode = seednode.to_owned();
let context = self.context.clone();
let remote = self.event_loop_handle.remote().clone();
let connection_config = self.config.connection.clone();
let dns_lookup = resolver.resolve(seednode).then(move |result| {
match result {
Ok(address) => match address.pick_one() {
Some(socket) => {
trace!("Dns lookup of seednode {} finished. Connecting to {}", owned_seednode, socket);
remote.spawn(move |handle| {
let connection = Context::connect::<SeednodeSessionFactory>(context.clone(), socket, handle, &connection_config);
context.spawn(connection);
Ok(())
});
},
None => {
trace!("Dns lookup of seednode {} resolved with no results", owned_seednode);
}
},
Err(_err) => {
trace!("Dns lookup of seednode {} failed", owned_seednode);
}
}
finished(())
});
let pool_work = self.pool.spawn(dns_lookup);
self.event_loop_handle.spawn(pool_work);
}
fn listen(&self) -> Result<(), Box<error::Error>> {
let server = try!(Context::listen(self.context.clone(), &self.event_loop_handle, self.config.connection.clone()));
let pool_work = self.pool.spawn(server);
self.event_loop_handle.spawn(pool_work);

View File

@ -6,7 +6,7 @@ pub struct Config {
pub magic: Magic,
pub port: u16,
pub connect: Option<net::IpAddr>,
pub seednode: Option<net::IpAddr>,
pub seednode: Option<String>,
pub print_to_console: bool,
}