]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/radius/radius.cpp
Sending attributes from <auth>/send section. (#13)
[stg.git] / projects / stargazer / plugins / other / radius / radius.cpp
1 #include "radius.h"
2 #include "radproto/error.h"
3 #include "stg/common.h"
4 #include <boost/tokenizer.hpp>
5
6 #include <utility>
7
8 using STG::RADIUS;
9
10 extern "C" STG::Plugin* GetPlugin()
11 {
12     static RADIUS plugin;
13     return &plugin;
14 }
15
16 RADIUS::RADIUS()
17     : m_running(false),
18       m_users(nullptr),
19       m_logger(PluginLogger::get("radius"))
20 {
21 }
22
23 int RADIUS::ParseSettings()
24 {
25     auto ret = m_config.ParseSettings(m_settings);
26     if (ret != 0)
27         m_errorStr = m_config.GetStrError();
28
29     return ret;
30 }
31
32 std::string RADIUS::GetVersion() const
33 {
34     return "Radius v.1.0";
35 }
36
37 int RADIUS::Start()
38 {
39     m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
40     return 0;
41 }
42
43 int RADIUS::Stop()
44 {
45     if (!m_thread.joinable())
46         return 0;
47
48     m_thread.request_stop();
49
50     if (m_server)
51         m_server->stop();
52
53     m_thread.join();
54     return 0;
55 }
56
57 bool RADIUS::IsRunning()
58 {
59     const std::lock_guard lock(m_mutex);
60     return m_running;
61 }
62
63 void RADIUS::SetRunning(bool val)
64 {
65     const std::lock_guard lock(m_mutex);
66     m_running = val;
67 }
68
69 int RADIUS::Run(std::stop_token token)
70 {
71     SetRunning(true);
72
73     try
74     {
75         if (!m_server)
76            m_server = std::make_unique<Server>(m_ioContext, m_config.GetSecret(), m_config.GetPort(), m_config.GetDictionaries(), std::move(token), m_logger, m_users, m_config);
77         m_ioContext.run();
78     }
79     catch (const std::exception& e)
80     {
81         m_errorStr = "Exception in RADIUS::Run(): " + std::string(e.what());
82         m_logger("Exception in RADIUS:: Run(): %s", e.what());
83         printfd(__FILE__, "Exception in RADIUS:: Run(). Message: '%s'\n", e.what());
84     }
85
86     SetRunning(false);
87     return 0;
88 }