Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

detect lan ip too

  • Loading branch information...
commit bee1ead422c3336d36e08b70f7eba1a39f44b56b 1 parent a8f80b0
@RJ authored
View
1  CMakeLists.txt
@@ -9,6 +9,7 @@ SET(NATPMP_DIR "third-party/libnatpmp")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
ADD_DEFINITIONS(-Wall -O2 -DNDEBUG)
+ADD_DEFINITIONS(-fPIC)
INCLUDE_DIRECTORIES(${MINIUPNP_DIR} include)
ADD_LIBRARY(portfwd STATIC
View
18 include/portfwd/portfwd.h
@@ -12,13 +12,25 @@ class Portfwd
public:
Portfwd();
~Portfwd();
- bool init();
- bool add(const std::string& ip, unsigned short port);
+ /// timeout: milliseconds to wait for a router to respond
+ /// 2000 is typically enough.
+ bool init(unsigned int timeout);
+ void get_status();
+ bool add(unsigned short port);
bool remove(unsigned short port);
- std::string external_ip();
+
+ const std::string& external_ip() const
+ { return m_externalip; }
+ const std::string& lan_ip() const
+ { return m_lanip; }
+ unsigned int max_upstream_bps() const { return m_upbps; }
+ unsigned int max_downstream_bps() const { return m_downbps; }
protected:
struct UPNPUrls* urls;
struct IGDdatas* data;
+
+ std::string m_lanip, m_externalip;
+ unsigned int m_upbps, m_downbps;
};
View
16 src/main.cpp
@@ -1,22 +1,24 @@
#include "portfwd/portfwd.h"
int main(int argc, char** argv)
{
- if(argc!=3)
+ if(argc!=2)
{
- printf("Usage: %s <lan ip> <port>\n", argv[0]);
+ printf("Usage: %s <port>\n", argv[0]);
return 1;
}
- int port = atoi(argv[2]);
- std::string lanip = std::string(argv[1]);
- printf("Using %s:%d\n", lanip.c_str(), port);
+ int port = atoi(argv[1]);
Portfwd pf;
- if(!pf.init())
+ if(!pf.init(2000))
{
printf("Portfwd.init() failed.\n");
return 2;
}
printf("External IP: %s\n", pf.external_ip().c_str());
- printf("%s\n", ((pf.add( lanip, port ))?"Added":"Failed to add") );
+ printf("LAN IP: %s\n", pf.lan_ip().c_str());
+ printf("Max upstream: %d bps, max downstream: %d bps\n",
+ pf.max_upstream_bps(), pf.max_downstream_bps() );
+
+ printf("%s\n", ((pf.add( port ))?"Added":"Failed to add") );
printf("Any key to exit...\n");
char foo;
View
55 src/portfwd.cpp
@@ -16,18 +16,18 @@ Portfwd::~Portfwd()
}
bool
-Portfwd::init()
+Portfwd::init(unsigned int timeout)
{
struct UPNPDev * devlist;
struct UPNPDev * dev;
char * descXML;
int descXMLsize = 0;
- printf("TB : init_upnp()\n");
+ printf("Portfwd::init()\n");
urls = (UPNPUrls*)malloc(sizeof(struct UPNPUrls));
data = (IGDdatas*)malloc(sizeof(struct IGDdatas));
memset(urls, 0, sizeof(struct UPNPUrls));
memset(data, 0, sizeof(struct IGDdatas));
- devlist = upnpDiscover(2000, NULL, NULL, 0);
+ devlist = upnpDiscover(timeout, NULL, NULL, 0);
if (devlist)
{
dev = devlist;
@@ -51,29 +51,55 @@ Portfwd::init()
free (descXML); descXML = 0;
GetUPNPUrls (urls, data, dev->descURL);
}
+ // get lan IP:
+ char lanaddr[16];
+ int i;
+ i = UPNP_GetValidIGD(devlist, urls, data, (char*)&lanaddr, 16);
+ m_lanip = std::string(lanaddr);
+
freeUPNPDevlist(devlist);
+ get_status();
return true;
}
return false;
}
+void
+Portfwd::get_status()
+{
+ // get connection speed
+ UPNP_GetLinkLayerMaxBitRates(
+ urls->controlURL_CIF, data->servicetype_CIF, &m_downbps, &m_upbps);
+
+ // get external IP adress
+ char ip[16];
+ if( 0 != UPNP_GetExternalIPAddress( urls->controlURL,
+ data->servicetype,
+ (char*)&ip ) )
+ {
+ m_externalip = ""; //failed
+ }else{
+ m_externalip = std::string(ip);
+ }
+}
+
bool
-Portfwd::add( const std::string& ip, unsigned short port )
+Portfwd::add( unsigned short port )
{
char port_str[16];
int r;
- printf("TB : upnp_add_redir (%s, %d)\n", ip.c_str(), port);
+ printf("Portfwd::add (%s, %d)\n", m_lanip.c_str(), port);
if(urls->controlURL[0] == '\0')
{
- printf("TB : the init was not done !\n");
+ printf("Portfwd - the init was not done !\n");
return false;
}
sprintf(port_str, "%d", port);
r = UPNP_AddPortMapping(urls->controlURL, data->servicetype,
- port_str, port_str, ip.c_str(), 0, "TCP", NULL);
+ port_str, port_str, m_lanip.c_str(), 0, "TCP", NULL);
if(r!=0)
{
- printf("AddPortMapping(%s, %s, %s) failed, code %d\n", port_str, port_str, ip.c_str(), r);
+ printf("AddPortMapping(%s, %s, %s) failed, code %d\n", port_str, port_str, m_lanip.c_str(), r);
return false;
}
return true;
@@ -83,10 +109,10 @@ bool
Portfwd::remove( unsigned short port )
{
char port_str[16];
- printf("TB : upnp_rem_redir (%d)\n", port);
+ printf("Portfwd::remove(%d)\n", port);
if(urls->controlURL[0] == '\0')
{
- printf("TB : the init was not done !\n");
+ printf("Portfwd - the init was not done !\n");
return false;
}
sprintf(port_str, "%d", port);
@@ -94,12 +120,3 @@ Portfwd::remove( unsigned short port )
return r == 0;
}
-std::string
-Portfwd::external_ip()
-{
- char ip[16];
- int r = UPNP_GetExternalIPAddress( urls->controlURL, data->servicetype, (char*)&ip );
- if( r != 0 ) return "";
- return std::string(ip);
-}
-
Please sign in to comment.
Something went wrong with that request. Please try again.