From 5e7a64653f23f70a5bf4c609ff240ed6e0e3a091 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 4 Oct 2021 17:40:58 -0400
Subject: [PATCH] upnp: make mapping operations atomic

Change-Id: I2c04e3831f68a55b7682c42eb93464e1fab78b1b
---
 src/upnp/protocol/mapping.cpp | 38 ++++++++++-------------------------
 1 file changed, 11 insertions(+), 27 deletions(-)

diff --git a/src/upnp/protocol/mapping.cpp b/src/upnp/protocol/mapping.cpp
index 0773065ff9..792ec9e92f 100644
--- a/src/upnp/protocol/mapping.cpp
+++ b/src/upnp/protocol/mapping.cpp
@@ -43,7 +43,7 @@ Mapping::Mapping(PortType type, uint16_t portExternal, uint16_t portInternal, bo
 
 Mapping::Mapping(const Mapping& other)
 {
-    std::lock_guard<std::mutex> lock(mutex_);
+    std::lock_guard<std::mutex> lock(other.mutex_);
 
     internalAddr_ = other.internalAddr_;
     internalPort_ = other.internalPort_;
@@ -95,17 +95,6 @@ Mapping::setAvailable(bool val)
 void
 Mapping::setState(const MappingState& state)
 {
-    {
-        std::lock_guard<std::mutex> lock(mutex_);
-        if (state_ == state)
-            return;
-    }
-
-    JAMI_DBG("Changed mapping %s state from %s to %s",
-             toString().c_str(),
-             getStateStr(),
-             getStateStr(state));
-
     std::lock_guard<std::mutex> lock(mutex_);
     state_ = state;
 }
@@ -136,22 +125,17 @@ Mapping::toString(bool extraInfo) const
 bool
 Mapping::isValid() const
 {
-    {
-        std::lock_guard<std::mutex> lock(mutex_);
-        if (state_ == MappingState::FAILED)
-            return false;
-        if (internalPort_ == 0)
-            return false;
-        if (externalPort_ == 0)
-            return false;
-        if (not igd_ or not igd_->isValid())
-            return false;
-    }
-
-    if (not hasValidHostAddress())
+    std::lock_guard<std::mutex> lock(mutex_);
+    if (state_ == MappingState::FAILED)
         return false;
-
-    return true;
+    if (internalPort_ == 0)
+        return false;
+    if (externalPort_ == 0)
+        return false;
+    if (not igd_ or not igd_->isValid())
+        return false;
+    IpAddr intAddr(internalAddr_);
+    return intAddr and not intAddr.isLoopback();
 }
 
 bool
-- 
GitLab