COM-50: Handshake Requests need to be decoupled.
authorMarkus Schaber <markus@pep-security.net>
Sun, 12 Feb 2017 23:02:53 +0100
changeset 235581e3f7a92f5
parent 234 a3cc1847d197
child 236 70982ee747c5
COM-50: Handshake Requests need to be decoupled.

Use recursive_mutex instead of normal mutex, to prevent reentrancy problems.
CpEpEngine.cpp
CpEpEngine.h
stdafx.h
     1.1 --- a/CpEpEngine.cpp	Sat Feb 11 17:49:02 2017 +0100
     1.2 +++ b/CpEpEngine.cpp	Sun Feb 12 23:02:53 2017 +0100
     1.3 @@ -1066,7 +1066,7 @@
     1.4  void CpEpEngine::start_keysync()
     1.5  {
     1.6      // acquire the lock
     1.7 -    std::unique_lock<std::mutex> lock(keysync_mutex);
     1.8 +    std::unique_lock<std::recursive_mutex> lock(keysync_mutex);
     1.9  
    1.10      // Assert if we're not already running.
    1.11      assert(!this->keysync_thread);
    1.12 @@ -1132,7 +1132,7 @@
    1.13  void CpEpEngine::stop_keysync()
    1.14  {
    1.15      // acquire the lock
    1.16 -    std::unique_lock<std::mutex> lock(keysync_mutex);
    1.17 +    std::unique_lock<std::recursive_mutex> lock(keysync_mutex);
    1.18  
    1.19      // Do nothing if keysync is not running.
    1.20      if (!keysync_thread)
    1.21 @@ -1175,7 +1175,7 @@
    1.22      CpEpEngine* me = (CpEpEngine*)management;
    1.23  
    1.24      // acquire the lock
    1.25 -    std::unique_lock<std::mutex> lock(me->keysync_mutex);
    1.26 +    std::unique_lock<std::recursive_mutex> lock(me->keysync_mutex);
    1.27  
    1.28      // check whether we're in a valid state running:
    1.29      if (!me->keysync_thread)
    1.30 @@ -1215,7 +1215,7 @@
    1.31      }
    1.32  
    1.33      // acquire the lock
    1.34 -    std::unique_lock<std::mutex> lock(me->keysync_mutex);
    1.35 +    std::unique_lock<std::recursive_mutex> lock(me->keysync_mutex);
    1.36  
    1.37      if (me->notify_handshake_finished)
    1.38          me->notify_handshake_deliver_result();
     2.1 --- a/CpEpEngine.h	Sat Feb 11 17:49:02 2017 +0100
     2.2 +++ b/CpEpEngine.h	Sun Feb 12 23:02:53 2017 +0100
     2.3 @@ -138,8 +138,8 @@
     2.4      static void do_keysync_in_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks);
     2.5      void stop_keysync();
     2.6  
     2.7 -    std::mutex keysync_mutex;
     2.8 -    std::condition_variable keysync_condition;
     2.9 +    std::recursive_mutex keysync_mutex;
    2.10 +    std::condition_variable_any keysync_condition;
    2.11      std::thread *keysync_thread = NULL;
    2.12      std::queue<void*> keysync_queue;
    2.13      bool keysync_abort_requested = false;
     3.1 --- a/stdafx.h	Sat Feb 11 17:49:02 2017 +0100
     3.2 +++ b/stdafx.h	Sun Feb 12 23:02:53 2017 +0100
     3.3 @@ -39,6 +39,7 @@
     3.4  #include <list>
     3.5  #include <thread>
     3.6  #include <mutex>
     3.7 +#include <condition_variable>
     3.8  #include <atomic>
     3.9  #include <random>
    3.10  #include <iomanip>