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_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
@ -272,7 +275,22 @@ void p3ConnectMgr::setOwnNetConfig(uint32_t netMode, uint32_t visState)
void p3ConnectMgr::netReset()
{
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;
shutdown(); /* blocking shutdown call */
@ -351,8 +369,8 @@ void p3ConnectMgr::netStartup()
RsStackMutex stack(connMtx); /****** STACK LOCK MUTEX *******/
mNetInitTS = time(NULL);
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::netStartup() resetting mNetInitTS timestamp" << std::endl;
std::cerr << "p3ConnectMgr::netStartup() tou_stunkeepalive() enabled" << std::endl;
#endif
tou_stunkeepalive(1);
@ -604,6 +622,9 @@ void p3ConnectMgr::netUpnpCheck()
connMtx.lock(); /* LOCK MUTEX */
time_t delta = time(NULL) - mNetInitTS;
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr time since last reset : " << delta << std::endl;
#endif
connMtx.unlock(); /* UNLOCK MUTEX */
@ -613,6 +634,10 @@ void p3ConnectMgr::netUpnpCheck()
if ((upnpState < 0) ||
((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 */
connMtx.lock(); /* LOCK MUTEX */
@ -624,6 +649,10 @@ void p3ConnectMgr::netUpnpCheck()
}
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 */
connMtx.lock(); /* LOCK MUTEX */
@ -652,6 +681,15 @@ void p3ConnectMgr::netUdpCheck()
#endif
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)))
{
bool extValid = false;

View File

@ -733,24 +733,31 @@ const std::string CUPnPService::GetStateVariable(
GetAbsControlURL().c_str(),
stateVariableName.c_str(),
&StVarVal);
std::cerr << "GetStateVariable pausing in case of an UPnP event incomming.";
time_t begin_time = time(NULL);
while (true) {
if (time(NULL) - begin_time > 7) {
break;
}
}
std::cerr << "GetStateVariable pause finished.";
std::cerr << "GetStateVariable(" << stateVariableName << ") = ";
std::map<std::string, std::string>::iterator it;
it = propertyMap.find(stateVariableName);
if (it == propertyMap.end()) {
std::cerr << "Empty String" << std::endl;
return stdEmptyString;
if (StVarVal != NULL) {
std::string varValue = std::string(StVarVal);
std::cerr << "GetStateVariable varValue returned by UpnpGetServiceVarStatus : " << varValue << std::endl;
return varValue;
} else {
std::cerr << (*it).second << std::endl;
return (*it).second;
//use event to get state variable
std::cerr << "GetStateVariable pausing in case of an UPnP event incomming.";
time_t begin_time = time(NULL);
while (true) {
if (time(NULL) - begin_time > 7) {
break;
}
}
std::cerr << "GetStateVariable pause finished.";
std::cerr << "GetStateVariable(" << stateVariableName << ") = ";
std::map<std::string, std::string>::iterator it;
it = propertyMap.find(stateVariableName);
if (it == propertyMap.end()) {
std::cerr << "Empty String" << std::endl;
return stdEmptyString;
} else {
std::cerr << (*it).second << std::endl;
return (*it).second;
}
}
}
@ -1078,14 +1085,6 @@ bool CUPnPControlPoint::DeletePortMappings(
}
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
for (int i = 0; i < n; ++i) {
@ -1108,21 +1107,8 @@ bool CUPnPControlPoint::DeletePortMappings(
PrivateDeletePortMapping(upnpPortMapping[i]);
}
}
// Debug only
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;
return true;
}

View File

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

View File

@ -12,29 +12,6 @@
#include "upnp.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_UNAVAILABLE 1
#define RS_UPNP_S_READY 2
@ -42,8 +19,6 @@ class upnpforward
#define RS_UPNP_S_UDP_FAILED 4
#define RS_UPNP_S_ACTIVE 5
class uPnPConfigData;
class upnphandler: public pqiNetAssistFirewall
{
public:
@ -95,10 +70,6 @@ class upnphandler: public pqiNetAssistFirewall
/* info from upnp */
struct sockaddr_in upnp_iaddr;
struct sockaddr_in upnp_eaddr;
/* active port forwarding */
std::list<upnpforward> activeForwards;
};
/* info from upnp */