From d19ba2cb412eb935e141a5aed4bbe8c2607180c8 Mon Sep 17 00:00:00 2001
From: AmarOk <contact@enconn.fr>
Date: Wed, 6 Dec 2017 12:30:59 -0600
Subject: [PATCH] proxyserver: add lock_guard to avoid to manually unlock

---
 include/opendht/callbacks.h |  3 +-
 include/opendht/dht.h       |  5 ++--
 include/opendht/dhtrunner.h |  5 ++--
 include/opendht/securedht.h |  3 +-
 src/dht_proxy_client.cpp    |  2 --
 src/dht_proxy_server.cpp    | 60 ++++++++++++++++++++-----------------
 src/dhtrunner.cpp           |  5 ++--
 src/securedht.cpp           |  3 +-
 tools/dhtnode.cpp           |  5 ++--
 tools/tools_common.h        |  1 +
 10 files changed, 51 insertions(+), 41 deletions(-)

diff --git a/include/opendht/callbacks.h b/include/opendht/callbacks.h
index b58a8b1f..b3b1b6f7 100644
--- a/include/opendht/callbacks.h
+++ b/include/opendht/callbacks.h
@@ -1,7 +1,8 @@
 /*
  *  Copyright (C) 2014-2017 Savoir-faire Linux Inc.
- *  Author : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
+ *  Authors: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
  *           Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *           Sébastien Blin <sebastien.blin@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
diff --git a/include/opendht/dht.h b/include/opendht/dht.h
index 09bb0f56..2bc18b5b 100644
--- a/include/opendht/dht.h
+++ b/include/opendht/dht.h
@@ -1,7 +1,8 @@
 /*
  *  Copyright (C) 2014-2017 Savoir-faire Linux Inc.
- *  Author(s) : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
- *              Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *  Authors: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
+ *           Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *           Sébastien Blin <sebastien.blin@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h
index fbc89dee..d7f573e5 100644
--- a/include/opendht/dhtrunner.h
+++ b/include/opendht/dhtrunner.h
@@ -1,7 +1,8 @@
 /*
  *  Copyright (C) 2014-2017 Savoir-faire Linux Inc.
- *  Author(s) : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
- *              Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *  Authors: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
+ *           Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *           Sébastien Blin <sebastien.blin@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h
index cd67bff8..a4ff6ec4 100644
--- a/include/opendht/securedht.h
+++ b/include/opendht/securedht.h
@@ -1,7 +1,8 @@
 /*
  *  Copyright (C) 2014-2017 Savoir-faire Linux Inc.
- *  Author : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
+ *  Authors: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
  *           Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *           Sébastien Blin <sebastien.blin@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp
index 0a3eb547..31db722b 100644
--- a/src/dht_proxy_client.cpp
+++ b/src/dht_proxy_client.cpp
@@ -27,8 +27,6 @@
 
 #include "dhtrunner.h"
 
-#include <iostream>
-
 constexpr const char* const HTTP_PROTO {"http://"};
 
 namespace dht {
diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp
index 2b5756fe..5be8da73 100644
--- a/src/dht_proxy_server.cpp
+++ b/src/dht_proxy_server.cpp
@@ -82,33 +82,35 @@ DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port)
             std::this_thread::sleep_for(std::chrono::seconds(1));
         }
         while (service_->is_up()  && !stopListeners) {
-            lockListener_.lock();
+            {
+                std::lock_guard<std::mutex> lock(lockListener_);
+                auto listener = currentListeners_.begin();
+                while (listener != currentListeners_.end()) {
+                    if (dht_ && listener->session->is_closed()) {
+                        dht_->cancelListen(listener->hash, std::move(listener->token));
+                        // Remove listener if unused
+                        listener = currentListeners_.erase(listener);
+                    } else {
+                        ++listener;
+                    }
+                }
+            }
+            std::this_thread::sleep_for(std::chrono::seconds(1));
+        }
+        // Remove last listeners
+        {
+            std::lock_guard<std::mutex> lock(lockListener_);
             auto listener = currentListeners_.begin();
             while (listener != currentListeners_.end()) {
-                if (dht_ && listener->session->is_closed()) {
-                    dht_->cancelListen(listener->hash, std::move(listener->token));
+                if (dht_) {
+                    dht_->cancelListen(listener->hash, std::move(listener->token.get()));
                     // Remove listener if unused
                     listener = currentListeners_.erase(listener);
                 } else {
-                     ++listener;
+                    ++listener;
                 }
             }
-            lockListener_.unlock();
-            std::this_thread::sleep_for(std::chrono::seconds(1));
-        }
-        // Remove last listeners
-        lockListener_.lock();
-        auto listener = currentListeners_.begin();
-        while (listener != currentListeners_.end()) {
-            if (dht_) {
-                dht_->cancelListen(listener->hash, std::move(listener->token.get()));
-                // Remove listener if unused
-                listener = currentListeners_.erase(listener);
-            } else {
-                 ++listener;
-            }
         }
-        lockListener_.unlock();
     });
 
     dht->forwardAllMessages(true);
@@ -123,13 +125,14 @@ void
 DhtProxyServer::stop()
 {
     service_->stop();
-    lockListener_.lock();
-    auto listener = currentListeners_.begin();
-    while (listener != currentListeners_.end()) {
-        listener->session->close();
-        ++ listener;
+    {
+        std::lock_guard<std::mutex> lock(lockListener_);
+        auto listener = currentListeners_.begin();
+        while (listener != currentListeners_.end()) {
+            listener->session->close();
+            ++ listener;
+        }
     }
-    lockListener_.unlock();
     stopListeners = true;
     // listenThreads_ will stop because there is no more sessions
     if (listenThread_.joinable())
@@ -248,9 +251,10 @@ DhtProxyServer::listen(const std::shared_ptr<restbed::Session>& session) const
                     }
                     return !s->is_closed();
                 });
-                lockListener_.lock();
-                currentListeners_.emplace_back(std::move(listener));
-                lockListener_.unlock();
+                {
+                    std::lock_guard<std::mutex> lock(lockListener_);
+                    currentListeners_.emplace_back(std::move(listener));
+                }
             } else {
                 session->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}");
             }
diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp
index 589bc80d..6a1fab5f 100644
--- a/src/dhtrunner.cpp
+++ b/src/dhtrunner.cpp
@@ -1,7 +1,8 @@
 /*
  *  Copyright (C) 2014-2017 Savoir-faire Linux Inc.
- *  Author(s) : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
- *              Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *  Authors: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
+ *           Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *           Sébastien Blin <sebastien.blin@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
diff --git a/src/securedht.cpp b/src/securedht.cpp
index 99c7557e..338bcb2c 100644
--- a/src/securedht.cpp
+++ b/src/securedht.cpp
@@ -1,7 +1,8 @@
 /*
  *  Copyright (C) 2014-2017 Savoir-faire Linux Inc.
- *  Author : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
+ *  Authors: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
  *           Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *           Sébastien Blin <sebastien.blin@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp
index 6776876f..7c2ca14f 100644
--- a/tools/dhtnode.cpp
+++ b/tools/dhtnode.cpp
@@ -1,8 +1,9 @@
 /*
  *  Copyright (C) 2014-2017 Savoir-faire Linux Inc.
  *
- *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
- *          Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *  Authors: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
+ *           Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
+ *           Sébastien Blin <sebastien.blin@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
diff --git a/tools/tools_common.h b/tools/tools_common.h
index c71fa680..cbd8a5c8 100644
--- a/tools/tools_common.h
+++ b/tools/tools_common.h
@@ -2,6 +2,7 @@
  *  Copyright (C) 2014-2017 Savoir-faire Linux Inc.
  *
  *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
+ *  Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
-- 
GitLab