From 8e4ce11532afdc35357dfcdc9e95b49be29bba3a Mon Sep 17 00:00:00 2001 From: joss17 Date: Mon, 14 Dec 2009 21:20:17 +0000 Subject: [PATCH] work on the timeout for tcp and udp connection git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1884 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/pqi/p3connmgr.cc | 30 +++++++++++++++++++----------- libretroshare/src/pqi/pqissl.cc | 21 ++++++++++++--------- libretroshare/src/pqi/pqissl.h | 4 ++-- libretroshare/src/pqi/pqissludp.cc | 22 ++++++++++++++++++---- 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/libretroshare/src/pqi/p3connmgr.cc b/libretroshare/src/pqi/p3connmgr.cc index d31b86bbd..b24a48f5e 100644 --- a/libretroshare/src/pqi/p3connmgr.cc +++ b/libretroshare/src/pqi/p3connmgr.cc @@ -73,7 +73,7 @@ const uint32_t PEER_IP_CONNECT_STATE_MAX_LIST_SIZE = 6; const uint32_t P3CONNMGR_TCP_DEFAULT_DELAY = 2; /* 2 Seconds? is it be enough! */ const uint32_t P3CONNMGR_UDP_DEFAULT_DELAY = 2; /* 2 Seconds? is it be enough! */ -const uint32_t P3CONNMGR_UDP_DEFAULT_TIMEOUT = 40; //a random timeout is set between P3CONNMGR_UDP_DEFAULT_TIMEOUT and 2 * P3CONNMGR_UDP_DEFAULT_TIMEOUT in the implementation +const uint32_t P3CONNMGR_UDP_DEFAULT_PERIOD = 20; //a random timeout is set between P3CONNMGR_UDP_DEFAULT_PERIOD and 2 * P3CONNMGR_UDP_DEFAULT_PERIOD in the implementation #define MAX_AVAIL_PERIOD 180 //times a peer stay in available state when not connected #define MIN_RETRY_PERIOD 90 @@ -2228,7 +2228,7 @@ bool p3ConnectMgr::retryConnectTCP(std::string id) #ifdef CONN_DEBUG std::cerr << "Adding tcp connection attempt list." << std::endl; #endif - peerConnectAddress pca; + peerConnectAddress pca; pca.addr = ipListIt->ipAddr; pca.type = RS_NET_CONN_TCP_UNKNOW_TOPOLOGY; pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY; @@ -2276,7 +2276,7 @@ bool p3ConnectMgr::retryConnectTCP(std::string id) pca.delay = P3CONNMGR_UDP_DEFAULT_DELAY; pca.ts = time(NULL); // pseudo random number generator from Wikipedia/Numerical Recipies. - pca.period = P3CONNMGR_UDP_DEFAULT_TIMEOUT + ((time(NULL)*1664525 + 1013904223) % P3CONNMGR_UDP_DEFAULT_TIMEOUT); //add a random timeout between 1 and 2 times P3CONNMGR_UDP_DEFAULT_TIMEOUT + pca.period = P3CONNMGR_UDP_DEFAULT_PERIOD + ((time(NULL)*1664525 + 1013904223) % P3CONNMGR_UDP_DEFAULT_PERIOD); //add a random period between 1 and 2 times P3CONNMGR_UDP_DEFAULT_PERIOD it->second.connAddrs.push_back(pca); #ifdef CONN_DEBUG @@ -2506,14 +2506,22 @@ bool p3ConnectMgr::setAddressList(std::string id, std::list I && (ipListIt->ipAddr.sin_addr.s_addr != 0) && (!isLoopbackNet(&ipListIt->ipAddr.sin_addr)) ) { - //the pointer ipListIt is pointing to an external address - #ifdef CONN_DEBUG - std::cerr << "p3ConnectMgr::setAddressList() setting own ext adress from p3disc : "; - std::cerr << inet_ntoa(ipListIt->ipAddr.sin_addr) << ":" << ntohs(ipListIt->ipAddr.sin_port) << " seenTime : " << ipListIt->seenTime << std::endl; - #endif - - setExtAddress(getOwnId(), ipListIt->ipAddr); - break; + //Let's check if the address is not to old + if ((time(NULL) - ipListIt->seenTime) < P3CONNMGR_UDP_DEFAULT_PERIOD) { + //the pointer ipListIt is pointing to an external address + #ifdef CONN_DEBUG + std::cerr << "p3ConnectMgr::setAddressList() setting own ext adress from p3disc : "; + std::cerr << inet_ntoa(ipListIt->ipAddr.sin_addr) << ":" << ntohs(ipListIt->ipAddr.sin_port) << " seenTime : " << ipListIt->seenTime << std::endl; + #endif + setExtAddress(getOwnId(), ipListIt->ipAddr); + break; + } else { + #ifdef CONN_DEBUG + std::cerr << "p3ConnectMgr::setAddressList() own ext adress from p3disc is too old to be used : "; + std::cerr << inet_ntoa(ipListIt->ipAddr.sin_addr) << ":" << ntohs(ipListIt->ipAddr.sin_port) << " seenTime : " << ipListIt->seenTime << std::endl; + #endif + break; + } } } diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index 97ed76d20..4ef60fcac 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -62,7 +62,6 @@ const int PQISSL_UDP_FLAG = 0x02; ***********/ static const int PQISSL_MAX_READ_ZERO_COUNT = 20; -static const int PQISSL_SSL_CONNECT_TIMEOUT = 30; /********** PQI SSL STUFF ****************************************** * @@ -94,7 +93,6 @@ pqissl::pqissl(pqissllistener *l, PQInterface *parent, p3AuthMgr *am, p3ConnectM sslmode(PQISSL_ACTIVE), ssl_connection(NULL), sockfd(-1), pqil(l), // no init for remote_addr. readpkt(NULL), pktlen(0), - attempt_ts(0), net_attempt(0), net_failure(0), net_unreachable(0), sameLAN(false), n_read_zero(0), mConnectDelay(0), mConnectTS(0), @@ -241,6 +239,7 @@ int pqissl::reset() sameLAN = false; n_read_zero = 0; total_len = 0 ; + mTimeoutTS = 0; if (neededReset) { @@ -369,7 +368,15 @@ int pqissl::tick() out << "Continuing Connection Attempt!"; rslog(RSL_DEBUG_BASIC, pqisslzone, out.str()); - ConnectAttempt(); + if (mTimeoutTS != 0 && time(NULL) > mTimeoutTS) + { + rslog(RSL_DEBUG_BASIC, pqisslzone, + "pqissl::Authorise_SSL_Connection() Connection Timed Out!"); + /* as sockfd is valid, this should close it all up */ + reset(); + } else { + ConnectAttempt(); + } return 1; } } @@ -928,9 +935,6 @@ int pqissl::Initiate_SSL_Connection() rslog(RSL_DEBUG_BASIC, pqisslzone, "pqissl::Initiate_SSL_Connection() Basic Connection Okay"); - // setup timeout value. - ssl_connect_timeout = time(NULL) + PQISSL_SSL_CONNECT_TIMEOUT; - // Perform SSL magic. // library already inited by sslroot(). SSL *ssl = SSL_new(mAuthMgr->getCTX()); @@ -1095,10 +1099,9 @@ int pqissl::Extract_Failed_SSL_Certificate() int pqissl::Authorise_SSL_Connection() { rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::Authorise_SSL_Connection()"); - ssl_connect_timeout = time(NULL) + PQISSL_SSL_CONNECT_TIMEOUT; + "pqissl::Authorise_SSL_Connection()"); - if (time(NULL) > ssl_connect_timeout) + if (time(NULL) > mTimeoutTS) { rslog(RSL_DEBUG_BASIC, pqisslzone, "pqissl::Authorise_SSL_Connection() Connection Timed Out!"); diff --git a/libretroshare/src/pqi/pqissl.h b/libretroshare/src/pqi/pqissl.h index 9e886d50c..6269432ce 100644 --- a/libretroshare/src/pqi/pqissl.h +++ b/libretroshare/src/pqi/pqissl.h @@ -184,7 +184,7 @@ virtual int net_internal_fcntl_nonblock(int fd) { return unix_fcntl_nonblock(fd) int pktlen; int total_len ; // saves the reading state accross successive calls. - int attempt_ts; + //int attempt_ts; // Some flags to indicate // the status of the various interfaces @@ -197,7 +197,7 @@ virtual int net_internal_fcntl_nonblock(int fd) { return unix_fcntl_nonblock(fd) int n_read_zero; /* a counter to determine if the connection is really dead */ - int ssl_connect_timeout; /* timeout to ensure that we don't get stuck (can happen on udp!) */ + //int ssl_connect_timeout; /* timeout to ensure that we don't get stuck (can happen on udp!) */ uint32_t mConnectDelay; time_t mConnectTS; diff --git a/libretroshare/src/pqi/pqissludp.cc b/libretroshare/src/pqi/pqissludp.cc index a97276534..b67c5f899 100644 --- a/libretroshare/src/pqi/pqissludp.cc +++ b/libretroshare/src/pqi/pqissludp.cc @@ -253,7 +253,7 @@ int pqissludp::Basic_Connection_Complete() { std::ostringstream out; out << "pqissludp::Basic_Connection_Complete() Connection Timed Out. "; - out << "Peer: " << PeerId() << " Period: "; + out << "Peer: " << PeerId() << " Timeout: "; out << mConnectTimeout; rslog(RSL_WARNING, pqissludpzone, out.str()); @@ -411,11 +411,25 @@ bool pqissludp::connect_parameter(uint32_t type, uint32_t value) std::ostringstream out; out << "pqissludp::connect_parameter() Peer: " << PeerId() << " PERIOD: " << value; rslog(RSL_WARNING, pqissludpzone, out.str()); - mConnectPeriod = value; return true; - } - return pqissl::connect_parameter(type, value); + }else if (type == NET_PARAM_CONNECT_DELAY) + { + std::ostringstream out; + out << "pqissludp::connect_parameter() Peer: " << PeerId() << " DELAY: " << value; + rslog(RSL_WARNING, pqissludpzone, out.str()); + mConnectDelay = value; + return true; + } + else if (type == NET_PARAM_CONNECT_TIMEOUT) + { + std::ostringstream out; + out << "pqissludp::connect_parameter() Peer: " << PeerId() << " TIMEOUT: " << value; + rslog(RSL_WARNING, pqissludpzone, out.str()); + mConnectTimeout = value; + return true; + } + return pqissl::connect_parameter(type, value); } /********** PQI STREAMER OVERLOADING *********************************/