Commit of the new UDP Connection methods and

the rewrite of the retroshare core networking stack.

This check-in commits the changes to the upnp code.
Modifications to use the new generic p3upnpmgr.h interface.




git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@310 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2008-01-25 07:25:05 +00:00
parent 456325dc08
commit 059d43244a
3 changed files with 166 additions and 196 deletions

View File

@ -1,7 +1,4 @@
#include "dht/dhthandler.h"
/* This stuff is actually C */ /* This stuff is actually C */
#ifdef __cplusplus #ifdef __cplusplus
@ -13,12 +10,6 @@ extern "C" {
#endif #endif
/* This stuff is actually C */ /* This stuff is actually C */
/* HACK TO SWITCH THIS OFF during testing */
/*define NO_UPNP_RUNNING 1*/
#include "upnp/upnputil.h" #include "upnp/upnputil.h"
#include "upnp/upnphandler.h" #include "upnp/upnphandler.h"
@ -163,7 +154,7 @@ bool upnphandler::initUPnPState()
/* convert to ipaddress. */ /* convert to ipaddress. */
inet_aton(upcd->lanaddr, &(upnp_iaddr.sin_addr)); inet_aton(upcd->lanaddr, &(upnp_iaddr.sin_addr));
upnp_iaddr.sin_port = iaddr.sin_port; upnp_iaddr.sin_port = htons(iport);
upnpState = RS_UPNP_S_READY; upnpState = RS_UPNP_S_READY;
upnpConfig = upcd; /* */ upnpConfig = upcd; /* */
@ -281,7 +272,7 @@ bool upnphandler::updateUPnP()
if (!eport_curr) if (!eport_curr)
{ {
/* use local port if eport is zero */ /* use local port if eport is zero */
eport_curr = ntohs(iaddr.sin_port); eport_curr = iport;
std::cerr << "Using LocalPort for extPort!"; std::cerr << "Using LocalPort for extPort!";
std::cerr << std::endl; std::cerr << std::endl;
} }
@ -302,14 +293,7 @@ bool upnphandler::updateUPnP()
char eport1[256]; char eport1[256];
char eport2[256]; char eport2[256];
//struct sockaddr_in localAddr = iaddr; upnp_iaddr.sin_port = htons(iport);
if (iaddr.sin_addr.s_addr != upnp_iaddr.sin_addr.s_addr)
{
std::cerr << "Warning ... Address Mismatch!";
std::cerr << std::endl;
}
upnp_iaddr.sin_port = iaddr.sin_port;
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));
@ -353,4 +337,148 @@ bool upnphandler::updateUPnP()
} }
/************************ External Interface *****************************
*
*
*
*/
upnphandler::upnphandler()
:toShutdown(false), toEnable(false),
toStart(false), toStop(false),
eport(0), eport_curr(0),
upnpState(RS_UPNP_S_UNINITIALISED),
upnpConfig(NULL)
{
return;
}
upnphandler::~upnphandler()
{
return;
}
/* RsIface */
void upnphandler::enableUPnP(bool active)
{
dataMtx.lock(); /*** LOCK MUTEX ***/
if (active != toEnable)
{
if (active)
{
toStart = true;
}
else
{
toStop = true;
}
}
toEnable = active;
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
void upnphandler::shutdownUPnP()
{
dataMtx.lock(); /*** LOCK MUTEX ***/
toShutdown = true;
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
bool upnphandler::getUPnPEnabled()
{
dataMtx.lock(); /*** LOCK MUTEX ***/
bool on = toEnable;
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
return on;
}
bool upnphandler::getUPnPActive()
{
dataMtx.lock(); /*** LOCK MUTEX ***/
bool on = (upnpState == RS_UPNP_S_ACTIVE);
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
return on;
}
/* the address that the listening port is on */
void upnphandler::setInternalPort(unsigned short iport_in)
{
// std::cerr << "UPnPHandler::setInternalAddress() pre Lock!" << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::setInternalAddress() postLock!" << std::endl;
if (iport != iport_in)
{
iport = iport_in;
if ((toEnable) &&
(upnpState == RS_UPNP_S_ACTIVE))
{
toStop = true;
toStart = true;
}
}
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
void upnphandler::setExternalPort(unsigned short eport_in)
{
// std::cerr << "UPnPHandler::getExternalPort() pre Lock!" << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getExternalPort() postLock!" << std::endl;
/* flag both shutdown/start -> for restart */
if (eport != eport_in)
{
eport = eport_in;
if ((toEnable) &&
(upnpState == RS_UPNP_S_ACTIVE))
{
toStop = true;
toStart = true;
}
}
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
/* as determined by uPnP */
bool upnphandler::getInternalAddress(struct sockaddr_in &addr)
{
// std::cerr << "UPnPHandler::getInternalAddress() pre Lock!" << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getInternalAddress() postLock!" << std::endl;
addr = upnp_iaddr;
bool valid = (upnpState >= RS_UPNP_S_READY);
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
return valid;
}
bool upnphandler::getExternalAddress(struct sockaddr_in &addr)
{
// std::cerr << "UPnPHandler::getExternalAddress() pre Lock!" << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getExternalAddress() postLock!" << std::endl;
addr = upnp_eaddr;
bool valid = (upnpState >= RS_UPNP_S_READY);
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
return valid;
}

View File

@ -3,13 +3,12 @@
#include <string.h> #include <string.h>
#include "util/rsthreads.h"
#include <string> #include <string>
#include <map> #include <map>
/* platform independent networking... */ /* platform independent networking... */
#include "pqi/pqinetwork.h" #include "pqi/pqinetwork.h"
#include "pqi/pqiaddrstore.h" #include "pqi/p3upnpmgr.h"
class upnpentry class upnpentry
{ {
@ -33,6 +32,7 @@ class upnpforward
int lastTs; 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,153 +42,24 @@ class upnpforward
class uPnPConfigData; class uPnPConfigData;
class upnphandler: public RsThread class upnphandler: public p3UpnpMgr
{ {
public: public:
upnphandler() upnphandler();
:toShutdown(false), toEnable(false), virtual ~upnphandler();
toStart(false), toStop(false),
eport(0), eport_curr(0),
upnpState(RS_UPNP_S_UNINITIALISED),
upnpConfig(NULL)
{ /* External Interface */
return; virtual void enableUPnP(bool active);
} virtual void shutdownUPnP();
~upnphandler() virtual bool getUPnPEnabled();
{ virtual bool getUPnPActive();
return;
}
/* RsIface */ virtual void setInternalPort(unsigned short iport_in);
void enableUPnP(bool active) virtual void setExternalPort(unsigned short eport_in);
{ virtual bool getInternalAddress(struct sockaddr_in &addr);
dataMtx.lock(); /*** LOCK MUTEX ***/ virtual bool getExternalAddress(struct sockaddr_in &addr);
toEnable = active;
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
/* RsIface */
void shutdownUPnP()
{
dataMtx.lock(); /*** LOCK MUTEX ***/
toShutdown = true;
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
void setupUPnPForwarding()
{
dataMtx.lock(); /*** LOCK MUTEX ***/
toStart = true;
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
void shutdownUPnPForwarding()
{
dataMtx.lock(); /*** LOCK MUTEX ***/
toStop = true;
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
/* the address that the listening port is on */
void setInternalAddress(struct sockaddr_in iaddr_in)
{
// std::cerr << "UPnPHandler::setInternalAddress() pre Lock!" << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::setInternalAddress() postLock!" << std::endl;
if ((iaddr.sin_addr.s_addr != iaddr_in.sin_addr.s_addr) ||
(iaddr.sin_port != iaddr_in.sin_port))
{
iaddr = iaddr_in;
if (toEnable)
{
toStop = true;
toStart = true;
}
}
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
void setExternalPort(unsigned short eport_in)
{
// std::cerr << "UPnPHandler::getExternalPort() pre Lock!" << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getExternalPort() postLock!" << std::endl;
/* flag both shutdown/start -> for restart */
if (eport != eport_in)
{
eport = eport_in;
if (toEnable)
{
toStop = true;
toStart = true;
}
}
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
}
/* as determined by uPnP */
bool getInternalAddress(struct sockaddr_in &addr)
{
// std::cerr << "UPnPHandler::getInternalAddress() pre Lock!" << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getInternalAddress() postLock!" << std::endl;
addr = upnp_iaddr;
bool valid = (upnpState >= RS_UPNP_S_READY);
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
return valid;
}
bool getExternalAddress(struct sockaddr_in &addr)
{
// std::cerr << "UPnPHandler::getExternalAddress() pre Lock!" << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getExternalAddress() postLock!" << std::endl;
addr = upnp_eaddr;
bool valid = (upnpState >= RS_UPNP_S_READY);
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
return valid;
}
int getUPnPStatus(upnpentry &ent)
{
// std::cerr << "UPnPHandler::getUPnPStatus() pre Lock!" << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getUPnPStatus() postLock!" << std::endl;
/* TODO - define data structure first */
int state = upnpState;
dataMtx.unlock(); /*** UNLOCK MUTEX ***/
return state;
}
int init();
int shutdown();
int print();
/* must run thread */ /* must run thread */
virtual void run(); virtual void run();
@ -211,7 +82,7 @@ bool updateUPnP();
bool toStart; /* if set start forwarding */ bool toStart; /* if set start forwarding */
bool toStop; /* if set stop forwarding */ bool toStop; /* if set stop forwarding */
struct sockaddr_in iaddr; unsigned short iport;
unsigned short eport; /* config */ unsigned short eport; /* config */
unsigned short eport_curr; /* current forwarded */ unsigned short eport_curr; /* current forwarded */

View File

@ -48,34 +48,7 @@ int main(int argc, char **argv)
upnphandler upnp; upnphandler upnp;
upnp.start(); upnp.start();
upnp.setInternalPort(12122);
#ifdef NOTEVER
if (id == 0)
{
dht.setOwnPort(port1);
dht.setOwnHash(hash1);
dht.addFriend(hash2);
dht.addFriend(hash3);
}
else if (id == 1)
{
dht.setOwnPort(port2);
dht.setOwnHash(hash2);
dht.addFriend(hash1);
dht.addFriend(hash3);
}
else
{
dht.setOwnPort(port3);
dht.setOwnHash(hash3);
dht.addFriend(hash1);
dht.addFriend(hash2);
}
#endif /* NOTEVER */
for(int i = 0; 1; i++) for(int i = 0; 1; i++)
{ {
@ -92,19 +65,17 @@ int main(int argc, char **argv)
if (i % 300 == 10) if (i % 300 == 10)
{ {
/* start up a forward */ /* start up a forward */
upnp.setupUPnPForwarding(); upnp.enableUPnP(true);
} }
if (i % 300 == 20) if (i % 300 == 20)
{ {
/* shutdown a forward */ /* shutdown a forward */
upnp.shutdownUPnPForwarding(); upnp.enableUPnP(false);
}
} }
} }
}