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