Skip to content
This repository was archived by the owner on Dec 10, 2022. It is now read-only.

Commit 457d7e7

Browse files
authored
Merge pull request #34 from tox-rs/fix_dns_resolving
Ignore bootstrap nodes with invalid address
2 parents 91ce31f + b346ac7 commit 457d7e7

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/cli_config.rs

+31-8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ arg_enum! {
5454
}
5555
}
5656

57+
/// Bootstrap node with generic string address which might be either IP address
58+
/// or DNS name.
59+
#[derive(Clone, PartialEq, Eq, Debug)]
60+
pub struct BootstrapNode {
61+
/// `PublicKey` of the node.
62+
pk: PublicKey,
63+
/// Generic string address which might be either IP address or DNS name.
64+
addr: String,
65+
}
66+
67+
impl BootstrapNode {
68+
/// Resolve string address of the node to possible multiple `SocketAddr`s.
69+
pub fn resolve(&self) -> impl Iterator<Item = PackedNode> {
70+
let pk = self.pk;
71+
let addrs = match self.addr.to_socket_addrs() {
72+
Ok(addrs) => addrs,
73+
Err(e) => {
74+
warn!("Failed to resolve bootstrap node address '{}': {}", self.addr, e);
75+
Vec::new().into_iter()
76+
},
77+
};
78+
addrs.map(move |addr| PackedNode::new(addr, &pk))
79+
}
80+
}
81+
5782
/// Config parsed from command line arguments.
5883
#[derive(Clone, PartialEq, Eq, Debug)]
5984
pub struct CliConfig {
@@ -70,7 +95,7 @@ pub struct CliConfig {
7095
/// Path to the file where DHT keys are stored.
7196
pub keys_file: Option<String>,
7297
/// List of bootstrap nodes.
73-
pub bootstrap_nodes: Vec<PackedNode>,
98+
pub bootstrap_nodes: Vec<BootstrapNode>,
7499
/// Number of threads for execution.
75100
pub threads_config: ThreadsConfig,
76101
/// Specifies where to write logs.
@@ -192,18 +217,16 @@ pub fn cli_parse() -> CliConfig {
192217
.into_iter()
193218
.flat_map(|values| values)
194219
.tuples()
195-
.map(|(pk, saddr)| {
220+
.map(|(pk, addr)| {
196221
// get PK bytes of the bootstrap node
197222
let bootstrap_pk_bytes: [u8; 32] = FromHex::from_hex(pk).expect("Invalid node key");
198223
// create PK from bytes
199224
let bootstrap_pk = PublicKey::from_slice(&bootstrap_pk_bytes).expect("Invalid node key");
200225

201-
let saddr = saddr
202-
.to_socket_addrs()
203-
.expect("Invalid node address")
204-
.next()
205-
.expect("Invalid node address");
206-
PackedNode::new(saddr, &bootstrap_pk)
226+
BootstrapNode {
227+
pk: bootstrap_pk,
228+
addr: addr.to_owned(),
229+
}
207230
})
208231
.collect();
209232

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ fn run_udp(cli_config: &CliConfig, dht_pk: PublicKey, dht_sk: &SecretKey, udp_on
252252
warn!("No bootstrap nodes!");
253253
}
254254

255-
for &node in &cli_config.bootstrap_nodes {
255+
for node in cli_config.bootstrap_nodes.iter().flat_map(|node| node.resolve()) {
256256
server.add_initial_bootstrap(node);
257257
}
258258

0 commit comments

Comments
 (0)