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