diff --git a/libretroshare/src/pqi/p3connmgr.cc b/libretroshare/src/pqi/p3connmgr.cc index bee151ffe..3d4df2817 100644 --- a/libretroshare/src/pqi/p3connmgr.cc +++ b/libretroshare/src/pqi/p3connmgr.cc @@ -43,7 +43,7 @@ const uint32_t RS_STUN_DHT = 0x0001; const uint32_t RS_STUN_DONE = 0x0002; const uint32_t RS_STUN_LIST_MIN = 100; -const uint32_t MAX_UPNP_INIT = 10; /* seconds UPnP timeout */ +const uint32_t MAX_UPNP_INIT = 60; /* seconds UPnP timeout */ #define CONN_DEBUG 1 @@ -308,6 +308,12 @@ void p3ConnectMgr::netUpnpInit() /* get the ports from the configuration */ mNetStatus = RS_NET_UPNP_SETUP; + iport = ntohs(ownState.localaddr.sin_port); + eport = ntohs(ownState.serveraddr.sin_port); + if ((eport < 1000) || (eport > 10000)) + { + eport = iport; + } connMtx.unlock(); /* UNLOCK MUTEX */ @@ -337,7 +343,7 @@ void p3ConnectMgr::netUpnpCheck() mUpnpAddrValid = false; mNetStatus = RS_NET_UDP_SETUP; - ownState.netMode = RS_NET_MODE_UDP; /* UPnP Failed us! */ + //ownState.netMode = RS_NET_MODE_UDP; /* UPnP Failed us! */ connMtx.unlock(); /* UNLOCK MUTEX */ } @@ -360,7 +366,7 @@ void p3ConnectMgr::netUdpCheck() #ifdef CONN_DEBUG std::cerr << "p3ConnectMgr::netUdpCheck()" << std::endl; #endif - if (stunCheck()) + if (stunCheck() || (mUpnpAddrValid)) { bool extValid = false; struct sockaddr_in extAddr; @@ -390,8 +396,7 @@ void p3ConnectMgr::netUdpCheck() { ownState.serveraddr = extAddr; mode = RS_NET_CONN_TCP_LOCAL | RS_NET_CONN_UDP_DHT_SYNC; - if ((ownState.netMode == RS_NET_MODE_UPNP) || - (ownState.netMode == RS_NET_MODE_EXT)) + if (mUpnpAddrValid || (ownState.netMode == RS_NET_MODE_EXT)) { mode |= RS_NET_CONN_TCP_EXTERNAL; } diff --git a/libretroshare/src/pqi/p3dhtmgr.cc b/libretroshare/src/pqi/p3dhtmgr.cc index 6a7a70b0b..bce8e4f48 100644 --- a/libretroshare/src/pqi/p3dhtmgr.cc +++ b/libretroshare/src/pqi/p3dhtmgr.cc @@ -51,10 +51,10 @@ #define DHT_STATE_ACTIVE 4 /* TIMEOUTS */ -#define DHT_SEARCH_PERIOD 600 /* 10 min */ -#define DHT_CHECK_PERIOD 60 //1800 /* 30 min */ -#define DHT_PUBLISH_PERIOD 1800 /* 30 min */ -#define DHT_NOTIFY_PERIOD 20 //120 /* 2 min - Notify Check period */ +#define DHT_SEARCH_PERIOD 1800 /* PeerKeys: if we haven't found them: 30 min */ +#define DHT_CHECK_PERIOD 3600 /* PeerKeys: re-lookup peer: 60 min */ +#define DHT_PUBLISH_PERIOD 1800 /* OwnKey 30 min */ +#define DHT_NOTIFY_PERIOD 600 /* 10 min - Notify Check period */ #define DHT_RESTART_PERIOD 300 /* 5 min */ #define DHT_DEFAULT_PERIOD 600 /* Default period if no work to do */ @@ -63,7 +63,7 @@ #define DHT_DEFAULT_WAITTIME 1 /* Std sleep break period */ /* TTLs for DHTs posts */ -#define DHT_TTL_PUBLISH DHT_PUBLISH_PERIOD +#define DHT_TTL_PUBLISH (DHT_PUBLISH_PERIOD + 30) #define DHT_TTL_NOTIFY DHT_NOTIFY_PERIOD diff --git a/libretroshare/src/rsserver/p3face-startup.cc b/libretroshare/src/rsserver/p3face-startup.cc index 68a618a6c..cf8224e29 100644 --- a/libretroshare/src/rsserver/p3face-startup.cc +++ b/libretroshare/src/rsserver/p3face-startup.cc @@ -631,7 +631,13 @@ int RsServer::StartupRetroShare(RsInit *config) /* Force Any Last Configuration Options */ /**************************************************************************/ + /**************************************************************************/ + /* Start up Threads */ + /**************************************************************************/ + server->StartupMonitor(); + mUpnpMgr->start(); + #ifdef PQI_USE_CHANNELS server->setP3Channel(pqih->getP3Channel()); diff --git a/libretroshare/src/tcponudp/udpsorter.cc b/libretroshare/src/tcponudp/udpsorter.cc index a67965c86..00bbd4596 100644 --- a/libretroshare/src/tcponudp/udpsorter.cc +++ b/libretroshare/src/tcponudp/udpsorter.cc @@ -24,6 +24,7 @@ */ #include "udpsorter.h" +#include "util/rsnet.h" #include #include @@ -38,8 +39,10 @@ static const int STUN_TTL = 64; #define DEBUG_UDP_SORTER 1 UdpSorter::UdpSorter(struct sockaddr_in &local) - : udpLayer(NULL), laddr(local) + :udpLayer(NULL), laddr(local), eaddrKnown(false) { + sockaddr_clear(&eaddr); + openSocket(); return; } diff --git a/libretroshare/src/upnp/upnphandler.cc b/libretroshare/src/upnp/upnphandler.cc index de6b2b82b..588fa510f 100644 --- a/libretroshare/src/upnp/upnphandler.cc +++ b/libretroshare/src/upnp/upnphandler.cc @@ -25,6 +25,8 @@ class uPnPConfigData #include #include +#include "util/rsnet.h" + void upnphandler::run() { @@ -73,8 +75,8 @@ void upnphandler::run() case RS_UPNP_S_TCP_FAILED: case RS_UPNP_S_UDP_FAILED: case RS_UPNP_S_ACTIVE: - /* working ... normal 15 seconds */ - allowedSleep = 15; + /* working ... normal 10 seconds */ + allowedSleep = 10; break; default: @@ -243,7 +245,7 @@ bool upnphandler::updateUPnP() char eport2[256]; snprintf(eport1, 256, "%d", eport_curr); - snprintf(eport2, 256, "%d", eport_curr + 1); + snprintf(eport2, 256, "%d", eport_curr); std::cerr << "Attempting To Remove Redirection: port: " << eport1; std::cerr << " Prot: " << eprot1; @@ -297,7 +299,7 @@ bool upnphandler::updateUPnP() struct sockaddr_in localAddr = upnp_iaddr; snprintf(in_port1, 256, "%d", ntohs(localAddr.sin_port)); - snprintf(in_port2, 256, "%d", ntohs(localAddr.sin_port) + 1); + snprintf(in_port2, 256, "%d", ntohs(localAddr.sin_port)); snprintf(in_addr, 256, "%d.%d.%d.%d", ((localAddr.sin_addr.s_addr >> 0) & 0xff), ((localAddr.sin_addr.s_addr >> 8) & 0xff), @@ -305,7 +307,7 @@ bool upnphandler::updateUPnP() ((localAddr.sin_addr.s_addr >> 24) & 0xff)); snprintf(eport1, 256, "%d", eport_curr); - snprintf(eport2, 256, "%d", eport_curr + 1); + snprintf(eport2, 256, "%d", eport_curr); std::cerr << "Attempting Redirection: InAddr: " << in_addr; std::cerr << " InPort: " << in_port1; @@ -327,11 +329,35 @@ bool upnphandler::updateUPnP() { upnpState = RS_UPNP_S_ACTIVE; } + + + /* now store the external address */ + char externalIPAddress[32]; + UPNP_GetExternalIPAddress(config -> urls.controlURL, + config->data.servicetype, + externalIPAddress); + + sockaddr_clear(&upnp_eaddr); + + if(externalIPAddress[0]) + { + std::cerr << "Stored External address: " << externalIPAddress; + std::cerr << ":" << eport_curr; + std::cerr << std::endl; + + inet_aton(externalIPAddress, &(upnp_eaddr.sin_addr)); + upnp_eaddr.sin_family = AF_INET; + upnp_eaddr.sin_port = htons(eport_curr); + } + else + { + std::cerr << "FAILED To get external Address"; + std::cerr << std::endl; + } } dataMtx.unlock(); /* UNLOCK MUTEX */ - return true; } @@ -418,6 +444,9 @@ void upnphandler::setInternalPort(unsigned short iport_in) dataMtx.lock(); /*** LOCK MUTEX ***/ // std::cerr << "UPnPHandler::setInternalAddress() postLock!" << std::endl; + std::cerr << "UPnPHandler::setInternalPort(" << iport_in << ") current port: "; + std::cerr << iport << std::endl; + if (iport != iport_in) { iport = iport_in; @@ -437,6 +466,9 @@ void upnphandler::setExternalPort(unsigned short eport_in) dataMtx.lock(); /*** LOCK MUTEX ***/ // std::cerr << "UPnPHandler::getExternalPort() postLock!" << std::endl; + std::cerr << "UPnPHandler::setExternalPort(" << eport_in << ") current port: "; + std::cerr << eport << std::endl; + /* flag both shutdown/start -> for restart */ if (eport != eport_in) { @@ -459,8 +491,10 @@ bool upnphandler::getInternalAddress(struct sockaddr_in &addr) dataMtx.lock(); /*** LOCK MUTEX ***/ // std::cerr << "UPnPHandler::getInternalAddress() postLock!" << std::endl; + std::cerr << "UPnPHandler::getInternalAddress()" << std::endl; + addr = upnp_iaddr; - bool valid = (upnpState >= RS_UPNP_S_READY); + bool valid = (upnpState >= RS_UPNP_S_ACTIVE); dataMtx.unlock(); /*** UNLOCK MUTEX ***/ @@ -473,8 +507,9 @@ bool upnphandler::getExternalAddress(struct sockaddr_in &addr) dataMtx.lock(); /*** LOCK MUTEX ***/ // std::cerr << "UPnPHandler::getExternalAddress() postLock!" << std::endl; + std::cerr << "UPnPHandler::getExternalAddress()" << std::endl; addr = upnp_eaddr; - bool valid = (upnpState >= RS_UPNP_S_READY); + bool valid = (upnpState == RS_UPNP_S_ACTIVE); dataMtx.unlock(); /*** UNLOCK MUTEX ***/