diff --git a/libretroshare/src/dht/opendht.cc b/libretroshare/src/dht/opendht.cc index 8453d617c..ca3185d26 100644 --- a/libretroshare/src/dht/opendht.cc +++ b/libretroshare/src/dht/opendht.cc @@ -30,6 +30,7 @@ #include #include "util/rsnet.h" +#include "util/rsprint.h" const std::string openDHT_Client = "Retroshare V0.4"; const std::string openDHT_Agent = "RS-HTTP-V0.4"; @@ -303,7 +304,7 @@ bool OpenDHTClient::publishKey(std::string key, std::string value, uint32_t ttl) { /* create request */ #ifdef OPENDHT_DEBUG - std::cerr << "OpenDHTClient::openDHT_publishKey() key: " << key << " value: " << value; + std::cerr << "OpenDHTClient::openDHT_publishKey() key: 0x" << RsUtil::BinToHex(key) << " value: 0x" << RsUtil::BinToHex(value); std::cerr << std::endl; #endif std::string putmsg = createOpenDHT_put(key, value, ttl, openDHT_Client); @@ -391,7 +392,7 @@ bool OpenDHTClient::searchKey(std::string key, std::list &values) { std::cerr << "openDHT_searchKey() Value:" << value << ":END:" << std::endl; std::string result = convertFromBase64(value); - std::cerr << "openDHT_searchKey() Result:" << result << ":END:" << std::endl; + std::cerr << "openDHT_searchKey() Result: 0x" << RsUtil::BinToHex(result) << ":END:" << std::endl; values.push_back(result); } diff --git a/libretroshare/src/pqi/p3connmgr.cc b/libretroshare/src/pqi/p3connmgr.cc index 2cd59b2ba..2a8758749 100644 --- a/libretroshare/src/pqi/p3connmgr.cc +++ b/libretroshare/src/pqi/p3connmgr.cc @@ -45,16 +45,24 @@ const uint32_t RS_STUN_LIST_MIN = 100; const uint32_t MAX_UPNP_INIT = 60; /* seconds UPnP timeout */ -#define CONN_DEBUG 1 -#define P3CONNMGR_NO_TCP_CONNECTIONS 1 - +/**** + * #define CONN_DEBUG 1 + ***/ +/**** + * #define P3CONNMGR_NO_TCP_CONNECTIONS 1 + ***/ /**** * #define P3CONNMGR_NO_AUTO_CONNECTION 1 ***/ +#define CONN_DEBUG 1 + const uint32_t P3CONNMGR_TCP_DEFAULT_DELAY = 10; /* 10 Seconds should be enough! */ -const uint32_t P3CONNMGR_UDP_DHT_DELAY = 300 + 60; /* 5 minutes FIND + 1 minute for DHT POST */ -const uint32_t P3CONNMGR_UDP_PROXY_DELAY = 30; /* 30 seconds */ +const uint32_t P3CONNMGR_UDP_DHT_DELAY = DHT_NOTIFY_PERIOD + 60; /* + 1 minute for DHT POST */ +const uint32_t P3CONNMGR_UDP_PROXY_DELAY = 30; /* 30 seconds (NOT IMPLEMENTED YET!) */ + +#define MAX_AVAIL_PERIOD (2 * DHT_NOTIFY_PERIOD) // If we haven't connected in 2 DHT periods. +#define MIN_RETRY_PERIOD (DHT_CHECK_PERIOD + 120) // just over DHT CHECK_PERIOD void printConnectState(peerConnectState &peer); @@ -263,10 +271,19 @@ void p3ConnectMgr::netStartup() case RS_NET_MODE_TRY_UPNP: default: + /* Force it here (could be default!) */ + ownState.netMode |= RS_NET_MODE_TRY_UPNP; ownState.netMode |= RS_NET_MODE_UDP; mNetStatus = RS_NET_UPNP_INIT; break; } + + /* add Bootstrap Peers if we've got none from config */ + if (mStunList.size() < 1) + { + addBootstrapStunPeers(); + } + connMtx.unlock(); /* UNLOCK MUTEX */ } @@ -279,8 +296,6 @@ void p3ConnectMgr::tick() } -#define MAX_AVAIL_PERIOD 900 // 15 minutes -#define MIN_RETRY_PERIOD 1900 // just over 30 minutes. (DHT retry period) void p3ConnectMgr::statusTick() { @@ -338,7 +353,7 @@ void p3ConnectMgr::statusTick() std::cerr << std::endl; #endif /* retry it! */ - retryConnect(*it2); + retryConnectTCP(*it2); } #endif @@ -1606,7 +1621,13 @@ void p3ConnectMgr::peerStatus(std::string id, std::cerr << "p3ConnectMgr::peerStatus() ALREADY IN CONNECT ATTEMPT: "; std::cerr << " id: " << id; std::cerr << std::endl; + /* -> it'll automatically use the addresses */ + + std::cerr << "p3ConnectMgr::peerStatus() Resulting Peer State:" << std::endl; + printConnectState(it->second); + std::cerr << std::endl; + return; } @@ -1628,6 +1649,10 @@ void p3ConnectMgr::peerStatus(std::string id, std::cerr << std::endl; } + std::cerr << "p3ConnectMgr::peerStatus() Resulting Peer State:" << std::endl; + printConnectState(it->second); + std::cerr << std::endl; + } void p3ConnectMgr::peerConnectRequest(std::string id, struct sockaddr_in raddr, @@ -2674,3 +2699,22 @@ void printConnectState(peerConnectState &peer) return; } + + +bool p3ConnectMgr::addBootstrapStunPeers() +{ + std::string id; + + // Two Defaults for The Initial Release. + id = "7ad672ea4d4af8560d5230aff3c88b59"; + mStunList.push_back(RsUtil::HashId(id, false)); + + id = "8ad7c08e7778e0289de04843bf57a6ae"; + mStunList.push_back(RsUtil::HashId(id, false)); + +} + + + + + diff --git a/libretroshare/src/pqi/p3connmgr.h b/libretroshare/src/pqi/p3connmgr.h index 0813001e5..6e595be55 100644 --- a/libretroshare/src/pqi/p3connmgr.h +++ b/libretroshare/src/pqi/p3connmgr.h @@ -248,6 +248,7 @@ void udpStunPeer(std::string id, struct sockaddr_in &addr); void stunInit(); bool stunCheck(); void stunCollect(std::string id, struct sockaddr_in addr, uint32_t flags); +bool addBootstrapStunPeers(); /* monitor control */ void tickMonitors(); diff --git a/libretroshare/src/pqi/p3dhtmgr.cc b/libretroshare/src/pqi/p3dhtmgr.cc index 4837af9e8..d62a421b3 100644 --- a/libretroshare/src/pqi/p3dhtmgr.cc +++ b/libretroshare/src/pqi/p3dhtmgr.cc @@ -53,22 +53,13 @@ #define DHT_STATE_FIND_STUN 3 #define DHT_STATE_ACTIVE 4 -/* TIMEOUTS */ -#define DHT_SEARCH_PERIOD 1800 /* PeerKeys: if we haven't found them: 30 min */ -#define DHT_CHECK_PERIOD 1800 /* PeerKeys: re-lookup peer: 30 min */ -#define DHT_PUBLISH_PERIOD 900 /* OwnKey 30 min (15 minutes for now) */ -#define DHT_NOTIFY_PERIOD 300 /* 5 min - Notify Check period */ +/* TIMEOUTS (Key ones in .h) */ #define DHT_RESTART_PERIOD 300 /* 5 min */ - -#define DHT_DEFAULT_PERIOD 600 /* Default period if no work to do */ +#define DHT_DEFAULT_PERIOD 300 /* Default period if no work to do */ #define DHT_MIN_PERIOD 1 /* to ensure we don't get too many requests */ #define DHT_DEFAULT_WAITTIME 1 /* Std sleep break period */ -/* TTLs for DHTs posts */ -#define DHT_TTL_PUBLISH (DHT_PUBLISH_PERIOD + 120) // for a little overlap. -#define DHT_TTL_NOTIFY (DHT_NOTIFY_PERIOD + 60) // for time to find it... - void printDhtPeerEntry(dhtPeerEntry *ent, std::ostream &out); @@ -509,7 +500,7 @@ int p3DhtMgr::checkOwnDHTKeys() #ifdef DHT_DEBUG std::cerr << "PUBLISH: "; - std::cerr << " hash1: " << peer.hash1; + std::cerr << " hash1: " << RsUtil::BinToHex(peer.hash1); std::cerr << " laddr: " << inet_ntoa(peer.laddr.sin_addr); std::cerr << ":" << ntohs(peer.laddr.sin_port); std::cerr << " raddr: " << inet_ntoa(peer.raddr.sin_addr); @@ -1071,7 +1062,7 @@ bool p3DhtMgr::dhtPublish(std::string idhash, #ifdef DHT_DEBUG std::cerr << "p3DhtMgr::dhtPublish()" << std::endl; - std::cerr << "PUBLISHing: idhash: " << idhash; + std::cerr << "PUBLISHing: idhash: " << RsUtil::BinToHex(idhash); std::cerr << " laddr: " << inet_ntoa(laddr.sin_addr); std::cerr << ":" << ntohs(laddr.sin_port); std::cerr << " raddr: " << inet_ntoa(raddr.sin_addr); @@ -1108,7 +1099,7 @@ bool p3DhtMgr::dhtPublish(std::string idhash, #ifdef DHT_DEBUG std::cerr << "p3DhtMgr::dhtPublish()" << std::endl; - std::cerr << "PUBLISH: key: " << idhash; + std::cerr << "PUBLISH: key: " << RsUtil::BinToHex(idhash); std::cerr << " value: " << value; std::cerr << std::endl; #endif @@ -1198,7 +1189,7 @@ bool p3DhtMgr::resultDHT(std::string key, std::string value) /* get the hash */ std::string notifyHash = value.substr(loc); #ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() NOTIFY msg HASH:->" << notifyHash << "<-" << std::endl; + std::cerr << "p3DhtMgr::resultDHT() NOTIFY msg HASH:-> 0x" << RsUtil::BinToHex(notifyHash) << "<-" << std::endl; #endif /* call out */ dhtResultNotify(notifyHash); @@ -1344,16 +1335,16 @@ bool p3DhtMgr::dhtResultSearch(std::string idhash, #endif it->second.lastTS = now; - /* Do callback all the time */ - ent = it->second; - doCb = true; - /* update info .... always */ it->second.state = DHT_PEER_FOUND; it->second.laddr = laddr; it->second.raddr = raddr; it->second.type = type; + /* Do callback all the time */ + ent = it->second; + doCb = true; + if (it->second.notifyPending) { /* no wait if we have pendingNotification */ diff --git a/libretroshare/src/pqi/p3dhtmgr.h b/libretroshare/src/pqi/p3dhtmgr.h index 7c95bd958..7730a19c8 100644 --- a/libretroshare/src/pqi/p3dhtmgr.h +++ b/libretroshare/src/pqi/p3dhtmgr.h @@ -57,6 +57,21 @@ #define DHT_MODE_NOTIFY 2 + +/* TIMEOUTS: Reference Values are set here... */ + +#define DHT_SEARCH_PERIOD 1800 /* PeerKeys: if we haven't found them: 30 min */ +#define DHT_CHECK_PERIOD 1800 /* PeerKeys: re-lookup peer: 30 min */ +#define DHT_PUBLISH_PERIOD 1800 /* OwnKey: 30 min */ +#define DHT_NOTIFY_PERIOD 300 /* 5 min - Notify Check period */ + +/* TTLs for DHTs posts */ +#define DHT_TTL_PUBLISH (DHT_PUBLISH_PERIOD + 120) // for a little overlap. +#define DHT_TTL_NOTIFY (DHT_NOTIFY_PERIOD + 60) // for time to find it... + + + + class dhtPeerEntry { public: diff --git a/libretroshare/src/serialiser/rsconfigitems.cc b/libretroshare/src/serialiser/rsconfigitems.cc index 774f07871..76f4b21be 100644 --- a/libretroshare/src/serialiser/rsconfigitems.cc +++ b/libretroshare/src/serialiser/rsconfigitems.cc @@ -743,7 +743,7 @@ std::ostream &RsPeerStunItem::print(std::ostream &out, uint16_t indent) printRsItemBase(out, "RsPeerStunItem", indent); uint16_t int_Indent = indent + 2; - stunList.print(out, int_Indent); + stunList.printHex(out, int_Indent); printRsItemEnd(out, "RsPeerStunItem", indent); return out; diff --git a/libretroshare/src/serialiser/rstlvtypes.cc b/libretroshare/src/serialiser/rstlvtypes.cc index 1721f5759..f9f44dfee 100644 --- a/libretroshare/src/serialiser/rstlvtypes.cc +++ b/libretroshare/src/serialiser/rstlvtypes.cc @@ -27,6 +27,7 @@ #include "rstlvbase.h" #include "rstlvtypes.h" #include "rsbaseserial.h" +#include "util/rsprint.h" #include #include #include @@ -322,6 +323,25 @@ std::ostream &RsTlvPeerIdSet::print(std::ostream &out, uint16_t indent) } +/// print to screen RsTlvPeerIdSet contents +std::ostream &RsTlvPeerIdSet::printHex(std::ostream &out, uint16_t indent) +{ + printBase(out, "RsTlvPeerIdSet", indent); + uint16_t int_Indent = indent + 2; + + std::list::iterator it; + for(it = ids.begin(); it != ids.end() ; ++it) + { + printIndent(out, int_Indent); + out << "id: 0x" << RsUtil::BinToHex(*it); + out << std::endl; + } + + printEnd(out, "RsTlvPeerIdSet", indent); + return out; + +} + /************************************* Service Id Set ************************************/ diff --git a/libretroshare/src/serialiser/rstlvtypes.h b/libretroshare/src/serialiser/rstlvtypes.h index 135d74829..c937b328a 100644 --- a/libretroshare/src/serialiser/rstlvtypes.h +++ b/libretroshare/src/serialiser/rstlvtypes.h @@ -150,6 +150,7 @@ virtual void TlvClear(); virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */ virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */ virtual std::ostream &print(std::ostream &out, uint16_t indent); +virtual std::ostream &printHex(std::ostream &out, uint16_t indent); /* SPECIAL One */ std::list ids; /* Mandatory */ }; diff --git a/libretroshare/src/tcponudp/tcpstream.h b/libretroshare/src/tcponudp/tcpstream.h index 5a4b8bc4a..2639b798f 100644 --- a/libretroshare/src/tcponudp/tcpstream.h +++ b/libretroshare/src/tcponudp/tcpstream.h @@ -41,7 +41,7 @@ #define MAX_SEG 1500 #define TCP_MAX_SEQ UINT_MAX #define TCP_MAX_WIN 65500 -#define TCP_ALIVE_TIMEOUT 20 /* 20 sec */ +#define TCP_ALIVE_TIMEOUT 15 /* 15 sec ... < 20 sec UDP state limit on some firewalls */ #define TCP_RETRANS_TIMEOUT 1 /* 1 sec (Initial value) */ #define kNoPktTimeout 60 /* 1 min */ diff --git a/libretroshare/src/tcponudp/udpsorter.cc b/libretroshare/src/tcponudp/udpsorter.cc index 6b1f7f74d..12e7f7eee 100644 --- a/libretroshare/src/tcponudp/udpsorter.cc +++ b/libretroshare/src/tcponudp/udpsorter.cc @@ -25,6 +25,7 @@ #include "udpsorter.h" #include "util/rsnet.h" +#include "util/rsprint.h" #include #include @@ -222,7 +223,8 @@ bool UdpSorter::locked_handleStunPkt(void *data, int size, struct sockaddr_in &f if (size == 20) /* request */ { #ifdef DEBUG_UDP_SORTER - std::cerr << "UdpSorter::handleStunPkt() got Request"; + std::cerr << "UdpSorter::handleStunPkt() got Request from: "; + std::cerr << inet_ntoa(from.sin_addr) << ":" << ntohs(from.sin_port); std::cerr << std::endl; #endif @@ -752,8 +754,10 @@ bool UdpSorter::locked_printStunList() std::list::iterator it; for(it = mStunList.begin(); it != mStunList.end(); it++) { - out << "id:" << it->id << " addr: " << inet_ntoa(it->remote.sin_addr); + out << "id:" << RsUtil::BinToHex(it->id) << " addr: " << inet_ntoa(it->remote.sin_addr); out << ":" << htons(it->remote.sin_port); + out << " eaddr: " << inet_ntoa(it->eaddr.sin_addr); + out << ":" << htons(it->eaddr.sin_port); out << " failCount: " << it->failCount; out << " lastSend: " << now - it->lastsend; out << std::endl;