]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/rscript.h
Use "connected" subscription for detecting connection and disconnection.
[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 <pthread.h>
26
27 #include <string>
28 #include <list>
29 #include <map>
30 #include <functional>
31 #include <utility>
32
33 #include "stg/plugin.h"
34 #include "stg/module_settings.h"
35 #include "stg/os_int.h"
36 #include "stg/notifer.h"
37 #include "stg/user.h"
38 #include "stg/blowfish.h"
39 #include "stg/rs_packets.h"
40 #include "stg/logger.h"
41
42 #include "nrmap_parser.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     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     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) {}
93     IP_NOTIFIER(const IP_NOTIFIER & rhs)
94         : PROPERTY_NOTIFIER_BASE<uint32_t>(), user(rhs.user), rs(rhs.rs) {}
95     void Notify(const uint32_t & oldValue, const uint32_t & newValue);
96     USER_PTR GetUser() { return user; }
97
98 private:
99     IP_NOTIFIER & operator=(const IP_NOTIFIER & rhs);
100
101     USER_PTR user;
102     REMOTE_SCRIPT & rs;
103 };
104 //-----------------------------------------------------------------------------
105 class USER;
106 //-----------------------------------------------------------------------------
107 class CONNECTED_NOTIFIER: public PROPERTY_NOTIFIER_BASE<bool> {
108 public:
109     CONNECTED_NOTIFIER(REMOTE_SCRIPT & r, USER & u)
110         : PROPERTY_NOTIFIER_BASE<bool>(), user(u), rs(r) {}
111     CONNECTED_NOTIFIER(const CONNECTED_NOTIFIER & rhs)
112         : PROPERTY_NOTIFIER_BASE<bool>(), user(rhs.user), rs(rhs.rs) {}
113     void Notify(const bool & oldValue, const bool & newValue);
114
115 private:
116     CONNECTED_NOTIFIER & operator=(const CONNECTED_NOTIFIER & rhs);
117
118     USER & user;
119     REMOTE_SCRIPT & rs;
120 };
121 //-----------------------------------------------------------------------------
122 struct USER {
123     USER(const std::vector<uint32_t> & r, USER_PTR it, REMOTE_SCRIPT & rs);
124     USER(const USER & rhs);
125     ~USER();
126
127     time_t lastSentTime;
128     USER_PTR user;
129     std::vector<uint32_t> routers;
130     int shortPacketsCount;
131     uint32_t ip;
132     CONNECTED_NOTIFIER notifier;
133 };
134 //-----------------------------------------------------------------------------
135 class SETTINGS {
136 public:
137                         SETTINGS();
138     virtual             ~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 & rhs);
184     REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
185
186     static void *       Run(void *);
187     bool                PrepareNet();
188     bool                FinalizeNet();
189
190     bool                Send(uint32_t ip, USER & rsu, bool forceDisconnect = false) const;
191     bool                SendDirect(uint32_t ip, USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
192     bool                PreparePacket(char * buf, size_t bufSize, uint32_t ip, 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<IP_NOTIFIER> ipNotifierList;
208     std::map<uint32_t, USER> authorizedUsers;
209
210     mutable std::string errorStr;
211     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     ADD_USER_NONIFIER onAddUserNotifier;
229     DEL_USER_NONIFIER onDelUserNotifier;
230
231     PLUGIN_LOGGER       logger;
232
233     friend class RS::UpdateRouter;
234     friend class RS::DisconnectUser;
235     friend class RS::CONNECTED_NOTIFIER;
236 };
237 //-----------------------------------------------------------------------------
238 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER> &, void> {
239     public:
240         DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
241         void operator()(std::pair<const uint32_t, USER> & p)
242         {
243             rscript.Send(p.first, p.second, true);
244         }
245     private:
246         REMOTE_SCRIPT & rscript;
247 };
248 //-----------------------------------------------------------------------------
249 inline void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
250 {
251 rs.AddUser(user);
252 }
253 //-----------------------------------------------------------------------------
254 inline void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
255 {
256 rs.DelUser(user);
257 }
258 //-----------------------------------------------------------------------------
259
260 } // namespace RS
261
262 #endif