]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
Fix raw packet access in cap_divert
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / stgconfig.cpp
1 #include <unistd.h>
2
3 #include <csignal>
4 #include <algorithm>
5
6 #include "stg/tariffs.h"
7 #include "stg/admins.h"
8 #include "stg/users.h"
9 #include "stg/plugin_creator.h"
10 #include "stgconfig.h"
11
12 //-----------------------------------------------------------------------------
13 //-----------------------------------------------------------------------------
14 //-----------------------------------------------------------------------------
15 PLUGIN_CREATOR<STG_CONFIG> stgc;
16 //-----------------------------------------------------------------------------
17 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
19 int STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
20 {
21 int p;
22 PARAM_VALUE pv;
23 vector<PARAM_VALUE>::const_iterator pvi;
24 ///////////////////////////
25 pv.param = "Port";
26 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
27 if (pvi == s.moduleParams.end())
28     {
29     errorStr = "Parameter \'Port\' not found.";
30     printfd(__FILE__, "Parameter 'Port' not found\n");
31     return -1;
32     }
33 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
34     {
35     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
36     printfd(__FILE__, "%s\n", errorStr.c_str());
37     return -1;
38     }
39 port = p;
40
41 return 0;
42 }
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 PLUGIN * GetPlugin()
47 {
48 return stgc.GetPlugin();
49 }
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 const std::string STG_CONFIG::GetVersion() const
54 {
55 return "Stg configurator v.0.08";
56 }
57 //-----------------------------------------------------------------------------
58 STG_CONFIG::STG_CONFIG()
59     : errorStr(),
60       stgConfigSettings(),
61       thread(),
62       nonstop(false),
63       isRunning(false),
64       config(),
65       users(NULL),
66       admins(NULL),
67       tariffs(NULL),
68       store(NULL),
69       settings(),
70       stgSettings(NULL)
71 {
72 }
73 //-----------------------------------------------------------------------------
74 int STG_CONFIG::ParseSettings()
75 {
76 int ret = stgConfigSettings.ParseSettings(settings);
77 if (ret)
78     errorStr = stgConfigSettings.GetStrError();
79 return ret;
80 }
81 //-----------------------------------------------------------------------------
82 int STG_CONFIG::Start()
83 {
84 if (isRunning)
85     return 0;
86
87 nonstop = true;
88
89 config.SetPort(stgConfigSettings.GetPort());
90 config.SetAdmins(admins);
91 config.SetUsers(users);
92 config.SetTariffs(tariffs);
93 config.SetStgSettings(stgSettings);
94 config.SetStore(store);
95
96 if (config.Prepare())
97     {
98     errorStr = config.GetStrError();
99     return -1;
100     }
101
102 if (pthread_create(&thread, NULL, Run, this))
103     {
104     errorStr = "Cannot create thread.";
105     printfd(__FILE__, "Cannot create thread\n");
106     return -1;
107     }
108 errorStr = "";
109 return 0;
110 }
111 //-----------------------------------------------------------------------------
112 int STG_CONFIG::Stop()
113 {
114 if (!isRunning)
115     return 0;
116
117 config.Stop();
118
119 //5 seconds to thread stops itself
120 int i;
121 for (i = 0; i < 25; i++)
122     {
123     if (!isRunning)
124         break;
125
126     struct timespec ts = {0, 200000000};
127     nanosleep(&ts, NULL);
128     }
129
130 if (isRunning)
131     return -1;
132
133 return 0;
134 }
135 //-----------------------------------------------------------------------------
136 void * STG_CONFIG::Run(void * d)
137 {
138 sigset_t signalSet;
139 sigfillset(&signalSet);
140 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
141
142 STG_CONFIG * stgConf = (STG_CONFIG *)d;
143 stgConf->isRunning = true;
144
145 stgConf->config.Run();
146
147 stgConf->isRunning = false;
148 return NULL;
149 }
150 //-----------------------------------------------------------------------------