diff --git a/libbitdht/src/bitdht/bdconnection.cc b/libbitdht/src/bitdht/bdconnection.cc index 5ffaf4e32..2329c7823 100644 --- a/libbitdht/src/bitdht/bdconnection.cc +++ b/libbitdht/src/bitdht/bdconnection.cc @@ -35,12 +35,15 @@ #include "util/bdrandom.h" /* + * #define DEBUG_PROXY_CONNECTION 1 * #define DEBUG_NODE_CONNECTION 1 * #define DEBUG_NODE_CONNECTION_EXTRA 1 * #define DEBUG_CONNECTION_DELAY 1 */ -#define DEBUG_CONNECTION_DELAY 1 +//#define DEBUG_PROXY_CONNECTION 1 +//#define DEBUG_NODE_CONNECTION 1 +//#define DEBUG_CONNECTION_DELAY 1 #define BITDHT_CR_PAUSE_SHORT_PERIOD 1 @@ -341,7 +344,7 @@ int bdConnectManager::requestConnection_direct(struct sockaddr_in *laddr, bdNode int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay) { -#ifdef DEBUG_NODE_CONNECTION +#ifdef DEBUG_PROXY_CONNECTION std::cerr << "bdConnectManager::requestConnection_proxy()"; std::cerr << std::endl; #endif @@ -349,7 +352,7 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI if (checkExistingConnectionAttempt(target)) { -#ifdef DEBUG_NODE_CONNECTION +#ifdef DEBUG_PROXY_CONNECTION std::cerr << "bdConnectManager::requestConnection_proxy() Existing ConnectionRequest... NOOP"; std::cerr << std::endl; #endif @@ -360,10 +363,28 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI bdConnectionRequest connreq; connreq.setupProxyConnection(laddr, target, mode, delay); - /* grab any peers from any existing query */ + /**** + * We want different behaviour here - depending on whether it is a proxy or a relay request + * furthermore if we are in relay server mode - we should only use those. + * + * Try to unify logic. + * + * Proxy - take potential proxies... + * - if RelayServerMode then relays. + * - take friends, friends of friends. + * + * Relay - + * - if RelayServerMode take relays. + * - take friends, friends of friends + * - take potential proxies. + */ + + + /* get the proxy lists from existing query */ std::list::iterator pit; + std::list goodProxies; std::list potentialProxies; - mQueryMgr->proxies(target, connreq.mGoodProxies); + mQueryMgr->proxies(target, goodProxies); mQueryMgr->potentialProxies(target, potentialProxies); /* check any potential proxies, must be same DHT Type */ @@ -372,7 +393,12 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI /* check the type in bdSpace */ if (checkPeerForFlag(&(*pit), BITDHT_PEER_STATUS_DHT_ENGINE_VERSION)) { - connreq.mGoodProxies.push_back(*pit); +#ifdef DEBUG_PROXY_CONNECTION + std::cerr << "bdConnectManager::requestConnection_proxy() Shifting Pot -> Good: "; + mFns->bdPrintId(std::cerr, &(*pit)); + std::cerr << std::endl; +#endif + goodProxies.push_back(*pit); pit = potentialProxies.erase(pit); } else @@ -381,6 +407,108 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI } } + /* in proxy mode - put Good Proxies First */ + if (mode == BITDHT_CONNECT_MODE_PROXY) + { + for(pit = goodProxies.begin(); pit != goodProxies.end(); pit++) + { +#ifdef DEBUG_PROXY_CONNECTION + std::cerr << "bdConnectManager::requestConnection_proxy() Adding Good Proxy: "; + mFns->bdPrintId(std::cerr, &(*pit)); + std::cerr << std::endl; +#endif + connreq.mGoodProxies.push_back(*pit); + } + } + + + if (mRelayMode) + { + /* Add Relay Servers */ +#ifdef DEBUG_PROXY_CONNECTION + std::cerr << "bdConnectManager::requestConnection_proxy() In RelayMode... adding Relays"; +#endif + + std::list excluding; + std::multimap nearest; + + int number = CONNECT_NUM_PROXY_ATTEMPTS; + + mNodeSpace->find_nearest_nodes_with_flags(target, number, excluding, nearest, + BITDHT_PEER_STATUS_DHT_RELAY_SERVER); + + std::multimap::iterator it; + for(it = nearest.begin(); it != nearest.end(); it++) + { +#ifdef DEBUG_PROXY_CONNECTION + std::cerr << "bdConnectManager::requestConnection_proxy() Adding Relay Server: "; + mFns->bdPrintId(std::cerr, &(it->second)); + std::cerr << std::endl; +#endif + connreq.mGoodProxies.push_back(it->second); + } + } + + { + std::list excluding; + std::multimap nearest; + + int number = CONNECT_NUM_PROXY_ATTEMPTS; + + mNodeSpace->find_nearest_nodes_with_flags(target, number, excluding, nearest, + BITDHT_PEER_STATUS_DHT_FRIEND); + + std::multimap::iterator it; + for(it = nearest.begin(); it != nearest.end(); it++) + { +#ifdef DEBUG_PROXY_CONNECTION + std::cerr << "bdConnectManager::requestConnection_proxy() Adding Friend: "; + mFns->bdPrintId(std::cerr, &(it->second)); + std::cerr << std::endl; +#endif + connreq.mGoodProxies.push_back(it->second); + } + } + + + /* in relay mode - Good Proxies are the BackUp */ + if (mode == BITDHT_CONNECT_MODE_RELAY) + { + for(pit = goodProxies.begin(); pit != goodProxies.end(); pit++) + { +#ifdef DEBUG_PROXY_CONNECTION + std::cerr << "bdConnectManager::requestConnection_proxy() Adding Good Proxy: "; + mFns->bdPrintId(std::cerr, &(*pit)); + std::cerr << std::endl; +#endif + connreq.mGoodProxies.push_back(*pit); + } + } + + // Final Desperate Measures! + if (connreq.mGoodProxies.size() < MED_START_PROXY_COUNT) + { + std::list excluding; + std::multimap nearest; + + int number = CONNECT_NUM_PROXY_ATTEMPTS; + + mNodeSpace->find_nearest_nodes_with_flags(target, number, excluding, nearest, + BITDHT_PEER_STATUS_DHT_FOF); + + std::multimap::iterator it; + for(it = nearest.begin(); it != nearest.end(); it++) + { +#ifdef DEBUG_PROXY_CONNECTION + std::cerr << "bdConnectManager::requestConnection_proxy() Querying FOF: "; + mFns->bdPrintId(std::cerr, &(it->second)); + std::cerr << std::endl; +#endif + //connreq.mGoodProxies.push_back(it->second); + mPub->send_query(&(it->second), target); + } + } + /* if we don't have enough proxies ... ping the potentials */ if (connreq.mGoodProxies.size() < MED_START_PROXY_COUNT) @@ -396,7 +524,7 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI mPub->send_ping(&(*pit)); -#ifdef DEBUG_NODE_CONNECTION +#ifdef DEBUG_PROXY_CONNECTION std::cerr << "bdConnectManager::requestConnection_proxy() Pinging Potential Proxy"; mFns->bdPrintId(std::cerr, &(*pit)); std::cerr << std::endl; @@ -404,6 +532,12 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI } } + /*** ORIGINAL CODE - TRIES TO PING/SEARCH PEERS - INSTEAD OF JUST TRYING THEM + * NOT SURE WHAT THE BEST PLAN IS..... + * LEAVE THIS CODE HERE FOR REFERENCE.... + ***/ + +#if 0 // Final Desperate Measures! if (connreq.mGoodProxies.size() < MED_START_PROXY_COUNT) { @@ -445,6 +579,7 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI mPub->send_query(&(it->second), target); } } +#endif if (connreq.mGoodProxies.size() < 1) @@ -455,7 +590,7 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI if (connreq.mGoodProxies.size() < MIN_START_PROXY_COUNT) { -#ifdef DEBUG_NODE_CONNECTION +#ifdef DEBUG_PROXY_CONNECTION std::cerr << "bdConnectManager::requestConnection_proxy() WARNING initial proxyList.size() == SMALL PAUSING"; std::cerr << std::endl; #endif @@ -466,7 +601,7 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI connreq.mPauseTS = now + BITDHT_CR_PAUSE_START_PERIOD; } -#ifdef DEBUG_NODE_CONNECTION +#ifdef DEBUG_PROXY_CONNECTION std::cerr << "bdConnectManager::requestConnection_proxy() CRINITSTATE Init Connection State"; std::cerr << std::endl; std::cerr << connreq; @@ -480,6 +615,9 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI return 1; } + + + void bdConnectManager::addPotentialConnectionProxy(const bdId *srcId, const bdId *target) { #ifdef DEBUG_NODE_CONNECTION_EXTRA diff --git a/libbitdht/src/bitdht/bdmanager.cc b/libbitdht/src/bitdht/bdmanager.cc index a7ee7f256..e0bb28bec 100644 --- a/libbitdht/src/bitdht/bdmanager.cc +++ b/libbitdht/src/bitdht/bdmanager.cc @@ -1434,3 +1434,26 @@ int bdDebugCallback::dhtConnectCallback(const bdId *srcId, const bdId *proxyId, return 1; } + +int bdDebugCallback::dhtInfoCallback(const bdId *id, uint32_t type, uint32_t flags, std::string info) +{ + /* remove unused parameter warnings */ + (void) id; + (void) type; + (void) flags; + (void) info; + +#ifdef DEBUG_MGR + std::cerr << "bdDebugCallback::dhtInfoCallback() Type: " << type; + std::cerr << " Id: "; + bdStdPrintId(std::cerr, id); + std::cerr << " flags: " << flags; + std::cerr << " info: " << info; + std::cerr << std::endl; +#endif + + return 1; +} + + + diff --git a/libbitdht/src/bitdht/bdmanager.h b/libbitdht/src/bitdht/bdmanager.h index b29aaa759..635a5eb3f 100644 --- a/libbitdht/src/bitdht/bdmanager.h +++ b/libbitdht/src/bitdht/bdmanager.h @@ -201,6 +201,7 @@ virtual int dhtPeerCallback(const bdId *id, uint32_t status); virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status); virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId, uint32_t mode, uint32_t point, uint32_t param, uint32_t cbtype, uint32_t errcode); +virtual int dhtInfoCallback(const bdId *id, uint32_t type, uint32_t flags, std::string info); }; diff --git a/libbitdht/src/bitdht/bdnode.cc b/libbitdht/src/bitdht/bdnode.cc index b48660853..758cb89ed 100644 --- a/libbitdht/src/bitdht/bdnode.cc +++ b/libbitdht/src/bitdht/bdnode.cc @@ -60,9 +60,12 @@ * #define DEBUG_NODE_MSGIN 1 * #define DEBUG_NODE_MSGOUT 1 + * + * #define DISABLE_BAD_PEER_FILTER 1 + * ***/ -//#define DEBUG_NODE_MSGS 1 +//#define DISABLE_BAD_PEER_FILTER 1 bdNode::bdNode(bdNodeId *ownId, std::string dhtVersion, std::string bootfile, bdDhtFunctions *fns) @@ -461,9 +464,6 @@ void bdNode::send_connect_msg(bdId *id, int msgtype, bdId *srcAddr, bdId *destAd } - -//#define DISABLE_BAD_PEER_FILTER 1 - void bdNode::checkPotentialPeer(bdId *id, bdId *src) { /* Check BadPeer Filters for Potential Peers too */ @@ -488,6 +488,7 @@ void bdNode::checkPotentialPeer(bdId *id, bdId *src) { if (knownAddr.sin_addr.s_addr != id->addr.sin_addr.s_addr) { +#ifndef DISABLE_BAD_PEER_FILTER std::cerr << "bdNode::checkPotentialPeer("; mFns->bdPrintId(std::cerr, id); std::cerr << ") MASQARADING AS KNOWN PEER - FLAGGING AS BAD"; @@ -496,7 +497,6 @@ void bdNode::checkPotentialPeer(bdId *id, bdId *src) // Stores in queue for later callback and desemination around the network. mBadPeerQueue.queuePeer(id, 0); -#ifndef DISABLE_BAD_PEER_FILTER mFilterPeers->addPeerToFilter(id, 0); std::list filteredIPs; @@ -574,6 +574,7 @@ void bdNode::addPeer(const bdId *id, uint32_t peerflags) { if (knownAddr.sin_addr.s_addr != id->addr.sin_addr.s_addr) { +#ifndef DISABLE_BAD_PEER_FILTER std::cerr << "bdNode::addPeer("; mFns->bdPrintId(std::cerr, id); std::cerr << ", " << std::hex << peerflags << std::dec; @@ -584,13 +585,11 @@ void bdNode::addPeer(const bdId *id, uint32_t peerflags) // Stores in queue for later callback and desemination around the network. mBadPeerQueue.queuePeer(id, peerflags); -#ifndef DISABLE_BAD_PEER_FILTER mFilterPeers->addPeerToFilter(id, peerflags); std::list filteredIPs; mFilterPeers->filteredIPs(filteredIPs); mStore.filterIpList(filteredIPs); -#endif // DO WE EXPLICITLY NEED TO DO THIS, OR WILL THEY JUST BE DROPPED? //mNodeSpace.remove_badpeer(id); @@ -599,7 +598,6 @@ void bdNode::addPeer(const bdId *id, uint32_t peerflags) // FLAG in NodeSpace (Should be dropped very quickly anyway) mNodeSpace.flagpeer(id, 0, BITDHT_PEER_EXFLAG_BADPEER); -#ifndef DISABLE_BAD_PEER_FILTER return; #endif } @@ -657,15 +655,23 @@ void bdNode::processRemoteQuery() case BD_QUERY_NEIGHBOURS: { /* search bdSpace for neighbours */ - //std::list excludeList; std::list nearList; std::multimap nearest; std::multimap::iterator it; - //mNodeSpace.find_nearest_nodes(&(query.mQuery), BITDHT_QUERY_NEIGHBOUR_PEERS, excludeList, nearest, 0); + if (mRelayMode == BITDHT_RELAYS_SERVER) + { + std::list excludeList; + mNodeSpace.find_nearest_nodes_with_flags(&(query.mQuery), + BITDHT_QUERY_NEIGHBOUR_PEERS, + excludeList, nearest, BITDHT_PEER_STATUS_DHT_RELAY_SERVER); + } + else + { mNodeSpace.find_nearest_nodes(&(query.mQuery), BITDHT_QUERY_NEIGHBOUR_PEERS, nearest); + } for(it = nearest.begin(); it != nearest.end(); it++) { diff --git a/libbitdht/src/example/Makefile b/libbitdht/src/example/Makefile index b71d1f898..eeb1c8c0b 100644 --- a/libbitdht/src/example/Makefile +++ b/libbitdht/src/example/Makefile @@ -1,6 +1,6 @@ CXXFLAGS = -Wall -g -I.. -#CXXFLAGS += -arch i386 # OSX +CXXFLAGS += -arch i386 # OSX LIBS = -L../lib -lbitdht diff --git a/libbitdht/src/example/bdhandler.cc b/libbitdht/src/example/bdhandler.cc index 5b3e9b788..faa6afb88 100644 --- a/libbitdht/src/example/bdhandler.cc +++ b/libbitdht/src/example/bdhandler.cc @@ -45,7 +45,7 @@ virtual int dhtNodeCallback(const bdId *id, uint32_t peerflags) return mParent->NodeCallback(id, peerflags); } -virtual int dhtPeerCallback(const bdNodeId *id, uint32_t status) +virtual int dhtPeerCallback(const bdId *id, uint32_t status) { return mParent->PeerCallback(id, status); } @@ -55,6 +55,16 @@ virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t statu return mParent->ValueCallback(id, key, status); } +virtual int dhtConnectCallback(const bdId*, const bdId*, const bdId*, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t) +{ + return 1; +} + +virtual int dhtInfoCallback(const bdId*, uint32_t, uint32_t, std::string) +{ + return 1; +} + private: BitDhtHandler *mParent; @@ -187,10 +197,10 @@ int BitDhtHandler::NodeCallback(const bdId *id, uint32_t peerflags) return 0; } -int BitDhtHandler::PeerCallback(const bdNodeId *id, uint32_t status) +int BitDhtHandler::PeerCallback(const bdId *id, uint32_t status) { std::cerr << "BitDhtHandler::PeerCallback() NodeId: "; - bdStdPrintNodeId(std::cerr, id); + bdStdPrintId(std::cerr, id); std::cerr << std::endl; bool connect = false; diff --git a/libbitdht/src/example/bdhandler.h b/libbitdht/src/example/bdhandler.h index 88ef8bbee..e2ad55dc8 100644 --- a/libbitdht/src/example/bdhandler.h +++ b/libbitdht/src/example/bdhandler.h @@ -55,7 +55,7 @@ bool FindNode(bdNodeId *peerId); bool DropNode(bdNodeId *peerId); virtual int NodeCallback(const bdId *id, uint32_t peerflags); -virtual int PeerCallback(const bdNodeId *id, uint32_t status); +virtual int PeerCallback(const bdId *id, uint32_t status); virtual int ValueCallback(const bdNodeId *id, std::string key, uint32_t status); private: diff --git a/libbitdht/src/tests/scripts/config.mk b/libbitdht/src/tests/scripts/config.mk index 0859a4799..67b25b29f 100644 --- a/libbitdht/src/tests/scripts/config.mk +++ b/libbitdht/src/tests/scripts/config.mk @@ -4,8 +4,8 @@ ########################################################################### #Define OS. # -OS = Linux -#OS = MacOSX +#OS = Linux +OS = MacOSX #OS = Cygwin #OS = Win # MinGw. ########################################################################### diff --git a/libbitdht/src/udp/udpbitdht.cc b/libbitdht/src/udp/udpbitdht.cc index 6474bd444..17832a6a8 100644 --- a/libbitdht/src/udp/udpbitdht.cc +++ b/libbitdht/src/udp/udpbitdht.cc @@ -51,8 +51,8 @@ // Original RS 0.5.0/0.5.1 version, is un-numbered. //#define BITDHT_VERSION "00" // First Release of BitDHT with Connections (Proxy Support + Dht Stun) -#define BITDHT_VERSION "01" // Testing Connections -//#define BITDHT_VERSION "02" // Completed? Full Connections +//#define BITDHT_VERSION "01" // Testing Connections (Proxy Only) +#define BITDHT_VERSION "02" // Completed Relay Connections from svn 4766 //#define BITDHT_VERSION "04" // Full DHT implementation.? /*************************************/ diff --git a/libretroshare/src/dht/connectstatebox.cc b/libretroshare/src/dht/connectstatebox.cc index 6a005dc54..d2fa72319 100644 --- a/libretroshare/src/dht/connectstatebox.cc +++ b/libretroshare/src/dht/connectstatebox.cc @@ -38,6 +38,7 @@ * **/ + /* Have made the PROXY Attempts + MAX_TIME much larger, * have have potential for this to take a while. */ @@ -46,18 +47,18 @@ #define FAILED_WAIT_TIME (1800) // 5 minutes. #define TCP_WAIT_TIME (10) // 1/6 minutes. #define DIRECT_MAX_WAIT_TIME (30) // 1/6 minutes. - #define PROXY_BASE_WAIT_TIME (30) // 1/6 minutes. - #define PROXY_MAX_WAIT_TIME (120) // 1/6 minutes. + #define PROXY_BASE_WAIT_TIME (10) // 30 // 1/6 minutes. + #define PROXY_MAX_WAIT_TIME (30) // 120 // 1/6 minutes. #define RELAY_MAX_WAIT_TIME (30) // 1/6 minutes. #define REVERSE_WAIT_TIME (30) // 1/2 minutes. #define MAX_DIRECT_ATTEMPTS (3) - #define MAX_PROXY_ATTEMPTS (10) + #define MAX_PROXY_ATTEMPTS (3) #define MAX_RELAY_ATTEMPTS (3) #define MAX_DIRECT_FAILED_ATTEMPTS (1) #define MAX_PROXY_FAILED_ATTEMPTS (2) - #define MAX_RELAY_FAILED_ATTEMPTS (1) + #define MAX_RELAY_FAILED_ATTEMPTS (2) #else #define FAILED_WAIT_TIME (1800) // 30 minutes. #define TCP_WAIT_TIME (60) // 1 minutes. diff --git a/libretroshare/src/dht/p3bitdht.cc b/libretroshare/src/dht/p3bitdht.cc index a6616bb08..f6150f076 100644 --- a/libretroshare/src/dht/p3bitdht.cc +++ b/libretroshare/src/dht/p3bitdht.cc @@ -96,6 +96,8 @@ p3BitDht::p3BitDht(std::string id, pqiConnectCb *cb, p3NetMgr *nm, std::string dhtVersion = "RS51"; // should come from elsewhere! mOwnRsId = id; + mMinuteTS = 0; + #ifdef DEBUG_BITDHT std::cerr << "p3BitDht::p3BitDht()" << std::endl; std::cerr << "Using Id: " << id; diff --git a/libretroshare/src/dht/p3bitdht_peernet.cc b/libretroshare/src/dht/p3bitdht_peernet.cc index 642775689..1ec63604e 100644 --- a/libretroshare/src/dht/p3bitdht_peernet.cc +++ b/libretroshare/src/dht/p3bitdht_peernet.cc @@ -32,7 +32,6 @@ **/ - /****************************************************************************************** ************************************* Dht Callback *************************************** ******************************************************************************************/ @@ -1087,6 +1086,7 @@ int p3BitDht::tick() } #define MINUTE_IN_SECS 60 +#define TEN_IN_SECS 10 int p3BitDht::minuteTick() { @@ -1098,7 +1098,8 @@ int p3BitDht::minuteTick() deltaT = now-mMinuteTS; } - if (deltaT > MINUTE_IN_SECS) + //if (deltaT > MINUTE_IN_SECS) + if (deltaT > TEN_IN_SECS) { mRelay->checkRelays(); diff --git a/libretroshare/src/pqi/authgpg.h b/libretroshare/src/pqi/authgpg.h index 90caaeb34..6d0e02fdc 100644 --- a/libretroshare/src/pqi/authgpg.h +++ b/libretroshare/src/pqi/authgpg.h @@ -39,6 +39,7 @@ #ifndef RS_GPG_AUTH_HEADER #define RS_GPG_AUTH_HEADER +#include "util/rswin.h" #include #include #include diff --git a/libretroshare/src/pqi/authssl.h b/libretroshare/src/pqi/authssl.h index 7f0c67f6c..2eab130fd 100644 --- a/libretroshare/src/pqi/authssl.h +++ b/libretroshare/src/pqi/authssl.h @@ -39,6 +39,8 @@ * */ +#include "util/rswin.h" + #include #include diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index 47d0bff06..9496e9209 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -43,7 +43,9 @@ const int p3connectzone = 3431; #include "serialiser/rsconfigitems.h" #include "pqi/pqinotify.h" + #include "retroshare/rsiface.h" +#include "retroshare/rspeers.h" #include @@ -208,6 +210,28 @@ bool p3LinkMgrIMPL::isOnline(const std::string &ssl_id) return false; } + + +uint32_t p3LinkMgrIMPL::getLinkType(const std::string &ssl_id) +{ + RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ + + std::map::iterator it; + it = mFriendList.find(ssl_id); + if (it == mFriendList.end()) + { + return 0; + } + + if (it->second.state & RS_PEER_S_CONNECTED) + { + return it->second.linkType; + } + return 0; +} + + + void p3LinkMgrIMPL::getOnlineList(std::list &ssl_peers) { RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ @@ -654,11 +678,82 @@ bool p3LinkMgrIMPL::connectAttempt(const std::string &id, struct sockaddr_in &ra srcaddr = it->second.currentConnAddrAttempt.srcaddr; bandwidth = it->second.currentConnAddrAttempt.bandwidth; + /********* Setup LinkType parameters **********/ + +#define TRICKLE_LIMIT 2001 // 2kb +#define LOW_BANDWIDTH_LIMIT 5001 // 5kb + + it->second.linkType = 0; + if (type & RS_NET_CONN_TCP_ALL) + { + it->second.linkType |= RS_NET_CONN_TRANS_TCP_UNKNOWN; + } + else if (type & RS_NET_CONN_UDP_ALL) + { + if (flags & RS_CB_FLAG_MODE_UDP_DIRECT) + { + it->second.linkType |= RS_NET_CONN_TRANS_UDP_DIRECT; + } + else if (flags & RS_CB_FLAG_MODE_UDP_PROXY) + { + it->second.linkType |= RS_NET_CONN_TRANS_UDP_PROXY; + } + else if (flags & RS_CB_FLAG_MODE_UDP_RELAY) + { + it->second.linkType |= RS_NET_CONN_TRANS_UDP_RELAY; + } + else + { + it->second.linkType |= RS_NET_CONN_TRANS_UDP_UNKNOWN; + } + } + else if (type & RS_NET_CONN_TUNNEL) + { + it->second.linkType |= RS_NET_CONN_TRANS_TUNNEL; + } + else + { + it->second.linkType |= RS_NET_CONN_TRANS_UNKNOWN; + } + + if (flags & RS_CB_FLAG_MODE_UDP_RELAY) + { + if (bandwidth < TRICKLE_LIMIT) + { + it->second.linkType |= RS_NET_CONN_SPEED_TRICKLE; + } + else if (bandwidth < LOW_BANDWIDTH_LIMIT) + { + it->second.linkType |= RS_NET_CONN_SPEED_LOW; + } + else + { + it->second.linkType |= RS_NET_CONN_SPEED_NORMAL; + } + } + else + { + it->second.linkType |= RS_NET_CONN_SPEED_NORMAL; + } + + uint32_t connType = mPeerMgr->getConnectionType(id); + it->second.linkType |= connType; + + /********* Setup LinkType parameters **********/ + + // TEMP DEBUG. + std::cerr << "p3LinkMgrIMPL::connectAttempt() found an address: id: " << id << std::endl; + std::cerr << " laddr: " << rs_inet_ntoa(raddr.sin_addr) << " lport: " << ntohs(raddr.sin_port) << " delay: " << delay << " period: " << period; + std::cerr << " type: " << type << std::endl; + std::cerr << "p3LinkMgrIMPL::connectAttempt() set LinkType to: " << it->second.linkType << std::endl; + + #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::connectAttempt() found an address: id: " << id << std::endl; std::cerr << " laddr: " << rs_inet_ntoa(addr.sin_addr) << " lport: " << ntohs(addr.sin_port) << " delay: " << delay << " period: " << period; std::cerr << " type: " << type << std::endl; + std::cerr << "p3LinkMgrIMPL::connectAttempt() set LinkType to: " << it->second.linkType << std::endl; #endif if (raddr.sin_addr.s_addr == 0 || raddr.sin_port == 0) { #ifdef LINKMGR_DEBUG @@ -1485,9 +1580,7 @@ bool p3LinkMgrIMPL::retryConnectTCP(const std::string &id) std::map::iterator it; if (mFriendList.end() != (it = mFriendList.find(id))) { - locked_ConnectAttempt_CurrentAddresses(&(it->second), &lAddr, &eAddr); - locked_ConnectAttempt_HistoricalAddresses(&(it->second), histAddrs); uint16_t dynPort = ntohs(eAddr.sin_port); if (!dynPort) @@ -1497,6 +1590,8 @@ bool p3LinkMgrIMPL::retryConnectTCP(const std::string &id) locked_ConnectAttempt_AddDynDNS(&(it->second), dyndns, dynPort); } + locked_ConnectAttempt_HistoricalAddresses(&(it->second), histAddrs); + //locked_ConnectAttempt_AddTunnel(&(it->second)); /* finish it off */ diff --git a/libretroshare/src/pqi/p3linkmgr.h b/libretroshare/src/pqi/p3linkmgr.h index 1f8378f92..d15c1f0c0 100644 --- a/libretroshare/src/pqi/p3linkmgr.h +++ b/libretroshare/src/pqi/p3linkmgr.h @@ -29,8 +29,6 @@ #include "pqi/pqimonitor.h" #include "pqi/pqiipset.h" -//#include "pqi/p3dhtmgr.h" -//#include "pqi/p3upnpmgr.h" #include "pqi/pqiassist.h" #include "pqi/p3cfgmgr.h" @@ -40,22 +38,17 @@ class ExtAddrFinder ; class DNSResolver ; + /* order of attempts ... */ -const uint32_t RS_NET_CONN_TCP_ALL = 0x000f; -const uint32_t RS_NET_CONN_UDP_ALL = 0x00f0; -const uint32_t RS_NET_CONN_TUNNEL = 0x0f00; - -const uint32_t RS_NET_CONN_TCP_LOCAL = 0x0001; -const uint32_t RS_NET_CONN_TCP_EXTERNAL = 0x0002; -const uint32_t RS_NET_CONN_TCP_UNKNOW_TOPOLOGY = 0x0004; -const uint32_t RS_NET_CONN_UDP_DHT_SYNC = 0x0010; -const uint32_t RS_NET_CONN_UDP_PEER_SYNC = 0x0020; /* coming soon */ - -/* extra flags */ -// not sure if needed yet. -//const uint32_t RS_NET_CONN_PEERAGE = 0x0f00; -//const uint32_t RS_NET_CONN_SERVER = 0x0100; /* TCP only */ -//const uint32_t RS_NET_CONN_PEER = 0x0200; /* all UDP */ +const uint32_t RS_NET_CONN_TCP_ALL = 0x000f; +const uint32_t RS_NET_CONN_UDP_ALL = 0x00f0; +const uint32_t RS_NET_CONN_TUNNEL = 0x0f00; + +const uint32_t RS_NET_CONN_TCP_LOCAL = 0x0001; +const uint32_t RS_NET_CONN_TCP_EXTERNAL = 0x0002; +const uint32_t RS_NET_CONN_TCP_UNKNOW_TOPOLOGY = 0x0004; +const uint32_t RS_NET_CONN_UDP_DHT_SYNC = 0x0010; +const uint32_t RS_NET_CONN_UDP_PEER_SYNC = 0x0020; /* coming soon */ const uint32_t RS_TCP_STD_TIMEOUT_PERIOD = 5; /* 5 seconds! */ const uint32_t RS_UDP_STD_TIMEOUT_PERIOD = 80; /* 80 secs, allows UDP TTL to get to 40! - Plenty of time (30+80) = 110 secs */ @@ -95,38 +88,20 @@ class peerConnectState peerConnectState(); /* init */ std::string id; - //std::string gpg_id; - - //uint32_t netMode; /* EXT / UPNP / UDP / INVALID */ - //uint32_t visState; /* STD, GRAY, DARK */ - - //struct sockaddr_in localaddr, serveraddr; - - //used to store current ip (for config and connection management) - //struct sockaddr_in currentlocaladdr; /* Mandatory */ - //struct sockaddr_in currentserveraddr; /* Mandatory */ - //std::string dyndns; - - - /* list of addresses from various sources */ - //pqiIpAddrSet ipAddrs; /***** Below here not stored permanently *****/ bool dhtVisible; - //time_t lastcontact; - uint32_t connecttype; // RS_NET_CONN_TCP_ALL / RS_NET_CONN_UDP_ALL time_t lastavailable; time_t lastattempt; std::string name; - //std::string location; - uint32_t state; uint32_t actions; + uint32_t linkType; uint32_t source; /* most current source */ peerAddrInfo dht; @@ -170,6 +145,7 @@ virtual const std::string getOwnId() = 0; virtual bool isOnline(const std::string &ssl_id) = 0; virtual void getOnlineList(std::list &ssl_peers) = 0; virtual bool getPeerName(const std::string &ssl_id, std::string &name) = 0; +virtual uint32_t getLinkType(const std::string &ssl_id) = 0; /**************** handle monitors *****************/ virtual void addMonitor(pqiMonitor *mon) = 0; @@ -226,6 +202,7 @@ virtual const std::string getOwnId(); virtual bool isOnline(const std::string &ssl_id); virtual void getOnlineList(std::list &ssl_peers); virtual bool getPeerName(const std::string &ssl_id, std::string &name); +virtual uint32_t getLinkType(const std::string &ssl_id); /**************** handle monitors *****************/ diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index f869726bd..e69117afc 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -271,6 +271,11 @@ bool p3PeerMgrIMPL::getGpgId(const std::string &ssl_id, std::string &gpgId) return true; } +// Placeholder until we implement this functionality. +uint32_t p3PeerMgrIMPL::getConnectionType(const std::string &sslId) +{ + return RS_NET_CONN_TYPE_FRIEND; +} bool p3PeerMgrIMPL::getFriendNetStatus(const std::string &id, peerState &state) diff --git a/libretroshare/src/pqi/p3peermgr.h b/libretroshare/src/pqi/p3peermgr.h index 5582e699d..162ff26e3 100644 --- a/libretroshare/src/pqi/p3peermgr.h +++ b/libretroshare/src/pqi/p3peermgr.h @@ -183,6 +183,7 @@ virtual bool getOthersNetStatus(const std::string &id, peerState &state) = 0; virtual bool getPeerName(const std::string &ssl_id, std::string &name) = 0; virtual bool getGpgId(const std::string &sslId, std::string &gpgId) = 0; +virtual uint32_t getConnectionType(const std::string &sslId) = 0; /************* DEPRECIATED FUNCTIONS (TO REMOVE) ********/ @@ -268,6 +269,7 @@ virtual bool getOthersNetStatus(const std::string &id, peerState &state); virtual bool getPeerName(const std::string &ssl_id, std::string &name); virtual bool getGpgId(const std::string &sslId, std::string &gpgId); +virtual uint32_t getConnectionType(const std::string &sslId); /************* DEPRECIATED FUNCTIONS (TO REMOVE) ********/ diff --git a/libretroshare/src/pqi/pqi_base.h b/libretroshare/src/pqi/pqi_base.h index 6fd95f303..adc8b111c 100644 --- a/libretroshare/src/pqi/pqi_base.h +++ b/libretroshare/src/pqi/pqi_base.h @@ -58,7 +58,9 @@ class RateInterface public: RateInterface() - :bw_in(0), bw_out(0), bwMax_in(0), bwMax_out(0) { return; } + :bw_in(0), bw_out(0), bwMax_in(0), bwMax_out(0), + bwCapEnabled(false), bwCap_in(0), bwCap_out(0) { return; } + virtual ~RateInterface() { return; } virtual float getRate(bool in) @@ -78,12 +80,50 @@ virtual float getMaxRate(bool in) virtual void setMaxRate(bool in, float val) { if (in) + { bwMax_in = val; + if (bwCapEnabled) + { + if (bwMax_in > bwCap_in) + { + bwMax_in = bwCap_in; + } + } + } else + { bwMax_out = val; + if (bwCapEnabled) + { + if (bwMax_out > bwCap_out) + { + bwMax_out = bwCap_out; + } + } + } + return; } + +virtual void setRateCap(float val_in, float val_out) +{ + if ((val_in == 0) && (val_out == 0)) + { + std::cerr << "RateInterface::setRateCap() Now disabled" << std::endl; + bwCapEnabled = false; + } + else + { + std::cerr << "RateInterface::setRateCap() Enabled "; + std::cerr << "in: " << bwCap_in << " out: " << bwCap_out << std::endl; + bwCapEnabled = true; + bwCap_in = val_in; + bwCap_out = val_out; + } + return; +} + protected: void setRate(bool in, float val) @@ -97,6 +137,9 @@ void setRate(bool in, float val) private: float bw_in, bw_out, bwMax_in, bwMax_out; +bool bwCapEnabled; +float bwCap_in, bwCap_out; + }; diff --git a/libretroshare/src/pqi/pqiperson.cc b/libretroshare/src/pqi/pqiperson.cc index 5a6c519e4..0626733b4 100644 --- a/libretroshare/src/pqi/pqiperson.cc +++ b/libretroshare/src/pqi/pqiperson.cc @@ -189,7 +189,7 @@ int pqiperson::notifyEvent(NetInterface *ni, int newState) out << i << " of " << kids.size(); out << std::endl; out << " type: " << (it->first); - out << " ni: " << (it->second)->ni; + //out << " ni: " << (it->second)->ni; out << " in_ni: " << ni; pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str()); i++; @@ -431,6 +431,9 @@ int pqiperson::connect(uint32_t type, struct sockaddr_in raddr, pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::connect reset() before connection attempt"); (it->second)->reset(); + std::cerr << "pqiperson::connect() WARNING, clearing rate cap" << std::endl; + setRateCap(0,0); + #ifdef PERSON_DEBUG std::cerr << "pqiperson::connect() setting connect_parameters" << std::endl; #endif @@ -489,3 +492,17 @@ void pqiperson::setMaxRate(bool in, float val) } } +void pqiperson::setRateCap(float val_in, float val_out) +{ + // set to all of them. (and us) + PQInterface::setRateCap(val_in, val_out); + // clean up the children. + std::map::iterator it; + for(it = kids.begin(); it != kids.end(); it++) + { + (it->second) -> setRateCap(val_in, val_out); + } +} + + + diff --git a/libretroshare/src/pqi/pqiperson.h b/libretroshare/src/pqi/pqiperson.h index 7220447ee..17c822778 100644 --- a/libretroshare/src/pqi/pqiperson.h +++ b/libretroshare/src/pqi/pqiperson.h @@ -92,7 +92,7 @@ virtual std::string PeerId() // to check if our interface. virtual bool thisNetInterface(NetInterface *ni_in) { return (ni_in == ni); } -//protected: +protected: NetBinInterface *ni; protected: }; @@ -138,6 +138,7 @@ int notifyEvent(NetInterface *ni, int event); // PQInterface for rate control overloaded.... virtual float getRate(bool in); virtual void setMaxRate(bool in, float val); +virtual void setRateCap(float val_in, float val_out); pqiconnect *getKid(uint32_t type); diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index 0faefc7d1..1b11ae7ef 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -101,7 +101,6 @@ pqissl::pqissl(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm) pqil(l), // no init for remote_addr. readpkt(NULL), pktlen(0), attempt_ts(0), - net_attempt(0), net_failure(0), net_unreachable(0), sameLAN(false), n_read_zero(0), mReadZeroTS(0), mConnectDelay(0), mConnectTS(0), mConnectTimeout(0), mTimeoutTS(0), mLinkMgr(lm) @@ -151,7 +150,6 @@ pqissl::pqissl(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm) int pqissl::connect(struct sockaddr_in raddr) { // reset failures - net_failure = 0; remote_addr = raddr; remote_addr.sin_family = AF_INET; @@ -709,8 +707,6 @@ int pqissl::Initiate_Connection() //reset(); waiting = WAITING_FAIL_INTERFACE; - // removing unreachables... - //net_unreachable |= net_attempt; return -1; } @@ -923,8 +919,6 @@ int pqissl::Basic_Connection_Complete() //reset(); waiting = WAITING_FAIL_INTERFACE; - // removing unreachables... - //net_unreachable |= net_attempt; return -1; } diff --git a/libretroshare/src/pqi/pqissl.h b/libretroshare/src/pqi/pqissl.h index cc481d39e..b32eec8c9 100644 --- a/libretroshare/src/pqi/pqissl.h +++ b/libretroshare/src/pqi/pqissl.h @@ -28,6 +28,8 @@ #ifndef MRK_PQI_SSL_HEADER #define MRK_PQI_SSL_HEADER +#include "util/rswin.h" + #include // operating system specific network header. @@ -114,7 +116,7 @@ virtual bool cansend(); virtual int close(); /* BinInterface version of reset() */ virtual std::string gethash(); /* not used here */ -virtual bool bandwidthLimited() { return true ; } // replace by !sameLAN to avoid bandwidth limiting on lAN +virtual bool bandwidthLimited() { return true ; } // replace by !sameLAN to avoid bandwidth limiting on LAN protected: // A little bit of information to describe @@ -179,13 +181,6 @@ virtual int net_internal_fcntl_nonblock(int fd) { return unix_fcntl_nonblock(fd) int attempt_ts; - // Some flags to indicate - // the status of the various interfaces - // (local), (server) - unsigned int net_attempt; - unsigned int net_failure; - unsigned int net_unreachable; - bool sameLAN; /* flag use to allow high-speed transfers */ int n_read_zero; /* a counter to determine if the connection is really dead */ diff --git a/libretroshare/src/pqi/pqissltunnel.h b/libretroshare/src/pqi/pqissltunnel.h index 969e2e250..ed6d6f312 100644 --- a/libretroshare/src/pqi/pqissltunnel.h +++ b/libretroshare/src/pqi/pqissltunnel.h @@ -28,6 +28,7 @@ #ifndef MRK_PQI_SSL_TUNNEL_HEADER #define MRK_PQI_SSL_TUNNEL_HEADER +#include "util/rswin.h" #include diff --git a/libretroshare/src/pqi/pqissludp.cc b/libretroshare/src/pqi/pqissludp.cc index c4db38ece..a1098d890 100644 --- a/libretroshare/src/pqi/pqissludp.cc +++ b/libretroshare/src/pqi/pqissludp.cc @@ -245,7 +245,24 @@ int pqissludp::Initiate_Connection() } else if (mConnectFlags & RS_CB_FLAG_MODE_UDP_RELAY) { + std::cerr << "Calling tou_connect_via_relay("; + std::cerr << mConnectSrcAddr << ","; + std::cerr << mConnectProxyAddr << ","; + std::cerr << remote_addr << ")" << std::endl; + tou_connect_via_relay(sockfd, &(mConnectSrcAddr), &(mConnectProxyAddr), &(remote_addr)); + +/*** It seems that the UDP Layer sees x 1.2 the traffic of the SSL layer. + * We need to compensate somewhere... we drop the maximum traffic to 75% of limit + * to allow for extra lost packets etc. + * NB: If we have a lossy UDP transmission - re-transmission could cause excessive data to + * exceed the limit... This is difficult to account for without hacking the TcpOnUdp layer. + * If it is noticed as a problem - we'll deal with it then + */ +#define UDP_RELAY_TRANSPORT_OVERHEAD_FACTOR (0.7) + + parent()->setRateCap( UDP_RELAY_TRANSPORT_OVERHEAD_FACTOR * mConnectBandwidth / 1000.0, + UDP_RELAY_TRANSPORT_OVERHEAD_FACTOR * mConnectBandwidth / 1000.0); // Set RateCap. } if (0 != err) @@ -273,7 +290,6 @@ int pqissludp::Initiate_Connection() // Then send unreachable message. waiting = WAITING_FAIL_INTERFACE; - net_unreachable |= net_attempt; } out << "Error: Connection Failed: " << tou_err; @@ -359,8 +375,6 @@ int pqissludp::Basic_Connection_Complete() out2 << " - " << socket_errorType(err); rslog(RSL_DEBUG_BASIC, pqissludpzone, out2.str()); - net_unreachable |= net_attempt; - reset(); // Then send unreachable message. @@ -470,6 +484,7 @@ bool pqissludp::connect_parameter(uint32_t type, uint32_t value) rslog(RSL_WARNING, pqissludpzone, out.str()); mConnectPeriod = value; + std::cerr << out.str() << std::endl; return true; } else if (type == NET_PARAM_CONNECT_FLAGS) @@ -479,6 +494,7 @@ bool pqissludp::connect_parameter(uint32_t type, uint32_t value) rslog(RSL_WARNING, pqissludpzone, out.str()); mConnectFlags = value; + std::cerr << out.str() << std::endl; return true; } else if (type == NET_PARAM_CONNECT_BANDWIDTH) @@ -488,6 +504,7 @@ bool pqissludp::connect_parameter(uint32_t type, uint32_t value) rslog(RSL_WARNING, pqissludpzone, out.str()); mConnectBandwidth = value; + std::cerr << out.str() << std::endl; return true; } return pqissl::connect_parameter(type, value); @@ -495,8 +512,6 @@ bool pqissludp::connect_parameter(uint32_t type, uint32_t value) bool pqissludp::connect_additional_address(uint32_t type, struct sockaddr_in *addr) { - struct sockaddr_in mConnectProxyAddr; - struct sockaddr_in mConnectSrcAddr; if (type == NET_PARAM_CONNECT_PROXY) { std::ostringstream out; diff --git a/libretroshare/src/pqi/pqissludp.h b/libretroshare/src/pqi/pqissludp.h index f07632dd4..f5044fbd4 100644 --- a/libretroshare/src/pqi/pqissludp.h +++ b/libretroshare/src/pqi/pqissludp.h @@ -28,6 +28,8 @@ #ifndef MRK_PQI_SSL_UDP_HEADER #define MRK_PQI_SSL_UDP_HEADER +#include "util/rswin.h" + #include // operating system specific network header. diff --git a/libretroshare/src/pqi/sslfns.h b/libretroshare/src/pqi/sslfns.h index e1c61ea43..0c009df66 100644 --- a/libretroshare/src/pqi/sslfns.h +++ b/libretroshare/src/pqi/sslfns.h @@ -33,6 +33,8 @@ /******************** notify of new Cert **************************/ +#include "util/rswin.h" + #include #include #include diff --git a/libretroshare/src/retroshare/rspeers.h b/libretroshare/src/retroshare/rspeers.h index e1e5573a0..674b9a5fa 100644 --- a/libretroshare/src/retroshare/rspeers.h +++ b/libretroshare/src/retroshare/rspeers.h @@ -77,6 +77,47 @@ const int RS_PEER_CERT_CLEANING_CODE_NO_BEGIN_TAG = 0x02 ; const int RS_PEER_CERT_CLEANING_CODE_NO_END_TAG = 0x03 ; const int RS_PEER_CERT_CLEANING_CODE_NO_CHECKSUM = 0x04 ; +/* LinkType Flags */ + +// CONNECTION +const uint32_t RS_NET_CONN_TRANS_MASK = 0x0000ffff; +const uint32_t RS_NET_CONN_TRANS_TCP_MASK = 0x0000000f; +const uint32_t RS_NET_CONN_TRANS_TCP_UNKNOWN = 0x00000001; +const uint32_t RS_NET_CONN_TRANS_TCP_LOCAL = 0x00000002; +const uint32_t RS_NET_CONN_TRANS_TCP_EXTERNAL = 0x00000004; + +const uint32_t RS_NET_CONN_TRANS_UDP_MASK = 0x000000f0; +const uint32_t RS_NET_CONN_TRANS_UDP_UNKNOWN = 0x00000010; +const uint32_t RS_NET_CONN_TRANS_UDP_DIRECT = 0x00000020; +const uint32_t RS_NET_CONN_TRANS_UDP_PROXY = 0x00000040; +const uint32_t RS_NET_CONN_TRANS_UDP_RELAY = 0x00000080; + +const uint32_t RS_NET_CONN_TRANS_OTHER_MASK = 0x00000f00; +const uint32_t RS_NET_CONN_TRANS_TUNNEL = 0x00000100; + +const uint32_t RS_NET_CONN_TRANS_UNKNOWN = 0x00001000; + + +const uint32_t RS_NET_CONN_SPEED_MASK = 0x000f0000; +const uint32_t RS_NET_CONN_SPEED_UNKNOWN = 0x00000000; +const uint32_t RS_NET_CONN_SPEED_TRICKLE = 0x00010000; +const uint32_t RS_NET_CONN_SPEED_LOW = 0x00020000; +const uint32_t RS_NET_CONN_SPEED_NORMAL = 0x00040000; +const uint32_t RS_NET_CONN_SPEED_HIGH = 0x00080000; + +const uint32_t RS_NET_CONN_QUALITY_MASK = 0x00f00000; +const uint32_t RS_NET_CONN_QUALITY_UNKNOWN = 0x00000000; + +// THIS INFO MUST BE SUPPLIED BY PEERMGR.... +const uint32_t RS_NET_CONN_TYPE_MASK = 0x0f000000; +const uint32_t RS_NET_CONN_TYPE_UNKNOWN = 0x00000000; +const uint32_t RS_NET_CONN_TYPE_ACQUAINTANCE = 0x01000000; +const uint32_t RS_NET_CONN_TYPE_FRIEND = 0x02000000; +const uint32_t RS_NET_CONN_TYPE_SERVER = 0x04000000; +const uint32_t RS_NET_CONN_TYPE_CLIENT = 0x08000000; + + + /* Groups */ #define RS_GROUP_ID_FRIENDS "Friends" #define RS_GROUP_ID_FAMILY "Family" @@ -143,6 +184,9 @@ class RsPeerDetails std::string connectStateString; /* Additional string like ip address */ uint32_t connectPeriod; bool foundDHT; + + /* linkType */ + uint32_t linkType; }; class RsGroupInfo diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index 0b45dfc1a..cab5819ce 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -391,7 +391,7 @@ bool p3Peers::getPeerDetails(const std::string &id, RsPeerDetails &d) if (pcs.state & RS_PEER_S_UNREACHABLE) d.state |= RS_PEER_STATE_UNREACHABLE; - + d.linkType = pcs.linkType; /* Finally determine AutoConnect Status */ d.foundDHT = pcs.dht.found; diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 8f84a8d76..587e7de3b 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -313,6 +313,21 @@ void RsInit::InitRsConfig() //setZoneLevel(PQL_DEBUG_BASIC, 49787); // pqissllistener } +/******** + * LOCALNET_TESTING - allows port restrictions + * + * #define LOCALNET_TESTING 1 + * + ********/ + + +#ifdef LOCALNET_TESTING + +std::string portRestrictions; +bool doPortRestrictions = false; + +#endif + /******************************** WINDOWS/UNIX SPECIFIC PART ******************/ #ifndef WINDOWS_SYS @@ -395,7 +410,11 @@ int RsInit::InitRetroShare(int argcIgnored, char **argvIgnored, bool strictCheck /* getopt info: every availiable option is listed here. if it is followed by a ':' it needs an argument. If it is followed by a '::' the argument is optional. */ +#ifdef LOCALNET_TESTING + while((c = getopt(argc, argv,"hesamui:p:c:w:l:d:U:r:R:")) != -1) +#else while((c = getopt(argc, argv,"hesamui:p:c:w:l:d:U:r:")) != -1) +#endif { switch (c) { @@ -470,6 +489,14 @@ int RsInit::InitRetroShare(int argcIgnored, char **argvIgnored, bool strictCheck std::cerr << "Opt for RetroShare link"; std::cerr << std::endl; break; +#ifdef LOCALNET_TESTING + case 'R': + portRestrictions = optarg; + doPortRestrictions = true; + std::cerr << "Opt for Port Restrictions"; + std::cerr << std::endl; + break; +#endif case 'h': std::cerr << "Help: " << std::endl; std::cerr << "The commandline options are for retroshare-nogui, a headless server in a shell, or systems without QT." << std::endl << std::endl; @@ -486,6 +513,9 @@ int RsInit::InitRetroShare(int argcIgnored, char **argvIgnored, bool strictCheck std::cerr << "-e Use a forwarded external Port" << std::endl ; std::cerr << "-U [User Name/GPG id/SSL id] Sets Account to Use, Useful when Autologin is enabled." << std::endl; std::cerr << "-r link Use RetroShare link." << std::endl; +#ifdef LOCALNET_TESTING + std::cerr << "-R Port Restrictions." << std::endl; +#endif exit(1); break; default: @@ -1891,7 +1921,35 @@ int RsServer::StartupRetroShare() struct sockaddr_in tmpladdr; sockaddr_clear(&tmpladdr); tmpladdr.sin_port = htons(RsInitConfig::port); + + +#ifdef LOCALNET_TESTING + + rsUdpStack *mDhtStack = new rsUdpStack(UDP_TEST_RESTRICTED_LAYER, tmpladdr); + + /* parse portRestrictions */ + unsigned int lport, uport; + + if (doPortRestrictions) + { + if (2 == sscanf(portRestrictions.c_str(), "%u-%u", &lport, &uport)) + { + std::cerr << "Adding Port Restriction (" << lport << "-" << uport << ")"; + std::cerr << std::endl; + } + else + { + std::cerr << "Failed to parse Port Restrictions ... exiting"; + std::cerr << std::endl; + exit(1); + } + + RestrictedUdpLayer *url = (RestrictedUdpLayer *) mDhtStack->getUdpLayer(); + url->addRestrictedPortRange(lport, uport); + } +#else rsUdpStack *mDhtStack = new rsUdpStack(tmpladdr); +#endif #ifdef RS_USE_BITDHT @@ -1948,6 +2006,10 @@ int RsServer::StartupRetroShare() mDhtStunner->setTargetStunPeriod(300); /* slow (5mins) */ mDhtStack->addReceiver(mDhtStunner); +#ifdef LOCALNET_TESTING + mDhtStunner->SetAcceptLocalNet(); +#endif + // NEXT BITDHT. p3BitDht *mBitDht = new p3BitDht(ownId, mLinkMgr, mNetMgr, mDhtStack, bootstrapfile); /* install external Pointer for Interface */ @@ -1972,14 +2034,38 @@ int RsServer::StartupRetroShare() struct sockaddr_in sndladdr; sockaddr_clear(&sndladdr); - uint16_t rndport = MIN_RANDOM_PORT + RSRandom::random_u32() % (MAX_RANDOM_PORT - MIN_RANDOM_PORT); + +#ifdef LOCALNET_TESTING + // HACK Proxy Port near Dht Port - For Relay Testing. + uint16_t rndport = RsInitConfig::port + 3; sndladdr.sin_port = htons(rndport); +#else + uint16_t rndport = MIN_RANDOM_PORT + RSRandom::random_u32() % (MAX_RANDOM_PORT - MIN_RANDOM_PORT); +#endif + +#ifdef LOCALNET_TESTING + + rsFixedUdpStack *mProxyStack = new rsFixedUdpStack(UDP_TEST_RESTRICTED_LAYER, sndladdr); + + /* portRestrictions already parsed */ + if (doPortRestrictions) + { + RestrictedUdpLayer *url = (RestrictedUdpLayer *) mProxyStack->getUdpLayer(); + url->addRestrictedPortRange(lport, uport); + } +#else rsFixedUdpStack *mProxyStack = new rsFixedUdpStack(sndladdr); +#endif // FIRSTLY THE PROXY STUNNER. UdpStunner *mProxyStunner = new UdpStunner(mProxyStack); mProxyStunner->setTargetStunPeriod(300); /* slow (5mins) */ mProxyStack->addReceiver(mProxyStunner); + +#ifdef LOCALNET_TESTING + mProxyStunner->SetAcceptLocalNet(); +#endif + // FINALLY THE PROXY UDP CONNECTIONS udpReceivers[RSUDP_TOU_RECVER_PROXY_IDX] = new UdpPeerReceiver(mProxyStack); /* PROXY Connections (Alt UDP Port) */ diff --git a/libretroshare/src/tcponudp/udprelay.cc b/libretroshare/src/tcponudp/udprelay.cc index 553157c16..908787330 100644 --- a/libretroshare/src/tcponudp/udprelay.cc +++ b/libretroshare/src/tcponudp/udprelay.cc @@ -27,7 +27,8 @@ #include /* - * #define DEBUG_UDP_RELAY 1 + * #define DEBUG_UDP_RELAY 1 + * #define DEBUG_UDP_RELAY_PKTS 1 */ //#define DEBUG_UDP_RELAY 1 @@ -42,7 +43,11 @@ int displayUdpRelayPacketHeader(const void *data, const int size); /****************** UDP RELAY STUFF **********/ -#define MAX_RELAY_UDP_PACKET_SIZE 1024 +// This packet size must be able to handle TcpStream Packets. +// At the moment, they can be 1000 + 20 for TcpOnUdp ... + 16 => 1036 minimal size. +// See Notes in tcpstream.h for more info +#define MAX_RELAY_UDP_PACKET_SIZE (1400 + 20 + 16) + UdpRelayReceiver::UdpRelayReceiver(UdpPublisher *pub) :UdpSubReceiver(pub), udppeerMtx("UdpSubReceiver"), relayMtx("UdpSubReceiver") @@ -85,15 +90,19 @@ int UdpRelayReceiver::addUdpPeer(UdpPeer *peer, UdpRelayAddrSet *endPoints, bool ok = (it == mStreams.end()); if (!ok) { - #ifdef DEBUG_UDP_RELAY - std::cerr << "UdpPeerReceiver::addUdpPeer() ERROR Peer already exists!" << std::endl; - #endif +#ifdef DEBUG_UDP_RELAY + std::cerr << "UdpRelayReceiver::addUdpPeer() ERROR Peer already exists!" << std::endl; +#endif return 0; } /* setup a peer */ UdpRelayEnd ure(endPoints, proxyaddr); - + +#ifdef DEBUG_UDP_RELAY + std::cerr << "UdpRelayReceiver::addUdpPeer() Installing UdpRelayEnd: " << ure << std::endl; +#endif + mStreams[realPeerAddr] = ure; } @@ -102,11 +111,12 @@ int UdpRelayReceiver::addUdpPeer(UdpPeer *peer, UdpRelayAddrSet *endPoints, #ifdef DEBUG_UDP_RELAY - std::cerr << "UdpPeerReceiver::addUdpPeer() Just installing UdpPeer!" << std::endl; + std::cerr << "UdpRelayReceiver::addUdpPeer() Installing UdpPeer" << std::endl; #endif /* just overwrite */ mPeers[realPeerAddr] = peer; + } return 1; @@ -131,6 +141,9 @@ int UdpRelayReceiver::removeUdpPeer(UdpPeer *peer) found = true; realPeerAddr = it->first; +#ifdef DEBUG_UDP_RELAY + std::cerr << "UdpRelayReceiver::removeUdpPeer() removing UdpPeer" << std::endl; +#endif break; } } @@ -138,6 +151,9 @@ int UdpRelayReceiver::removeUdpPeer(UdpPeer *peer) if (!found) { +#ifdef DEBUG_UDP_RELAY + std::cerr << "UdpRelayReceiver::removeUdpPeer() Warning: Failed to find UdpPeer" << std::endl; +#endif return 0; } @@ -154,6 +170,9 @@ int UdpRelayReceiver::removeUdpPeer(UdpPeer *peer) else { /* ERROR */ +#ifdef DEBUG_UDP_RELAY + std::cerr << "UdpRelayReceiver::removeUdpPeer() ERROR failed to find Mapping" << std::endl; +#endif } } return 1; @@ -193,46 +212,80 @@ int UdpRelayReceiver::getRelayProxies(std::list &relayProxies) int UdpRelayReceiver::checkRelays() { + +#ifdef DEBUG_UDP_RELAY + // As this locks - must be out of the Mutex. + status(std::cerr); +#endif + RsStackMutex stack(relayMtx); /********** LOCK MUTEX *********/ /* iterate through the Relays */ +#ifdef DEBUG_UDP_RELAY std::cerr << "UdpRelayReceiver::checkRelays()"; std::cerr << std::endl; +#endif std::list eraseList; std::map::iterator rit; time_t now = time(NULL); + +#define BANDWIDTH_FILTER_K (0.8) for(rit = mRelays.begin(); rit != mRelays.end(); rit++) { /* calc bandwidth */ - rit->second.mBandwidth = rit->second.mDataSize / (float) (now - rit->second.mLastBandwidthTS); + //rit->second.mBandwidth = rit->second.mDataSize / (float) (now - rit->second.mLastBandwidthTS); + // Switch to a Low-Pass Filter to average it out. + float instantBandwidth = rit->second.mDataSize / (float) (now - rit->second.mLastBandwidthTS); + + rit->second.mBandwidth *= (BANDWIDTH_FILTER_K); + rit->second.mBandwidth += (1.0 - BANDWIDTH_FILTER_K) * instantBandwidth; + rit->second.mDataSize = 0; rit->second.mLastBandwidthTS = now; +#ifdef DEBUG_UDP_RELAY std::cerr << "UdpRelayReceiver::checkRelays()"; std::cerr << "Relay: " << rit->first; std::cerr << " using bandwidth: " << rit->second.mBandwidth; std::cerr << std::endl; +#endif + + // ONLY A WARNING. +#ifdef DEBUG_UDP_RELAY + if (instantBandwidth > rit->second.mBandwidthLimit) + { + std::cerr << "UdpRelayReceiver::checkRelays() "; + std::cerr << "Warning instantBandwidth: " << instantBandwidth; + std::cerr << " Exceeding Limit: " << rit->second.mBandwidthLimit; + std::cerr << " for Relay: " << rit->first; + std::cerr << std::endl; + } +#endif if (rit->second.mBandwidth > rit->second.mBandwidthLimit) { +#ifdef DEBUG_UDP_RELAY std::cerr << "UdpRelayReceiver::checkRelays() "; std::cerr << "Dropping Relay due to excessive Bandwidth: " << rit->second.mBandwidth; std::cerr << " Exceeding Limit: " << rit->second.mBandwidthLimit; std::cerr << " Relay: " << rit->first; std::cerr << std::endl; +#endif /* if exceeding bandwidth -> drop */ eraseList.push_back(rit->first); } else if (now - rit->second.mLastTS > RELAY_TIMEOUT) { +#ifdef DEBUG_UDP_RELAY /* if haven't transmitted for ages -> drop */ std::cerr << "UdpRelayReceiver::checkRelays() "; std::cerr << "Dropping Relay due to Timeout: " << rit->first; std::cerr << std::endl; +#endif eraseList.push_back(rit->first); } else @@ -254,11 +307,13 @@ int UdpRelayReceiver::checkRelays() } if (now - rit->second.mStartTS > lifetime) { +#ifdef DEBUG_UDP_RELAY std::cerr << "UdpRelayReceiver::checkRelays() "; std::cerr << "Dropping Relay due to Passing Lifetime Limit: " << lifetime; std::cerr << " for class: " << rit->second.mRelayClass; std::cerr << " Relay: " << rit->first; std::cerr << std::endl; +#endif eraseList.push_back(rit->first); } @@ -270,6 +325,7 @@ int UdpRelayReceiver::checkRelays() { removeUdpRelay_relayLocked(&(*it)); } + return 1; } @@ -551,6 +607,16 @@ int UdpRelayReceiver::RelayStatus(std::ostream &out) out << "\tDataSize: " << rit->second.mDataSize; out << "\tLastBandwidthTS: " << rit->second.mLastBandwidthTS; } + + out << "ClassLimits:" << std::endl; + for(int i = 0; i < UDP_RELAY_NUM_CLASS; i++) + { + out << "ClassLimit[" << i << "] = " << mClassLimit[i] << std::endl; + out << "ClassCount[" << i << "] = " << mClassCount[i] << std::endl; + out << "ClassBandwidth[" << i << "] = " << mClassBandwidth[i] << std::endl; + out << std::endl; + } + return 1; } @@ -562,20 +628,45 @@ int UdpRelayReceiver::status(std::ostream &out) RelayStatus(out); - RsStackMutex stack(relayMtx); /********** LOCK MUTEX *********/ - - out << "UdpRelayReceiver::Connections:" << std::endl; - - std::map::iterator pit; - for(pit = mStreams.begin(); pit != mStreams.end(); pit++) { - out << "\t" << pit->first << " : " << pit->second; - out << std::endl; + RsStackMutex stack(relayMtx); /********** LOCK MUTEX *********/ + + out << "UdpRelayReceiver::Connections:" << std::endl; + + std::map::iterator pit; + for(pit = mStreams.begin(); pit != mStreams.end(); pit++) + { + out << "\t" << pit->first << " : " << pit->second; + out << std::endl; + } } + UdpPeersStatus(out); + return 1; } +int UdpRelayReceiver::UdpPeersStatus(std::ostream &out) +{ + RsStackMutex stack(udppeerMtx); /********** LOCK MUTEX *********/ + + /* iterate through the Relays */ + out << "UdpRelayReceiver::UdpPeersStatus()"; + out << std::endl; + + std::map::iterator pit; + for(pit = mPeers.begin(); pit != mPeers.end(); pit++) + { + out << "UdpPeer for: " << pit->first; + out << " is: " << pit->second; + out << std::endl; + } + return 1; +} + + + + #define UDP_RELAY_HEADER_SIZE 16 /* higher level interface */ @@ -585,7 +676,7 @@ int UdpRelayReceiver::recvPkt(void *data, int size, struct sockaddr_in &from) (void) from; /* print packet information */ -#ifdef DEBUG_UDP_RELAY +#ifdef DEBUG_UDP_RELAY_PKTS std::cerr << "UdpRelayReceiver::recvPkt(" << size << ") from: " << from; std::cerr << std::endl; displayUdpRelayPacketHeader(data, size); @@ -594,7 +685,7 @@ int UdpRelayReceiver::recvPkt(void *data, int size, struct sockaddr_in &from) if (!isUdpRelayPacket(data, size)) { -#ifdef DEBUG_UDP_RELAY +#ifdef DEBUG_UDP_RELAY_PKTS std::cerr << "UdpRelayReceiver::recvPkt() is Not RELAY Pkt"; std::cerr << std::endl; #endif @@ -617,11 +708,11 @@ int UdpRelayReceiver::recvPkt(void *data, int size, struct sockaddr_in &from) if (rit != mRelays.end()) { /* we are the relay */ - #ifdef DEBUG_UDP_RELAY +#ifdef DEBUG_UDP_RELAY_PKTS std::cerr << "UdpRelayReceiver::recvPkt() We are the Relay. Passing onto: "; std::cerr << rit->first.mDestAddr; std::cerr << std::endl; - #endif +#endif /* do accounting */ rit->second.mLastTS = time(NULL); rit->second.mDataSize += size; @@ -642,11 +733,11 @@ int UdpRelayReceiver::recvPkt(void *data, int size, struct sockaddr_in &from) if (pit != mPeers.end()) { /* we are the end-point */ - #ifdef DEBUG_UDP_RELAY +#ifdef DEBUG_UDP_RELAY_PKTS std::cerr << "UdpRelayReceiver::recvPkt() Sending to UdpPeer: "; std::cerr << pit->first; std::cerr << std::endl; - #endif +#endif /* remove the header */ void *pktdata = (void *) (((uint8_t *) data) + UDP_RELAY_HEADER_SIZE); int pktsize = size - UDP_RELAY_HEADER_SIZE; @@ -657,10 +748,10 @@ int UdpRelayReceiver::recvPkt(void *data, int size, struct sockaddr_in &from) else { /* packet undersized */ - #ifdef DEBUG_UDP_RELAY +//#ifdef DEBUG_UDP_RELAY std::cerr << "UdpRelayReceiver::recvPkt() ERROR Packet Undersized"; std::cerr << std::endl; - #endif +//#endif } return 1; } @@ -668,10 +759,10 @@ int UdpRelayReceiver::recvPkt(void *data, int size, struct sockaddr_in &from) } /* unknown */ -#ifdef DEBUG_UDP_RELAY +//#ifdef DEBUG_UDP_RELAY std::cerr << "UdpRelayReceiver::recvPkt() Peer Unknown!"; std::cerr << std::endl; -#endif +//#endif return 0; } @@ -695,6 +786,10 @@ int UdpRelayReceiver::sendPkt(const void *data, int size, const struct sockaddr_ return 0; } +#ifdef DEBUG_UDP_RELAY_PKTS + std::cerr << "UdpRelayReceiver::sendPkt() to Relay: " << it->second; + std::cerr << std::endl; +#endif /* add a header to packet */ int finalPktSize = createRelayUdpPacket(data, size, mTmpSendPkt, MAX_RELAY_UDP_PACKET_SIZE, &(it->second)); @@ -755,6 +850,7 @@ int displayUdpRelayPacketHeader(const void *data, const int size) std::cerr << out.str(); std::cerr << std::endl; + return 1; } #endif @@ -800,6 +896,9 @@ int createRelayUdpPacket(const void *data, const int size, void *newpkt, int new { std::cerr << "createRelayUdpPacket() ERROR invalid size"; std::cerr << std::endl; + std::cerr << "Incoming DataSize: " << size << " + Header: " << UDP_RELAY_HEADER_SIZE; + std::cerr << " > " << newsize; + std::cerr << std::endl; return 0; } uint8_t *header = (uint8_t *) newpkt; diff --git a/libretroshare/src/tcponudp/udprelay.h b/libretroshare/src/tcponudp/udprelay.h index 2387e7d81..fd41dcd03 100644 --- a/libretroshare/src/tcponudp/udprelay.h +++ b/libretroshare/src/tcponudp/udprelay.h @@ -110,9 +110,9 @@ std::ostream &operator<<(std::ostream &out, const UdpRelayEnd &ure); //#define UDP_RELAY_LIFETIME_FOF 360 // 6 minutes. //#define UDP_RELAY_LIFETIME_FRIENDS 720 // 12 minutes. -#define UDP_RELAY_LIFETIME_GENERAL 1800 // 30 minutes -#define UDP_RELAY_LIFETIME_FOF 3600 // 1 Hour. -#define UDP_RELAY_LIFETIME_FRIENDS 7200 // 2 Hour. +#define UDP_RELAY_LIFETIME_GENERAL 3600 // 1 hour (chosen so we at least transfer 1 or 2 meg at lowest speed) +#define UDP_RELAY_LIFETIME_FOF 7200 // 2 Hours. +#define UDP_RELAY_LIFETIME_FRIENDS 14400 // 4 Hours. #define STD_RELAY_TTL 64 @@ -157,6 +157,7 @@ virtual int recvPkt(void *data, int size, struct sockaddr_in &from); virtual int sendPkt(const void *data, int size, const struct sockaddr_in &to, int ttl); int status(std::ostream &out); +int UdpPeersStatus(std::ostream &out); private: diff --git a/libretroshare/src/tcponudp/udpstunner.h b/libretroshare/src/tcponudp/udpstunner.h index 9d9f7b40a..74b07b517 100644 --- a/libretroshare/src/tcponudp/udpstunner.h +++ b/libretroshare/src/tcponudp/udpstunner.h @@ -72,8 +72,6 @@ class TouStunPeer * #define UDPSTUN_ALLOW_LOCALNET 1 */ -#define UDPSTUN_ALLOW_LOCALNET 1 - class UdpStunner: public UdpSubReceiver { public: diff --git a/libretroshare/src/zeroconf/p3zeroconf.cc b/libretroshare/src/zeroconf/p3zeroconf.cc index 16fd84ac0..552643e36 100644 --- a/libretroshare/src/zeroconf/p3zeroconf.cc +++ b/libretroshare/src/zeroconf/p3zeroconf.cc @@ -124,9 +124,7 @@ bool p3ZeroConf::getActive() bool p3ZeroConf::getNetworkStats(uint32_t &netsize, uint32_t &localnetsize) { - //netsize = mUdpBitDht->statsNetworkSize(); - //localnetsize = mUdpBitDht->statsBDVersionSize(); - return true; + return false; // Cannot provide Network Stats. } void p3ZeroConf::createTxtRecord()