From d24b281948816084d6938263c12a74a049996c9c Mon Sep 17 00:00:00 2001 From: sehraf Date: Tue, 15 Sep 2015 17:10:48 +0200 Subject: [PATCH] optimizations * reduced calls to mPeermgr * use switch convenience * small fixes --- libretroshare/src/pqi/p3linkmgr.cc | 30 +++++++-------- libretroshare/src/pqi/p3peermgr.cc | 34 ++++++++++++++++- libretroshare/src/pqi/p3peermgr.h | 2 + libretroshare/src/pqi/pqibin.cc | 2 +- libretroshare/src/pqi/pqipersongrp.cc | 17 ++++----- libretroshare/src/pqi/pqisslpersongrp.cc | 29 ++++++++++++--- libretroshare/src/rsserver/p3peers.cc | 47 ++++++++++++------------ 7 files changed, 105 insertions(+), 56 deletions(-) diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index 00c7f63e2..b0322420b 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -1624,6 +1624,17 @@ bool p3LinkMgrIMPL::retryConnectTCP(const RsPeerId &id) /* 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; + } + struct sockaddr_storage proxy_addr; std::string domain_addr; uint16_t domain_port; @@ -1631,17 +1642,6 @@ bool p3LinkMgrIMPL::retryConnectTCP(const RsPeerId &id) /* then we just have one connect attempt via the Proxy */ if (mPeerMgr->getProxyAddress(id, proxy_addr, domain_addr, domain_port)) { - /* check if it's a valid proxy address */ - uint32_t type = mPeerMgr->hiddenDomainToHiddenType(domain_addr); - if (type & (~RS_HIDDEN_TYPE_MASK)) - { -#ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::retryConnectTCP() domain (" << domain_addr << ") is invalid hidden type (" << type << ") -> return false"; - std::cerr << std::endl; -#endif - return false; - } - RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ std::map::iterator it; @@ -2038,15 +2038,15 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, pca.addr = proxy_addr; switch (type) { - case RS_HIDDEN_TYPE_I2P: - pca.type = RS_NET_CONN_TCP_HIDDEN_I2P; - break; case RS_HIDDEN_TYPE_TOR: pca.type = RS_NET_CONN_TCP_HIDDEN_TOR; break; + case RS_HIDDEN_TYPE_I2P: + pca.type = RS_NET_CONN_TCP_HIDDEN_I2P; + break; case RS_HIDDEN_TYPE_UNKNOWN: default: - /**** THIS CASE SHOULD NOT BE TRIGGERED - since this function is called only with a valid hidden type****/ + /**** THIS CASE SHOULD NOT BE TRIGGERED - since this function is called with a valid hidden type only ****/ std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress() hidden type of addr: " << domain_addr << " is unkown -> THIS SHOULD NEVER HAPPEN!" << std::endl; std::cerr << " - peer : " << peer->id << "(" << peer->name << ")" << std::endl; std::cerr << " - proxy: " << sockaddr_storage_tostring(proxy_addr) << std::endl; diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index a9fac0034..b8356a766 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -146,7 +146,7 @@ p3PeerMgrIMPL::p3PeerMgrIMPL(const RsPeerId& ssl_own_id, const RsPgpId& gpg_own_ sockaddr_storage_ipv4_setport(mProxyServerAddressI2P, kConfigDefaultProxyServerPortI2P); - mProxyServerStatusTor = RS_NET_PROXY_STATUS_UNKNOWN ; + mProxyServerStatusTor = RS_NET_PROXY_STATUS_UNKNOWN ; mProxyServerStatusI2P = RS_NET_PROXY_STATUS_UNKNOWN; } @@ -488,6 +488,38 @@ uint32_t p3PeerMgrIMPL::hiddenDomainToHiddenType(const std::string &domain) return RS_HIDDEN_TYPE_UNKNOWN; } +/** + * @brief returns the hidden type of a peer + * @param ssl_id peer id + * @return hidden type + */ +uint32_t p3PeerMgrIMPL::getHiddenType(const RsPeerId &ssl_id) +{ + RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + + if (ssl_id == AuthSSL::getAuthSSL()->OwnId()) + return mOwnState.hiddenType; + + /* check for existing */ + std::map::iterator it; + it = mFriendList.find(ssl_id); + if (it == mFriendList.end()) + { +#ifdef PEER_DEBUG + std::cerr << "p3PeerMgrIMPL::getHiddenType(" << ssl_id << ") Missing Peer => false"; + std::cerr << std::endl; +#endif + + return false; + } + +#ifdef PEER_DEBUG + std::cerr << "p3PeerMgrIMPL::getHiddenType(" << ssl_id << ") = " << (it->second).hiddenType; + std::cerr << std::endl; +#endif + return (it->second).hiddenType; +} + /** * @brief sets hidden domain and port for a given ssl ID * @param ssl_id peer to set domain and port for diff --git a/libretroshare/src/pqi/p3peermgr.h b/libretroshare/src/pqi/p3peermgr.h index 6880dd623..51789d296 100644 --- a/libretroshare/src/pqi/p3peermgr.h +++ b/libretroshare/src/pqi/p3peermgr.h @@ -195,6 +195,7 @@ virtual bool isHiddenPeer(const RsPeerId &ssl_id) = 0; virtual bool isHiddenPeer(const RsPeerId &ssl_id, const uint32_t type) = 0; virtual bool getProxyAddress(const RsPeerId &ssl_id, struct sockaddr_storage &proxy_addr, std::string &domain_addr, uint16_t &domain_port) = 0; virtual uint32_t hiddenDomainToHiddenType(const std::string &domain) = 0; +virtual uint32_t getHiddenType(const RsPeerId &ssl_id) = 0; virtual int getFriendCount(bool ssl, bool online) = 0; @@ -301,6 +302,7 @@ virtual bool isHiddenPeer(const RsPeerId &ssl_id); virtual bool isHiddenPeer(const RsPeerId &ssl_id, const uint32_t type); virtual bool getProxyAddress(const RsPeerId& ssl_id, struct sockaddr_storage &proxy_addr, std::string &domain_addr, uint16_t &domain_port); virtual uint32_t hiddenDomainToHiddenType(const std::string &domain); +virtual uint32_t getHiddenType(const RsPeerId &ssl_id); virtual int getFriendCount(bool ssl, bool online); diff --git a/libretroshare/src/pqi/pqibin.cc b/libretroshare/src/pqi/pqibin.cc index 10a67c77f..3ff000ec0 100644 --- a/libretroshare/src/pqi/pqibin.cc +++ b/libretroshare/src/pqi/pqibin.cc @@ -500,7 +500,7 @@ void printNetBinID(std::ostream &out, const RsPeerId& id, uint32_t t) { out << "TCP)"; } - else if (t == PQI_CONNECT_HIDDEN_TOR_TCP || t == PQI_CONNECT_HIDDEN_I2P_TCP) + else if (t & (PQI_CONNECT_HIDDEN_TOR_TCP | PQI_CONNECT_HIDDEN_I2P_TCP)) { out << "HTCP"; } diff --git a/libretroshare/src/pqi/pqipersongrp.cc b/libretroshare/src/pqi/pqipersongrp.cc index 8563ac0ac..e6a44d5cd 100644 --- a/libretroshare/src/pqi/pqipersongrp.cc +++ b/libretroshare/src/pqi/pqipersongrp.cc @@ -617,20 +617,19 @@ int pqipersongrp::connectPeer(const RsPeerId& id uint32_t ptype; if (type & RS_NET_CONN_TCP_ALL) { - if (type == RS_NET_CONN_TCP_HIDDEN_TOR) - { + switch (type) { + case RS_NET_CONN_TCP_HIDDEN_TOR: ptype = PQI_CONNECT_HIDDEN_TOR_TCP; timeout = RS_TCP_HIDDEN_TIMEOUT_PERIOD; - } - else if (type == RS_NET_CONN_TCP_HIDDEN_I2P) - { + break; + case RS_NET_CONN_TCP_HIDDEN_I2P: ptype = PQI_CONNECT_HIDDEN_I2P_TCP; timeout = RS_TCP_HIDDEN_TIMEOUT_PERIOD; - } - else - { + break; + default: ptype = PQI_CONNECT_TCP; - timeout = RS_TCP_STD_TIMEOUT_PERIOD; + timeout = RS_TCP_STD_TIMEOUT_PERIOD; + break; } #ifdef PGRP_DEBUG std::cerr << " pqipersongrp::connectPeer() connecting with TCP: Timeout :" << timeout; diff --git a/libretroshare/src/pqi/pqisslpersongrp.cc b/libretroshare/src/pqi/pqisslpersongrp.cc index a0103a60d..898b28bb5 100644 --- a/libretroshare/src/pqi/pqisslpersongrp.cc +++ b/libretroshare/src/pqi/pqisslpersongrp.cc @@ -92,17 +92,34 @@ pqiperson * pqisslpersongrp::locked_createPerson(const RsPeerId& id, pqilistener pqiconnect *pqisc = new pqiconnect(pqip, rss, pqis); /* first select type based on peer */ - if (mPeerMgr->isHiddenPeer(id, RS_HIDDEN_TYPE_I2P)) { - pqip -> addChildInterface(PQI_CONNECT_HIDDEN_I2P_TCP, pqisc); - } else if (mPeerMgr->isHiddenPeer(id, RS_HIDDEN_TYPE_TOR)) { + uint32_t typePeer = mPeerMgr->getHiddenType(id); + switch (typePeer) { + case RS_HIDDEN_TYPE_TOR: pqip -> addChildInterface(PQI_CONNECT_HIDDEN_TOR_TCP, pqisc); - } else { + break; + case RS_HIDDEN_TYPE_I2P: + pqip -> addChildInterface(PQI_CONNECT_HIDDEN_I2P_TCP, pqisc); + break; + default: /* peer is not a hidden one but we are */ /* select type based on ourselves */ - if (mPeerMgr->isHidden(RS_HIDDEN_TYPE_I2P)) + uint32_t typeOwn = mPeerMgr->getHiddenType(AuthSSL::getAuthSSL()->OwnId()); + switch (typeOwn) { + case RS_HIDDEN_TYPE_I2P: pqip -> addChildInterface(PQI_CONNECT_HIDDEN_I2P_TCP, pqisc); - else + break; + default: + /* this case shouldn't happen! */ + std::cerr << "pqisslpersongrp::locked_createPerson WARNING INVALID HIDDEN TYPES - THIS SHOULD NOT HAPPEN!" << std::endl; + std::cerr << " - ID: " << id << std::endl; + std::cerr << " - mPeerMgr->isHidden(): " << mPeerMgr->isHidden() << std::endl; + std::cerr << " - mPeerMgr->isHiddenPeer(id): " << mPeerMgr->isHiddenPeer(id) << std::endl; + std::cerr << " - hidden types: peer=" << typePeer << " own=" << typeOwn << std::endl; + std::cerr << " --> falling back to Tor" << std::endl; + case RS_HIDDEN_TYPE_TOR: pqip -> addChildInterface(PQI_CONNECT_HIDDEN_TOR_TCP, pqisc); + break; + } } } else diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index 3ff20174a..3bbb8de3f 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -440,40 +440,40 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d) /* peer is connected - determine how and set proper connectState */ if(mPeerMgr->isHidden()) { + uint32_t type; /* hidden location */ /* use connection direction to determine connection type */ if(pcs.actAsServer) { /* incoming connection */ /* use own type to set connectState */ - if (mPeerMgr->isHidden(RS_HIDDEN_TYPE_TOR)) - { + type = mPeerMgr->getHiddenType(AuthSSL::getAuthSSL()->OwnId()); + switch (type) { + case RS_HIDDEN_TYPE_TOR: d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_TOR; - } - else if (mPeerMgr->isHidden(RS_HIDDEN_TYPE_I2P)) - { + break; + case RS_HIDDEN_TYPE_I2P: d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_I2P; - } - else - { + break; + default: d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN; + break; } } else { /* outgoing connection */ /* use peer hidden type to set connectState */ - if (ps.hiddenType == RS_HIDDEN_TYPE_TOR) - { + switch (ps.hiddenType) { + case RS_HIDDEN_TYPE_TOR: d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_TOR; - } - else if (ps.hiddenType == RS_HIDDEN_TYPE_I2P) - { + break; + case RS_HIDDEN_TYPE_I2P: d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_I2P; - } - else - { + break; + default: d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN; + break; } } } @@ -481,17 +481,16 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d) { /* hidden peer */ /* use hidden type to set connectState */ - if (ps.hiddenType == RS_HIDDEN_TYPE_TOR) - { + switch (ps.hiddenType) { + case RS_HIDDEN_TYPE_TOR: d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_TOR; - } - else if (ps.hiddenType == RS_HIDDEN_TYPE_I2P) - { + break; + case RS_HIDDEN_TYPE_I2P: d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_I2P; - } - else - { + break; + default: d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN; + break; } } else