From ed7f958f95edb44bc615b4bfa84345b11cd930ba Mon Sep 17 00:00:00 2001 From: Gio Date: Thu, 31 Dec 2015 15:42:22 +0100 Subject: [PATCH] Add stacktrave.h, sanitize p3LinkMgrIMPL::retryConnectTCP avoid to look for port on null sockaddr_storage --- libretroshare/src/libretroshare.pro | 3 +- libretroshare/src/pqi/p3linkmgr.cc | 124 +++++++++------------------- libretroshare/src/pqi/p3peermgr.cc | 4 +- libretroshare/src/util/rsnet_ss.cc | 13 ++- 4 files changed, 46 insertions(+), 98 deletions(-) diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index bdb7af945..c6e64cd7e 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -494,7 +494,8 @@ HEADERS += util/folderiterator.h \ util/rsmemcache.h \ util/rstickevent.h \ util/rsrecogn.h \ - util/rsscopetimer.h + util/rsscopetimer.h \ + util/stacktrace.h SOURCES += dbase/cachestrapper.cc \ dbase/fimonitor.cc \ diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index d7262bd84..9662b6e64 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -1571,140 +1571,90 @@ bool p3LinkMgrIMPL::tryConnectUDP(const RsPeerId &id, const struct sockaddr_st - -bool p3LinkMgrIMPL::retryConnectTCP(const RsPeerId &id) +/* push all available addresses onto the connect addr stack... + * with the following exceptions: + * - id is our own + * - id is not our friend + * - id is already connected + * - id is hidden but of an unkown type + * - we are hidden but id is not + */ +bool p3LinkMgrIMPL::retryConnectTCP(const RsPeerId &id) { - /* Check if we should retry first */ - { - RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ - - /* push all available addresses onto the connect addr stack... - * with the following exceptions: - * - check local address, see if it is the same network as us - - check address age. don't add old ones - */ - #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::retryConnectTCP() id: " << id << std::endl; #endif - - if (id == getOwnId()) - { -#ifdef LINKMGR_DEBUG - rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::retryConnectTCP() Failed, connecting to own id: "); -#endif - return false; - } - - /* look up the id */ - std::map::iterator it; - if (mFriendList.end() == (it = mFriendList.find(id))) - { -#ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Peer is not Friend" << std::endl; -#endif - return false; - } - - /* if already connected -> done */ - if (it->second.state & RS_PEER_S_CONNECTED) - { -#ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Peer Already Connected" << std::endl; -#endif - return false; - } - } /****** END of LOCKED ******/ -#ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Getting Address from PeerMgr for : " << id; - std::cerr << std::endl; -#endif - /* If we reach here, must retry .... extract the required info from p3PeerMgr */ + if (id == getOwnId()) return false; - /* first possibility - is it a hidden peer */ + { + RS_STACK_MUTEX(mLinkMtx); + std::map::iterator it = mFriendList.find(id); + if ( it == mFriendList.end() ) return false; + if ( it->second.state & RS_PEER_S_CONNECTED ) return false; + } + + // Extract the required info from p3PeerMgr + + // first possibility - is it a hidden peer if (mPeerMgr->isHiddenPeer(id)) { /* check for valid hidden type */ uint32_t type = mPeerMgr->getHiddenType(id); - if (type & (~RS_HIDDEN_TYPE_MASK)) - { -#ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::retryConnectTCP() invalid hidden type (" << type << ") -> return false"; - std::cerr << std::endl; -#endif - return false; - } + if ( type & (~RS_HIDDEN_TYPE_MASK) ) return false; + /* then we just have one connect attempt via the Proxy */ struct sockaddr_storage proxy_addr; std::string domain_addr; uint16_t domain_port; - - /* then we just have one connect attempt via the Proxy */ - if (mPeerMgr->getProxyAddress(id, proxy_addr, domain_addr, domain_port)) + if ( mPeerMgr->getProxyAddress(id, proxy_addr, domain_addr, domain_port) ) { - RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ - - std::map::iterator it; - if (mFriendList.end() != (it = mFriendList.find(id))) + RS_STACK_MUTEX(mLinkMtx); + std::map::iterator it = mFriendList.find(id); + if (it != mFriendList.end()) { locked_ConnectAttempt_ProxyAddress(&(it->second), type, proxy_addr, domain_addr, domain_port); return locked_ConnectAttempt_Complete(&(it->second)); } } + return false; } - if (mPeerMgr->isHidden()) - { -#ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::retryConnectTCP() isHidden(): no connection attempts for : " << id; - std::cerr << std::endl; -#endif - return false; - } + if (mPeerMgr->isHidden()) return false; struct sockaddr_storage lAddr; struct sockaddr_storage eAddr; pqiIpAddrSet histAddrs; std::string dyndns; - if (mPeerMgr->getConnectAddresses(id, lAddr, eAddr, histAddrs, dyndns)) { - RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mLinkMtx); - std::map::iterator it; - if (mFriendList.end() != (it = mFriendList.find(id))) + std::map::iterator it = mFriendList.find(id); + if ( it != mFriendList.end() ) { locked_ConnectAttempt_CurrentAddresses(&(it->second), lAddr, eAddr); - uint16_t dynPort = sockaddr_storage_port(eAddr); - if (!dynPort) + uint16_t dynPort = 0; + if (!sockaddr_storage_isnull(eAddr)) dynPort = sockaddr_storage_port(eAddr); + if (!dynPort && !sockaddr_storage_isnull(lAddr)) dynPort = sockaddr_storage_port(lAddr); if (dynPort) - { locked_ConnectAttempt_AddDynDNS(&(it->second), dyndns, dynPort); - } locked_ConnectAttempt_HistoricalAddresses(&(it->second), histAddrs); - /* finish it off */ + // finish it off return locked_ConnectAttempt_Complete(&(it->second)); } else - { - std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to find friend data : " << id; - std::cerr << std::endl; - } + std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to find friend data : " << id << std::endl; } else - { - std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to addresses from PeerMgr for: " << id; - std::cerr << std::endl; - } + std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to get addresses from PeerMgr for: " << id << std::endl; return false; - } diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index 79978277c..e99d302c6 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -379,9 +379,9 @@ bool p3PeerMgrIMPL::getGpgId(const RsPeerId &ssl_id, RsPgpId &gpgId) /**** HIDDEN STUFF ****/ -bool p3PeerMgrIMPL::isHidden() +bool p3PeerMgrIMPL::isHidden() { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mPeerMtx); return mOwnState.hiddenNode; } diff --git a/libretroshare/src/util/rsnet_ss.cc b/libretroshare/src/util/rsnet_ss.cc index 1ca56f7ea..dec7776af 100644 --- a/libretroshare/src/util/rsnet_ss.cc +++ b/libretroshare/src/util/rsnet_ss.cc @@ -29,6 +29,7 @@ #include "util/rsnet.h" #include "util/rsstring.h" #include "pqi/pqinetwork.h" +#include "util/stacktrace.h" /***************************** Internal Helper Fns ******************************/ @@ -186,13 +187,12 @@ uint16_t sockaddr_storage_port(const struct sockaddr_storage &addr) { case AF_INET: return sockaddr_storage_ipv4_port(addr); - break; case AF_INET6: return sockaddr_storage_ipv6_port(addr); - break; default: std::cerr << "sockaddr_storage_port() invalid addr.ss_family" << std::endl; sockaddr_storage_dump(addr); + print_stacktrace(); break; } return 0; @@ -508,22 +508,19 @@ bool sockaddr_storage_isnull(const struct sockaddr_storage &addr) bool sockaddr_storage_isValidNet(const struct sockaddr_storage &addr) { #ifdef SS_DEBUG - std::cerr << "sockaddr_storage_isValidNet()"; - std::cerr << std::endl; + std::cerr << "sockaddr_storage_isValidNet()" << std::endl; #endif switch(addr.ss_family) { case AF_INET: return sockaddr_storage_ipv4_isValidNet(addr); - break; case AF_INET6: return sockaddr_storage_ipv6_isValidNet(addr); - break; default: #ifdef SS_DEBUG - std::cerr << "sockaddr_storage_isValidNet() INVALID Family - error: " << sockaddr_storage_iptostring(addr); - std::cerr << std::endl; + std::cerr << "sockaddr_storage_isValidNet() INVALID Family" << std::endl; + sockaddr_storage_dump(addr); #endif break; }