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