improve udp stun and upnp collaboration

Conflicts:

	libretroshare/src/libretroshare.pro

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1749 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
joss17 2009-10-30 00:35:12 +00:00
parent e7273b0453
commit 324b51e250
4 changed files with 74 additions and 72 deletions

View File

@ -52,7 +52,10 @@ const uint32_t RS_STUN_DONE = 0x0002;
const uint32_t RS_STUN_LIST_MIN = 100; const uint32_t RS_STUN_LIST_MIN = 100;
const uint32_t RS_STUN_FOUND_MIN = 10; const uint32_t RS_STUN_FOUND_MIN = 10;
const uint32_t MAX_UPNP_INIT = 10; /* seconds UPnP timeout */ const uint32_t MAX_UPNP_INIT = 120; /* seconds UPnP timeout */
const uint32_t MAX_UDP_INIT = 150; /* seconds Udp timeout */
const uint32_t MIN_TIME_BETWEEN_NET_RESET = 150; /* seconds Udp timeout */
/**** /****
* #define CONN_DEBUG 1 * #define CONN_DEBUG 1
@ -272,7 +275,22 @@ void p3ConnectMgr::setOwnNetConfig(uint32_t netMode, uint32_t visState)
void p3ConnectMgr::netReset() void p3ConnectMgr::netReset()
{ {
std::cerr << "p3ConnectMgr::netReset()" << std::endl; std::cerr << "p3ConnectMgr::netReset()" << std::endl;
//don't do a net reset if the MIN_TIME_BETWEEN_NET_RESET is not reached
time_t delta = time(NULL) - mNetInitTS;
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr time since last reset : " << delta << std::endl;
#endif
if (delta < MIN_TIME_BETWEEN_NET_RESET) {
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::netStartup() don't do a net reset if the MIN_TIME_BETWEEN_NET_RESET is not reached" << std::endl;
#endif
return;
}
std::cerr << "p3ConnectMgr::netReset() shutdown" << std::endl; std::cerr << "p3ConnectMgr::netReset() shutdown" << std::endl;
shutdown(); /* blocking shutdown call */ shutdown(); /* blocking shutdown call */
@ -351,8 +369,8 @@ void p3ConnectMgr::netStartup()
RsStackMutex stack(connMtx); /****** STACK LOCK MUTEX *******/ RsStackMutex stack(connMtx); /****** STACK LOCK MUTEX *******/
mNetInitTS = time(NULL); mNetInitTS = time(NULL);
#ifdef CONN_DEBUG #ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::netStartup() resetting mNetInitTS timestamp" << std::endl;
std::cerr << "p3ConnectMgr::netStartup() tou_stunkeepalive() enabled" << std::endl; std::cerr << "p3ConnectMgr::netStartup() tou_stunkeepalive() enabled" << std::endl;
#endif #endif
tou_stunkeepalive(1); tou_stunkeepalive(1);
@ -604,6 +622,9 @@ void p3ConnectMgr::netUpnpCheck()
connMtx.lock(); /* LOCK MUTEX */ connMtx.lock(); /* LOCK MUTEX */
time_t delta = time(NULL) - mNetInitTS; time_t delta = time(NULL) - mNetInitTS;
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr time since last reset : " << delta << std::endl;
#endif
connMtx.unlock(); /* UNLOCK MUTEX */ connMtx.unlock(); /* UNLOCK MUTEX */
@ -613,6 +634,10 @@ void p3ConnectMgr::netUpnpCheck()
if ((upnpState < 0) || if ((upnpState < 0) ||
((upnpState == 0) && (delta > MAX_UPNP_INIT))) ((upnpState == 0) && (delta > MAX_UPNP_INIT)))
{ {
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::netUpnpCheck() ";
std::cerr << "Upnp Check failed." << std::endl;
#endif
/* fallback to UDP startup */ /* fallback to UDP startup */
connMtx.lock(); /* LOCK MUTEX */ connMtx.lock(); /* LOCK MUTEX */
@ -624,6 +649,10 @@ void p3ConnectMgr::netUpnpCheck()
} }
else if ((upnpState > 0) && netAssistExtAddress(extAddr)) else if ((upnpState > 0) && netAssistExtAddress(extAddr))
{ {
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::netUpnpCheck() ";
std::cerr << "Upnp Check successed." << std::endl;
#endif
/* switch to UDP startup */ /* switch to UDP startup */
connMtx.lock(); /* LOCK MUTEX */ connMtx.lock(); /* LOCK MUTEX */
@ -652,6 +681,15 @@ void p3ConnectMgr::netUdpCheck()
#endif #endif
struct sockaddr_in tmpip ; struct sockaddr_in tmpip ;
//don't check it if the udp init is not finished
time_t delta = time(NULL) - mNetInitTS;
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr time since last reset : " << delta << std::endl;
#endif
if (delta < MAX_UDP_INIT) {
return;
}
if (udpExtAddressCheck() || (mUpnpAddrValid) || (use_extr_addr_finder && mExtAddrFinder->hasValidIP(&tmpip))) if (udpExtAddressCheck() || (mUpnpAddrValid) || (use_extr_addr_finder && mExtAddrFinder->hasValidIP(&tmpip)))
{ {
bool extValid = false; bool extValid = false;

View File

@ -733,6 +733,12 @@ const std::string CUPnPService::GetStateVariable(
GetAbsControlURL().c_str(), GetAbsControlURL().c_str(),
stateVariableName.c_str(), stateVariableName.c_str(),
&StVarVal); &StVarVal);
if (StVarVal != NULL) {
std::string varValue = std::string(StVarVal);
std::cerr << "GetStateVariable varValue returned by UpnpGetServiceVarStatus : " << varValue << std::endl;
return varValue;
} else {
//use event to get state variable
std::cerr << "GetStateVariable pausing in case of an UPnP event incomming."; std::cerr << "GetStateVariable pausing in case of an UPnP event incomming.";
time_t begin_time = time(NULL); time_t begin_time = time(NULL);
while (true) { while (true) {
@ -753,6 +759,7 @@ const std::string CUPnPService::GetStateVariable(
return (*it).second; return (*it).second;
} }
} }
}
CUPnPDevice::CUPnPDevice( CUPnPDevice::CUPnPDevice(
@ -1078,14 +1085,6 @@ bool CUPnPControlPoint::DeletePortMappings(
} }
int n = upnpPortMapping.size(); int n = upnpPortMapping.size();
bool ok = false;
// Check the number of port mappings before
std::istringstream PortMappingNumberOfEntries(
m_WanService->GetStateVariable(
"PortMappingNumberOfEntries"));
unsigned long oldNumberOfEntries;
PortMappingNumberOfEntries >> oldNumberOfEntries;
// Delete the enabled port mappings // Delete the enabled port mappings
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
@ -1109,20 +1108,7 @@ bool CUPnPControlPoint::DeletePortMappings(
} }
} }
// Debug only return true;
std::cerr << "CUPnPControlPoint::DeletePortMappings: "
"m_ActivePortMappingsMap.size() == " <<
m_ActivePortMappingsMap.size() << std::endl;
// Not very good, must find a better test
PortMappingNumberOfEntries.str(
m_WanService->GetStateVariable(
"PortMappingNumberOfEntries"));
unsigned long newNumberOfEntries;
PortMappingNumberOfEntries >> newNumberOfEntries;
ok = oldNumberOfEntries - newNumberOfEntries >= 4;
return ok;
} }

View File

@ -59,6 +59,7 @@ class upnpThreadData
/* Thread routines */ /* Thread routines */
extern "C" void* doSetupUPnP(void* p) extern "C" void* doSetupUPnP(void* p)
{ {
std::cerr << "doSetupUPnP Creating upnp thread." << std::endl;
upnpThreadData *data = (upnpThreadData *) p; upnpThreadData *data = (upnpThreadData *) p;
if ((!data) || (!data->handler)) if ((!data) || (!data->handler))
{ {
@ -88,7 +89,7 @@ bool upnphandler::background_setup_upnp(bool start, bool stop)
pthread_t tid; pthread_t tid;
/* launch thread */ /* launch thread */
std::cerr << "Creating upnp thread." << std::endl; std::cerr << "background_setup_upnp Creating upnp thread." << std::endl;
upnpThreadData *data = new upnpThreadData(); upnpThreadData *data = new upnpThreadData();
data->handler = this; data->handler = this;
data->start = start; data->start = start;
@ -249,7 +250,10 @@ bool upnphandler::shutdown_upnp()
//destroy the upnp object //destroy the upnp object
cUPnPControlPoint->~CUPnPControlPoint(); cUPnPControlPoint->~CUPnPControlPoint();
upnpState = RS_UPNP_S_UNINITIALISED; upnpState = RS_UPNP_S_UNINITIALISED;
//stopping os ok, set starting to true for next net reset
toStop = false; toStop = false;
toStart = true;
} }
return true; return true;
@ -276,6 +280,9 @@ upnphandler::~upnphandler()
/* RsIface */ /* RsIface */
void upnphandler::enable(bool active) void upnphandler::enable(bool active)
{ {
std::cerr << "upnphandler::enable called with argument active : " << active << std::endl;
std::cerr << "toEnable : " << toEnable << std::endl;
std::cerr << "toStart : " << toStart << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/ dataMtx.lock(); /*** LOCK MUTEX ***/
if (active != toEnable) if (active != toEnable)

View File

@ -12,29 +12,6 @@
#include "upnp.h" #include "upnp.h"
#include "upnp/UPnPBase.h" #include "upnp/UPnPBase.h"
class upnpentry
{
public:
std::string name;
std::string id;
struct sockaddr_in addr;
unsigned int flags;
int status;
int lastTs;
};
class upnpforward
{
public:
std::string name;
unsigned int flags;
struct sockaddr_in iaddr;
struct sockaddr_in eaddr;
int status;
int lastTs;
};
#define RS_UPNP_S_UNINITIALISED 0 #define RS_UPNP_S_UNINITIALISED 0
#define RS_UPNP_S_UNAVAILABLE 1 #define RS_UPNP_S_UNAVAILABLE 1
#define RS_UPNP_S_READY 2 #define RS_UPNP_S_READY 2
@ -42,8 +19,6 @@ class upnpforward
#define RS_UPNP_S_UDP_FAILED 4 #define RS_UPNP_S_UDP_FAILED 4
#define RS_UPNP_S_ACTIVE 5 #define RS_UPNP_S_ACTIVE 5
class uPnPConfigData;
class upnphandler: public pqiNetAssistFirewall class upnphandler: public pqiNetAssistFirewall
{ {
public: public:
@ -95,10 +70,6 @@ class upnphandler: public pqiNetAssistFirewall
/* info from upnp */ /* info from upnp */
struct sockaddr_in upnp_iaddr; struct sockaddr_in upnp_iaddr;
struct sockaddr_in upnp_eaddr; struct sockaddr_in upnp_eaddr;
/* active port forwarding */
std::list<upnpforward> activeForwards;
}; };
/* info from upnp */ /* info from upnp */