2
0
mirror of https://github.com/Revertron/Alfis synced 2024-11-07 09:20:31 +00:00

Implemented peer domains resolve.

This commit is contained in:
Revertron 2021-02-21 01:21:40 +01:00
parent 483be5215a
commit 0847b7b8a6
2 changed files with 22 additions and 15 deletions

View File

@ -6,7 +6,8 @@
"listen": "[::]:4244",
"public": true,
"peers": [
"[200:6ad5:e516:962b:2928:959d:1ab4:8a97]:4244"
"test-ip4.alfis.name:4244",
"test-ip6.alfis.name:4244"
],
"dns": {
"port": 53,

View File

@ -2,7 +2,7 @@ extern crate serde;
extern crate serde_json;
use std::{io, thread};
use std::io::{Read, Write};
use std::io::{Read, Write, Error};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
@ -13,7 +13,7 @@ use mio::net::{TcpListener, TcpStream};
use log::{trace, debug, info, warn, error};
use crate::{Context, Block, p2p::Message, p2p::State, p2p::Peer, p2p::Peers};
use std::net::{SocketAddr, IpAddr, SocketAddrV4, Shutdown};
use std::net::{SocketAddr, IpAddr, SocketAddrV4, Shutdown, ToSocketAddrs};
const SERVER: Token = Token(0);
const POLL_TIMEOUT: Option<Duration> = Some(Duration::from_millis(3000));
@ -334,18 +334,24 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
/// Connecting to configured (bootstrap) peers
fn connect_peers(peers_addrs: Vec<String>, poll: &mut Poll, peers: &mut Peers, unique_token: &mut Token) {
for peer in peers_addrs.iter() {
let addr: SocketAddr = peer.parse().expect(&format!("Error parsing peer address {}", &peer));
match TcpStream::connect(addr.clone()) {
Ok(mut stream) => {
info!("Created connection to peer {}", &peer);
let token = next(unique_token);
poll.registry().register(&mut stream, token, Interest::WRITABLE).unwrap();
let mut peer = Peer::new(addr, stream, State::Connecting, false);
peer.set_public(true);
peers.add_peer(token, peer);
}
Err(e) => {
error!("Error connecting to peer {}: {}", &peer, e);
let addresses: Vec<SocketAddr> = match peer.to_socket_addrs() {
Ok(peers) => { peers.collect() }
Err(_) => { error!("Can't resolve address {}", &peer); continue; }
};
for addr in addresses {
match TcpStream::connect(addr.clone()) {
Ok(mut stream) => {
info!("Created connection to peer {}", &addr);
let token = next(unique_token);
poll.registry().register(&mut stream, token, Interest::WRITABLE).unwrap();
let mut peer = Peer::new(addr, stream, State::Connecting, false);
peer.set_public(true);
peers.add_peer(token, peer);
}
Err(e) => {
error!("Error connecting to peer {}: {}", &addr, e);
}
}
}
}