]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/rscript.h
Introduced logger for plugins.
[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 /*
23  $Revision: 1.16 $
24  $Date: 2010/09/10 06:43:59 $
25  $Author: faust $
26 */
27
28 #ifndef RSCRIPT_H
29 #define RSCRIPT_H
30
31 #include <pthread.h>
32
33 #include <cstring>
34 #include <string>
35 #include <list>
36 #include <map>
37 #include <functional>
38 #include <utility>
39
40 #include "stg/plugin.h"
41 #include "stg/store.h"
42 #include "stg/module_settings.h"
43 #include "stg/os_int.h"
44 #include "stg/notifer.h"
45 #include "stg/user_ips.h"
46 #include "stg/user.h"
47 #include "stg/users.h"
48 #include "stg/blowfish.h"
49 #include "stg/rs_packets.h"
50 #include "stg/logger.h"
51 #include "nrmap_parser.h"
52
53 extern "C" PLUGIN * GetPlugin();
54
55 #define RS_DEBUG (1)
56
57 #define MAX_SHORT_PCKT  (3)
58
59 class REMOTE_SCRIPT;
60 class SETTINGS;
61 //-----------------------------------------------------------------------------
62 class RS_ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
63 public:
64     RS_ADD_USER_NONIFIER(REMOTE_SCRIPT & r)
65         : NOTIFIER_BASE<USER_PTR>(), rs(r) {}
66     virtual ~RS_ADD_USER_NONIFIER() {}
67     void Notify(const USER_PTR & user);
68
69 private:
70     RS_ADD_USER_NONIFIER(const RS_ADD_USER_NONIFIER & rvalue);
71     RS_ADD_USER_NONIFIER & operator=(const RS_ADD_USER_NONIFIER);
72
73     REMOTE_SCRIPT & rs;
74 };
75 //-----------------------------------------------------------------------------
76 class RS_DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
77 public:
78     RS_DEL_USER_NONIFIER(REMOTE_SCRIPT & r)
79         : NOTIFIER_BASE<USER_PTR>(), rs(r) {}
80     virtual ~RS_DEL_USER_NONIFIER() {}
81     void Notify(const USER_PTR & user);
82
83 private:
84     RS_DEL_USER_NONIFIER(const RS_DEL_USER_NONIFIER & rvalue);
85     RS_DEL_USER_NONIFIER & operator=(const RS_DEL_USER_NONIFIER);
86
87     REMOTE_SCRIPT & rs;
88 };
89 //-----------------------------------------------------------------------------
90 template <typename T>
91 class RS_CHG_AFTER_NOTIFIER: public PROPERTY_NOTIFIER_BASE<T> {
92 public:
93     RS_CHG_AFTER_NOTIFIER(REMOTE_SCRIPT & r, USER_PTR u)
94         : PROPERTY_NOTIFIER_BASE<T>(), user(u), rs(r) {}
95     RS_CHG_AFTER_NOTIFIER(const RS_CHG_AFTER_NOTIFIER<T> & rvalue)
96         : PROPERTY_NOTIFIER_BASE<T>(), user(rvalue.user), rs(rvalue.rs) {}
97     void Notify(const T & oldValue, const T & newValue);
98     USER_PTR GetUser() { return user; }
99
100 private:
101     RS_CHG_AFTER_NOTIFIER<T> & operator=(const RS_CHG_AFTER_NOTIFIER<T> & rvalue);
102
103     USER_PTR user;
104     REMOTE_SCRIPT & rs;
105 };
106 //-----------------------------------------------------------------------------
107 struct RS_USER {
108     RS_USER()
109         : lastSentTime(0),
110           user(NULL),
111           routers(),
112           shortPacketsCount(0)
113     {}
114     RS_USER(const std::vector<uint32_t> & r, USER_PTR it)
115         : lastSentTime(0),
116           user(it),
117           routers(r),
118           shortPacketsCount(0)
119     {}
120     RS_USER(const RS_USER & rvalue)
121         : lastSentTime(rvalue.lastSentTime),
122           user(rvalue.user),
123           routers(rvalue.routers),
124           shortPacketsCount(rvalue.shortPacketsCount)
125     {}
126
127     RS_USER & operator=(const RS_USER & rvalue);
128
129     time_t lastSentTime;
130     USER_PTR user;
131     std::vector<uint32_t> routers;
132     int shortPacketsCount;
133 };
134 //-----------------------------------------------------------------------------
135 class RS_SETTINGS {
136 public:
137                         RS_SETTINGS();
138     virtual             ~RS_SETTINGS() {}
139     const std::string & GetStrError() const { return errorStr; }
140     int                 ParseSettings(const MODULE_SETTINGS & s);
141     int                 GetSendPeriod() const { return sendPeriod; }
142     int                 GetPort() const { return port; }
143     const std::vector<NET_ROUTER> & GetSubnetsMap() const { return netRouters; }
144     const std::vector<std::string> & GetUserParams() const { return userParams; }
145     const std::string & GetPassword() const { return password; }
146     const std::string & GetMapFileName() const { return subnetFile; }
147
148 private:
149     int                 sendPeriod;
150     uint16_t            port;
151     string              errorStr;
152     std::vector<NET_ROUTER> netRouters;
153     std::vector<string> userParams;
154     string              password;
155     string              subnetFile;
156 };
157 //-----------------------------------------------------------------------------
158 class REMOTE_SCRIPT : public PLUGIN {
159 public:
160                         REMOTE_SCRIPT();
161     virtual             ~REMOTE_SCRIPT();
162
163     void                SetUsers(USERS * u) { users = u; }
164     void                SetSettings(const MODULE_SETTINGS & s) { settings = s; }
165     int                 ParseSettings();
166
167     int                 Start();
168     int                 Stop();
169     int                 Reload();
170     bool                IsRunning() { return isRunning; }
171
172     const std::string & GetStrError() const { return errorStr; }
173     const std::string   GetVersion() const { return "Remote script v 0.3"; }
174     uint16_t            GetStartPosition() const { return 10; }
175     uint16_t            GetStopPosition() const { return 10; }
176
177     void                DelUser(USER_PTR u) { UnSetUserNotifier(u); }
178     void                AddUser(USER_PTR u) { SetUserNotifier(u); }
179
180     void                ChangedIP(USER_PTR u, uint32_t oldIP, uint32_t newIP);
181
182 private:
183     REMOTE_SCRIPT(const REMOTE_SCRIPT & rvalue);
184     REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rvalue);
185
186     static void *       Run(void *);
187     bool                PrepareNet();
188     bool                FinalizeNet();
189
190     bool                Send(uint32_t ip, RS_USER & rsu, bool forceDisconnect = false) const;
191     bool                SendDirect(uint32_t ip, RS_USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
192     bool                PreparePacket(char * buf, size_t bufSize, uint32_t ip, RS_USER &rsu, bool forceDisconnect = false) const;
193     void                PeriodicSend();
194
195     std::vector<uint32_t> IP2Routers(uint32_t ip);
196     bool                GetUsers();
197     std::string         GetUserParam(USER_PTR u, const std::string & paramName) const;
198
199     void                SetUserNotifier(USER_PTR u);
200     void                UnSetUserNotifier(USER_PTR u);
201
202     void                InitEncrypt(BLOWFISH_CTX * ctx, const string & password) const;
203     void                Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, size_t len8) const;
204
205     mutable BLOWFISH_CTX ctx;
206
207     std::list<RS_CHG_AFTER_NOTIFIER<uint32_t> > afterChgIPNotifierList;
208     std::map<uint32_t, RS_USER> authorizedUsers;
209
210     mutable std::string errorStr;
211     RS_SETTINGS         rsSettings;
212     MODULE_SETTINGS     settings;
213     int                 sendPeriod;
214     int                 halfPeriod;
215
216     bool                nonstop;
217     bool                isRunning;
218
219     USERS *             users;
220
221     std::vector<NET_ROUTER> netRouters;
222
223     pthread_t           thread;
224     pthread_mutex_t     mutex;
225
226     int                 sock;
227
228     RS_ADD_USER_NONIFIER onAddUserNotifier;
229     RS_DEL_USER_NONIFIER onDelUserNotifier;
230
231     PLUGIN_LOGGER       logger;
232
233     friend class UpdateRouter;
234     friend class DisconnectUser;
235 };
236 //-----------------------------------------------------------------------------
237 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, RS_USER> &, void> {
238     public:
239         DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
240         void operator()(std::pair<const uint32_t, RS_USER> & p)
241         {
242             rscript.Send(p.first, p.second, true);
243         }
244     private:
245         REMOTE_SCRIPT & rscript;
246 };
247 //-----------------------------------------------------------------------------
248 inline void RS_ADD_USER_NONIFIER::Notify(const USER_PTR & user)
249 {
250 rs.AddUser(user);
251 }
252 //-----------------------------------------------------------------------------
253 inline void RS_DEL_USER_NONIFIER::Notify(const USER_PTR & user)
254 {
255 rs.DelUser(user);
256 }
257 //-----------------------------------------------------------------------------
258
259 #endif