]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/rscript.h
Проведен легкий рефакторинг кода плагина для работы с rscriptd,
[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 <map>
36 #include <functional>
37 #include <utility>
38
39 #include "base_store.h"
40 #include "os_int.h"
41 #include "notifer.h"
42 #include "user_ips.h"
43 #include "../../../user.h"
44 #include "../../../users.h"
45 #include "blowfish.h"
46 #include "rs_packets.h"
47 #include "nrmap_parser.h"
48
49 extern "C" BASE_PLUGIN * GetPlugin();
50
51 #define RS_DEBUG (1)
52
53 #define MAX_SHORT_PCKT  (3)
54
55 class REMOTE_SCRIPT;
56 //-----------------------------------------------------------------------------
57 class RS_ADD_USER_NONIFIER: public NOTIFIER_BASE<user_iter>
58 {
59 public:
60     RS_ADD_USER_NONIFIER(REMOTE_SCRIPT & r) : rs(r) {}
61     virtual ~RS_ADD_USER_NONIFIER() {}
62     void Notify(const user_iter & user);
63
64 private:
65     REMOTE_SCRIPT & rs;
66 };
67 //-----------------------------------------------------------------------------
68 class RS_DEL_USER_NONIFIER: public NOTIFIER_BASE<user_iter>
69 {
70 public:
71     RS_DEL_USER_NONIFIER(REMOTE_SCRIPT & r) : rs(r) {}
72     virtual ~RS_DEL_USER_NONIFIER() {}
73     void Notify(const user_iter & user);
74
75 private:
76     REMOTE_SCRIPT & rs;
77 };
78 //-----------------------------------------------------------------------------
79 template <typename varParamType>
80 class RS_CHG_AFTER_NOTIFIER: public PROPERTY_NOTIFIER_BASE<varParamType>
81 {
82 public:
83     RS_CHG_AFTER_NOTIFIER(REMOTE_SCRIPT & r, user_iter u) : user(u), rs(r) {}
84     void Notify(const varParamType & oldValue, const varParamType & newValue);
85     user_iter GetUser() {return user; }
86
87 private:
88     user_iter   user;
89     REMOTE_SCRIPT & rs;
90 };
91 //-----------------------------------------------------------------------------
92 struct RS_USER
93 {
94 RS_USER();
95 RS_USER(const std::vector<uint32_t> & r, user_iter it);
96
97 time_t lastSentTime;
98 user_iter user;
99 std::vector<uint32_t> routers;
100 int shortPacketsCount;
101 };
102 //-----------------------------------------------------------------------------
103 class RS_SETTINGS
104 {
105 public:
106                         RS_SETTINGS();
107     virtual             ~RS_SETTINGS() {};
108     const std::string & GetStrError() const { return errorStr; };
109     int                 ParseSettings(const MODULE_SETTINGS & s);
110     int                 GetSendPeriod() const { return sendPeriod; };
111     int                 GetPort() const { return port; };
112     const std::vector<NET_ROUTER> & GetSubnetsMap() const { return netRouters; };
113     const std::vector<std::string> & GetUserParams() const { return userParams; };
114     const std::string & GetPassword() const { return password; };
115     const std::string & GetMapFileName() const { return subnetFile; };
116
117 private:
118     int                 ParseIntInRange(const std::string & str, int min, int max, int * val);
119     int                 sendPeriod;
120     uint16_t            port;
121     string              errorStr;
122     std::vector<NET_ROUTER> netRouters;
123     std::vector<string>     userParams;
124     string              password;
125     string              subnetFile;
126 };
127 //-----------------------------------------------------------------------------
128 class REMOTE_SCRIPT : public BASE_PLUGIN
129 {
130 public:
131                         REMOTE_SCRIPT();
132     virtual             ~REMOTE_SCRIPT();
133
134     void                SetUsers(USERS * u) { users = u; };
135     void                SetTariffs(TARIFFS *) {};
136     void                SetAdmins(ADMINS *) {};
137     void                SetTraffcounter(TRAFFCOUNTER *) {};
138     void                SetStore(BASE_STORE *) {};
139     void                SetStgSettings(const SETTINGS *) {};
140     void                SetSettings(const MODULE_SETTINGS & s) { settings = s; };
141     int                 ParseSettings();
142
143     int                 Start();
144     int                 Stop();
145     int                 Reload();
146     bool                IsRunning() { return isRunning; };
147
148     const std::string & GetStrError() const { return errorStr; };
149     const std::string   GetVersion() const { return "Remote script v 0.3"; };
150     uint16_t            GetStartPosition() const { return 20; };
151     uint16_t            GetStopPosition() const { return 20; };
152
153     void                DelUser(user_iter u) { UnSetUserNotifier(u); };
154     void                AddUser(user_iter u) { SetUserNotifier(u); };
155
156     void                ChangedIP(user_iter u, uint32_t oldIP, uint32_t newIP);
157
158 private:
159     static void *       Run(void *);
160     bool                PrepareNet();
161     bool                FinalizeNet();
162
163     bool                Send(uint32_t ip, RS_USER & rsu, bool forceDisconnect = false) const;
164     bool                SendDirect(uint32_t ip, RS_USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
165     bool                PreparePacket(char * buf, size_t bufSize, uint32_t ip, RS_USER &rsu, bool forceDisconnect = false) const;
166     void                PeriodicSend();
167
168     std::vector<uint32_t> IP2Routers(uint32_t ip);
169     bool                GetUsers();
170     std::string         GetUserParam(user_iter u, const std::string & paramName) const;
171
172     void                SetUserNotifier(user_iter u);
173     void                UnSetUserNotifier(user_iter u);
174
175     void                InitEncrypt(BLOWFISH_CTX * ctx, const string & password) const;
176     void                Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, size_t len8) const;
177
178     mutable BLOWFISH_CTX ctx;
179
180     std::list<RS_CHG_AFTER_NOTIFIER<uint32_t> > afterChgIPNotifierList;
181     std::map<uint32_t, RS_USER> authorizedUsers;
182
183     mutable std::string errorStr;
184     RS_SETTINGS         rsSettings;
185     MODULE_SETTINGS     settings;
186     int                 sendPeriod;
187     int                 halfPeriod;
188
189     bool                nonstop;
190     bool                isRunning;
191
192     USERS *             users;
193
194     std::vector<NET_ROUTER> netRouters;
195
196     pthread_t           thread;
197     pthread_mutex_t     mutex;
198
199     int                 sock;
200
201     RS_ADD_USER_NONIFIER onAddUserNotifier;
202     RS_DEL_USER_NONIFIER onDelUserNotifier;
203
204     friend class UpdateRouter;
205     friend class DisconnectUser;
206 };
207 //-----------------------------------------------------------------------------
208 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, RS_USER> &, void>
209 {
210     public:
211         DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {};
212         void operator()(std::pair<const uint32_t, RS_USER> & p)
213         {
214             rscript.Send(p.first, p.second, true);
215         }
216     private:
217         REMOTE_SCRIPT & rscript;
218 };
219 //-----------------------------------------------------------------------------
220 inline void RS_ADD_USER_NONIFIER::Notify(const user_iter & user)
221 {
222 printfd(__FILE__, "ADD_USER_NONIFIER\n");
223 rs.AddUser(user);
224 }
225 //-----------------------------------------------------------------------------
226 inline void RS_DEL_USER_NONIFIER::Notify(const user_iter & user)
227 {
228 printfd(__FILE__, "DEL_USER_NONIFIER\n");
229 rs.DelUser(user);
230 }
231 //-----------------------------------------------------------------------------
232
233 #endif