X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/a6680ce3d763763a6010c81c8a5a8f7a1ce052db..085c36723242f7bd1e0fd9bf49b950da6cb96039:/projects/stargazer/traffcounter_impl.h diff --git a/projects/stargazer/traffcounter_impl.h b/projects/stargazer/traffcounter_impl.h index 86d8c389..03c118df 100644 --- a/projects/stargazer/traffcounter_impl.h +++ b/projects/stargazer/traffcounter_impl.h @@ -18,39 +18,36 @@ * Author : Boris Mikhailenko */ - /* - $Revision: 1.23 $ - $Date: 2010/04/22 12:57:46 $ - $Author: faust $ - */ - - -#ifndef TRAFFCOUNTER_IMPL_H -#define TRAFFCOUNTER_IMPL_H +#pragma once #include "stg/traffcounter.h" -#include "stg/os_int.h" #include "stg/logger.h" #include "stg/raw_ip_packet.h" -#include "stg/noncopyable.h" -#include "stg/notifer.h" -#include "actions.h" -#include "eventloop.h" +#include "stg/subscriptions.h" #include "user_impl.h" -#include #include +#include +#include #include #include - -#include +#include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#include +#pragma GCC diagnostic pop +#include +#include #define PROTOMAX (5) -class USERS_IMPL; +namespace STG +{ + +class UsersImpl; //----------------------------------------------------------------------------- -struct RULE { +struct Rule { uint32_t ip; // IP uint32_t mask; // Network mask uint16_t port1; // Min port @@ -59,219 +56,102 @@ uint8_t proto; // Protocol uint32_t dir; // Direction }; //----------------------------------------------------------------------------- -struct PACKET_EXTRA_DATA { -PACKET_EXTRA_DATA() - : flushTime(0), - updateTime(0), - userU(NULL), - userUPresent(false), - userD(NULL), - userDPresent(false), - dirU(DIR_NUM), - dirD(DIR_NUM), - lenU(0), - lenD(0) -{} - -time_t flushTime; // Last flush time -time_t updateTime; // Last update time -USER_IMPL * userU; // Uploader -bool userUPresent; // Uploader is registered user -USER_IMPL * userD; // Downloader -bool userDPresent; // Downloader is registered user -int dirU; // Upload direction -int dirD; // Download direction -uint32_t lenU; // Upload length -uint32_t lenD; // Download length -}; -//----------------------------------------------------------------------------- -class TRAFFCOUNTER_IMPL; -//----------------------------------------------------------------------------- -class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE { -public: - TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_IMPL * u) - : PROPERTY_NOTIFIER_BASE(), - traffCnt(t), - user(u) - {} - TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue) - : PROPERTY_NOTIFIER_BASE(), - traffCnt(rvalue.traffCnt), - user(rvalue.user) - {} - void Notify(const uint32_t & oldValue, const uint32_t & newValue); - void SetUser(USER_IMPL * u) { user = u; } - USER_IMPL * GetUser() const { return user; } - -private: - TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue); - - TRAFFCOUNTER_IMPL & traffCnt; - USER_IMPL * user; -}; -//----------------------------------------------------------------------------- -class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE { -public: - TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_IMPL * u) - : PROPERTY_NOTIFIER_BASE(), - traffCnt(t), - user(u) - {} - TRF_IP_AFTER(const TRF_IP_AFTER & rvalue) - : PROPERTY_NOTIFIER_BASE(), - traffCnt(rvalue.traffCnt), - user(rvalue.user) - {} - void Notify(const uint32_t & oldValue, const uint32_t & newValue); - void SetUser(USER_IMPL * u) { user = u; } - USER_IMPL * GetUser() const { return user; } -private: - TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue); - - TRAFFCOUNTER_IMPL & traffCnt; - USER_IMPL * user; -}; -//----------------------------------------------------------------------------- -class ADD_USER_NONIFIER: public NOTIFIER_BASE { -public: - explicit ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) : - NOTIFIER_BASE(), - traffCnt(t) - {} - virtual ~ADD_USER_NONIFIER() {} - void Notify(const USER_IMPL_PTR & user); - -private: - ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue); - ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue); - - TRAFFCOUNTER_IMPL & traffCnt; -}; -//----------------------------------------------------------------------------- -class DEL_USER_NONIFIER: public NOTIFIER_BASE { -public: - explicit DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) : - NOTIFIER_BASE(), - traffCnt(t) - {} - virtual ~DEL_USER_NONIFIER() {} - void Notify(const USER_IMPL_PTR & user); - -private: - DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue); - DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue); - - TRAFFCOUNTER_IMPL & traffCnt; +struct PacketExtraData { + PacketExtraData() + : flushTime(0), + updateTime(0), + userU(NULL), + userUPresent(false), + userD(NULL), + userDPresent(false), + dirU(DIR_NUM), + dirD(DIR_NUM), + lenU(0), + lenD(0) + {} + + time_t flushTime; // Last flush time + time_t updateTime; // Last update time + UserImpl * userU; // Uploader + bool userUPresent; // Uploader is registered user + UserImpl * userD; // Downloader + bool userDPresent; // Downloader is registered user + int dirU; // Upload direction + int dirD; // Download direction + uint32_t lenU; // Upload length + uint32_t lenD; // Download length }; //----------------------------------------------------------------------------- -class TRAFFCOUNTER_IMPL : public TRAFFCOUNTER, private NONCOPYABLE { -friend class ADD_USER_NONIFIER; -friend class DEL_USER_NONIFIER; -friend class TRF_IP_BEFORE; -friend class TRF_IP_AFTER; -public: - TRAFFCOUNTER_IMPL(USERS_IMPL * users, const std::string & rulesFileName); - ~TRAFFCOUNTER_IMPL(); - - int Reload(); - int Start(); - int Stop(); +class TraffCounterImpl : public TraffCounter { + public: + TraffCounterImpl(UsersImpl * users, const std::string & rulesFileName); + ~TraffCounterImpl(); - void Process(const RAW_PACKET & rawPacket); - void SetMonitorDir(const std::string & monitorDir); + int Reload(); + int Start(); + int Stop(); - size_t RulesCount() const { return rules.size(); } + void process(const RawPacket & rawPacket) override; + void SetMonitorDir(const std::string & monitorDir); -private: - TRAFFCOUNTER_IMPL(const TRAFFCOUNTER_IMPL &); - TRAFFCOUNTER_IMPL & operator=(const TRAFFCOUNTER_IMPL &); + size_t rulesCount() const override { return rules.size(); } - bool ParseAddress(const char * ta, RULE * rule) const; - uint32_t CalcMask(uint32_t msk) const; - void FreeRules(); - bool ReadRules(bool test = false); + private: + bool ParseAddress(const char * ta, Rule * rule) const; + uint32_t CalcMask(uint32_t msk) const; + void FreeRules(); + bool ReadRules(bool test = false); - static void * Run(void * data); + void Run(std::stop_token token); - void DeterminateDir(const RAW_PACKET & packet, - int * dirU, // Direction for upload - int * dirD) const; // Direction for download + void DeterminateDir(const RawPacket & packet, + int * dirU, // Direction for upload + int * dirD) const; // Direction for download - void FlushAndRemove(); + void FlushAndRemove(); - void AddUser(USER_IMPL * user); - void DelUser(uint32_t uip); - void SetUserNotifiers(USER_IMPL * user); - void UnSetUserNotifiers(USER_IMPL * user); + void AddUser(UserImpl * user); + void DelUser(uint32_t uip); + void SetUserNotifiers(UserImpl* user); + void UnSetUserNotifiers(UserImpl* user); - typedef std::list::iterator rule_iter; + typedef std::list::iterator rule_iter; - std::list rules; + std::list rules; - typedef std::map Packets; - typedef Packets::iterator pp_iter; - typedef std::multimap Index; - typedef Index::iterator ip2p_iter; - typedef Index::const_iterator ip2p_citer; + typedef std::map Packets; + typedef Packets::iterator pp_iter; + typedef std::multimap Index; + typedef Index::iterator ip2p_iter; + typedef Index::const_iterator ip2p_citer; - Packets packets; // Packets tree + Packets packets; // Packets tree - Index ip2packets; // IP-to-Packet index + Index ip2packets; // IP-to-Packet index - std::string dirName[DIR_NUM + 1]; + std::string dirName[DIR_NUM + 1]; - STG_LOGGER & WriteServLog; - std::string rulesFileName; + Logger & WriteServLog; + std::string rulesFileName; - std::string monitorDir; - bool monitoring; - time_t touchTimeP; + std::string monitorDir; + bool monitoring; + time_t touchTimeP; - USERS_IMPL * users; + UsersImpl * users; - bool running; - bool stopped; - pthread_mutex_t mutex; - pthread_t thread; + bool stopped; + std::mutex m_mutex; + std::jthread m_thread; - std::list ipBeforeNotifiers; - std::list ipAfterNotifiers; + ScopedConnection m_onAddUserConn; + ScopedConnection m_onDelUserConn; - ADD_USER_NONIFIER addUserNotifier; - DEL_USER_NONIFIER delUserNotifier; + using OnIPConns = std::tuple; + std::vector m_onIPConns; + void beforeIPChange(uint32_t oldVal); + void afterIPChange(UserImpl* user, uint32_t newVal); }; -//----------------------------------------------------------------------------- -inline -void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &) -{ -// User changes his address. Remove old IP -if (!oldValue) - return; -EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, oldValue); -} -//----------------------------------------------------------------------------- -inline -void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue) -{ -// User changes his address. Add new IP -if (!newValue) - return; - -EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::AddUser, user); -} -//----------------------------------------------------------------------------- -inline -void ADD_USER_NONIFIER::Notify(const USER_IMPL_PTR & user) -{ -EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user); -} -//----------------------------------------------------------------------------- -inline -void DEL_USER_NONIFIER::Notify(const USER_IMPL_PTR & user) -{ -EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user); -EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP()); } //----------------------------------------------------------------------------- -#endif //TRAFFCOUNTER_H