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>
24 $Date: 2010/09/10 06:43:59 $
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"
47 #include "stg/users.h"
48 #include "stg/blowfish.h"
49 #include "stg/rs_packets.h"
50 #include "nrmap_parser.h"
52 extern "C" PLUGIN * GetPlugin();
56 #define MAX_SHORT_PCKT (3)
60 //-----------------------------------------------------------------------------
61 class RS_ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
63 RS_ADD_USER_NONIFIER(REMOTE_SCRIPT & r) : rs(r) {}
64 virtual ~RS_ADD_USER_NONIFIER() {}
65 void Notify(const USER_PTR & user);
70 //-----------------------------------------------------------------------------
71 class RS_DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
73 RS_DEL_USER_NONIFIER(REMOTE_SCRIPT & r) : rs(r) {}
74 virtual ~RS_DEL_USER_NONIFIER() {}
75 void Notify(const USER_PTR & user);
80 //-----------------------------------------------------------------------------
81 template <typename varParamType>
82 class RS_CHG_AFTER_NOTIFIER: public PROPERTY_NOTIFIER_BASE<varParamType> {
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; }
92 //-----------------------------------------------------------------------------
95 RS_USER(const std::vector<uint32_t> & r, USER_PTR it);
99 std::vector<uint32_t> routers;
100 int shortPacketsCount;
102 //-----------------------------------------------------------------------------
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; }
117 int ParseIntInRange(const std::string & str, int min, int max, int * val);
121 std::vector<NET_ROUTER> netRouters;
122 std::vector<string> userParams;
126 //-----------------------------------------------------------------------------
127 class REMOTE_SCRIPT : public PLUGIN {
130 virtual ~REMOTE_SCRIPT();
132 void SetUsers(USERS * u) { users = u; }
133 void SetTariffs(TARIFFS *) {}
134 void SetAdmins(ADMINS *) {}
135 void SetTraffcounter(TRAFFCOUNTER *) {}
136 void SetStore(STORE *) {}
137 void SetStgSettings(const SETTINGS *) {}
138 void SetSettings(const MODULE_SETTINGS & s) { settings = s; }
144 bool IsRunning() { return isRunning; }
146 const std::string & GetStrError() const { return errorStr; }
147 const std::string GetVersion() const { return "Remote script v 0.3"; }
148 uint16_t GetStartPosition() const { return 20; }
149 uint16_t GetStopPosition() const { return 20; }
151 void DelUser(USER_PTR u) { UnSetUserNotifier(u); }
152 void AddUser(USER_PTR u) { SetUserNotifier(u); }
154 void ChangedIP(USER_PTR u, uint32_t oldIP, uint32_t newIP);
157 static void * Run(void *);
161 bool Send(uint32_t ip, RS_USER & rsu, bool forceDisconnect = false) const;
162 bool SendDirect(uint32_t ip, RS_USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
163 bool PreparePacket(char * buf, size_t bufSize, uint32_t ip, RS_USER &rsu, bool forceDisconnect = false) const;
166 std::vector<uint32_t> IP2Routers(uint32_t ip);
168 std::string GetUserParam(USER_PTR u, const std::string & paramName) const;
170 void SetUserNotifier(USER_PTR u);
171 void UnSetUserNotifier(USER_PTR u);
173 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password) const;
174 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, size_t len8) const;
176 mutable BLOWFISH_CTX ctx;
178 std::list<RS_CHG_AFTER_NOTIFIER<uint32_t> > afterChgIPNotifierList;
179 std::map<uint32_t, RS_USER> authorizedUsers;
181 mutable std::string errorStr;
182 RS_SETTINGS rsSettings;
183 MODULE_SETTINGS settings;
192 std::vector<NET_ROUTER> netRouters;
195 pthread_mutex_t mutex;
199 RS_ADD_USER_NONIFIER onAddUserNotifier;
200 RS_DEL_USER_NONIFIER onDelUserNotifier;
202 friend class UpdateRouter;
203 friend class DisconnectUser;
205 //-----------------------------------------------------------------------------
206 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, RS_USER> &, void> {
208 DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
209 void operator()(std::pair<const uint32_t, RS_USER> & p)
211 rscript.Send(p.first, p.second, true);
214 REMOTE_SCRIPT & rscript;
216 //-----------------------------------------------------------------------------
217 inline void RS_ADD_USER_NONIFIER::Notify(const USER_PTR & user)
219 printfd(__FILE__, "ADD_USER_NONIFIER\n");
222 //-----------------------------------------------------------------------------
223 inline void RS_DEL_USER_NONIFIER::Notify(const USER_PTR & user)
225 printfd(__FILE__, "DEL_USER_NONIFIER\n");
228 //-----------------------------------------------------------------------------