diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 96f26f1f1..864210aae 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -9,17 +9,23 @@ profiling { QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer } +release { + # UDP and TUNNEL dont work anymore. + DEFINES *= PQI_DISABLE_UDP + DEFINES *= PQI_DISABLE_TUNNEL +} + testnetwork { DEFINES *= PQI_DISABLE_UDP DEFINES *= PQI_DISABLE_TUNNEL - DEFINES *= AUTHSSL_DEBUG GPG_DEBUG - DEFINES *= CONN_DEBUG + # DEFINES *= AUTHSSL_DEBUG GPG_DEBUG + # DEFINES *= CONN_DEBUG # DEFINES *= P3DISC_DEBUG - DEFINES *= PGRP_DEBUG - DEFINES *= PERSON_DEBUG - DEFINES *= DEBUG_PQISSL + # DEFINES *= PGRP_DEBUG + # DEFINES *= PERSON_DEBUG + # DEFINES *= DEBUG_PQISSL #DEFINES *= DEBUG_UDP_SORTER DEBUG_UDP_LAYER EXTADDRSEARCH_DEBUG diff --git a/libretroshare/src/pqi/p3connmgr.cc b/libretroshare/src/pqi/p3connmgr.cc index f39202bb8..e33447e42 100644 --- a/libretroshare/src/pqi/p3connmgr.cc +++ b/libretroshare/src/pqi/p3connmgr.cc @@ -72,9 +72,6 @@ const uint32_t PEER_IP_CONNECT_STATE_MAX_LIST_SIZE = 4; * #define CONN_DEBUG_TICK 1 ***/ -#define CONN_DEBUG 1 -#define CONN_DEBUG_RESET 1 -#define CONN_DEBUG_TICK 1 /**** * #define P3CONNMGR_NO_TCP_CONNECTIONS 1 @@ -200,9 +197,9 @@ p3ConnectMgr::p3ConnectMgr() mOldNetFlags = pqiNetStatus(); } - #ifdef CONN_DEBUG_RESET - std::cerr << "p3ConnectMgr() Calling NetReset" << std::endl; - #endif +#ifdef CONN_DEBUG + std::cerr << "p3ConnectMgr() Startup" << std::endl; +#endif netReset(); @@ -234,7 +231,9 @@ void p3ConnectMgr::setIPServersEnabled(bool b) { IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/ } +#ifdef CONN_DEBUG std::cerr << "p3ConnectMgr: setIPServers to " << b << std::endl ; +#endif } void p3ConnectMgr::setTunnelConnection(bool b) @@ -395,9 +394,9 @@ void p3ConnectMgr::netReset() } #endif - #ifdef CONN_DEBUG_RESET - std::cerr << "p3ConnectMgr::netReset() shutdown" << std::endl; - #endif +#ifdef CONN_DEBUG_RESET + std::cerr << "p3ConnectMgr::netReset() Called" << std::endl; +#endif shutdown(); /* blocking shutdown call */ @@ -406,13 +405,19 @@ void p3ConnectMgr::netReset() { #ifdef CONN_DEBUG_RESET std::cerr << "p3ConnectMgr::netReset() restarting AddrFinder" << std::endl; - #endif +#endif mExtAddrFinder->reset() ; } + else + { +#ifdef CONN_DEBUG_RESET + std::cerr << "p3ConnectMgr::netReset() ExtAddrFinder Disabled" << std::endl; +#endif + } - #ifdef CONN_DEBUG_RESET +#ifdef CONN_DEBUG_RESET std::cerr << "p3ConnectMgr::netReset() resetting NetStatus" << std::endl; - #endif +#endif /* reset udp network - handled by tou_init! */ /* reset tcp network - if necessary */ @@ -425,12 +430,16 @@ void p3ConnectMgr::netReset() * as it calls back to p3ConnMgr. */ +#ifdef CONN_DEBUG_RESET std::cerr << "p3ConnectMgr::netReset() resetting listeners" << std::endl; +#endif std::list::const_iterator it; for(it = mNetListeners.begin(); it != mNetListeners.end(); it++) { - std::cerr << "p3ConnectMgr::netReset() reset listener" << std::endl; - (*it)->reset_listener(); + (*it)->reset_listener(); +#ifdef CONN_DEBUG_RESET + std::cerr << "p3ConnectMgr::netReset() reset listener" << std::endl; +#endif } } @@ -443,11 +452,12 @@ void p3ConnectMgr::netReset() /* check Network Address. This happens later */ //checkNetAddress(); - #ifdef CONN_DEBUG_RESET +#ifdef CONN_DEBUG_RESET std::cerr << "p3ConnectMgr::netReset() done" << std::endl; - #endif +#endif } + /* to allow resets of network stuff */ void p3ConnectMgr::addNetListener(pqiNetListener *listener) { @@ -490,6 +500,7 @@ void p3ConnectMgr::netStartup() mNetInitTS = time(NULL); netStatusReset_locked(); + #ifdef CONN_DEBUG_RESET std::cerr << "p3ConnectMgr::netStartup() resetting mNetInitTS / Status" << std::endl; #endif @@ -547,6 +558,10 @@ void p3ConnectMgr::tick() bool p3ConnectMgr::shutdown() /* blocking shutdown call */ { +#ifdef CONN_DEBUG + std::cerr << "p3ConnectMgr::shutdown()"; + std::cerr << std::endl; +#endif { RsStackMutex stack(connMtx); /****** STACK LOCK MUTEX *******/ mNetStatus = RS_NET_UNKNOWN; @@ -629,7 +644,7 @@ void p3ConnectMgr::netTick() { #ifdef CONN_DEBUG_TICK - //std::cerr << "p3ConnectMgr::netTick()" << std::endl; + std::cerr << "p3ConnectMgr::netTick()" << std::endl; #endif // Check whether we are stuck on loopback. This happens if RS starts when @@ -651,17 +666,14 @@ void p3ConnectMgr::netTick() { case RS_NET_NEEDS_RESET: -#ifdef CONN_DEBUG_TICK - std::cerr << "p3ConnectMgr::netTick() STATUS: NEEDS_RESET" << std::endl; -#endif -#ifdef CONN_DEBUG_RESET +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netTick() STATUS: NEEDS_RESET" << std::endl; #endif netReset(); break; case RS_NET_UNKNOWN: -#ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netTick() STATUS: UNKNOWN" << std::endl; #endif @@ -671,7 +683,7 @@ void p3ConnectMgr::netTick() #define STARTUP_DELAY 5 if (age < STARTUP_DELAY) { -#ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netTick() Delaying Startup" << std::endl; #endif } @@ -683,14 +695,14 @@ void p3ConnectMgr::netTick() break; case RS_NET_UPNP_INIT: -#ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netTick() STATUS: UPNP_INIT" << std::endl; #endif netUpnpInit(); break; case RS_NET_UPNP_SETUP: -#ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netTick() STATUS: UPNP_SETUP" << std::endl; #endif netUpnpCheck(); @@ -698,7 +710,7 @@ void p3ConnectMgr::netTick() case RS_NET_EXT_SETUP: -#ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netTick() STATUS: EXT_SETUP" << std::endl; #endif netExtCheck(); @@ -715,7 +727,7 @@ void p3ConnectMgr::netTick() case RS_NET_LOOPBACK: //don't do a shutdown because a client in a computer without local network might be usefull for debug. //shutdown(); -#ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netTick() STATUS: RS_NET_LOOPBACK" << std::endl; #endif default: @@ -728,7 +740,7 @@ void p3ConnectMgr::netTick() void p3ConnectMgr::netUdpInit() { -#ifdef CONN_DEBUG +#if defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netUdpInit()" << std::endl; #endif connMtx.lock(); /* LOCK MUTEX */ @@ -744,7 +756,7 @@ void p3ConnectMgr::netUdpInit() void p3ConnectMgr::netDhtInit() { -#ifdef CONN_DEBUG +#if defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netDhtInit()" << std::endl; #endif connMtx.lock(); /* LOCK MUTEX */ @@ -759,7 +771,7 @@ void p3ConnectMgr::netDhtInit() void p3ConnectMgr::netUpnpInit() { -#ifdef CONN_DEBUG +#if defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netUpnpInit()" << std::endl; #endif uint16_t eport, iport; @@ -789,9 +801,9 @@ void p3ConnectMgr::netUpnpCheck() time_t delta = time(NULL) - mNetInitTS; - #ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netUpnpCheck() age: " << delta << std::endl; - #endif +#endif connMtx.unlock(); /* UNLOCK MUTEX */ @@ -801,10 +813,10 @@ void p3ConnectMgr::netUpnpCheck() if (((upnpState == 0) && (delta > (time_t)MAX_UPNP_INIT)) || ((upnpState > 0) && (delta > (time_t)MAX_UPNP_COMPLETE))) { - #ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netUpnpCheck() "; std::cerr << "Upnp Check failed." << std::endl; - #endif +#endif /* fallback to UDP startup */ connMtx.lock(); /* LOCK MUTEX */ @@ -816,7 +828,7 @@ void p3ConnectMgr::netUpnpCheck() } else if ((upnpState > 0) && netAssistExtAddress(extAddr)) { -#ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netUpnpCheck() "; std::cerr << "Upnp Check success state: " << upnpState << std::endl; #endif @@ -829,7 +841,7 @@ void p3ConnectMgr::netUpnpCheck() */ if (isValidNet(&(extAddr.sin_addr))) { -#ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netUpnpCheck() "; std::cerr << "UpnpAddr: " << inet_ntoa(extAddr.sin_addr); std::cerr << ":" << ntohs(extAddr.sin_port); @@ -848,10 +860,10 @@ void p3ConnectMgr::netUpnpCheck() } else { - #ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netUpnpCheck() "; std::cerr << "Upnp Check Continues: status: " << upnpState << std::endl; - #endif +#endif } } @@ -859,7 +871,7 @@ void p3ConnectMgr::netUpnpCheck() void p3ConnectMgr::netExtCheck() { -#ifdef CONN_DEBUG +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck()" << std::endl; #endif { @@ -872,14 +884,14 @@ void p3ConnectMgr::netExtCheck() /* (1) UPnP -> which handles itself */ if (!mNetFlags.mExtAddrOk) { -#ifdef CONN_DEBUG +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck() Ext Not Ok" << std::endl; #endif /* net Assist */ if (netAssistExtAddress(tmpip)) { -#ifdef CONN_DEBUG +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck() Ext supplied from netAssistExternalAddress()" << std::endl; #endif if (isValidNet(&(tmpip.sin_addr))) @@ -892,7 +904,7 @@ void p3ConnectMgr::netExtCheck() } else { -#ifdef CONN_DEBUG +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck() Bad Address supplied from netAssistExternalAddress()" << std::endl; #endif } @@ -906,23 +918,32 @@ void p3ConnectMgr::netExtCheck() /* ExtAddrFinder */ if (mUseExtAddrFinder) { +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck() checking ExtAddrFinder" << std::endl; +#endif bool extFinderOk = mExtAddrFinder->hasValidIP(&(tmpip.sin_addr)); if (extFinderOk) { +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck() Ext supplied by ExtAddrFinder" << std::endl; +#endif /* best guess at port */ tmpip.sin_port = mNetFlags.mLocalAddr.sin_port; -#ifdef CONN_DEBUG_TICK - std::cerr << "p3ConnectMgr::netExtCheck() "; - std::cerr << "ExtAddr: " << inet_ntoa(tmpip.sin_addr); - std::cerr << ":" << ntohs(tmpip.sin_port); - std::cerr << std::endl; +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) + std::cerr << "p3ConnectMgr::netExtCheck() "; + std::cerr << "ExtAddr: " << inet_ntoa(tmpip.sin_addr); + std::cerr << ":" << ntohs(tmpip.sin_port); + std::cerr << std::endl; #endif mNetFlags.mExtAddr = tmpip; mNetFlags.mExtAddrOk = true; mNetFlags.mExtAddrStableOk = isStable; + + /* XXX HACK TO FIX */ +#warning "ALLOWING ExtAddrFinder -> ExtAddrStableOk = true (which it is not normally)" + mNetFlags.mExtAddrStableOk = true; + } } } @@ -933,7 +954,7 @@ void p3ConnectMgr::netExtCheck() if (mNetFlags.mExtAddrOk) { -#ifdef CONN_DEBUG_TICK +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck() "; std::cerr << "ExtAddr: " << inet_ntoa(mNetFlags.mExtAddr.sin_addr); std::cerr << ":" << ntohs(mNetFlags.mExtAddr.sin_port); @@ -949,11 +970,13 @@ void p3ConnectMgr::netExtCheck() mOwnState.ipAddrs.mExt.updateIpAddressList(addrInfo); mNetStatus = RS_NET_DONE; +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck() Ext Ok: RS_NET_DONE" << std::endl; +#endif if (!mNetFlags.mExtAddrStableOk) { -#ifdef CONN_DEBUG +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netUdpCheck() UDP Unstable :( "; std::cerr << std::endl; std::cerr << "p3ConnectMgr::netUdpCheck() We are unreachable"; @@ -994,7 +1017,9 @@ void p3ConnectMgr::netExtCheck() if (mNetFlags.mExtAddrOk) { +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck() setting netAssistSetAddress()" << std::endl; +#endif netAssistSetAddress(mNetFlags.mLocalAddr, mNetFlags.mExtAddr, mOwnState.netMode); } #if 0 @@ -1009,7 +1034,9 @@ void p3ConnectMgr::netExtCheck() /* flag unreachables! */ if ((mNetFlags.mExtAddrOk) && (!mNetFlags.mExtAddrStableOk)) { +#if defined(CONN_DEBUG_TICK) || defined(CONN_DEBUG_RESET) std::cerr << "p3ConnectMgr::netExtCheck() Ext Unstable - Unreachable Check" << std::endl; +#endif netUnreachableCheck(); } } diff --git a/libretroshare/src/pqi/pqiipset.cc b/libretroshare/src/pqi/pqiipset.cc index 94743810a..370db030e 100644 --- a/libretroshare/src/pqi/pqiipset.cc +++ b/libretroshare/src/pqi/pqiipset.cc @@ -75,7 +75,7 @@ bool pqiIpAddrList::updateIpAddressList(const pqiIpAddress &addr) #endif add = true; } - else if (mAddrs.begin()->mSeenTime < addr.mSeenTime) + else if (mAddrs.back().mSeenTime < addr.mSeenTime) { #ifdef IPADDR_DEBUG std::cerr << "pqiIpAddrList::updateIpAddressList() oldAddr: Add"; @@ -121,11 +121,11 @@ bool pqiIpAddrList::updateIpAddressList(const pqiIpAddress &addr) } } - // ordered by increasing time. (oldest at front) + // ordered by decreaseing time. (newest at front) bool added = false; for(it = mAddrs.begin(); it != mAddrs.end(); it++) { - if (it->mSeenTime > addr.mSeenTime) + if (it->mSeenTime < addr.mSeenTime) { #ifdef IPADDR_DEBUG std::cerr << "pqiIpAddrList::updateIpAddressList() added orig SeenTime: " << it->mSeenTime << " new SeenTime: " << addr.mSeenTime; @@ -147,13 +147,13 @@ bool pqiIpAddrList::updateIpAddressList(const pqiIpAddress &addr) } /* pop if necessary */ - if (mAddrs.size() > MAX_ADDRESS_LIST_SIZE) + while (mAddrs.size() > MAX_ADDRESS_LIST_SIZE) { #ifdef IPADDR_DEBUG - std::cerr << "pqiIpAddrList::updateIpAddressList() popping front"; + std::cerr << "pqiIpAddrList::updateIpAddressList() popping back"; std::cerr << std::endl; #endif - mAddrs.pop_front(); + mAddrs.pop_back(); } return newAddr; diff --git a/libretroshare/src/serialiser/rsdiscitems.cc b/libretroshare/src/serialiser/rsdiscitems.cc index 7df84c3c4..5e76c5306 100644 --- a/libretroshare/src/serialiser/rsdiscitems.cc +++ b/libretroshare/src/serialiser/rsdiscitems.cc @@ -36,7 +36,6 @@ /*** #define RSSERIAL_DEBUG 1 ***/ -#define RSSERIAL_DEBUG 1 #include diff --git a/libretroshare/src/services/p3disc.cc b/libretroshare/src/services/p3disc.cc index deb5778bd..57d499728 100644 --- a/libretroshare/src/services/p3disc.cc +++ b/libretroshare/src/services/p3disc.cc @@ -70,6 +70,8 @@ const uint32_t P3DISC_FLAGS_ASK_VERSION = 0x0080; * #define P3DISC_DEBUG 1 ****/ +//#define P3DISC_DEBUG 1 + /*********** NOTE *************** * * Only need Mutexs for neighbours information @@ -94,16 +96,22 @@ p3disc::p3disc(p3ConnectMgr *cm, pqipersongrp *pqih) //add own version to versions map versions[AuthSSL::getAuthSSL()->OwnId()] = RsUtil::retroshareVersion(); +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::p3disc() setup"; + std::cerr << std::endl; +#endif + return; } int p3disc::tick() { //send a heartbeat to all connected peers - if (time(NULL) - lastSentHeartbeatTime > HEARTBEAT_REPEAT_TIME) { - #ifdef P3DISC_DEBUG + if (time(NULL) - lastSentHeartbeatTime > HEARTBEAT_REPEAT_TIME) + { +#ifdef P3DISC_DEBUG std::cerr << "p3disc::tick() sending heartbeat to all peers" << std::endl; - #endif +#endif lastSentHeartbeatTime = time(NULL); std::list peers; mConnMgr->getOnlineList(peers); @@ -120,20 +128,21 @@ int p3disc::handleIncoming() { RsItem *item = NULL; - #ifdef P3DISC_DEBUG - std::cerr << "p3disc::handleIncoming()" << std::endl; - #endif +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::handleIncoming()" << std::endl; +#endif // if off discard item. peerConnectState detail; - if (!mConnMgr->getOwnNetStatus(detail) || (detail.visState & RS_VIS_STATE_NODISC)) { + if (!mConnMgr->getOwnNetStatus(detail) || (detail.visState & RS_VIS_STATE_NODISC)) + { while(NULL != (item = recvItem())) { - #ifdef P3DISC_DEBUG - std::cerr << "p3disc::handleIncoming() Deleting - Cos RemoteDisc Off!" << std::endl; - item -> print(std::cerr); - std::cerr << std::endl; - #endif +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::handleIncoming() Deleting - Cos RemoteDisc Off!" << std::endl; + item -> print(std::cerr); + std::cerr << std::endl; +#endif delete item; } @@ -149,11 +158,11 @@ int p3disc::handleIncoming() RsDiscVersion *dvi = NULL; RsDiscHeartbeat *dta = NULL; - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::handleIncoming() Received Message!" << std::endl; item -> print(std::cerr); std::cerr << std::endl; - #endif +#endif // if discovery reply then respond if haven't already. @@ -174,12 +183,22 @@ int p3disc::handleIncoming() recvHeartbeatMsg(dta); nhandled++ ; } + else + { +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::handleIncoming() Unknown Received Message!" << std::endl; + item -> print(std::cerr); + std::cerr << std::endl; +#endif + } delete item; } - #ifdef P3DISC_DEBUG + +#ifdef P3DISC_DEBUG std::cerr << "p3disc::handleIncoming() finished." << std::endl; - #endif +#endif + return nhandled; } @@ -194,20 +213,35 @@ void p3disc::statusChange(const std::list &plist) std::list::const_iterator pit; /* if any have switched to 'connected' then we notify */ - for(pit = plist.begin(); pit != plist.end(); pit++) { - if ((pit->state & RS_PEER_S_FRIEND) && (pit->actions & RS_PEER_CONNECTED)) { + for(pit = plist.begin(); pit != plist.end(); pit++) + { + if ((pit->state & RS_PEER_S_FRIEND) && (pit->actions & RS_PEER_CONNECTED)) + { +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::statusChange() Starting Disc with: " << pit->id << std::endl; +#endif sendOwnVersion(pit->id); sendAllInfoToJustConnectedPeer(pit->id); sendJustConnectedPeerInfoToAllPeer(pit->id); } else if (!(pit->state & RS_PEER_S_FRIEND) && (pit->actions & RS_PEER_MOVED)) + { +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::statusChange() Removing Friend: " << pit->id << std::endl; +#endif removeFriend(pit->id); + } else if ((pit->state & RS_PEER_S_FRIEND) && (pit->actions & RS_PEER_NEW)) + { +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::statusChange() Adding Friend: " << pit->id << std::endl; +#endif askInfoToAllPeers(pit->id); - } + } + } #ifdef P3DISC_DEBUG std::cerr << "p3disc::statusChange() finished." << std::endl; - #endif +#endif return; } @@ -215,15 +249,16 @@ void p3disc::sendAllInfoToJustConnectedPeer(std::string id) { /* get a peer lists */ - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendAllInfoToJustConnectedPeer() id: " << id << std::endl; - #endif +#endif + RsPeerDetails pd; rsPeers->getPeerDetails(id, pd); if (!pd.accept_connection || (!pd.ownsign && pd.gpg_id != rsPeers->getGPGOwnId())) { //only send info when connection is accepted and gpg key is signed or our own key - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendAllInfoToJustConnectedPeer() we're not sending the info because the destination gpg key is not signed or not accepted." << std::cerr << std::endl; - #endif +#endif return; } @@ -282,18 +317,19 @@ void p3disc::sendJustConnectedPeerInfoToAllPeer(std::string connectedPeerId) } /* (dest (to), source (cert)) */ -void p3disc::sendPeerDetails(std::string to, std::string about) { +void p3disc::sendPeerDetails(std::string to, std::string about) +{ - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendPeerDetails() called. Sending details of: " << about << " to: " << to << std::endl; - #endif +#endif RsPeerDetails pd; rsPeers->getPeerDetails(to, pd); if (!pd.accept_connection || !pd.ownsign) { - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendPeerDetails() we're not sending the info because the destination gpg key is not signed or not accepted." << std::cerr << std::endl; - #endif +#endif return; } @@ -304,19 +340,20 @@ void p3disc::sendPeerDetails(std::string to, std::string about) { return; } - about = rsPeers->getGPGId(about); + std::string aboutGpgId = rsPeers->getGPGId(about); if (about == "") { - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendPeerDetails() no info about this id" << std::endl; - #endif +#endif return; } peerConnectState detailAbout; - if (mConnMgr->getFriendNetStatus(about, detailAbout) && detailAbout.visState & RS_VIS_STATE_NODISC) { - #ifdef P3DISC_DEBUG + if (mConnMgr->getFriendNetStatus(aboutGpgId, detailAbout) && detailAbout.visState & RS_VIS_STATE_NODISC) + { +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendPeerDetails() don't send info about this peer because he has no disc enabled." << std::endl; - #endif +#endif return; } @@ -326,20 +363,37 @@ void p3disc::sendPeerDetails(std::string to, std::string about) { // Fill the message // Set Target as input cert. di -> PeerId(to); - di -> aboutId = about; + di -> aboutId = aboutGpgId; // set the ip addresse list. std::list sslChilds; - rsPeers->getSSLChildListOfGPGId(about, sslChilds); + rsPeers->getSSLChildListOfGPGId(aboutGpgId, sslChilds); bool shouldWeSendGPGKey = false;//the GPG key is send only if we've got a valid friend with DISC enabled - for (std::list::iterator sslChildIt = sslChilds.begin(); sslChildIt != sslChilds.end(); sslChildIt++) + std::list::iterator sslChildIt; + for (sslChildIt = sslChilds.begin(); sslChildIt != sslChilds.end(); sslChildIt++) + { +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::sendPeerDetails() Found Child SSL Id:" << *sslChildIt; + std::cerr << std::endl; +#endif if(to != *sslChildIt) // We don't send info to a peer about itself, but we allow sending info { // about peers with the same GPG id. peerConnectState detail; - if (!mConnMgr->getFriendNetStatus(*sslChildIt, detail) || detail.visState & RS_VIS_STATE_NODISC) { + if (!mConnMgr->getFriendNetStatus(*sslChildIt, detail) + || detail.visState & RS_VIS_STATE_NODISC) + { +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::sendPeerDetails() Skipping cos No Details or NODISC flag"; + std::cerr << std::endl; +#endif continue; } + +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::sendPeerDetails() Adding Child SSL Id Details"; + std::cerr << std::endl; +#endif shouldWeSendGPGKey = true; RsPeerNetItem *rsPeerNetItem = new RsPeerNetItem(); rsPeerNetItem->clear(); @@ -359,6 +413,14 @@ void p3disc::sendPeerDetails(std::string to, std::string about) { di->rsPeerList.push_back(*rsPeerNetItem); } + else + { +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::sendPeerDetails() Skipping cos \"to == sslChildId\""; + std::cerr << std::endl; +#endif + } + } //send own details @@ -388,22 +450,28 @@ void p3disc::sendPeerDetails(std::string to, std::string about) { } if (!shouldWeSendGPGKey) { - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendPeerDetails() GPG key should not be send, no friend with disc on found about it." << std::endl; - #endif +#endif + // cleanup! + delete di; return; } std::string cert = AuthGPG::getAuthGPG()->SaveCertificateToString(about); if (cert == "") { - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendPeerDetails() don't send details because the gpg cert is not good" << std::endl; - #endif + +#endif + // cleanup! + delete di; return; } di -> certGPG = cert; // Send off message #ifdef P3DISC_DEBUG + std::cerr << "p3disc::sendPeerDetails() About to Send Message:" << std::endl; di->print(std::cerr, 5); #endif sendItem(di); @@ -415,9 +483,9 @@ void p3disc::sendPeerDetails(std::string to, std::string about) { void p3disc::sendOwnVersion(std::string to) { - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendOwnVersion() Sending rs version to: " << to << std::endl; - #endif +#endif RsDiscVersion *di = new RsDiscVersion(); di->PeerId(to); @@ -426,9 +494,9 @@ void p3disc::sendOwnVersion(std::string to) /* send the message */ sendItem(di); - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::sendOwnVersion() finished." << std::endl; - #endif +#endif } void p3disc::sendHeartbeat(std::string to) @@ -456,29 +524,32 @@ void p3disc::sendHeartbeat(std::string to) void p3disc::askInfoToAllPeers(std::string about) { - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr <<"p3disc::askInfoToAllPeers() about " << about << std::endl; - #endif +#endif peerConnectState connectState; if (!mConnMgr->getFriendNetStatus(about, connectState)) // || (connectState.visState & RS_VIS_STATE_NODISC)) { { - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::askInfoToAllPeers() friend disc is off, don't send the request." << std::endl; - #endif +#endif return; } - about = rsPeers->getGPGId(about); - if (about == "") { - #ifdef P3DISC_DEBUG + std::string aboutGpgId = rsPeers->getGPGId(about); + if (aboutGpgId == "") { +#ifdef P3DISC_DEBUG std::cerr << "p3disc::askInfoToAllPeers() no gpg id found" << std::endl; - #endif +#endif } // if off discard item. if (!mConnMgr->getOwnNetStatus(connectState) || (connectState.visState & RS_VIS_STATE_NODISC)) { +#ifdef P3DISC_DEBUG + std::cerr << "p3disc::askInfoToAllPeers() no gpg id found" << std::endl; +#endif return; } @@ -488,26 +559,28 @@ void p3disc::askInfoToAllPeers(std::string about) rsPeers->getOnlineList(onlineIds); /* ask info to trusted friends */ - for(onlineIdsIt = onlineIds.begin(); onlineIdsIt != onlineIds.end(); onlineIdsIt++) { + for(onlineIdsIt = onlineIds.begin(); onlineIdsIt != onlineIds.end(); onlineIdsIt++) + { RsPeerDetails details; rsPeers->getPeerDetails(*onlineIdsIt, details); - if (!details.accept_connection || !details.ownsign) { - #ifdef P3DISC_DEBUG + if (!details.accept_connection || !details.ownsign) + { +#ifdef P3DISC_DEBUG std::cerr << "p3disc::askInfoToAllPeers() don't ask info message to untrusted peer." << std::endl; - #endif +#endif continue; } RsDiscAskInfo *di = new RsDiscAskInfo(); di->PeerId(*onlineIdsIt); di->gpg_id = about; sendItem(di); - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::askInfoToAllPeers() question sent to : " << *onlineIdsIt << std::endl; - #endif +#endif } - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::askInfoToAllPeers() finished." << std::endl; - #endif +#endif } void p3disc::recvPeerDetails(RsDiscReply *item) @@ -524,13 +597,13 @@ void p3disc::recvPeerDetails(RsDiscReply *item) #endif std::string certGpgId; if (!AuthGPG::getAuthGPG()->LoadCertificateFromString(item->certGPG, certGpgId)) { - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::recvPeerFriendMsg() gpg cert is not good, aborting" << std::endl; - #endif +#endif return; } if (item->aboutId == "" || item->aboutId != certGpgId) { - #ifdef P3DISC_DEBUG +#ifdef P3DISC_DEBUG std::cerr << "p3disc::recvPeerFriendMsg() Error : about id is not the same as gpg id." << std::endl; #endif return; @@ -538,12 +611,6 @@ void p3disc::recvPeerDetails(RsDiscReply *item) for (std::list::iterator pitem = item->rsPeerList.begin(); pitem != item->rsPeerList.end(); pitem++) { - // there's no way we can receive from a peer some info about our own dummy friend - //don't update dummy friends - //if (rsPeers->isDummyFriend(pitem->pid)) { - // continue; - //} - addDiscoveryData(item->PeerId(), pitem->pid, pitem->currentlocaladdr, pitem->currentremoteaddr, 0, time(NULL)); #ifdef P3DISC_DEBUG @@ -560,8 +627,6 @@ void p3disc::recvPeerDetails(RsDiscReply *item) // if(AuthGPG::getAuthGPG()->isGPGAccepted(pitem->gpg_id) || pitem->gpg_id == AuthGPG::getAuthGPG()->getGPGOwnId()) { - bool merge = true ; - // Add with no disc by default. If friend already exists, it will do nothing // #ifdef P3DISC_DEBUG @@ -570,20 +635,19 @@ void p3disc::recvPeerDetails(RsDiscReply *item) mConnMgr->addFriend(pitem->pid, pitem->gpg_id, pitem->netMode, 0, 0); RsPeerDetails storedDetails; - // Update if know this peer, and if it's not already connected. - // + // Update if know this peer if(rsPeers->getPeerDetails(pitem->pid, storedDetails)) { -#ifdef P3DISC_DEBUG - std::cerr << "Friend is not connected -> updating info" << std::endl; - std::cerr << " -> network mode: " << pitem->netMode << std::endl; - std::cerr << " -> location: " << pitem->location << std::endl; -#endif // Update if it's fresh info or if it's from the peer itself // their info is fresher than ours, update ours // if(!(storedDetails.state & RS_PEER_CONNECTED)) { +#ifdef P3DISC_DEBUG + std::cerr << "Friend is not connected -> updating info" << std::endl; + std::cerr << " -> network mode: " << pitem->netMode << std::endl; + std::cerr << " -> location: " << pitem->location << std::endl; +#endif mConnMgr->setNetworkMode(pitem->pid, pitem->netMode); mConnMgr->setLocation(pitem->pid, pitem->location); } @@ -603,26 +667,26 @@ void p3disc::recvPeerDetails(RsDiscReply *item) std::cerr << " -> current remote addr = " << pitem->currentremoteaddr << std::endl; std::cerr << " -> clearing NODISC flag " << std::endl; #endif - //mConnMgr->setLocalAddress(pitem->pid, pitem->currentlocaladdr); - //mConnMgr->setExtAddress(pitem->pid, pitem->currentremoteaddr); - pitem->visState &= ~RS_VIS_STATE_NODISC ; - mConnMgr->setVisState(pitem->pid, pitem->visState); // When the peer sends his own list of IPs, the info replaces the existing info, because the // peer is the primary source of his own IPs. - // - merge = false ; + + mConnMgr->setLocalAddress(pitem->pid, pitem->currentlocaladdr); + mConnMgr->setExtAddress(pitem->pid, pitem->currentremoteaddr); + pitem->visState &= ~RS_VIS_STATE_NODISC ; + mConnMgr->setVisState(pitem->pid, pitem->visState); } } + else + { + std::cerr << "p3disc:: ERROR HOW DID WE GET HERE?" << std::endl; + } pqiIpAddrSet addrsFromPeer; - addrsFromPeer.mLocal.loadTlv(pitem->localAddrList); - addrsFromPeer.mExt.loadTlv(pitem->extAddrList); - + addrsFromPeer.mLocal.extractFromTlv(pitem->localAddrList); + addrsFromPeer.mExt.extractFromTlv(pitem->extAddrList); #ifdef P3DISC_DEBUG - std::cerr << "Friend is already connected -> not updating" << std::endl; - std::cerr << "Setting address list to peer " << pitem->pid << ", to be:" << std::endl ; addrsFromPeer.printAddrs(std::cerr); @@ -636,12 +700,16 @@ void p3disc::recvPeerDetails(RsDiscReply *item) } #ifdef P3DISC_DEBUG else + { std::cerr << " skipping unknown gpg id " << pitem->gpg_id << std::endl ; + } #endif } #ifdef P3DISC_DEBUG else + { std::cerr << "Skipping info about own id " << pitem->pid << std::endl ; + } #endif } diff --git a/libretroshare/src/tests/pqi/pqiipset_test.cc b/libretroshare/src/tests/pqi/pqiipset_test.cc index be75be764..9fc715750 100644 --- a/libretroshare/src/tests/pqi/pqiipset_test.cc +++ b/libretroshare/src/tests/pqi/pqiipset_test.cc @@ -68,6 +68,20 @@ bool test_addr_list() addr.mSeenTime = time(NULL) - i; list.updateIpAddressList(addr); + + if (i < 100 + 4) + { + /* check that was added to the back */ + CHECK(list.mAddrs.back().mSeenTime == addr.mSeenTime); + CHECK(list.mAddrs.back().mAddr.sin_addr.s_addr == addr.mAddr.sin_addr.s_addr); + CHECK(list.mAddrs.back().mAddr.sin_port == addr.mAddr.sin_port); + } + else + { + /* check that wasn't added to the back */ + CHECK(list.mAddrs.back().mSeenTime != addr.mSeenTime); + CHECK(list.mAddrs.back().mAddr.sin_addr.s_addr != addr.mAddr.sin_addr.s_addr); + } } /* print out the list */ @@ -97,19 +111,19 @@ bool test_addr_list() } /* make sure it more recent than the previous ones */ - for(int i = 100; i > 89; i--) + for(int i = 99; i > 89; i--) { addr.mSeenTime = time(NULL) - i; list.updateIpAddressList(addr); - /* check that was added to the back */ - CHECK(list.mAddrs.back().mSeenTime == addr.mSeenTime); - CHECK(list.mAddrs.back().mAddr.sin_addr.s_addr == addr.mAddr.sin_addr.s_addr); - CHECK(list.mAddrs.back().mAddr.sin_port == addr.mAddr.sin_port); + /* check that was added to the front */ + CHECK(list.mAddrs.front().mSeenTime == addr.mSeenTime); + CHECK(list.mAddrs.front().mAddr.sin_addr.s_addr == addr.mAddr.sin_addr.s_addr); + CHECK(list.mAddrs.front().mAddr.sin_port == addr.mAddr.sin_port); } /* print out the list */ - std::cerr << "IpAddressList (last item to be 192.168.2.200:8812)"; + std::cerr << "IpAddressList (first item to be 192.168.2.200:8812)"; std::cerr << std::endl; list.printIpAddressList(std::cerr); std::cerr << std::endl; @@ -124,9 +138,9 @@ bool test_addr_list() list.updateIpAddressList(addr); /* check that was added to the back */ - CHECK(list.mAddrs.back().mSeenTime == addr.mSeenTime); - CHECK(list.mAddrs.back().mAddr.sin_addr.s_addr == addr.mAddr.sin_addr.s_addr); - CHECK(list.mAddrs.back().mAddr.sin_port == addr.mAddr.sin_port); + CHECK(list.mAddrs.front().mSeenTime == addr.mSeenTime); + CHECK(list.mAddrs.front().mAddr.sin_addr.s_addr == addr.mAddr.sin_addr.s_addr); + CHECK(list.mAddrs.front().mAddr.sin_port == addr.mAddr.sin_port); }