From 387db6e7b6bd01f3a51b468cac5dd4df39ceebeb Mon Sep 17 00:00:00 2001 From: drbob Date: Wed, 13 Jul 2011 22:23:40 +0000 Subject: [PATCH] Done most of the piping/callback between the DHT and LinkMgr/NetMgr. * Added ConnectionFeedback fn to NetAssistConnect & p3BitDht. * Added TYPE definitions to pqiassist.h * added extra parameters to p3LinkMgr::connectAttempt() (flags which will be used to pass DIRECT/PROXY/DIRECT + PASSIVE/ACTIVE) * Added callback from p3LinkMgr to NetMgr::netAssistStatusUpdate() => Dht::ConnectionFeedback. * updated peerConnectRequest to trigger either TCP attempt (first) or UDP connection. * updated parameters for p3LinkMgrIMPL::tryConnectUDP() * added p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddress() for DHT => TCP attempt. * added extra parameter to addAddressIfUnique... bool addFront => DHT attempts get pushed to front of Queue due to timing requirements. * added extra parameter (flags) to pqiperson::connect() - matches extra parameters to p3LinkMgr::connectAttempt(). * added p3NetMgr::netAssistStatusUpdate() * added mConnectFlags to pqissludp git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-netupgrade@4443 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/dht/p3bitdht.h | 8 ++ libretroshare/src/dht/p3bitdht_peernet.cc | 34 +++++ libretroshare/src/pqi/p3dhtmgr.cc | 4 +- libretroshare/src/pqi/p3linkmgr.cc | 158 +++++++++++++++++++--- libretroshare/src/pqi/p3linkmgr.h | 19 +-- libretroshare/src/pqi/p3netmgr.cc | 18 +++ libretroshare/src/pqi/p3netmgr.h | 5 + libretroshare/src/pqi/pqi_base.h | 1 + libretroshare/src/pqi/pqiassist.h | 2 + libretroshare/src/pqi/pqimonitor.h | 25 +++- libretroshare/src/pqi/pqiperson.cc | 7 +- libretroshare/src/pqi/pqiperson.h | 2 +- libretroshare/src/pqi/pqipersongrp.cc | 14 +- libretroshare/src/pqi/pqissludp.cc | 26 +++- libretroshare/src/pqi/pqissludp.h | 1 + 15 files changed, 275 insertions(+), 49 deletions(-) diff --git a/libretroshare/src/dht/p3bitdht.h b/libretroshare/src/dht/p3bitdht.h index 8542ae157..5715263d4 100644 --- a/libretroshare/src/dht/p3bitdht.h +++ b/libretroshare/src/dht/p3bitdht.h @@ -169,6 +169,9 @@ virtual bool getNetworkStats(uint32_t &netsize, uint32_t &localnetsize); virtual bool findPeer(std::string id); virtual bool dropPeer(std::string id); + /* feedback on success failure of Connections */ +virtual void ConnectionFeedback(std::string pid, int state); + /* extract current peer status */ virtual bool getPeerStatus(std::string id, struct sockaddr_in &laddr, struct sockaddr_in &raddr, @@ -181,12 +184,16 @@ virtual bool getExternalInterface(struct sockaddr_in &raddr, * hould all be removed from NetAssist? */ + + /* pqiNetAssistConnect - external interface functions */ /*********************************************************************************************** ****************************** Connections (p3bitdht_peernet.cc) ****************************** ************************************************************************************************/ + /* Feedback from RS Upper Layers */ +//virtual void ConnectionFeedback(std::string pid, int state); /* Callback functions - from bitdht */ int NodeCallback(const bdId *id, uint32_t peerflags); @@ -213,6 +220,7 @@ void Feedback_ConnectionFailed(std::string pid); void UdpConnectionFailed_locked(DhtPeerDetails *dpd); void Feedback_ConnectionClosed(std::string pid); + /*********************************************************************************************** ************************** Internal Accounting (p3bitdht_peers.cc) **************************** ************************************************************************************************/ diff --git a/libretroshare/src/dht/p3bitdht_peernet.cc b/libretroshare/src/dht/p3bitdht_peernet.cc index cf1249f86..e09a55755 100644 --- a/libretroshare/src/dht/p3bitdht_peernet.cc +++ b/libretroshare/src/dht/p3bitdht_peernet.cc @@ -1357,6 +1357,10 @@ int p3BitDht::checkConnectionAllowed(const bdId *peerId, int mode) /* So initiateConnection has to call out to other bits of RS. * critical information is: * Peer RsId, Peer Address, Connect Mode (includes Proxy/OrNot). + + * + * What do we need: ACTIVE / PASSIVE / UNSPEC + * + Min Delay Time, */ @@ -1760,4 +1764,34 @@ void p3BitDht::Feedback_ConnectionClosed(std::string pid) +void p3BitDht::ConnectionFeedback(std::string pid, int mode) +{ + std::cerr << "p3BitDht::ConnectionFeedback() peer: " << pid; + std::cerr << " mode: " << mode; + std::cerr << std::endl; + + switch(mode) + { + case NETMGR_DHT_FEEDBACK_CONNECTED: + std::cerr << "p3BitDht::ConnectionFeedback() HAVE CONNECTED (tcp?/udp) to peer: " << pid; + std::cerr << std::endl; + + Feedback_Connected(pid); + break; + + case NETMGR_DHT_FEEDBACK_CONN_FAILED: + std::cerr << "p3BitDht::ConnectionFeedback() UDP CONNECTION FAILED to peer: " << pid; + std::cerr << std::endl; + Feedback_ConnectionFailed(pid); + break; + + case NETMGR_DHT_FEEDBACK_CONN_CLOSED: + std::cerr << "p3BitDht::ConnectionFeedback() CONNECTION (tcp?/udp) CLOSED to peer: " << pid; + std::cerr << std::endl; + Feedback_ConnectionClosed(pid); + break; + } +} + + diff --git a/libretroshare/src/pqi/p3dhtmgr.cc b/libretroshare/src/pqi/p3dhtmgr.cc index 2696db498..f5858eb8b 100644 --- a/libretroshare/src/pqi/p3dhtmgr.cc +++ b/libretroshare/src/pqi/p3dhtmgr.cc @@ -942,9 +942,9 @@ int p3DhtMgr::checkNotifyDHT() /* feedback to say we started it! */ #ifdef P3DHTMGR_USE_LOCAL_UDP_CONN - mConnCb->peerConnectRequest(peer.id, peer.laddr, RS_CB_DHT); + mConnCb->peerConnectRequest(peer.id, peer.laddr, RS_CB_DHT, 0, 0); #else - mConnCb->peerConnectRequest(peer.id, peer.raddr, RS_CB_DHT); + mConnCb->peerConnectRequest(peer.id, peer.raddr, RS_CB_DHT, 0, 0); #endif return DHT_MIN_PERIOD; diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index e5e630436..627544520 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -587,7 +587,7 @@ const std::string p3LinkMgrIMPL::getOwnId() bool p3LinkMgrIMPL::connectAttempt(const std::string &id, struct sockaddr_in &addr, - uint32_t &delay, uint32_t &period, uint32_t &type) + uint32_t &delay, uint32_t &period, uint32_t &type, uint32_t &flags) { RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ @@ -630,6 +630,7 @@ bool p3LinkMgrIMPL::connectAttempt(const std::string &id, struct sockaddr_in &ad delay = it->second.currentConnAddrAttempt.delay; period = it->second.currentConnAddrAttempt.period; type = it->second.currentConnAddrAttempt.type; + flags = it->second.currentConnAddrAttempt.flags; #ifdef LINKMGR_DEBUG @@ -812,6 +813,41 @@ bool p3LinkMgrIMPL::connectResult(const std::string &id, bool success, uint32_t } + /* inform NetAssist of result. This is slightly duplicating below, as we switch it on/off + * in a second anyway. However, the FAILURE of UDP connection, must be informed. + * + * actually the way the DHT works at the moment, both forms of feedback are required. + * this handles connection requests, the other searches. As they are independent... do both. + */ + + if (flags == RS_NET_CONN_UDP_ALL) + { +#ifdef LINKMGR_DEBUG +#endif + std::cerr << "p3LinkMgrIMPL::connectResult() Sending Feedback for UDP connection"; + std::cerr << std::endl; + if (success) + { +#ifdef LINKMGR_DEBUG +#endif + std::cerr << "p3LinkMgrIMPL::connectResult() UDP Update CONNECTED to: " << id; + std::cerr << std::endl; + + mNetMgr->netAssistStatusUpdate(id, NETMGR_DHT_FEEDBACK_CONNECTED); + } + else + { +#ifdef LINKMGR_DEBUG +#endif + std::cerr << "p3LinkMgrIMPL::connectResult() UDP Update FAILED to: " << id; + std::cerr << std::endl; + + /* have no differentiation between failure and closed? */ + mNetMgr->netAssistStatusUpdate(id, NETMGR_DHT_FEEDBACK_CONN_FAILED); + } + } + + if (success) { #ifdef LINKMGR_DEBUG @@ -1122,42 +1158,84 @@ void p3LinkMgrIMPL::peerStatus(std::string id, const pqiIpAddrSet &addrs, } -void p3LinkMgrIMPL::peerConnectRequest(std::string id, struct sockaddr_in raddr, - uint32_t source) +void p3LinkMgrIMPL::peerConnectRequest(std::string id, struct sockaddr_in raddr, + uint32_t source, uint32_t flags, uint32_t delay) { #ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::peerConnectRequest() id: " << id << " raddr: " << rs_inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port); - std::cerr << " source: " << source << std::endl; + std::cerr << "p3LinkMgrIMPL::peerConnectRequest() id: " << id; + std::cerr << " raddr: " << rs_inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port); + std::cerr << " source: " << source; + std::cerr << " flags: " << flags; + std::cerr << " delay: " << delay; + std::cerr << std::endl; #endif { /* Log */ std::ostringstream out; out << "p3LinkMgrIMPL::peerConnectRequest() id: " << id << " raddr: " << rs_inet_ntoa(raddr.sin_addr); out << ":" << ntohs(raddr.sin_port) << " source: " << source; + out << " flags: " << flags; + out << " delay: " << delay; rslog(RSL_WARNING, p3connectzone, out.str()); } /******************** TCP PART *****************************/ #ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::peerConnectRequest() Try TCP first" << std::endl; + std::cerr << "p3LinkMgrIMPL::peerConnectRequest() (From DHT Only)" << std::endl; #endif if (source == RS_CB_DHT) { + if (flags & RS_CB_FLAG_MODE_TCP) + { #ifdef LINKMGR_DEBUG - std::cerr << "p3LinkMgrIMPL::peerConnectRequest() source DHT ==> retryConnectUDP()"; - std::cerr << std::endl; + std::cerr << "p3LinkMgrIMPL::peerConnectRequest() DHT says Online ==> so try TCP"; + std::cerr << std::endl; #endif - retryConnectUDP(id, raddr); + { + RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ + + std::map::iterator it; + if (mFriendList.end() == (it = mFriendList.find(id))) + { +#ifdef LINKMGR_DEBUG + std::cerr << "p3LinkMgrIMPL::peerConnectRequest() ERROR Peer is not Friend" << std::endl; +#endif + return; + } + + /* if already connected -> done */ + if (it->second.state & RS_PEER_S_CONNECTED) + { +#ifdef LINKMGR_DEBUG + std::cerr << "p3LinkMgrIMPL::peerConnectRequest() ERROR Peer Already Connected" << std::endl; +#endif + } + /* setup specific attempt for DHT found address. */ + locked_ConnectAttempt_SpecificAddress(&(it->second), &raddr); + } + + retryConnect(id); + } + else + { + /* UDP Attempt! */ +#ifdef LINKMGR_DEBUG + std::cerr << "p3LinkMgrIMPL::peerConnectRequest() DHT says CONNECT ==> tryConnectUDP()"; + std::cerr << std::endl; +#endif + tryConnectUDP(id, raddr, flags, delay); + + } return; + } else { // IS THIS USED??? - std::cerr << "p3LinkMgrIMPL::peerConnectRequest() ERROR source OTHER ==> retryConnect()" << std::endl; + std::cerr << "p3LinkMgrIMPL::peerConnectRequest() ERROR source OTHER ==> NOOP" << std::endl; std::cerr << std::endl; - retryConnect(id); return; } } @@ -1184,7 +1262,7 @@ bool p3LinkMgrIMPL::retryConnect(const std::string &id) -bool p3LinkMgrIMPL::retryConnectUDP(const std::string &id, struct sockaddr_in &rUdpAddr) +bool p3LinkMgrIMPL::tryConnectUDP(const std::string &id, struct sockaddr_in &rUdpAddr, uint32_t flags, uint32_t delay) { RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ @@ -1240,11 +1318,13 @@ bool p3LinkMgrIMPL::retryConnectUDP(const std::string &id, struct sockaddr_in peerConnectAddress pca; pca.addr = rUdpAddr; pca.type = RS_NET_CONN_UDP_PEER_SYNC; - pca.delay = P3CONNMGR_UDP_DEFAULT_DELAY; + pca.delay = delay; pca.ts = time(NULL); pca.period = P3CONNMGR_UDP_DEFAULT_PERIOD; + pca.flags = flags; - addAddressIfUnique(it->second.connAddrs, pca); + // Push address to the front... so it happens quickly (before any timings are lost). + addAddressIfUnique(it->second.connAddrs, pca, true); } /* finish it off */ @@ -1462,6 +1542,33 @@ bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_in *addr, t } +void p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddress(peerConnectState *peer, struct sockaddr_in *remoteAddr) +{ +#ifdef LINKMGR_DEBUG + std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddresses()"; + std::cerr << std::endl; +#endif + if ((remoteAddr) && (locked_CheckPotentialAddr(remoteAddr, 0))) + { +#ifdef LINKMGR_DEBUG + std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddresses() "; + std::cerr << "Adding tcp connection attempt: "; + std::cerr << "Addr: " << rs_inet_ntoa(remoteAddr->sin_addr); + std::cerr << ":" << ntohs(remoteAddr->sin_port); + std::cerr << std::endl; +#endif + peerConnectAddress pca; + pca.addr = *remoteAddr; + pca.type = RS_NET_CONN_TCP_EXTERNAL; + pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY; + pca.ts = time(NULL); + pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; + + addAddressIfUnique(peer->connAddrs, pca, false); + } +} + + void p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses(peerConnectState *peer, struct sockaddr_in *localAddr, struct sockaddr_in *serverAddr) { #ifdef LINKMGR_DEBUG @@ -1486,7 +1593,7 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses(peerConnectState *pe pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - addAddressIfUnique(peer->connAddrs, pca); + addAddressIfUnique(peer->connAddrs, pca, false); } if ((serverAddr) && (locked_CheckPotentialAddr(serverAddr, 0))) @@ -1505,7 +1612,7 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses(peerConnectState *pe pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - addAddressIfUnique(peer->connAddrs, pca); + addAddressIfUnique(peer->connAddrs, pca, false); } } @@ -1542,7 +1649,7 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses(peerConnectState pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - addAddressIfUnique(peer->connAddrs, pca); + addAddressIfUnique(peer->connAddrs, pca, false); } } @@ -1566,7 +1673,7 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses(peerConnectState pca.ts = time(NULL); pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD; - addAddressIfUnique(peer->connAddrs, pca); + addAddressIfUnique(peer->connAddrs, pca, false); } } } @@ -1603,7 +1710,7 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std /* check address validity */ if (locked_CheckPotentialAddr(&(pca.addr), 0)) { - addAddressIfUnique(peer->connAddrs, pca); + addAddressIfUnique(peer->connAddrs, pca, false); } } else @@ -1639,12 +1746,12 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_AddTunnel(peerConnectState *peer) sockaddr_clear(&pca.addr); - addAddressIfUnique(peer->connAddrs, pca); + addAddressIfUnique(peer->connAddrs, pca, false); } } -bool p3LinkMgrIMPL::addAddressIfUnique(std::list &addrList, peerConnectAddress &pca) +bool p3LinkMgrIMPL::addAddressIfUnique(std::list &addrList, peerConnectAddress &pca, bool pushFront) { /* iterate through the list, and make sure it isn't already * in the list @@ -1675,7 +1782,14 @@ bool p3LinkMgrIMPL::addAddressIfUnique(std::list &addrList, std::cerr << std::endl; #endif - addrList.push_back(pca); + if (pushFront) + { + addrList.push_front(pca); + } + else + { + addrList.push_back(pca); + } return true; } diff --git a/libretroshare/src/pqi/p3linkmgr.h b/libretroshare/src/pqi/p3linkmgr.h index cd18caf10..98edfc003 100644 --- a/libretroshare/src/pqi/p3linkmgr.h +++ b/libretroshare/src/pqi/p3linkmgr.h @@ -79,6 +79,7 @@ class peerConnectAddress uint32_t delay; /* to stop simultaneous connects */ uint32_t period; /* UDP only */ uint32_t type; + uint32_t flags; /* CB FLAGS defined in pqimonitor.h */ time_t ts; }; @@ -169,7 +170,7 @@ virtual void removeMonitor(pqiMonitor *mon) = 0; /****************** Connections *******************/ virtual bool connectAttempt(const std::string &id, struct sockaddr_in &addr, - uint32_t &delay, uint32_t &period, uint32_t &type) = 0; + uint32_t &delay, uint32_t &period, uint32_t &type, uint32_t &flags) = 0; virtual bool connectResult(const std::string &id, bool success, uint32_t flags, struct sockaddr_in remote_peer_address) = 0; virtual bool retryConnect(const std::string &id) = 0; @@ -192,8 +193,8 @@ virtual bool getTunnelConnection() = 0; // ONLY used by p3peermgr.cc & p3 // THESE MUSTn't BE specfied HERE - as overloaded from pqiConnectCb. //virtual void peerStatus(std::string id, const pqiIpAddrSet &addrs, // uint32_t type, uint32_t flags, uint32_t source) = 0; -//virtual void peerConnectRequest(std::string id, -// struct sockaddr_in raddr, uint32_t source) = 0; +//virtual void peerConnectRequest(std::string id, struct sockaddr_in raddr, +// uint32_t source, uint32_t flags, uint32_t delay) = 0; /****************************************************************************/ /****************************************************************************/ @@ -222,7 +223,7 @@ virtual void removeMonitor(pqiMonitor *mon); /****************** Connections *******************/ virtual bool connectAttempt(const std::string &id, struct sockaddr_in &addr, - uint32_t &delay, uint32_t &period, uint32_t &type); + uint32_t &delay, uint32_t &period, uint32_t &type, uint32_t &flags); virtual bool connectResult(const std::string &id, bool success, uint32_t flags, struct sockaddr_in remote_peer_address); virtual bool retryConnect(const std::string &id); @@ -233,8 +234,8 @@ virtual struct sockaddr_in getLocalAddress(); /******* overloaded from pqiConnectCb *************/ virtual void peerStatus(std::string id, const pqiIpAddrSet &addrs, uint32_t type, uint32_t flags, uint32_t source); -virtual void peerConnectRequest(std::string id, - struct sockaddr_in raddr, uint32_t source); +virtual void peerConnectRequest(std::string id, struct sockaddr_in raddr, + uint32_t source, uint32_t flags, uint32_t delay); /************* DEPRECIATED FUNCTIONS (TO REMOVE) ********/ @@ -280,11 +281,12 @@ void statusTick(); void tickMonitors(); /* connect attempts UDP */ -bool retryConnectUDP(const std::string &id, struct sockaddr_in &rUdpAddr); +bool tryConnectUDP(const std::string &id, struct sockaddr_in &rUdpAddr, uint32_t flags, uint32_t delay); /* connect attempts TCP */ bool retryConnectTCP(const std::string &id); +void locked_ConnectAttempt_SpecificAddress(peerConnectState *peer, struct sockaddr_in *remoteAddr); void locked_ConnectAttempt_CurrentAddresses(peerConnectState *peer, struct sockaddr_in *localAddr, struct sockaddr_in *serverAddr); void locked_ConnectAttempt_HistoricalAddresses(peerConnectState *peer, const pqiIpAddrSet &ipAddrs); void locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std::string dyndns, uint16_t dynPort); @@ -293,8 +295,7 @@ void locked_ConnectAttempt_AddTunnel(peerConnectState *peer); bool locked_ConnectAttempt_Complete(peerConnectState *peer); bool locked_CheckPotentialAddr(const struct sockaddr_in *addr, time_t age); -bool addAddressIfUnique(std::list &addrList, - peerConnectAddress &pca); +bool addAddressIfUnique(std::list &addrList, peerConnectAddress &pca, bool pushFront); private: diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index 73896e1d3..aec2abade 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -1306,6 +1306,24 @@ bool p3NetMgrIMPL::netAssistFriend(std::string id, bool on) } + +bool p3NetMgrIMPL::netAssistStatusUpdate(std::string id, int state) +{ + std::map::iterator it; + +#ifdef NETMGR_DEBUG + std::cerr << "p3NetMgrIMPL::netAssistFriend(" << id << ", " << on << ")"; + std::cerr << std::endl; +#endif + + for(it = mDhts.begin(); it != mDhts.end(); it++) + { + (it->second)->ConnectionFeedback(id, state); + } + return true; +} + + bool p3NetMgrIMPL::netAssistSetAddress( struct sockaddr_in &laddr, struct sockaddr_in &eaddr, uint32_t mode) diff --git a/libretroshare/src/pqi/p3netmgr.h b/libretroshare/src/pqi/p3netmgr.h index 87c32f8ed..a04108454 100644 --- a/libretroshare/src/pqi/p3netmgr.h +++ b/libretroshare/src/pqi/p3netmgr.h @@ -93,6 +93,9 @@ class p3BitDht; class UdpRelayReceiver; +#define NETMGR_DHT_FEEDBACK_CONNECTED 0x0001 +#define NETMGR_DHT_FEEDBACK_CONN_FAILED 0x0002 +#define NETMGR_DHT_FEEDBACK_CONN_CLOSED 0x0003 /********** * p3NetMgr Interface.... @@ -115,6 +118,7 @@ virtual bool setVisState(uint32_t visState) = 0; // Switch DHT On/Off. virtual bool netAssistFriend(std::string id, bool on) = 0; +virtual bool netAssistStatusUpdate(std::string id, int mode) = 0; /* Get Network State */ virtual uint32_t getNetStateMode() = 0; @@ -168,6 +172,7 @@ virtual bool setVisState(uint32_t visState); // Switch DHT On/Off. virtual bool netAssistFriend(std::string id, bool on); +virtual bool netAssistStatusUpdate(std::string id, int mode); /* Get Network State */ virtual uint32_t getNetStateMode(); diff --git a/libretroshare/src/pqi/pqi_base.h b/libretroshare/src/pqi/pqi_base.h index 21b21f1b6..3a49c4647 100644 --- a/libretroshare/src/pqi/pqi_base.h +++ b/libretroshare/src/pqi/pqi_base.h @@ -229,6 +229,7 @@ static const int NET_CONNECT_FAILED = 5; static const uint32_t NET_PARAM_CONNECT_DELAY = 1; static const uint32_t NET_PARAM_CONNECT_PERIOD = 2; static const uint32_t NET_PARAM_CONNECT_TIMEOUT = 3; +static const uint32_t NET_PARAM_CONNECT_FLAGS = 4; /*! diff --git a/libretroshare/src/pqi/pqiassist.h b/libretroshare/src/pqi/pqiassist.h index 8ae0d67d7..0ad6866cc 100644 --- a/libretroshare/src/pqi/pqiassist.h +++ b/libretroshare/src/pqi/pqiassist.h @@ -118,6 +118,8 @@ virtual int tick() = 0; /* for internal accounting */ virtual bool findPeer(std::string id) = 0; virtual bool dropPeer(std::string id) = 0; +virtual void ConnectionFeedback(std::string pid, int mode) = 0; + /* extract current peer status */ virtual bool getPeerStatus(std::string id, struct sockaddr_in &laddr, struct sockaddr_in &raddr, diff --git a/libretroshare/src/pqi/pqimonitor.h b/libretroshare/src/pqi/pqimonitor.h index 46bb99855..2c375724f 100644 --- a/libretroshare/src/pqi/pqimonitor.h +++ b/libretroshare/src/pqi/pqimonitor.h @@ -65,14 +65,25 @@ const uint32_t RS_STUN_FRIEND = 0x0020; const uint32_t RS_STUN_FRIEND_OF_FRIEND = 0x0040; - +// for the old p3dhtmgr - amazed it still compiles ;) #define RS_CONNECT_PASSIVE 1 #define RS_CONNECT_ACTIVE 2 -#define RS_CB_DHT 1 /* from dht */ -#define RS_CB_DISC 2 /* from peers */ -#define RS_CB_PERSON 3 /* from connection */ -#define RS_CB_PROXY 4 /* via proxy */ +#define RS_CB_DHT 0x0001 /* from dht */ +#define RS_CB_DISC 0x0002 /* from peers */ +#define RS_CB_PERSON 0x0003 /* from connection */ +#define RS_CB_PROXY 0x0004 /* via proxy */ + + +#define RS_CB_FLAG_MASK_MODE 0x00ff +#define RS_CB_FLAG_MASK_CONN 0x00ff + +#define RS_CB_FLAG_MODE_TCP 0x0001 +#define RS_CB_FLAG_MODE_UDP 0x0002 + +#define RS_CB_FLAG_CONN_UNSPEC 0x0000 +#define RS_CB_FLAG_CONN_PASSIVE 0x0100 +#define RS_CB_FLAG_CONN_ACTIVE 0x0200 class pqipeer @@ -137,8 +148,8 @@ virtual ~pqiConnectCb() { return; } virtual void peerStatus(std::string id, const pqiIpAddrSet &addrs, uint32_t type, uint32_t flags, uint32_t source) = 0; -virtual void peerConnectRequest(std::string id, - struct sockaddr_in raddr, uint32_t source) = 0; +virtual void peerConnectRequest(std::string id, struct sockaddr_in raddr, + uint32_t source, uint32_t flags, uint32_t delay) = 0; //virtual void stunStatus(std::string id, struct sockaddr_in raddr, uint32_t type, uint32_t flags) = 0; }; diff --git a/libretroshare/src/pqi/pqiperson.cc b/libretroshare/src/pqi/pqiperson.cc index d8bc0b034..e4997ea20 100644 --- a/libretroshare/src/pqi/pqiperson.cc +++ b/libretroshare/src/pqi/pqiperson.cc @@ -378,7 +378,7 @@ int pqiperson::stoplistening() return 1; } -int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, uint32_t delay, uint32_t period, uint32_t timeout) +int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags) { #ifdef PERSON_DEBUG { @@ -390,6 +390,7 @@ int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, uint32_t delay, out << " delay: " << delay; out << " period: " << period; out << " timeout: " << timeout; + out << " flags: " << flags; out << std::endl; std::cerr << out.str(); //pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str()); @@ -416,6 +417,9 @@ int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, uint32_t delay, return 0; } + std::cerr << "pqiperson::connect() WARNING, resetting for new connection attempt" << std::endl; +#ifdef PERSON_DEBUG +#endif /* set the parameters */ (it->second)->reset(); @@ -425,6 +429,7 @@ int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, uint32_t delay, (it->second)->connect_parameter(NET_PARAM_CONNECT_DELAY, delay); (it->second)->connect_parameter(NET_PARAM_CONNECT_PERIOD, period); (it->second)->connect_parameter(NET_PARAM_CONNECT_TIMEOUT, timeout); + (it->second)->connect_parameter(NET_PARAM_CONNECT_FLAGS, flags); (it->second)->connect(raddr); diff --git a/libretroshare/src/pqi/pqiperson.h b/libretroshare/src/pqi/pqiperson.h index 3e6478c33..669b170e9 100644 --- a/libretroshare/src/pqi/pqiperson.h +++ b/libretroshare/src/pqi/pqiperson.h @@ -106,7 +106,7 @@ virtual ~pqiperson(); // must clean up children. int reset(); int listen(); int stoplistening(); -int connect(uint32_t type, struct sockaddr_in raddr, uint32_t delay, uint32_t period, uint32_t timeout); +int connect(uint32_t type, struct sockaddr_in raddr, uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags); int receiveHeartbeat(); // add in connection method. int addChildInterface(uint32_t type, pqiconnect *pqi); diff --git a/libretroshare/src/pqi/pqipersongrp.cc b/libretroshare/src/pqi/pqipersongrp.cc index f57f3100b..a85a8a19d 100644 --- a/libretroshare/src/pqi/pqipersongrp.cc +++ b/libretroshare/src/pqi/pqipersongrp.cc @@ -492,10 +492,12 @@ int pqipersongrp::connectPeer(std::string id std::cerr << std::endl; #endif - { RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/ - if (id == mLinkMgr->getOwnId()) { + { + RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/ + if (id == mLinkMgr->getOwnId()) + { + std::cerr << "pqipersongrp::connectPeer() ERROR Failed, connecting to own id." << std::endl; #ifdef PGRP_DEBUG - std::cerr << "pqipersongrp::connectPeer() Failed, connecting to own id." << std::endl; #endif return 0; } @@ -546,8 +548,9 @@ int pqipersongrp::connectPeer(std::string id uint32_t period; uint32_t timeout; uint32_t type; + uint32_t flags; - if (!mLinkMgr->connectAttempt(id, addr, delay, period, type)) + if (!mLinkMgr->connectAttempt(id, addr, delay, period, type, flags)) { #ifdef PGRP_DEBUG std::cerr << " pqipersongrp::connectPeer() No Net Address"; @@ -562,6 +565,7 @@ int pqipersongrp::connectPeer(std::string id std::cerr << " delay: " << delay; std::cerr << " period: " << period; std::cerr << " type: " << type; + std::cerr << " flags: " << flags; std::cerr << std::endl; #endif @@ -603,7 +607,7 @@ int pqipersongrp::connectPeer(std::string id return 0; } - p->connect(ptype, addr, delay, period, timeout); + p->connect(ptype, addr, delay, period, timeout, flags); } /* UNLOCKED */ diff --git a/libretroshare/src/pqi/pqissludp.cc b/libretroshare/src/pqi/pqissludp.cc index c22eb9d6d..adb81b68f 100644 --- a/libretroshare/src/pqi/pqissludp.cc +++ b/libretroshare/src/pqi/pqissludp.cc @@ -88,6 +88,8 @@ pqissludp::~pqissludp() int pqissludp::reset() { /* reset for next time.*/ + mConnectFlags = 0; + mConnectPeriod = PQI_SSLUDP_DEF_CONN_PERIOD; return pqissl::reset(); } @@ -131,14 +133,25 @@ int pqissludp::Initiate_Connection() "pqissludp::Initiate_Connection() Attempting Outgoing Connection...."); /* decide if we're active or passive */ - if (PeerId() < mLinkMgr->getOwnId()) + if (mConnectFlags & RS_CB_FLAG_CONN_ACTIVE) { sslmode = PQISSL_ACTIVE; } - else + else if (mConnectFlags & RS_CB_FLAG_CONN_PASSIVE) { sslmode = PQISSL_PASSIVE; } + else // likely UNSPEC - use old method to decide. + { + if (PeerId() < mLinkMgr->getOwnId()) + { + sslmode = PQISSL_ACTIVE; + } + else + { + sslmode = PQISSL_PASSIVE; + } + } if (waiting != WAITING_DELAY) { @@ -419,6 +432,15 @@ bool pqissludp::connect_parameter(uint32_t type, uint32_t value) mConnectPeriod = value; return true; } + else if (type == NET_PARAM_CONNECT_FLAGS) + { + std::ostringstream out; + out << "pqissludp::connect_parameter() Peer: " << PeerId() << " FLAGS: " << value; + rslog(RSL_WARNING, pqissludpzone, out.str()); + + mConnectFlags = value; + return true; + } return pqissl::connect_parameter(type, value); } diff --git a/libretroshare/src/pqi/pqissludp.h b/libretroshare/src/pqi/pqissludp.h index 04e63f8b6..df804f00e 100644 --- a/libretroshare/src/pqi/pqissludp.h +++ b/libretroshare/src/pqi/pqissludp.h @@ -99,6 +99,7 @@ private: long listen_checktime; uint32_t mConnectPeriod; + uint32_t mConnectFlags; }; #endif // MRK_PQI_SSL_UDP_HEADER