Skip to content

Commit

Permalink
explicit read_timeout and write_timeout during hand/shake
Browse files Browse the repository at this point in the history
  • Loading branch information
antiochp committed Feb 26, 2020
1 parent 8a15007 commit d6c4dbd
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions p2p/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use rand::{thread_rng, Rng};
use std::collections::VecDeque;
use std::net::{SocketAddr, TcpStream};
use std::sync::Arc;
use std::time::Duration;

/// Local generated nonce for peer connecting.
/// Used for self-connecting detection (on receiver side),
Expand All @@ -34,6 +35,9 @@ const NONCES_CAP: usize = 100;
/// 10 should be enough since most of servers don't have more than 10 IP addresses.
const ADDRS_CAP: usize = 10;

/// The read_timeout and write_timeout for initial hand/shake messages.
const HANDSHAKE_TIMEOUT: Duration = Duration::from_millis(2_000);

/// Handles the handshake negotiation when two peers connect and decides on
/// protocol.
pub struct Handshake {
Expand Down Expand Up @@ -83,6 +87,11 @@ impl Handshake {
self_addr: PeerAddr,
conn: &mut TcpStream,
) -> Result<PeerInfo, Error> {
// Set explicit timeouts on the tcp stream for hand/shake messages.
// Once the peer is up and running we will set new values for these.
let _ = conn.set_read_timeout(Some(HANDSHAKE_TIMEOUT));
let _ = conn.set_write_timeout(Some(HANDSHAKE_TIMEOUT));

// prepare the first part of the handshake
let nonce = self.next_nonce();
let peer_addr = match conn.peer_addr() {
Expand Down Expand Up @@ -148,6 +157,11 @@ impl Handshake {
total_difficulty: Difficulty,
conn: &mut TcpStream,
) -> Result<PeerInfo, Error> {
// Set explicit timeouts on the tcp stream for hand/shake messages.
// Once the peer is up and running we will set new values for these.
let _ = conn.set_read_timeout(Some(HANDSHAKE_TIMEOUT));
let _ = conn.set_write_timeout(Some(HANDSHAKE_TIMEOUT));

let hand: Hand = read_message(conn, self.protocol_version, Type::Hand)?;

// all the reasons we could refuse this connection for
Expand Down

0 comments on commit d6c4dbd

Please sign in to comment.