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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
33 #include "stg/plugin.h"
34 #include "stg/module_settings.h"
35 #include "stg/os_int.h"
36 #include "stg/notifer.h"
38 #include "stg/blowfish.h"
39 #include "stg/rs_packets.h"
40 #include "stg/logger.h"
42 #include "nrmap_parser.h"
44 extern "C" PLUGIN * GetPlugin();
48 #define MAX_SHORT_PCKT (3)
60 //-----------------------------------------------------------------------------
61 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
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);
69 ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rhs);
70 ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER);
74 //-----------------------------------------------------------------------------
75 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
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);
83 DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rhs);
84 DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER);
88 //-----------------------------------------------------------------------------
89 class IP_NOTIFIER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
91 IP_NOTIFIER(REMOTE_SCRIPT & r, USER_PTR u)
92 : PROPERTY_NOTIFIER_BASE<uint32_t>(), user(u), rs(r) { user->AddCurrIPAfterNotifier(this); }
93 IP_NOTIFIER(const IP_NOTIFIER & rhs)
94 : PROPERTY_NOTIFIER_BASE<uint32_t>(), user(rhs.user), rs(rhs.rs) { user->AddCurrIPAfterNotifier(this); }
95 ~IP_NOTIFIER() { user->DelCurrIPAfterNotifier(this); }
97 IP_NOTIFIER & operator=(const IP_NOTIFIER & rhs)
99 user->DelCurrIPAfterNotifier(this);
101 user->AddCurrIPAfterNotifier(this);
105 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
106 USER_PTR GetUser() const { return user; }
113 //-----------------------------------------------------------------------------
114 class CONNECTED_NOTIFIER: public PROPERTY_NOTIFIER_BASE<bool> {
116 CONNECTED_NOTIFIER(REMOTE_SCRIPT & r, USER_PTR u)
117 : PROPERTY_NOTIFIER_BASE<bool>(), user(u), rs(r) { user->AddConnectedAfterNotifier(this); }
118 CONNECTED_NOTIFIER(const CONNECTED_NOTIFIER & rhs)
119 : PROPERTY_NOTIFIER_BASE<bool>(), user(rhs.user), rs(rhs.rs) { user->AddConnectedAfterNotifier(this); }
120 ~CONNECTED_NOTIFIER() { user->DelConnectedAfterNotifier(this); }
122 CONNECTED_NOTIFIER & operator=(const CONNECTED_NOTIFIER & rhs)
124 user->DelConnectedAfterNotifier(this);
126 user->AddConnectedAfterNotifier(this);
130 void Notify(const bool & oldValue, const bool & newValue);
131 USER_PTR GetUser() const { return user; }
138 //-----------------------------------------------------------------------------
140 USER(const std::vector<uint32_t> & r, USER_PTR it)
143 ip(user->GetCurrIP())
148 std::vector<uint32_t> routers;
149 int shortPacketsCount;
152 //-----------------------------------------------------------------------------
156 virtual ~SETTINGS() {}
157 const std::string & GetStrError() const { return errorStr; }
158 int ParseSettings(const MODULE_SETTINGS & s);
159 int GetSendPeriod() const { return sendPeriod; }
160 uint16_t GetPort() const { return port; }
161 const std::vector<NET_ROUTER> & GetSubnetsMap() const { return netRouters; }
162 const std::vector<std::string> & GetUserParams() const { return userParams; }
163 const std::string & GetPassword() const { return password; }
164 const std::string & GetMapFileName() const { return subnetFile; }
169 std::string errorStr;
170 std::vector<NET_ROUTER> netRouters;
171 std::vector<std::string> userParams;
172 std::string password;
173 std::string subnetFile;
175 //-----------------------------------------------------------------------------
176 class REMOTE_SCRIPT : public PLUGIN {
179 virtual ~REMOTE_SCRIPT();
181 void SetUsers(USERS * u) { users = u; }
182 void SetSettings(const MODULE_SETTINGS & s) { settings = s; }
188 bool IsRunning() { return isRunning; }
190 const std::string & GetStrError() const { return errorStr; }
191 std::string GetVersion() const { return "Remote script v 0.3"; }
192 uint16_t GetStartPosition() const { return 10; }
193 uint16_t GetStopPosition() const { return 10; }
195 void DelUser(USER_PTR u) { UnSetUserNotifiers(u); }
196 void AddUser(USER_PTR u) { SetUserNotifiers(u); }
198 void AddRSU(USER_PTR user);
199 void DelRSU(USER_PTR user);
202 REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
203 REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
205 static void * Run(void *);
209 bool Send(USER & rsu, bool forceDisconnect = false) const;
210 bool SendDirect(USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
211 bool PreparePacket(char * buf, size_t bufSize, USER &rsu, bool forceDisconnect = false) const;
214 std::vector<uint32_t> IP2Routers(uint32_t ip);
216 // std::string GetUserParam(USER_PTR u, const std::string & paramName) const;
218 void SetUserNotifiers(USER_PTR u);
219 void UnSetUserNotifiers(USER_PTR u);
221 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password) const;
222 void Encrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, size_t len8) const;
224 mutable BLOWFISH_CTX ctx;
226 std::list<IP_NOTIFIER> ipNotifierList;
227 std::list<CONNECTED_NOTIFIER> connNotifierList;
228 std::map<uint32_t, USER> authorizedUsers;
230 mutable std::string errorStr;
232 MODULE_SETTINGS settings;
241 std::vector<NET_ROUTER> netRouters;
244 pthread_mutex_t mutex;
248 ADD_USER_NONIFIER onAddUserNotifier;
249 DEL_USER_NONIFIER onDelUserNotifier;
251 PLUGIN_LOGGER logger;
253 friend class RS::UpdateRouter;
254 friend class RS::DisconnectUser;
255 friend class RS::CONNECTED_NOTIFIER;
257 //-----------------------------------------------------------------------------
258 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER> &, void> {
260 DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
261 void operator()(std::pair<const uint32_t, USER> & p)
263 rscript.Send(p.second, true);
266 REMOTE_SCRIPT & rscript;
268 //-----------------------------------------------------------------------------
269 inline void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
273 //-----------------------------------------------------------------------------
274 inline void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
278 //-----------------------------------------------------------------------------