]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/rscript.h
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / plugins / other / rscript / rscript.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #ifndef RSCRIPT_H
23 #define RSCRIPT_H
24
25 #include "stg/plugin.h"
26 #include "stg/module_settings.h"
27 #include "stg/os_int.h"
28 #include "stg/notifer.h"
29 #include "stg/user.h"
30 #include "stg/blowfish.h"
31 #include "stg/rs_packets.h"
32 #include "stg/logger.h"
33
34 #include "nrmap_parser.h"
35
36 #include <string>
37 #include <list>
38 #include <map>
39 #include <functional>
40 #include <utility>
41
42 #include <pthread.h>
43
44 extern "C" PLUGIN * GetPlugin();
45
46 #define RS_DEBUG (1)
47
48 #define MAX_SHORT_PCKT  (3)
49
50 class SETTINGS;
51 class USERS;
52
53 namespace RS
54 {
55
56 class REMOTE_SCRIPT;
57 class UpdateRouter;
58 class DisconnectUser;
59
60 //-----------------------------------------------------------------------------
61 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
62 public:
63     explicit ADD_USER_NONIFIER(REMOTE_SCRIPT & r)
64         : NOTIFIER_BASE<USER_PTR>(), rs(r) {}
65     virtual ~ADD_USER_NONIFIER() {}
66     void Notify(const USER_PTR & user);
67
68 private:
69     ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rhs);
70     ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER);
71
72     REMOTE_SCRIPT & rs;
73 };
74 //-----------------------------------------------------------------------------
75 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
76 public:
77     explicit DEL_USER_NONIFIER(REMOTE_SCRIPT & r)
78         : NOTIFIER_BASE<USER_PTR>(), rs(r) {}
79     virtual ~DEL_USER_NONIFIER() {}
80     void Notify(const USER_PTR & user);
81
82 private:
83     DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rhs);
84     DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER);
85
86     REMOTE_SCRIPT & rs;
87 };
88 //-----------------------------------------------------------------------------
89 class IP_NOTIFIER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
90 public:
91     IP_NOTIFIER(REMOTE_SCRIPT & r, USER_PTR u)
92         : PROPERTY_NOTIFIER_BASE<uint32_t>(), user(u), rs(r) { user->AddCurrIPAfterNotifier(this); }
93     IP_NOTIFIER(const IP_NOTIFIER & rhs)
94         : PROPERTY_NOTIFIER_BASE<uint32_t>(), user(rhs.user), rs(rhs.rs) { user->AddCurrIPAfterNotifier(this); }
95     ~IP_NOTIFIER() { user->DelCurrIPAfterNotifier(this); }
96
97     IP_NOTIFIER & operator=(const IP_NOTIFIER & rhs)
98     {
99         user->DelCurrIPAfterNotifier(this);
100         user = rhs.user;
101         user->AddCurrIPAfterNotifier(this);
102         return *this;
103     }
104
105     void Notify(const uint32_t & oldValue, const uint32_t & newValue);
106     USER_PTR GetUser() const { return user; }
107
108 private:
109
110     USER_PTR user;
111     REMOTE_SCRIPT & rs;
112 };
113 //-----------------------------------------------------------------------------
114 class CONNECTED_NOTIFIER: public PROPERTY_NOTIFIER_BASE<bool> {
115 public:
116     CONNECTED_NOTIFIER(REMOTE_SCRIPT & r, USER_PTR u)
117         : PROPERTY_NOTIFIER_BASE<bool>(), user(u), rs(r) { user->AddConnectedAfterNotifier(this); }
118     CONNECTED_NOTIFIER(const CONNECTED_NOTIFIER & rhs)
119         : PROPERTY_NOTIFIER_BASE<bool>(), user(rhs.user), rs(rhs.rs) { user->AddConnectedAfterNotifier(this); }
120     ~CONNECTED_NOTIFIER() { user->DelConnectedAfterNotifier(this); }
121
122     CONNECTED_NOTIFIER & operator=(const CONNECTED_NOTIFIER & rhs)
123     {
124         user->DelConnectedAfterNotifier(this);
125         user = rhs.user;
126         user->AddConnectedAfterNotifier(this);
127         return *this;
128     }
129
130     void Notify(const bool & oldValue, const bool & newValue);
131     USER_PTR GetUser() const { return user; }
132
133 private:
134
135     USER_PTR user;
136     REMOTE_SCRIPT & rs;
137 };
138 //-----------------------------------------------------------------------------
139 struct USER {
140     USER(const std::vector<uint32_t> & r, USER_PTR it)
141         : lastSentTime(0),
142           user(it),
143           routers(r),
144           shortPacketsCount(0),
145           ip(user->GetCurrIP())
146     {}
147
148     time_t lastSentTime;
149     USER_PTR user;
150     std::vector<uint32_t> routers;
151     int shortPacketsCount;
152     uint32_t ip;
153 };
154 //-----------------------------------------------------------------------------
155 class SETTINGS {
156 public:
157                         SETTINGS();
158     virtual             ~SETTINGS() {}
159     const std::string & GetStrError() const { return errorStr; }
160     int                 ParseSettings(const MODULE_SETTINGS & s);
161     int                 GetSendPeriod() const { return sendPeriod; }
162     uint16_t            GetPort() const { return port; }
163     const std::vector<NET_ROUTER> & GetSubnetsMap() const { return netRouters; }
164     const std::vector<std::string> & GetUserParams() const { return userParams; }
165     const std::string & GetPassword() const { return password; }
166     const std::string & GetMapFileName() const { return subnetFile; }
167
168 private:
169     int                 sendPeriod;
170     uint16_t            port;
171     std::string         errorStr;
172     std::vector<NET_ROUTER> netRouters;
173     std::vector<std::string> userParams;
174     std::string         password;
175     std::string         subnetFile;
176 };
177 //-----------------------------------------------------------------------------
178 class REMOTE_SCRIPT : public PLUGIN {
179 public:
180                         REMOTE_SCRIPT();
181     virtual             ~REMOTE_SCRIPT();
182
183     void                SetUsers(USERS * u) { users = u; }
184     void                SetSettings(const MODULE_SETTINGS & s) { settings = s; }
185     int                 ParseSettings();
186
187     int                 Start();
188     int                 Stop();
189     int                 Reload(const MODULE_SETTINGS & ms);
190     bool                IsRunning() { return isRunning; }
191
192     const std::string & GetStrError() const { return errorStr; }
193     std::string         GetVersion() const { return "Remote script v 0.3"; }
194     uint16_t            GetStartPosition() const { return 10; }
195     uint16_t            GetStopPosition() const { return 10; }
196
197     void                DelUser(USER_PTR u) { UnSetUserNotifiers(u); }
198     void                AddUser(USER_PTR u) { SetUserNotifiers(u); }
199
200     void                AddRSU(USER_PTR user);
201     void                DelRSU(USER_PTR user);
202
203 private:
204     REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
205     REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
206
207     static void *       Run(void *);
208     bool                PrepareNet();
209     bool                FinalizeNet();
210
211     bool                Send(USER & rsu, bool forceDisconnect = false) const;
212     bool                SendDirect(USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
213     bool                PreparePacket(char * buf, size_t bufSize, USER &rsu, bool forceDisconnect = false) const;
214     void                PeriodicSend();
215
216     std::vector<uint32_t> IP2Routers(uint32_t ip);
217     bool                GetUsers();
218
219     void                SetUserNotifiers(USER_PTR u);
220     void                UnSetUserNotifiers(USER_PTR u);
221
222     void                InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password) const;
223     void                Encrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, size_t len8) const;
224
225     mutable BLOWFISH_CTX ctx;
226
227     std::list<IP_NOTIFIER> ipNotifierList;
228     std::list<CONNECTED_NOTIFIER> connNotifierList;
229     std::map<uint32_t, USER> authorizedUsers;
230
231     mutable std::string errorStr;
232     SETTINGS         rsSettings;
233     MODULE_SETTINGS     settings;
234     int                 sendPeriod;
235     int                 halfPeriod;
236
237     bool                nonstop;
238     bool                isRunning;
239
240     USERS *             users;
241
242     std::vector<NET_ROUTER> netRouters;
243
244     pthread_t           thread;
245     pthread_mutex_t     mutex;
246
247     int                 sock;
248
249     ADD_USER_NONIFIER onAddUserNotifier;
250     DEL_USER_NONIFIER onDelUserNotifier;
251
252     PLUGIN_LOGGER       logger;
253
254     friend class RS::UpdateRouter;
255     friend class RS::DisconnectUser;
256     friend class RS::CONNECTED_NOTIFIER;
257 };
258 //-----------------------------------------------------------------------------
259 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER> &, void> {
260     public:
261         explicit DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
262         void operator()(std::pair<const uint32_t, USER> & p)
263         {
264             rscript.Send(p.second, true);
265         }
266     private:
267         REMOTE_SCRIPT & rscript;
268 };
269 //-----------------------------------------------------------------------------
270 inline void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
271 {
272 rs.AddUser(user);
273 }
274 //-----------------------------------------------------------------------------
275 inline void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
276 {
277 rs.DelUser(user);
278 }
279 //-----------------------------------------------------------------------------
280
281 } // namespace RS
282
283 #endif