Commit 3ee3e77b authored by Eden Abitbol's avatar Eden Abitbol

natpmp: activate natpmp thread

Enable the natpmp thread by setting the pmpRun_ variable to true.

Removed extra mutex lock in UPnPContext class. Removed swap IGD
protocol feature.

Change-Id: I0490b38a5b2094c2751ddc6de8ac7f1e064e8644
parent 31806a8d
......@@ -78,7 +78,7 @@ NatPmp::NatPmp()
NatPmp::~NatPmp()
{
{
std::lock_guard<std::mutex> lock(validIgdMutex);
std::lock_guard<std::mutex> lock(validIgdMutex_);
if (pmpIGD_) {
{
std::lock_guard<std::mutex> lk(pmpMutex_);
......@@ -100,7 +100,7 @@ void
NatPmp::clearIGDs()
{
// Lock valid IGD.
std::lock_guard<std::mutex> lock(validIgdMutex);
std::lock_guard<std::mutex> lock(validIgdMutex_);
// Clear internal IGD (nat pmp only supports one).
if (pmpIGD_) {
......@@ -116,7 +116,7 @@ void
NatPmp::searchForIGD()
{
// Lock valid IGD.
std::lock_guard<std::mutex> lock(validIgdMutex);
std::lock_guard<std::mutex> lock(validIgdMutex_);
if (pmpIGD_) {
// Clear internal IGD (nat pmp only supports one).
std::lock_guard<std::mutex> lk(pmpMutex_);
......@@ -181,7 +181,7 @@ NatPmp::removeAllLocalMappings(IGD* /*igd*/)
void
NatPmp::removeMapping(const Mapping& igdMapping)
{
std::lock_guard<std::mutex> lock(validIgdMutex);
std::lock_guard<std::mutex> lock(validIgdMutex_);
if (not pmpIGD_) {
JAMI_WARN("NAT-PMP: no valid IGD available");
......@@ -249,7 +249,7 @@ NatPmp::searchForIGD(const std::shared_ptr<PMPIGD>& pmp_igd, natpmp_t& natpmp)
}
// Keep IGD internally.
std::lock_guard<std::mutex> lock(validIgdMutex);
std::lock_guard<std::mutex> lock(validIgdMutex_);
pmpIGD_ = pmp_igd;
}
}
......
......@@ -84,7 +84,7 @@ private:
std::mutex pmpMutex_ {}; // NatPmp mutex.
std::condition_variable pmpCv_ {}; // Condition variable for thread-safe signaling.
std::atomic_bool pmpRun_ { false }; // Variable to allow the thread to run.
std::atomic_bool pmpRun_ { true }; // Variable to allow the thread to run.
std::thread pmpThread_ {}; // NatPmp thread.
std::shared_ptr<PMPIGD> pmpIGD_ {}; // IGD discovered by NatPmp.
......
......@@ -124,7 +124,7 @@ PUPnP::~PUPnP()
{
// Clear all the lists.
{
std::lock_guard<std::mutex> lk(validIgdMutex);
std::lock_guard<std::mutex> lk(validIgdMutex_);
for(auto const &it : validIgdList_) {
if (auto igd = dynamic_cast<UPnPIGD*>(it.second.get()))
actionDeletePortMappingsByDesc(*igd, Mapping::UPNP_DEFAULT_MAPPING_DESCRIPTION);
......@@ -145,7 +145,7 @@ void
PUPnP::clearIGDs()
{
// Lock internal IGD list.
std::lock_guard<std::mutex> lk(validIgdMutex);
std::lock_guard<std::mutex> lk(validIgdMutex_);
// Clear internal IGD list.
validIgdList_.clear();
......@@ -211,7 +211,7 @@ void
PUPnP::removeMapping(const Mapping& igdMapping)
{
// Lock mutex to protect IGD list.
std::lock_guard<std::mutex> lk(validIgdMutex);
std::lock_guard<std::mutex> lk(validIgdMutex_);
// Iterate over all IGDs in internal list and try to remove selected mapping.
for (auto const& item : validIgdList_) {
......@@ -329,7 +329,7 @@ PUPnP::handleCtrlPtUPnPEvents(Upnp_EventType event_type, const void* event)
// Check if this device ID is already in the list.
std::string cpDeviceId = UpnpDiscovery_get_DeviceID_cstr(d_event);
std::lock_guard<std::mutex> lk(validIgdMutex);
std::lock_guard<std::mutex> lk(validIgdMutex_);
if (cpDeviceList_.count(cpDeviceId) > 0) {
break;
}
......@@ -436,7 +436,7 @@ PUPnP::handleCtrlPtUPnPEvents(Upnp_EventType event_type, const void* event)
{
const UpnpDiscovery *d_event = (const UpnpDiscovery *)event;
std::lock_guard<std::mutex> lk(validIgdMutex);
std::lock_guard<std::mutex> lk(validIgdMutex_);
// Remvoe device Id from list.
std::string cpDeviceId(UpnpDiscovery_get_DeviceID_cstr(d_event));
......
......@@ -91,7 +91,7 @@ public:
void setOnIgdChanged(IgdListChangedCallback&& cb) { updateIgdListCb_ = std::move(cb); }
protected:
mutable std::mutex validIgdMutex; // Mutex used to access these lists and IGDs in a thread-safe manner.
mutable std::mutex validIgdMutex_; // Mutex used to access these lists and IGDs in a thread-safe manner.
IgdListChangedCallback updateIgdListCb_; // Callback for when the IGD list changes.
};
......
......@@ -268,11 +268,9 @@ UPnPContext::isIgdInList(const IpAddr& publicIpAddr)
return false;
}
UPnPProtocol::Type
UPnPProtocol::Type
UPnPContext::getIgdProtocol(IGD* igd)
{
std::lock_guard<std::mutex> igdListLock(igdListMutex_);
for (auto const& item : igdList_) {
if (item.second->publicIp_ == igd->publicIp_) {
return item.first->getType();
......@@ -285,7 +283,8 @@ UPnPContext::getIgdProtocol(IGD* igd)
bool
UPnPContext::igdListChanged(UPnPProtocol* protocol, IGD* igd, IpAddr publicIpAddr, bool added)
{
std::lock_guard<std::mutex> lock(igdListMutex_);
std::lock_guard<std::mutex> igdListLock(igdListMutex_);
if (added) {
return addIgdToList(protocol, igd);
} else {
......@@ -307,20 +306,11 @@ UPnPContext::addIgdToList(UPnPProtocol* protocol, IGD* igd)
}
if (isIgdInList(igd->publicIp_)) {
// If the protocol of the IGD that is already in the list isn't NatPmp, then swap.
if (getIgdProtocol(igd) != UPnPProtocol::Type::NAT_PMP and protocol->getType() == UPnPProtocol::Type::NAT_PMP) {
JAMI_WARN("UPnPContext: Attempting to swap IGD UPnP protocol");
if (!removeIgdFromList(igd)) {
JAMI_WARN("UPnPContext: Failed to swap IGD UPnP protocol");
return false;
}
} else {
return false;
}
return false;
}
igdList_.emplace_back(protocol, igd);
for (const auto& item : igdListeners_) {
item.second();
}
......
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