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

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