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 $
39 #include "base_store.h"
43 #include "../../../user.h"
44 #include "../../../users.h"
46 #include "rs_packets.h"
47 #include "nrmap_parser.h"
49 extern "C" BASE_PLUGIN * GetPlugin();
53 #define MAX_SHORT_PCKT (3)
56 //-----------------------------------------------------------------------------
57 class RS_ADD_USER_NONIFIER: public NOTIFIER_BASE<user_iter>
60 RS_ADD_USER_NONIFIER(REMOTE_SCRIPT & r) : rs(r) {}
61 virtual ~RS_ADD_USER_NONIFIER() {}
62 void Notify(const user_iter & user);
67 //-----------------------------------------------------------------------------
68 class RS_DEL_USER_NONIFIER: public NOTIFIER_BASE<user_iter>
71 RS_DEL_USER_NONIFIER(REMOTE_SCRIPT & r) : rs(r) {}
72 virtual ~RS_DEL_USER_NONIFIER() {}
73 void Notify(const user_iter & user);
78 //-----------------------------------------------------------------------------
79 template <typename varParamType>
80 class RS_CHG_AFTER_NOTIFIER: public PROPERTY_NOTIFIER_BASE<varParamType>
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; }
91 //-----------------------------------------------------------------------------
95 RS_USER(const std::vector<uint32_t> & r, user_iter it);
99 std::vector<uint32_t> routers;
100 int shortPacketsCount;
102 //-----------------------------------------------------------------------------
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; };
118 int ParseIntInRange(const std::string & str, int min, int max, int * val);
122 std::vector<NET_ROUTER> netRouters;
123 std::vector<string> userParams;
127 //-----------------------------------------------------------------------------
128 class REMOTE_SCRIPT : public BASE_PLUGIN
132 virtual ~REMOTE_SCRIPT();
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; };
146 bool IsRunning() { return isRunning; };
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; };
153 void DelUser(user_iter u) { UnSetUserNotifier(u); };
154 void AddUser(user_iter u) { SetUserNotifier(u); };
156 void ChangedIP(user_iter u, uint32_t oldIP, uint32_t newIP);
159 static void * Run(void *);
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;
168 std::vector<uint32_t> IP2Routers(uint32_t ip);
170 std::string GetUserParam(user_iter u, const std::string & paramName) const;
172 void SetUserNotifier(user_iter u);
173 void UnSetUserNotifier(user_iter u);
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;
178 mutable BLOWFISH_CTX ctx;
180 std::list<RS_CHG_AFTER_NOTIFIER<uint32_t> > afterChgIPNotifierList;
181 std::map<uint32_t, RS_USER> authorizedUsers;
183 mutable std::string errorStr;
184 RS_SETTINGS rsSettings;
185 MODULE_SETTINGS settings;
194 std::vector<NET_ROUTER> netRouters;
197 pthread_mutex_t mutex;
201 RS_ADD_USER_NONIFIER onAddUserNotifier;
202 RS_DEL_USER_NONIFIER onDelUserNotifier;
204 friend class UpdateRouter;
205 friend class DisconnectUser;
207 //-----------------------------------------------------------------------------
208 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, RS_USER> &, void>
211 DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {};
212 void operator()(std::pair<const uint32_t, RS_USER> & p)
214 rscript.Send(p.first, p.second, true);
217 REMOTE_SCRIPT & rscript;
219 //-----------------------------------------------------------------------------
220 inline void RS_ADD_USER_NONIFIER::Notify(const user_iter & user)
222 printfd(__FILE__, "ADD_USER_NONIFIER\n");
225 //-----------------------------------------------------------------------------
226 inline void RS_DEL_USER_NONIFIER::Notify(const user_iter & user)
228 printfd(__FILE__, "DEL_USER_NONIFIER\n");
231 //-----------------------------------------------------------------------------