Return error when peer_addr() fails on connection

Likely issue is the connection failed right after being
established, leading `peer_addr()` to fail and the `unwrap()`
crashes the process instead of failing gracefully. Fix #293.
This commit is contained in:
Ignotus Peverell 2017-11-17 14:26:25 -05:00
parent efb41596b2
commit bb7a61d284
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211

View file

@ -16,7 +16,7 @@ use std::collections::VecDeque;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use futures::Future; use futures::{self, Future};
use rand::Rng; use rand::Rng;
use rand::os::OsRng; use rand::os::OsRng;
use tokio_core::net::TcpStream; use tokio_core::net::TcpStream;
@ -57,14 +57,19 @@ impl Handshake {
self_addr: SocketAddr, self_addr: SocketAddr,
conn: TcpStream, conn: TcpStream,
) -> Box<Future<Item = (TcpStream, ProtocolV1, PeerInfo), Error = Error>> { ) -> Box<Future<Item = (TcpStream, ProtocolV1, PeerInfo), Error = Error>> {
// prepare the first part of the handshake // prepare the first part of the handshake
let nonce = self.next_nonce(); let nonce = self.next_nonce();
let peer_addr = match conn.peer_addr() {
Ok(pa) => pa,
Err(e) => return Box::new(futures::future::err(Error::Connection(e))),
};
debug!( debug!(
LOGGER, LOGGER,
"handshake connect with nonce - {}, sender - {:?}, receiver - {:?}", "handshake connect with nonce - {}, sender - {:?}, receiver - {:?}",
nonce, nonce,
self_addr, self_addr,
conn.peer_addr().unwrap(), peer_addr,
); );
let hand = Hand { let hand = Hand {
@ -73,7 +78,7 @@ impl Handshake {
nonce: nonce, nonce: nonce,
total_difficulty: total_difficulty, total_difficulty: total_difficulty,
sender_addr: SockAddr(self_addr), sender_addr: SockAddr(self_addr),
receiver_addr: SockAddr(conn.peer_addr().unwrap()), receiver_addr: SockAddr(peer_addr),
user_agent: USER_AGENT.to_string(), user_agent: USER_AGENT.to_string(),
}; };
@ -81,7 +86,7 @@ impl Handshake {
Box::new( Box::new(
write_msg(conn, hand, Type::Hand) write_msg(conn, hand, Type::Hand)
.and_then(|conn| read_msg::<Shake>(conn)) .and_then(|conn| read_msg::<Shake>(conn))
.and_then(|(conn, shake)| { .and_then(move |(conn, shake)| {
if shake.version != 1 { if shake.version != 1 {
Err(Error::Serialization(ser::Error::UnexpectedData { Err(Error::Serialization(ser::Error::UnexpectedData {
expected: vec![PROTOCOL_VERSION as u8], expected: vec![PROTOCOL_VERSION as u8],
@ -91,7 +96,7 @@ impl Handshake {
let peer_info = PeerInfo { let peer_info = PeerInfo {
capabilities: shake.capabilities, capabilities: shake.capabilities,
user_agent: shake.user_agent, user_agent: shake.user_agent,
addr: conn.peer_addr().unwrap(), addr: peer_addr,
version: shake.version, version: shake.version,
total_difficulty: shake.total_difficulty, total_difficulty: shake.total_difficulty,
}; };