summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2012-07-15 07:42:42 (GMT)
committerRemko Tronçon <git@el-tramo.be>2012-07-15 07:42:42 (GMT)
commit562e37bca8ec283a382964e29ccb0ec0e4ba9d0f (patch)
tree1bbeaf3c935be8baa86fac3ef8f54e4dadc948f7 /Swiften/Network/MiniUPnPInterface.cpp
parent7a591138e89ad71ee1dd9825d7705c94f9bb50d2 (diff)
downloadswift-contrib-562e37bca8ec283a382964e29ccb0ec0e4ba9d0f.zip
swift-contrib-562e37bca8ec283a382964e29ccb0ec0e4ba9d0f.tar.bz2
Hide libminiupnp and libnatpmp.
Diffstat (limited to 'Swiften/Network/MiniUPnPInterface.cpp')
-rw-r--r--Swiften/Network/MiniUPnPInterface.cpp45
1 files changed, 30 insertions, 15 deletions
diff --git a/Swiften/Network/MiniUPnPInterface.cpp b/Swiften/Network/MiniUPnPInterface.cpp
index c9f682f..c729371 100644
--- a/Swiften/Network/MiniUPnPInterface.cpp
+++ b/Swiften/Network/MiniUPnPInterface.cpp
@@ -6,42 +6,53 @@
#include <Swiften/Network/MiniUPnPInterface.h>
+#include <miniupnpc.h>
#include <upnpcommands.h>
#include <upnperrors.h>
+#include <boost/smart_ptr/make_shared.hpp>
#include <boost/lexical_cast.hpp>
#include <Swiften/Base/Log.h>
namespace Swift {
-MiniUPnPInterface::MiniUPnPInterface() : isValid(false) {
+struct MiniUPnPInterface::Private {
+ bool isValid;
+ std::string localAddress;
+ UPNPDev* deviceList;
+ UPNPUrls urls;
+ IGDdatas data;
+};
+
+MiniUPnPInterface::MiniUPnPInterface() : p(boost::make_shared<Private>()) {
+ p->isValid = false;
int error = 0;
- deviceList = upnpDiscover(1500 /* timeout in ms */, 0, 0, 0, 0 /* do IPv6? */, &error);
- if (!deviceList) {
+ p->deviceList = upnpDiscover(1500 /* timeout in ms */, 0, 0, 0, 0 /* do IPv6? */, &error);
+ if (!p->deviceList) {
return;
}
char lanAddress[64];
- if (!UPNP_GetValidIGD(deviceList, &urls, &data, lanAddress, sizeof(lanAddress))) {
+ if (!UPNP_GetValidIGD(p->deviceList, &p->urls, &p->data, lanAddress, sizeof(lanAddress))) {
return;
}
- localAddress = std::string(lanAddress);
- isValid = true;
+ p->localAddress = std::string(lanAddress);
+ p->isValid = true;
}
MiniUPnPInterface::~MiniUPnPInterface() {
- if (isValid) {
- FreeUPNPUrls(&urls);
+ if (p->isValid) {
+ FreeUPNPUrls(&p->urls);
}
- freeUPNPDevlist(deviceList);
+ freeUPNPDevlist(p->deviceList);
}
boost::optional<HostAddress> MiniUPnPInterface::getPublicIP() {
- if (!isValid) {
+ if (!p->isValid) {
return boost::optional<HostAddress>();
}
char externalIPAddress[40];
- int ret = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
+ int ret = UPNP_GetExternalIPAddress(p->urls.controlURL, p->data.first.servicetype, externalIPAddress);
if (ret != UPNPCOMMAND_SUCCESS) {
return boost::optional<HostAddress>();
}
@@ -51,7 +62,7 @@ boost::optional<HostAddress> MiniUPnPInterface::getPublicIP() {
}
boost::optional<NATPortMapping> MiniUPnPInterface::addPortForward(int actualLocalPort, int actualPublicPort) {
- if (!isValid) {
+ if (!p->isValid) {
return boost::optional<NATPortMapping>();
}
@@ -61,7 +72,7 @@ boost::optional<NATPortMapping> MiniUPnPInterface::addPortForward(int actualLoca
std::string localPort = boost::lexical_cast<std::string>(mapping.getLocalPort());
std::string leaseSeconds = boost::lexical_cast<std::string>(mapping.getLeaseInSeconds());
- int ret = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, publicPort.c_str(), localPort.c_str(), localAddress.c_str(), 0, mapping.getPublicPort() == NATPortMapping::TCP ? "TCP" : "UDP", 0, leaseSeconds.c_str());
+ int ret = UPNP_AddPortMapping(p->urls.controlURL, p->data.first.servicetype, publicPort.c_str(), localPort.c_str(), p->localAddress.c_str(), 0, mapping.getPublicPort() == NATPortMapping::TCP ? "TCP" : "UDP", 0, leaseSeconds.c_str());
if (ret == UPNPCOMMAND_SUCCESS) {
return mapping;
}
@@ -71,7 +82,7 @@ boost::optional<NATPortMapping> MiniUPnPInterface::addPortForward(int actualLoca
}
bool MiniUPnPInterface::removePortForward(const NATPortMapping& mapping) {
- if (!isValid) {
+ if (!p->isValid) {
return false;
}
@@ -79,8 +90,12 @@ bool MiniUPnPInterface::removePortForward(const NATPortMapping& mapping) {
std::string localPort = boost::lexical_cast<std::string>(mapping.getLocalPort());
std::string leaseSeconds = boost::lexical_cast<std::string>(mapping.getLeaseInSeconds());
- int ret = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, publicPort.c_str(), mapping.getProtocol() == NATPortMapping::TCP ? "TCP" : "UDP", 0);
+ int ret = UPNP_DeletePortMapping(p->urls.controlURL, p->data.first.servicetype, publicPort.c_str(), mapping.getProtocol() == NATPortMapping::TCP ? "TCP" : "UDP", 0);
return ret == UPNPCOMMAND_SUCCESS;
}
+bool MiniUPnPInterface::isAvailable() {
+ return p->isValid;
+}
+
}