Radius (#9)
authorHelenMamontova <44774239+HelenMamontova@users.noreply.github.com>
Tue, 18 Mar 2025 15:49:02 +0000 (17:49 +0200)
committerGitHub <noreply@github.com>
Tue, 18 Mar 2025 15:49:02 +0000 (17:49 +0200)
* Radius. Header file <mutex>, <jthread.hpp> added. Functions start, stop
declaration changed. Class members m_thread, m_mutex added. Function Run
declaration added.

* Radius. Header files added. Functions Start, Stop, Run definition added.

* Radius. New files server.h, server.cpp added.

* Radius. Definition of functions startReceive, makeResponse, handleSend,
handleReceive added.

* The Run function name fixed. Definition of secret variable sdded.

* The file server.cpp added to add_library command, command
find_package(OpenSSL 1.0.0 Required) added, OpenSSL::Crypto added to
target_link_libraries command in the block if(BUILD_MOD_RADIUS).

* Parameter token added to function Run() declaration.

* Radius. Method Start: variable isRunning=true removed, m_thread
definition changed. Method Stop: variable isRunning=false removed,
m_thread.joinable check added, isRunning check added, request_stop call added.
Method Run: parameter token added, variables secret, port removed,
object lock and isRunning=true added before cycle while,
cycle while added, isRunning=false added after cycle while.

* Radius. Hold the mutex removed,extra conditions
for m_thread.join() removed in the Stop function.

* Radius. Cycle while removed in function Run.

* Radius. The variables isRunning, errorStr replaced by m_running,
m_errorStr in class Radius.

* Radius. The variable isRunning replaced by m_running in function Run.

* Radius. Object name io_service changed to ioService in the function Run.

* Method SetRunning declaration added to the class RADIUS.

* Method SetRunning definition added. Method SetRunning call added to
function Run.

* Radius. Namespace STG added.

* Radius. Declaration using STG::Server added.

* Radius. Header file "stg/logger.h" added. RADIUS class member m_loger added.

* Radius. Header file "stg/common.h" added. Initialization of m_logger
added to constructor RADIUS. Output cerr replaced by logger and
printfd().

* Radius. Variable except added, messages fixed in the function Run.

* Radius. Thread join logic fixed in the function Stop.

* Keyword const added to std::lock_guard in the function SetRunning.

* Radius. Class member m_mutex moved to the top of list.

* Radius. Class member m_logger put after the m_running.

* Radius. Unnecessary variable except removed in the function Run.

* Radius. Extra whitespace removed in the function Stop. Function
IsRunning definition added.

* Radius. Function IsRunning declaration changed.

* Radius. Formatting fixed.

* Radius. Extra symbols '//' removed.

* Radius. Class member m_running initialization added to constructor
RADIUS.

* Radius.Header file "stg/module_settings.h" added. Class members variable
m_settings and method SetSettings added to class RADIUS.

* Radius. The parameters Secret, Dictionaries added, parameter Port
changed, other parameters removed.

* Radius. Header files added. Class RAD_SETTINGS added. Methods
SetSettings, copy constructor RADIUS, assignment operator added. Method
ParseSettings changed. Formatting fixed.

* Radius. Parameter default changed to optional and parameters value
commented out for the parameters dictionaries, port.

* Radius. Description of parameters secret, port, dictionaries changed.

* Radius. Copy constructor RADIUS and assignment operator declaration
changed and moved to public section.

* Radius. Class member m_mutex moved to the top of section private.

* Radius. The m_port variable value initialization changed to 1812,
m_dictionaries variable initialization added to constructor RAD_SETTINGS.

* Radius. Check for missing parameters port and dictionaries removed in function ParseSettings.

* Radius. The dist directory added.

* Radius. The methods declaration stop and start added.

* Radius. The methods stop, start definition added. Method start call
added to Server constructor.

* Radius. The header files "server.h", <boost/asio.hpp>, <memory> added.
The class members m_ioService, m_server added.

* Radius. The header files "server", <boost/asio.hpp>, <string> removed.
The method m_server->stop() call added to method Stop.

* Radius. Header files <stop_token> and <jthread.hpp> added. Parameter
token added to constructor Server. Class member m_token added.

* Radius. Parameter token and m_token class member initialization added to Server
constructor definition. The check m_token.stop_requested() added to
method handleSend before startReceive method call.

* Radius. The m_server class member initialization removed.

* Radius. The m_server->stop() call adnd m_thread.request_stop() call
reodered, the m_server check added before m_server->stop call in the
method Stop. Parameter token added to constructor Server in the function
Run.

* Radius. Header file "stg/logger.h" added. Class member m_logger added to
class Server.

* Radius. Header file "stg/common.h" added, header file <iostream>
removed. Class member m_logger initialization added to constructor
Server. Object std::cout replaced by function printfd and m_logger.

* Radius. Class member m_logger initialization changed in the constructor
Server. Check token moved before checking the error code in the
handleSend method.

* Radius. Check token added to handleReceive method.

* Radius. Header file <stop_token> replaced by <stop_token.hpp>, header
file <jthread.hpp> removed.

* Radius. Parameter m_logger added to constructor Server call in the
method Run.

* Radius. Parameter logger added to constructor Server declaration. Class
member m_logger added to class Server.

* Radius. Parameter logger added to constructor Server definition. Class
member m_logger initialization changed in the constructor Server.

* Radius. Extra whitespace removed ib the method Run.

* Radius. The order of operations changed in the method handleSend.

* Radius. The order of operations changed in the method handleReceive.

* Radius. Operator return removed when checking ec, operator else removed
when checking packet in the handleReceive method.

* Radius. Parameter token replaced by std::move(token) in the constructor Server
in method Run.

* Radius. Class member m_token initialization value changed to
std::move(token) in the constructor Server.

* Radius. The c_str() function added to message when checking error in the
handleSend and handleReceive methods.

* Radius. Header file <cstring> added.

.gitignore
projects/stargazer/plugins/other/radius/radius.cpp
projects/stargazer/plugins/other/radius/radius.h
projects/stargazer/plugins/other/radius/server.cpp
projects/stargazer/plugins/other/radius/server.h

index ba791f708430ac7dbfc2a5483237a186290b4e11..9fe21c8f62550e643f15b48caea244017cbc1c2e 100644 (file)
@@ -8,3 +8,4 @@ CVS*
 doc/xmlrpc-doc/book
 doc/help/book
 doc/help/help.pdf
+dist
index e517c959ec81882907a96ff349a812c261ca8542..8e5b37a895edc3f5b2cdb195087faf5787276721 100644 (file)
@@ -1,11 +1,7 @@
 #include "radius.h"
-#include "server.h"
 #include "radproto/error.h"
-
 #include "stg/common.h"
 
-#include <boost/asio.hpp>
-#include <string>
 #include <iterator>
 #include <iostream>
 
@@ -67,27 +63,6 @@ RADIUS::RADIUS()
 {
 }
 
-int RADIUS::Run(std::stop_token token)
-{
-    SetRunning(true);
-
-    try
-    {
-        boost::asio::io_service ioService;
-        Server server(ioService, m_radSettings.GetSecret(), m_radSettings.GetPort(), m_radSettings.GetDictionaries());
-        ioService.run();
-    }
-    catch (const std::exception& e)
-    {
-        m_errorStr = "Exception in RADIUS::Run(): " + std::string(e.what());
-        m_logger("Exception in RADIUS:: Run(): %s", e.what());
-        printfd(__FILE__, "Exception in RADIUS:: Run(). Message: '%s'\n", e.what());
-    }
-
-    SetRunning(false);
-    return 0;
-}
-
 int RADIUS::ParseSettings()
 {
     auto ret = m_radSettings.ParseSettings(m_settings);
@@ -115,6 +90,9 @@ int RADIUS::Stop()
 
     m_thread.request_stop();
 
+    if (m_server)
+        m_server->stop();
+
     m_thread.join();
     return 0;
 }
@@ -130,3 +108,24 @@ void RADIUS::SetRunning(bool val)
     const std::lock_guard lock(m_mutex);
     m_running = val;
 }
+
+int RADIUS::Run(std::stop_token token)
+{
+    SetRunning(true);
+
+    try
+    {
+        if (!m_server)
+           m_server = std::make_unique<Server>(m_ioService, m_radSettings.GetSecret(), m_radSettings.GetPort(), m_radSettings.GetDictionaries(), std::move(token), m_logger);
+        m_ioService.run();
+    }
+    catch (const std::exception& e)
+    {
+        m_errorStr = "Exception in RADIUS::Run(): " + std::string(e.what());
+        m_logger("Exception in RADIUS:: Run(): %s", e.what());
+        printfd(__FILE__, "Exception in RADIUS:: Run(). Message: '%s'\n", e.what());
+    }
+
+    SetRunning(false);
+    return 0;
+}
index 8eedace11e83cdb822674b4564b9416f920b1c1f..6b742de5395bf4c60b493bb79e4764142371ab55 100644 (file)
@@ -5,8 +5,11 @@
 #include "stg/module_settings.h"
 #include "stg/subscriptions.h"
 #include "stg/logger.h"
+#include "server.h"
 
+#include <boost/asio.hpp>
 #include <string>
+#include <memory>
 #include <mutex>
 #include <jthread.hpp>
 #include <cstdint> //uint8_t, uint32_t
@@ -61,6 +64,7 @@ namespace STG
         private:
             std::mutex m_mutex;
 
+            boost::asio::io_service m_ioService;
             int Run(std::stop_token token);
 
             mutable std::string m_errorStr;
@@ -72,5 +76,7 @@ namespace STG
             std::jthread m_thread;
 
             PluginLogger m_logger;
+
+            std::unique_ptr<Server> m_server;
     };
 }
index 4d16b6cbcf33f69c12110e95136322dc51497e53..850847fa1ad711f9667b366de2e3fd6e23278bc4 100644 (file)
@@ -1,18 +1,32 @@
 #include "server.h"
 #include "radproto/packet_codes.h"
+#include "stg/common.h"
+#include <cstring>
 #include <functional>
-#include <iostream>
 
 using STG::Server;
 using boost::system::error_code;
 
-Server::Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath)
+Server::Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger)
     : m_radius(io_service, secret, port),
-      m_dictionaries(filePath)
+      m_dictionaries(filePath),
+      m_token(std::move(token)),
+      m_logger(logger)
+{
+    start();
+}
+
+void Server::start()
 {
     startReceive();
 }
 
+void Server::stop()
+{
+    error_code ec;
+    m_radius.close(ec);
+}
+
 void Server::startReceive()
 {
     m_radius.asyncReceive([this](const auto& error, const auto& packet, const boost::asio::ip::udp::endpoint& source){ handleReceive(error, packet, source); });
@@ -42,27 +56,33 @@ RadProto::Packet Server::makeResponse(const RadProto::Packet& request)
 
 void Server::handleSend(const error_code& ec)
 {
-    if (ec)
-        std::cout << "Error asyncSend: " << ec.message() << "\n";
+    if (m_token.stop_requested())
+        return;
 
+    if (ec)
+    {
+        m_logger("Error asyncSend: %s", ec.message().c_str());
+        printfd(__FILE__, "Error asyncSend: '%s'\n", ec.message().c_str());
+    }
     startReceive();
 }
 
 void Server::handleReceive(const error_code& error, const std::optional<RadProto::Packet>& packet, const boost::asio::ip::udp::endpoint& source)
 {
+    if (m_token.stop_requested())
+        return;
+
     if (error)
     {
-        std::cout << "Error asyncReceive: " << error.message() << "\n";
-        return;
+        m_logger("Error asyncReceive: %s", error.message().c_str());
+        printfd(__FILE__, "Error asyncReceive: '%s'\n", error.message().c_str());
     }
 
     if (packet == std::nullopt)
     {
-        std::cout << "Error asyncReceive: the request packet is missing\n";
+        m_logger("Error asyncReceive: the request packet is missing\n");
+        printfd(__FILE__, "Error asyncReceive: the request packet is missing\n");
         return;
     }
-    else
-    {
-        m_radius.asyncSend(makeResponse(*packet), source, [this](const auto& ec){ handleSend(ec); });
-    }
+    m_radius.asyncSend(makeResponse(*packet), source, [this](const auto& ec){ handleSend(ec); });
 }
index b88889be960b820674344de460e2111809411d64..e33a23b3f20bcc99113776a0a08792b91956b42c 100644 (file)
@@ -3,7 +3,9 @@
 #include "radproto/socket.h"
 #include "radproto/packet.h"
 #include "radproto/dictionaries.h"
+#include "stg/logger.h"
 #include <boost/asio.hpp>
+#include <stop_token.hpp>
 #include <optional>
 #include <cstdint> //uint8_t, uint32_t
 
@@ -12,15 +14,19 @@ namespace STG
     class Server
     {
         public:
-            Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath);
-
+            Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger);
+            void stop();
         private:
             RadProto::Packet makeResponse(const RadProto::Packet& request);
             void handleReceive(const boost::system::error_code& error, const std::optional<RadProto::Packet>& packet, const boost::asio::ip::udp::endpoint& source);
             void handleSend(const boost::system::error_code& ec);
+            void start();
             void startReceive();
 
             RadProto::Socket m_radius;
             RadProto::Dictionaries m_dictionaries;
+            std::stop_token m_token;
+
+            PluginLogger& m_logger;
     };
 }