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 */
#ifdef __cplusplus
@ -13,12 +10,6 @@ extern "C" {
#endif
/* This stuff is actually C */
/* HACK TO SWITCH THIS OFF during testing */
/*define NO_UPNP_RUNNING 1*/
#include "upnp/upnputil.h"
#include "upnp/upnphandler.h"
@ -163,7 +154,7 @@ bool upnphandler::initUPnPState()
/* convert to ipaddress. */
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;
upnpConfig = upcd; /* */
@ -281,7 +272,7 @@ bool upnphandler::updateUPnP()
if (!eport_curr)
{
/* use local port if eport is zero */
eport_curr = ntohs(iaddr.sin_port);
eport_curr = iport;
std::cerr << "Using LocalPort for extPort!";
std::cerr << std::endl;
}
@ -302,14 +293,7 @@ bool upnphandler::updateUPnP()
char eport1[256];
char eport2[256];
//struct sockaddr_in localAddr = iaddr;
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;
upnp_iaddr.sin_port = htons(iport);
struct sockaddr_in localAddr = upnp_iaddr;
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 "util/rsthreads.h"
#include <string>
#include <map>
/* platform independent networking... */
#include "pqi/pqinetwork.h"
#include "pqi/pqiaddrstore.h"
#include "pqi/p3upnpmgr.h"
class upnpentry
{
@ -33,6 +32,7 @@ class upnpforward
int lastTs;
};
#define RS_UPNP_S_UNINITIALISED 0
#define RS_UPNP_S_UNAVAILABLE 1
#define RS_UPNP_S_READY 2
@ -42,153 +42,24 @@ class upnpforward
class uPnPConfigData;
class upnphandler: public RsThread
class upnphandler: public p3UpnpMgr
{
public:
upnphandler()
:toShutdown(false), toEnable(false),
toStart(false), toStop(false),
eport(0), eport_curr(0),
upnpState(RS_UPNP_S_UNINITIALISED),
upnpConfig(NULL)
upnphandler();
virtual ~upnphandler();
{
return;
}
/* External Interface */
virtual void enableUPnP(bool active);
virtual void shutdownUPnP();
~upnphandler()
{
return;
}
virtual bool getUPnPEnabled();
virtual bool getUPnPActive();
/* RsIface */
void enableUPnP(bool active)
{
dataMtx.lock(); /*** LOCK MUTEX ***/
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();
virtual void setInternalPort(unsigned short iport_in);
virtual void setExternalPort(unsigned short eport_in);
virtual bool getInternalAddress(struct sockaddr_in &addr);
virtual bool getExternalAddress(struct sockaddr_in &addr);
/* must run thread */
virtual void run();
@ -211,7 +82,7 @@ bool updateUPnP();
bool toStart; /* if set start forwarding */
bool toStop; /* if set stop forwarding */
struct sockaddr_in iaddr;
unsigned short iport;
unsigned short eport; /* config */
unsigned short eport_curr; /* current forwarded */

View File

@ -48,34 +48,7 @@ int main(int argc, char **argv)
upnphandler upnp;
upnp.start();
#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 */
upnp.setInternalPort(12122);
for(int i = 0; 1; i++)
{
@ -92,19 +65,17 @@ int main(int argc, char **argv)
if (i % 300 == 10)
{
/* start up a forward */
upnp.setupUPnPForwarding();
upnp.enableUPnP(true);
}
if (i % 300 == 20)
{
/* shutdown a forward */
upnp.shutdownUPnPForwarding();
}
upnp.enableUPnP(false);
}
}
}