From 89da6105aa4c94e563661a0c684d457c5427c54e Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Mon, 13 Aug 2012 11:19:25 +0300 Subject: [PATCH] When not connected, (1) client hops port numbers (2) server stops sending. --- src/frontend/mosh-server.cc | 4 ++-- src/network/network.cc | 27 +++++++++++++++++++++++++++ src/network/network.h | 6 ++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/frontend/mosh-server.cc b/src/frontend/mosh-server.cc index 3e3936d..711bba7 100644 --- a/src/frontend/mosh-server.cc +++ b/src/frontend/mosh-server.cc @@ -526,7 +526,7 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network const int timeout_if_no_client = 60000; int timeout = min( network.wait_time(), terminal.wait_time( now ) ); - if ( !network.has_remote_addr() ) { + if ( !network.get_remote_state_num() ) { timeout = min( timeout, timeout_if_no_client ); } @@ -696,7 +696,7 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network } } - if ( !network.has_remote_addr() + if ( !network.get_remote_state_num() && time_since_remote_state >= uint64_t( timeout_if_no_client ) ) { fprintf( stderr, "No connection within %d seconds.\n", timeout_if_no_client / 1000 ); diff --git a/src/network/network.cc b/src/network/network.cc index 2e9db55..a5cc510 100644 --- a/src/network/network.cc +++ b/src/network/network.cc @@ -107,6 +107,20 @@ Packet Connection::new_packet( string &s_payload ) return p; } +void Connection::reset( void ) +{ + if ( server ) { + has_remote_addr = false; + } else { /* client */ + if ( close( sock ) < 0 ) { + throw NetworkException( "close", errno ); + } + + setup(); + last_association = timestamp(); + } +} + void Connection::setup( void ) { /* create socket */ @@ -144,6 +158,8 @@ Connection::Connection( const char *desired_ip, const char *desired_port ) /* se saved_timestamp( -1 ), saved_timestamp_received_at( 0 ), expected_receiver_seq( 0 ), + last_heard( -1 ), + last_association( -1 ), RTT_hit( false ), SRTT( 1000 ), RTTVAR( 500 ), @@ -253,6 +269,8 @@ Connection::Connection( const char *key_str, const char *ip, int port ) /* clien saved_timestamp( -1 ), saved_timestamp_received_at( 0 ), expected_receiver_seq( 0 ), + last_heard( -1 ), + last_association( -1 ), RTT_hit( false ), SRTT( 1000 ), RTTVAR( 500 ), @@ -272,6 +290,7 @@ Connection::Connection( const char *key_str, const char *ip, int port ) /* clien } has_remote_addr = true; + last_association = timestamp(); } void Connection::send( string s ) @@ -294,6 +313,13 @@ void Connection::send( string s ) have_send_exception = true; send_exception = NetworkException( "sendto", errno ); } + + /* association timeout */ + uint64_t now = timestamp(); + if ( ( now - last_association > ASSOCIATION_TIMEOUT ) + && ( now - last_heard > ASSOCIATION_TIMEOUT ) ) { + reset(); + } } string Connection::recv( void ) @@ -351,6 +377,7 @@ string Connection::recv( void ) /* auto-adjust to remote host */ has_remote_addr = true; + last_heard = last_association = timestamp(); if ( server ) { /* only client can roam */ if ( (remote_addr.sin_addr.s_addr != packet_remote_addr.sin_addr.s_addr) diff --git a/src/network/network.h b/src/network/network.h index 5219f27..cc6a450 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -91,6 +91,8 @@ namespace Network { static const int PORT_RANGE_LOW = 60001; static const int PORT_RANGE_HIGH = 60999; + static const unsigned int ASSOCIATION_TIMEOUT = 20000; /* ms */ + static bool try_bind( int socket, uint32_t addr, int port ); int sock; @@ -112,6 +114,8 @@ namespace Network { uint64_t saved_timestamp_received_at; uint64_t expected_receiver_seq; + uint64_t last_heard, last_association; + bool RTT_hit; double SRTT; double RTTVAR; @@ -123,6 +127,8 @@ namespace Network { Packet new_packet( string &s_payload ); + void reset( void ); + public: Connection( const char *desired_ip, const char *desired_port ); /* server */ Connection( const char *key_str, const char *ip, int port ); /* client */