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