Add stacktrave.h, sanitize p3LinkMgrIMPL::retryConnectTCP avoid to look for port on null sockaddr_storage

This commit is contained in:
Gio 2015-12-31 15:42:22 +01:00
parent 69f6bd0ea9
commit ed7f958f95
4 changed files with 46 additions and 98 deletions

View File

@ -494,7 +494,8 @@ HEADERS += util/folderiterator.h \
util/rsmemcache.h \ util/rsmemcache.h \
util/rstickevent.h \ util/rstickevent.h \
util/rsrecogn.h \ util/rsrecogn.h \
util/rsscopetimer.h util/rsscopetimer.h \
util/stacktrace.h
SOURCES += dbase/cachestrapper.cc \ SOURCES += dbase/cachestrapper.cc \
dbase/fimonitor.cc \ dbase/fimonitor.cc \

View File

@ -1571,140 +1571,90 @@ bool p3LinkMgrIMPL::tryConnectUDP(const RsPeerId &id, const struct sockaddr_st
/* push all available addresses onto the connect addr stack...
bool p3LinkMgrIMPL::retryConnectTCP(const RsPeerId &id) * 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 #ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() id: " << id << std::endl; std::cerr << "p3LinkMgrIMPL::retryConnectTCP() id: " << id << std::endl;
#endif #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<RsPeerId, peerConnectState>::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 if (id == getOwnId()) return false;
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 */
/* first possibility - is it a hidden peer */ {
RS_STACK_MUTEX(mLinkMtx);
std::map<RsPeerId, peerConnectState>::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)) if (mPeerMgr->isHiddenPeer(id))
{ {
/* check for valid hidden type */ /* check for valid hidden type */
uint32_t type = mPeerMgr->getHiddenType(id); uint32_t type = mPeerMgr->getHiddenType(id);
if (type & (~RS_HIDDEN_TYPE_MASK)) if ( type & (~RS_HIDDEN_TYPE_MASK) ) return false;
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() invalid hidden type (" << type << ") -> return false";
std::cerr << std::endl;
#endif
return false;
}
/* then we just have one connect attempt via the Proxy */
struct sockaddr_storage proxy_addr; struct sockaddr_storage proxy_addr;
std::string domain_addr; std::string domain_addr;
uint16_t domain_port; uint16_t domain_port;
if ( mPeerMgr->getProxyAddress(id, proxy_addr, domain_addr, domain_port) )
/* then we just have one connect attempt via the Proxy */
if (mPeerMgr->getProxyAddress(id, proxy_addr, domain_addr, domain_port))
{ {
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ RS_STACK_MUTEX(mLinkMtx);
std::map<RsPeerId, peerConnectState>::iterator it = mFriendList.find(id);
std::map<RsPeerId, peerConnectState>::iterator it; if (it != mFriendList.end())
if (mFriendList.end() != (it = mFriendList.find(id)))
{ {
locked_ConnectAttempt_ProxyAddress(&(it->second), type, proxy_addr, domain_addr, domain_port); locked_ConnectAttempt_ProxyAddress(&(it->second), type, proxy_addr, domain_addr, domain_port);
return locked_ConnectAttempt_Complete(&(it->second)); return locked_ConnectAttempt_Complete(&(it->second));
} }
} }
return false; return false;
} }
if (mPeerMgr->isHidden()) if (mPeerMgr->isHidden()) return false;
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() isHidden(): no connection attempts for : " << id;
std::cerr << std::endl;
#endif
return false;
}
struct sockaddr_storage lAddr; struct sockaddr_storage lAddr;
struct sockaddr_storage eAddr; struct sockaddr_storage eAddr;
pqiIpAddrSet histAddrs; pqiIpAddrSet histAddrs;
std::string dyndns; std::string dyndns;
if (mPeerMgr->getConnectAddresses(id, lAddr, eAddr, histAddrs, dyndns)) if (mPeerMgr->getConnectAddresses(id, lAddr, eAddr, histAddrs, dyndns))
{ {
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ RS_STACK_MUTEX(mLinkMtx);
std::map<RsPeerId, peerConnectState>::iterator it; std::map<RsPeerId, peerConnectState>::iterator it = mFriendList.find(id);
if (mFriendList.end() != (it = mFriendList.find(id))) if ( it != mFriendList.end() )
{ {
locked_ConnectAttempt_CurrentAddresses(&(it->second), lAddr, eAddr); locked_ConnectAttempt_CurrentAddresses(&(it->second), lAddr, eAddr);
uint16_t dynPort = sockaddr_storage_port(eAddr); uint16_t dynPort = 0;
if (!dynPort) if (!sockaddr_storage_isnull(eAddr)) dynPort = sockaddr_storage_port(eAddr);
if (!dynPort && !sockaddr_storage_isnull(lAddr))
dynPort = sockaddr_storage_port(lAddr); dynPort = sockaddr_storage_port(lAddr);
if (dynPort) if (dynPort)
{
locked_ConnectAttempt_AddDynDNS(&(it->second), dyndns, dynPort); locked_ConnectAttempt_AddDynDNS(&(it->second), dyndns, dynPort);
}
locked_ConnectAttempt_HistoricalAddresses(&(it->second), histAddrs); locked_ConnectAttempt_HistoricalAddresses(&(it->second), histAddrs);
/* finish it off */ // finish it off
return locked_ConnectAttempt_Complete(&(it->second)); return locked_ConnectAttempt_Complete(&(it->second));
} }
else else
{ std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to find friend data : " << id << std::endl;
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to find friend data : " << id;
std::cerr << std::endl;
}
} }
else else
{ std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to get addresses from PeerMgr for: " << id << std::endl;
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to addresses from PeerMgr for: " << id;
std::cerr << std::endl;
}
return false; return false;
} }

View File

@ -379,9 +379,9 @@ bool p3PeerMgrIMPL::getGpgId(const RsPeerId &ssl_id, RsPgpId &gpgId)
/**** HIDDEN STUFF ****/ /**** HIDDEN STUFF ****/
bool p3PeerMgrIMPL::isHidden() bool p3PeerMgrIMPL::isHidden()
{ {
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ RS_STACK_MUTEX(mPeerMtx);
return mOwnState.hiddenNode; return mOwnState.hiddenNode;
} }

View File

@ -29,6 +29,7 @@
#include "util/rsnet.h" #include "util/rsnet.h"
#include "util/rsstring.h" #include "util/rsstring.h"
#include "pqi/pqinetwork.h" #include "pqi/pqinetwork.h"
#include "util/stacktrace.h"
/***************************** Internal Helper Fns ******************************/ /***************************** Internal Helper Fns ******************************/
@ -186,13 +187,12 @@ uint16_t sockaddr_storage_port(const struct sockaddr_storage &addr)
{ {
case AF_INET: case AF_INET:
return sockaddr_storage_ipv4_port(addr); return sockaddr_storage_ipv4_port(addr);
break;
case AF_INET6: case AF_INET6:
return sockaddr_storage_ipv6_port(addr); return sockaddr_storage_ipv6_port(addr);
break;
default: default:
std::cerr << "sockaddr_storage_port() invalid addr.ss_family" << std::endl; std::cerr << "sockaddr_storage_port() invalid addr.ss_family" << std::endl;
sockaddr_storage_dump(addr); sockaddr_storage_dump(addr);
print_stacktrace();
break; break;
} }
return 0; return 0;
@ -508,22 +508,19 @@ bool sockaddr_storage_isnull(const struct sockaddr_storage &addr)
bool sockaddr_storage_isValidNet(const struct sockaddr_storage &addr) bool sockaddr_storage_isValidNet(const struct sockaddr_storage &addr)
{ {
#ifdef SS_DEBUG #ifdef SS_DEBUG
std::cerr << "sockaddr_storage_isValidNet()"; std::cerr << "sockaddr_storage_isValidNet()" << std::endl;
std::cerr << std::endl;
#endif #endif
switch(addr.ss_family) switch(addr.ss_family)
{ {
case AF_INET: case AF_INET:
return sockaddr_storage_ipv4_isValidNet(addr); return sockaddr_storage_ipv4_isValidNet(addr);
break;
case AF_INET6: case AF_INET6:
return sockaddr_storage_ipv6_isValidNet(addr); return sockaddr_storage_ipv6_isValidNet(addr);
break;
default: default:
#ifdef SS_DEBUG #ifdef SS_DEBUG
std::cerr << "sockaddr_storage_isValidNet() INVALID Family - error: " << sockaddr_storage_iptostring(addr); std::cerr << "sockaddr_storage_isValidNet() INVALID Family" << std::endl;
std::cerr << std::endl; sockaddr_storage_dump(addr);
#endif #endif
break; break;
} }