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() {};
 
  61     virtual ~RS_ADD_USER_NONIFIER() {};
 
  63     void SetRemoteScript(REMOTE_SCRIPT * a) { rs = a; }
 
  64     void Notify(const user_iter & user);
 
  69 //-----------------------------------------------------------------------------
 
  70 class RS_DEL_USER_NONIFIER: public NOTIFIER_BASE<user_iter>
 
  73     RS_DEL_USER_NONIFIER() {};
 
  74     virtual ~RS_DEL_USER_NONIFIER() {};
 
  76     void SetRemoteScript(REMOTE_SCRIPT * a) { rs = a; }
 
  77     void Notify(const user_iter & user);
 
  82 //-----------------------------------------------------------------------------
 
  83 template <typename varParamType>
 
  84 class RS_CHG_AFTER_NOTIFIER: public PROPERTY_NOTIFIER_BASE<varParamType>
 
  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; }
 
  96 //-----------------------------------------------------------------------------
 
 100                       RS_USER(const std::vector<uint32_t> & r, user_iter it);
 
 103 std::vector<uint32_t> routers;
 
 104 int                   shortPacketsCount;
 
 106 //-----------------------------------------------------------------------------
 
 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; };
 
 122     int                 ParseIntInRange(const std::string & str, int min, int max, int * val);
 
 126     std::vector<NET_ROUTER> netRouters;
 
 127     std::vector<string>     userParams;
 
 131 //-----------------------------------------------------------------------------
 
 132 class REMOTE_SCRIPT : public BASE_PLUGIN
 
 136     virtual             ~REMOTE_SCRIPT();
 
 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; };
 
 150     bool                IsRunning() { return isRunning; };
 
 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; };
 
 157     void                DelUser(user_iter u) { UnSetUserNotifier(u); };
 
 158     void                AddUser(user_iter u) { SetUserNotifier(u); };
 
 160     void                ChangedIP(user_iter u, uint32_t oldIP, uint32_t newIP);
 
 163     static void *       Run(void *);
 
 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;
 
 172     std::vector<uint32_t> IP2Routers(uint32_t ip);
 
 174     std::string         GetUserParam(user_iter u, const std::string & paramName) const;
 
 176     void                SetUserNotifier(user_iter u);
 
 177     void                UnSetUserNotifier(user_iter u);
 
 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;
 
 182     mutable BLOWFISH_CTX ctx;
 
 184     std::list<RS_CHG_AFTER_NOTIFIER<uint32_t> > AfterChgIPNotifierList;
 
 185     std::map<uint32_t, RS_USER> authorizedUsers;
 
 187     mutable std::string errorStr;
 
 188     RS_SETTINGS         rsSettings;
 
 189     MODULE_SETTINGS     settings;
 
 198     std::vector<NET_ROUTER> netRouters;
 
 201     pthread_mutex_t     mutex;
 
 205     RS_ADD_USER_NONIFIER onAddUserNotifier;
 
 206     RS_DEL_USER_NONIFIER onDelUserNotifier;
 
 208     friend class UpdateRouter;
 
 209     friend class DisconnectUser;
 
 211 //-----------------------------------------------------------------------------
 
 212 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, RS_USER> &, void>
 
 215         DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {};
 
 216         void operator()(std::pair<const uint32_t, RS_USER> & p)
 
 218             rscript.Send(p.first, p.second, true);
 
 221         REMOTE_SCRIPT & rscript;
 
 223 //-----------------------------------------------------------------------------
 
 224 inline void RS_ADD_USER_NONIFIER::Notify(const user_iter & user)
 
 226 printfd(__FILE__, "ADD_USER_NONIFIER\n");
 
 229 //-----------------------------------------------------------------------------
 
 230 inline void RS_DEL_USER_NONIFIER::Notify(const user_iter & user)
 
 232 printfd(__FILE__, "DEL_USER_NONIFIER\n");
 
 235 //-----------------------------------------------------------------------------