Commit 1afa10bc authored by Guillaume Roguez's avatar Guillaume Roguez

Revert "NAT-PMP: add initial support"

This reverts commit 150035a8.

reasons of the revert:
- doesn't link on Mac
- doesn't build on IOS
- build and link on linux, but the shared library is not found in rpath

Change-Id: Ie2eb1fe587adea1b607fde727abe6c641762495b
Notes: libnatpmp is not used on Android
parent 150035a8
......@@ -531,20 +531,6 @@ AS_IF([test "x$with_upnp" = "xyes"],
AC_DEFINE([HAVE_LIBUPNP], 0, [Define if you have libupnp])])
])
# LIBNATPMP
dnl check for libnatpmp
AC_ARG_WITH([natpmp], [AS_HELP_STRING([--without-natpmp],
[disable support for NAT-PMP])], [], [with_natpmp=yes])
AS_IF([test "x$with_natpmp" != xno],
[AC_CHECK_HEADER([natpmp.h],
AC_CHECK_LIB([natpmp], [initnatpmp], [], [with_natpmp=no]),
[AC_MSG_WARN([Unable to find the libnatpmp headers (you may need to install the dev package). You may use --without-natpmp to compile without NAT-PMP support.]);
with_natpmp=no])
],[])
AC_DEFINE_UNQUOTED([HAVE_LIBNATPMP], `if test "x$with_natpmp" != xno; then echo 1; else echo 0; fi`, [Define if you have libnatpmp])
AC_DEFINE_UNQUOTED([HAVE_SHM], `if test -z "${HAVE_LINUX_TRUE}" && test -z "${HAVE_ANDROID_FALSE}" ; then echo 1; else echo 0; fi`,
[Define if you have shared memory support])
......
e50b1f68ce9254bb2f068ddc37417a3c417b80f7b3fb3d84e3e9af4a144d89e204ab993b54c01657335e855d0124a8fcbbf96ce78db7b9ae0b03b6eb79de2e09 libnatpmp-20150609.tar.gz
# libnatpmp
NATPMP_VERSION := 20150609
NATPMP_URL := http://miniupnp.free.fr/files/download.php?file=libnatpmp-$(NATPMP_VERSION).tar.gz
PKGS += natpmp
ifeq ($(call need_pkg,'libnatpmp'),)
PKGS_FOUND += natpmp
endif
$(TARBALLS)/libnatpmp-$(NATPMP_VERSION).tar.gz:
$(call download,$(NATPMP_URL))
.sum-natpmp: libnatpmp-$(NATPMP_VERSION).tar.gz
natpmp: libnatpmp-$(NATPMP_VERSION).tar.gz .sum-natpmp
$(UNPACK)
$(MOVE)
.natpmp: natpmp
cd $< && $(MAKE) INSTALLPREFIX="$(PREFIX)" install
touch $@
This diff is collapsed.
......@@ -18,7 +18,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#ifndef UPNP_CONTEXT_H_
#define UPNP_CONTEXT_H_
#ifdef HAVE_CONFIG_H
#include "config.h"
......@@ -31,7 +32,6 @@
#include <condition_variable>
#include <chrono>
#include <atomic>
#include <thread>
#if HAVE_LIBUPNP
#ifdef _WIN32
......@@ -43,10 +43,6 @@
#include <upnp/upnptools.h>
#endif
#if HAVE_LIBNATPMP
#include <natpmp.h>
#endif
#include "noncopyable.h"
#include "upnp_igd.h"
......@@ -60,6 +56,7 @@ class UPnPContext {
public:
constexpr static unsigned SEARCH_TIMEOUT {30};
#if HAVE_LIBUPNP
UPnPContext();
~UPnPContext();
......@@ -107,63 +104,21 @@ public:
*/
IpAddr getLocalIP() const;
private:
NON_COPYABLE(UPnPContext);
std::atomic_bool clientRegistered_ {false};
/**
* map of valid IGDs - IGDs which have the correct services and are connected
* to some external network (have an external IP)
*
* the UDN string is used to uniquely identify the IGD
*
* the mutex is used to access these lists and IGDs in a thread-safe manner
*/
std::map<std::string, std::unique_ptr<IGD>> validIGDs_;
mutable std::mutex validIGDMutex_;
std::condition_variable validIGDCondVar_;
/**
* Map of valid IGD listeners.
*/
std::map<size_t, IGDFoundCallback> igdListeners_;
/**
* Last provided token for valid IGD listeners.
* 0 is the invalid token.
*/
size_t listenerToken_ {0};
/**
* chooses the IGD to use (currently selects the first one in the map)
* assumes you already have a lock on igd_mutex_
* callback function for the UPnP client (control point)
* all UPnP events received by the client are processed here
*/
IGD* chooseIGD_unlocked() const;
/* tries to add mapping, assumes you alreayd have lock on igd_mutex_ */
Mapping addMapping(IGD* igd,
uint16_t port_external,
uint16_t port_internal,
PortType type,
int *upnp_error);
uint16_t chooseRandomPort(const IGD& igd, PortType type);
#if HAVE_LIBNATPMP
std::thread pmpThread_;
std::mutex pmpMutex_;
std::condition_variable pmpCv_;
std::atomic_bool pmpRun_ {true};
void PMPsearchForIGD(PMPIGD* pmp_igd, natpmp_t& natpmp, bool& found);
void PMPaddPortMapping(const PMPIGD& pmp_igd, natpmp_t& natpmp, GlobalMapping& mapping, bool remove=false) const;
void PMPdeleteAllPortMapping(const PMPIGD& pmp_igd, natpmp_t& natpmp, int proto) const;
int handleUPnPEvents(Upnp_EventType event_type, void* event);
#else
/* use default constructor and destructor */
UPnPContext() = default;
~UPnPContext() = default;
#endif
private:
NON_COPYABLE(UPnPContext);
#if HAVE_LIBUPNP
/**
......@@ -195,18 +150,40 @@ private:
UpnpDevice_Handle deviceHandle_ {-1};
/**
* keep track if we've successfully registered a device
* keep track if we've successfully registered
* a client and/ore device
*/
std::atomic_bool clientRegistered_ {false};
bool deviceRegistered_ {false};
static int cp_callback(Upnp_EventType event_type, void* event, void* user_data);
/**
* map of valid IGDs - IGDs which have the correct services and are connected
* to some external network (have an external IP)
*
* the UDN string is used to uniquely identify the IGD
*
* the mutex is used to access these lists and IGDs in a thread-safe manner
*/
std::map<std::string, std::unique_ptr<IGD>> validIGDs_;
mutable std::mutex validIGDMutex_;
std::condition_variable validIGDCondVar_;
/**
* callback function for the UPnP client (control point)
* all UPnP events received by the client are processed here
* Map of valid IGD listeners.
*/
int handleUPnPEvents(Upnp_EventType event_type, void* event);
std::map<size_t, IGDFoundCallback> igdListeners_;
/**
* Last provided token for valid IGD listeners.
* 0 is the invalid token.
*/
size_t listenerToken_ {0};
/**
* chooses the IGD to use (currently selects the first one in the map)
* assumes you already have a lock on igd_mutex_
*/
IGD* chooseIGD_unlocked() const;
/* sends out async search for IGD */
void searchForIGD();
......@@ -219,24 +196,33 @@ private:
void parseIGD(IXML_Document* doc, const Upnp_Discovery* d_event);
/* tries to add mapping, assumes you alreayd have lock on igd_mutex_ */
Mapping addMapping(IGD* igd,
uint16_t port_external,
uint16_t port_internal,
PortType type,
int *upnp_error);
uint16_t chooseRandomPort(const IGD* igd, PortType type);
/* these functions directly create UPnP actions
* and make synchronous UPnP control point calls
* they assume you have a lock on the igd_mutex_ */
bool isIGDConnected(const UPnPIGD& igd);
bool isIGDConnected(const IGD* igd);
IpAddr getExternalIP(const UPnPIGD& igd);
IpAddr getExternalIP(const IGD* igd);
void removeMappingsByLocalIPAndDescription(const UPnPIGD& igd,
void removeMappingsByLocalIPAndDescription(const IGD* igd,
const std::string& description);
bool deletePortMapping(const UPnPIGD& igd,
bool deletePortMapping(const IGD* igd,
const std::string& port_external,
const std::string& protocol);
bool addPortMapping(const UPnPIGD& igd,
bool addPortMapping(const IGD* igd,
const Mapping& mapping,
int* error_code);
#endif /* HAVE_LIBUPNP */
};
......@@ -250,3 +236,5 @@ private:
std::shared_ptr<UPnPContext> getUPnPContext();
}} // namespace ring::upnp
#endif /* UPNP_CONTEXT_H_ */
......@@ -46,14 +46,19 @@ Controller::~Controller()
{
/* remove all mappings */
removeMappings();
#if HAVE_LIBUPNP
if (listToken_ and upnpContext_)
upnpContext_->removeIGDListener(listToken_);
#endif
}
bool
Controller::hasValidIGD(std::chrono::seconds timeout)
{
#if HAVE_LIBUPNP
return upnpContext_ and upnpContext_->hasValidIGD(timeout);
#endif
return false;
}
void
......@@ -61,9 +66,11 @@ Controller::setIGDListener(IGDFoundCallback&& cb)
{
if (not upnpContext_)
return;
#if HAVE_LIBUPNP
if (listToken_)
upnpContext_->removeIGDListener(listToken_);
listToken_ = cb ? upnpContext_->addIGDListener(std::move(cb)) : 0;
#endif
}
bool
......@@ -74,6 +81,7 @@ Controller::addAnyMapping(uint16_t port_desired,
bool unique,
uint16_t *port_used)
{
#if HAVE_LIBUPNP
if (not upnpContext_)
return false;
......@@ -89,6 +97,7 @@ Controller::addAnyMapping(uint16_t port_desired,
instanceMappings.emplace(usedPort, std::move(mapping));
return true;
}
#endif
return false;
}
......@@ -104,6 +113,7 @@ Controller::addAnyMapping(uint16_t port_desired,
void
Controller::removeMappings(PortType type) {
#if HAVE_LIBUPNP
if (not upnpContext_)
return;
......@@ -113,28 +123,34 @@ Controller::removeMappings(PortType type) {
upnpContext_->removeMapping(mapping);
iter = instanceMappings.erase(iter);
}
#endif
}
void
Controller::removeMappings()
{
#if HAVE_LIBUPNP
removeMappings(PortType::UDP);
removeMappings(PortType::TCP);
#endif
}
IpAddr
Controller::getLocalIP() const
{
#if HAVE_LIBUPNP
if (upnpContext_)
return upnpContext_->getLocalIP();
#endif
return {}; // empty address
}
IpAddr
Controller::getExternalIP() const
{
#if HAVE_LIBUPNP
if (upnpContext_)
return upnpContext_->getExternalIP();
#endif
return {}; // empty address
}
......
......@@ -18,16 +18,12 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef UPNP_IGD_H_
#define UPNP_IGD_H_
#include <string>
#include <map>
#include <functional>
#include <chrono>
#include "noncopyable.h"
#include "ip_utils.h"
......@@ -67,13 +63,13 @@ public:
friend bool operator== (const Mapping& cRedir1, const Mapping& cRedir2);
friend bool operator!= (const Mapping& cRedir1, const Mapping& cRedir2);
uint16_t getPortExternal() const { return port_external_; }
std::string getPortExternalStr() const { return ring::to_string(port_external_); }
uint16_t getPortInternal() const { return port_internal_; }
std::string getPortInternalStr() const { return ring::to_string(port_internal_); }
PortType getType() const { return type_; }
uint16_t getPortExternal() const { return port_external_; };
std::string getPortExternalStr() const { return ring::to_string(port_external_); };
uint16_t getPortInternal() const { return port_internal_; };
std::string getPortInternalStr() const { return ring::to_string(port_internal_); };
PortType getType() const { return type_; };
std::string getTypeStr() const { return type_ == PortType::UDP ? "UDP" : "TCP"; }
std::string getDescription() const { return description_; }
std::string getDescription() const { return description_; };
std::string toString() const {
return getPortExternalStr() + ":" + getPortInternalStr() + ", " + getTypeStr();
......@@ -87,11 +83,6 @@ public:
return isValid();
}
#if HAVE_LIBNATPMP
std::chrono::system_clock::time_point renewal_ {std::chrono::system_clock::time_point::min()};
bool remove {false};
#endif
private:
NON_COPYABLE(Mapping);
......@@ -132,6 +123,7 @@ using IGDFoundCallback = std::function<void()>;
/* defines a UPnP capable Internet Gateway Device (a router) */
class IGD {
public:
/* device address seen by IGD */
IpAddr localIp;
......@@ -144,22 +136,7 @@ public:
/* constructors */
IGD() {}
/* move constructor and operator */
IGD(IGD&&) = default;
IGD& operator=(IGD&&) = default;
virtual ~IGD() = default;
private:
NON_COPYABLE(IGD);
};
#if HAVE_LIBUPNP
class UPnPIGD : public IGD {
public:
UPnPIGD(std::string UDN,
IGD(std::string UDN,
std::string baseURL,
std::string friendlyName,
std::string serviceType,
......@@ -175,6 +152,12 @@ public:
, eventSubURL_(eventSubURL)
{}
/* move constructor and operator */
IGD(IGD&&) = default;
IGD& operator=(IGD&&) = default;
~IGD() = default;
const std::string& getUDN() const { return UDN_; };
const std::string& getBaseURL() const { return baseURL_; };
const std::string& getFriendlyName() const { return friendlyName_; };
......@@ -184,6 +167,8 @@ public:
const std::string& getEventSubURL() const { return eventSubURL_; };
private:
NON_COPYABLE(IGD);
/* root device info */
std::string UDN_ {}; /* used to uniquely identify this UPnP device */
std::string baseURL_ {};
......@@ -194,47 +179,9 @@ private:
std::string serviceId_ {};
std::string controlURL_ {};
std::string eventSubURL_ {};
};
#endif
#if HAVE_LIBNATPMP
using clock = std::chrono::system_clock;
using time_point = clock::time_point;
class PMPIGD : public IGD {
public:
void clearAll() {
toRemove_.clear();
udpMappings.clear();
tcpMappings.clear();
clearAll_ = true;
}
GlobalMapping* getNextMappingToRenew() const {
const GlobalMapping* mapping {nullptr};
for (const auto& m : udpMappings)
if (!mapping or m.second.renewal_ < mapping->renewal_)
mapping = &m.second;
for (const auto& m : tcpMappings)
if (!mapping or m.second.renewal_ < mapping->renewal_)
mapping = &m.second;
return (GlobalMapping*)mapping;
}
time_point getRenewalTime() const {
const auto next = getNextMappingToRenew();
auto nextTime = std::min(renewal_, next ? next->renewal_ : time_point::max());
return toRemove_.empty() ? nextTime : std::min(nextTime, time_point::min());
}
time_point renewal_ {time_point::min()};
std::vector<GlobalMapping> toRemove_ {};
bool clearAll_ {false};
};
#endif
}} // namespace ring::upnp
#endif /* UPNP_IGD_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment