Tweaks to get the UPnP working. Corrected network address passing,

and also adjusted DHT timeout periods.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@345 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2008-02-11 16:27:55 +00:00
parent ade0665664
commit 98a14ba715
5 changed files with 68 additions and 19 deletions

View File

@ -43,7 +43,7 @@ const uint32_t RS_STUN_DHT = 0x0001;
const uint32_t RS_STUN_DONE = 0x0002; 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 MAX_UPNP_INIT = 10; /* seconds UPnP timeout */ const uint32_t MAX_UPNP_INIT = 60; /* seconds UPnP timeout */
#define CONN_DEBUG 1 #define CONN_DEBUG 1
@ -308,6 +308,12 @@ void p3ConnectMgr::netUpnpInit()
/* get the ports from the configuration */ /* get the ports from the configuration */
mNetStatus = RS_NET_UPNP_SETUP; 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 */ connMtx.unlock(); /* UNLOCK MUTEX */
@ -337,7 +343,7 @@ void p3ConnectMgr::netUpnpCheck()
mUpnpAddrValid = false; mUpnpAddrValid = false;
mNetStatus = RS_NET_UDP_SETUP; 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 */ connMtx.unlock(); /* UNLOCK MUTEX */
} }
@ -360,7 +366,7 @@ void p3ConnectMgr::netUdpCheck()
#ifdef CONN_DEBUG #ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::netUdpCheck()" << std::endl; std::cerr << "p3ConnectMgr::netUdpCheck()" << std::endl;
#endif #endif
if (stunCheck()) if (stunCheck() || (mUpnpAddrValid))
{ {
bool extValid = false; bool extValid = false;
struct sockaddr_in extAddr; struct sockaddr_in extAddr;
@ -390,8 +396,7 @@ void p3ConnectMgr::netUdpCheck()
{ {
ownState.serveraddr = extAddr; ownState.serveraddr = extAddr;
mode = RS_NET_CONN_TCP_LOCAL | RS_NET_CONN_UDP_DHT_SYNC; mode = RS_NET_CONN_TCP_LOCAL | RS_NET_CONN_UDP_DHT_SYNC;
if ((ownState.netMode == RS_NET_MODE_UPNP) || if (mUpnpAddrValid || (ownState.netMode == RS_NET_MODE_EXT))
(ownState.netMode == RS_NET_MODE_EXT))
{ {
mode |= RS_NET_CONN_TCP_EXTERNAL; mode |= RS_NET_CONN_TCP_EXTERNAL;
} }

View File

@ -51,10 +51,10 @@
#define DHT_STATE_ACTIVE 4 #define DHT_STATE_ACTIVE 4
/* TIMEOUTS */ /* TIMEOUTS */
#define DHT_SEARCH_PERIOD 600 /* 10 min */ #define DHT_SEARCH_PERIOD 1800 /* PeerKeys: if we haven't found them: 30 min */
#define DHT_CHECK_PERIOD 60 //1800 /* 30 min */ #define DHT_CHECK_PERIOD 3600 /* PeerKeys: re-lookup peer: 60 min */
#define DHT_PUBLISH_PERIOD 1800 /* 30 min */ #define DHT_PUBLISH_PERIOD 1800 /* OwnKey 30 min */
#define DHT_NOTIFY_PERIOD 20 //120 /* 2 min - Notify Check period */ #define DHT_NOTIFY_PERIOD 600 /* 10 min - Notify Check period */
#define DHT_RESTART_PERIOD 300 /* 5 min */ #define DHT_RESTART_PERIOD 300 /* 5 min */
#define DHT_DEFAULT_PERIOD 600 /* Default period if no work to do */ #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 */ #define DHT_DEFAULT_WAITTIME 1 /* Std sleep break period */
/* TTLs for DHTs posts */ /* 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 #define DHT_TTL_NOTIFY DHT_NOTIFY_PERIOD

View File

@ -631,7 +631,13 @@ int RsServer::StartupRetroShare(RsInit *config)
/* Force Any Last Configuration Options */ /* Force Any Last Configuration Options */
/**************************************************************************/ /**************************************************************************/
/**************************************************************************/
/* Start up Threads */
/**************************************************************************/
server->StartupMonitor(); server->StartupMonitor();
mUpnpMgr->start();
#ifdef PQI_USE_CHANNELS #ifdef PQI_USE_CHANNELS
server->setP3Channel(pqih->getP3Channel()); server->setP3Channel(pqih->getP3Channel());

View File

@ -24,6 +24,7 @@
*/ */
#include "udpsorter.h" #include "udpsorter.h"
#include "util/rsnet.h"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -38,8 +39,10 @@ static const int STUN_TTL = 64;
#define DEBUG_UDP_SORTER 1 #define DEBUG_UDP_SORTER 1
UdpSorter::UdpSorter(struct sockaddr_in &local) UdpSorter::UdpSorter(struct sockaddr_in &local)
: udpLayer(NULL), laddr(local) :udpLayer(NULL), laddr(local), eaddrKnown(false)
{ {
sockaddr_clear(&eaddr);
openSocket(); openSocket();
return; return;
} }

View File

@ -25,6 +25,8 @@ class uPnPConfigData
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "util/rsnet.h"
void upnphandler::run() void upnphandler::run()
{ {
@ -73,8 +75,8 @@ void upnphandler::run()
case RS_UPNP_S_TCP_FAILED: case RS_UPNP_S_TCP_FAILED:
case RS_UPNP_S_UDP_FAILED: case RS_UPNP_S_UDP_FAILED:
case RS_UPNP_S_ACTIVE: case RS_UPNP_S_ACTIVE:
/* working ... normal 15 seconds */ /* working ... normal 10 seconds */
allowedSleep = 15; allowedSleep = 10;
break; break;
default: default:
@ -243,7 +245,7 @@ bool upnphandler::updateUPnP()
char eport2[256]; char eport2[256];
snprintf(eport1, 256, "%d", eport_curr); 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 << "Attempting To Remove Redirection: port: " << eport1;
std::cerr << " Prot: " << eprot1; std::cerr << " Prot: " << eprot1;
@ -297,7 +299,7 @@ bool upnphandler::updateUPnP()
struct sockaddr_in localAddr = upnp_iaddr; struct sockaddr_in localAddr = upnp_iaddr;
snprintf(in_port1, 256, "%d", ntohs(localAddr.sin_port)); 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", snprintf(in_addr, 256, "%d.%d.%d.%d",
((localAddr.sin_addr.s_addr >> 0) & 0xff), ((localAddr.sin_addr.s_addr >> 0) & 0xff),
((localAddr.sin_addr.s_addr >> 8) & 0xff), ((localAddr.sin_addr.s_addr >> 8) & 0xff),
@ -305,7 +307,7 @@ bool upnphandler::updateUPnP()
((localAddr.sin_addr.s_addr >> 24) & 0xff)); ((localAddr.sin_addr.s_addr >> 24) & 0xff));
snprintf(eport1, 256, "%d", eport_curr); 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 << "Attempting Redirection: InAddr: " << in_addr;
std::cerr << " InPort: " << in_port1; std::cerr << " InPort: " << in_port1;
@ -327,11 +329,35 @@ bool upnphandler::updateUPnP()
{ {
upnpState = RS_UPNP_S_ACTIVE; 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 */ dataMtx.unlock(); /* UNLOCK MUTEX */
return true; return true;
} }
@ -418,6 +444,9 @@ void upnphandler::setInternalPort(unsigned short iport_in)
dataMtx.lock(); /*** LOCK MUTEX ***/ dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::setInternalAddress() postLock!" << std::endl; // std::cerr << "UPnPHandler::setInternalAddress() postLock!" << std::endl;
std::cerr << "UPnPHandler::setInternalPort(" << iport_in << ") current port: ";
std::cerr << iport << std::endl;
if (iport != iport_in) if (iport != iport_in)
{ {
iport = iport_in; iport = iport_in;
@ -437,6 +466,9 @@ void upnphandler::setExternalPort(unsigned short eport_in)
dataMtx.lock(); /*** LOCK MUTEX ***/ dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getExternalPort() postLock!" << std::endl; // 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 */ /* flag both shutdown/start -> for restart */
if (eport != eport_in) if (eport != eport_in)
{ {
@ -459,8 +491,10 @@ bool upnphandler::getInternalAddress(struct sockaddr_in &addr)
dataMtx.lock(); /*** LOCK MUTEX ***/ dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getInternalAddress() postLock!" << std::endl; // std::cerr << "UPnPHandler::getInternalAddress() postLock!" << std::endl;
std::cerr << "UPnPHandler::getInternalAddress()" << std::endl;
addr = upnp_iaddr; addr = upnp_iaddr;
bool valid = (upnpState >= RS_UPNP_S_READY); bool valid = (upnpState >= RS_UPNP_S_ACTIVE);
dataMtx.unlock(); /*** UNLOCK MUTEX ***/ dataMtx.unlock(); /*** UNLOCK MUTEX ***/
@ -473,8 +507,9 @@ bool upnphandler::getExternalAddress(struct sockaddr_in &addr)
dataMtx.lock(); /*** LOCK MUTEX ***/ dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getExternalAddress() postLock!" << std::endl; // std::cerr << "UPnPHandler::getExternalAddress() postLock!" << std::endl;
std::cerr << "UPnPHandler::getExternalAddress()" << std::endl;
addr = upnp_eaddr; addr = upnp_eaddr;
bool valid = (upnpState >= RS_UPNP_S_READY); bool valid = (upnpState == RS_UPNP_S_ACTIVE);
dataMtx.unlock(); /*** UNLOCK MUTEX ***/ dataMtx.unlock(); /*** UNLOCK MUTEX ***/