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/rstickevent.h \
util/rsrecogn.h \
util/rsscopetimer.h
util/rsscopetimer.h \
util/stacktrace.h
SOURCES += dbase/cachestrapper.cc \
dbase/fimonitor.cc \

View File

@ -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<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
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<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))
{
/* 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<RsPeerId, peerConnectState>::iterator it;
if (mFriendList.end() != (it = mFriendList.find(id)))
RS_STACK_MUTEX(mLinkMtx);
std::map<RsPeerId, peerConnectState>::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<RsPeerId, peerConnectState>::iterator it;
if (mFriendList.end() != (it = mFriendList.find(id)))
std::map<RsPeerId, peerConnectState>::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;
}

View File

@ -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;
}

View File

@ -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;
}