]> git.stg.codes - stg.git/blobdiff - projects/stargazer/traffcounter_impl.h
Fix old-style casts.
[stg.git] / projects / stargazer / traffcounter_impl.h
index eb4762392332c21740b799f0fa39d5544605532e..ed639f5c3ddcffb2fc1db1d669678a41a673add6 100644 (file)
  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
  */
 
- /*
- $Revision: 1.23 $
- $Date: 2010/04/22 12:57:46 $
- $Author: faust $
- */
-
-
-#ifndef TRAFFCOUNTER_IMPL_H
-#define TRAFFCOUNTER_IMPL_H
+#pragma once
 
-#include <pthread.h>
+#include "stg/traffcounter.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 "user_impl.h"
 
 #include <ctime>
 #include <list>
 #include <map>
 #include <string>
-
-#include "traffcounter.h"
-#include "os_int.h"
-#include "stg_logger.h"
-#include "raw_ip_packet.h"
-#include "users.h"
-#include "actions.h"
-#include "noncopyable.h"
-#include "eventloop.h"
+#include <mutex>
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wshadow"
+#include <jthread.hpp>
+#pragma GCC diagnostic pop
+#include <cstdint>
 
 #define PROTOMAX    (5)
 
-class TARIFFS;
+namespace STG
+{
+
+class UsersImpl;
 
 //-----------------------------------------------------------------------------
-struct RULE {
+struct Rule {
 uint32_t    ip;             // IP
 uint32_t    mask;           // Network mask
 uint16_t    port1;          // Min port
@@ -58,171 +57,184 @@ uint8_t     proto;          // Protocol
 uint32_t    dir;            // Direction
 };
 //-----------------------------------------------------------------------------
-struct PACKET_EXTRA_DATA {
-PACKET_EXTRA_DATA()
-    : flushTime(0),
-      updateTime(0),
-      userU(),
-      userUPresent(false),
-      userD(),
-      userDPresent(false),
-      dirU(DIR_NUM),
-      dirD(DIR_NUM),
-      lenU(0),
-      lenD(0)
-{}
-
-PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp)
-    : flushTime(pp.flushTime),
-      updateTime(pp.updateTime),
-      userU(pp.userU),
-      userUPresent(pp.userUPresent),
-      userD(pp.userD),
-      userDPresent(pp.userDPresent),
-      dirU(pp.dirU),
-      dirD(pp.dirD),
-      lenU(pp.lenU),
-      lenD(pp.lenD)
-{}
-
-time_t      flushTime;          // Last flush time
-time_t      updateTime;         // Last update time
-USER_PTR    userU;              // Uploader
-bool        userUPresent;       // Uploader is registered user
-USER_PTR    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
+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;
+class TraffCounterImpl;
 //-----------------------------------------------------------------------------
-class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
+class TRF_IP_BEFORE: public PropertyNotifierBase<uint32_t> {
 public:
-                    TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_PTR u)
-                        : PROPERTY_NOTIFIER_BASE<uint32_t>(),
-                          traffCnt(t),
-                          user(u)
-                    {}
-    void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
-    void            SetUser(USER_PTR u) { user = u; }
-    USER_PTR        GetUser() const { return user; }
+                TRF_IP_BEFORE(TraffCounterImpl & t, UserImpl * u)
+                    : PropertyNotifierBase<uint32_t>(),
+                      traffCnt(t),
+                      user(u)
+                {}
+                TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue)
+                    : PropertyNotifierBase<uint32_t>(),
+                      traffCnt(rvalue.traffCnt),
+                      user(rvalue.user)
+                {}
+    void        Notify(const uint32_t & oldValue, const uint32_t & newValue);
+    void        SetUser(UserImpl * u) { user = u; }
+    UserImpl * GetUser() const { return user; }
 
 private:
-    TRAFFCOUNTER_IMPL & traffCnt;
-    USER_PTR            user;
+    TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue);
+
+    TraffCounterImpl & traffCnt;
+    UserImpl * user;
 };
 //-----------------------------------------------------------------------------
-class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
+class TRF_IP_AFTER: public PropertyNotifierBase<uint32_t> {
 public:
-                    TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_PTR u)
-                        : PROPERTY_NOTIFIER_BASE<uint32_t>(),
-                          traffCnt(t),
-                          user(u)
-                    {}
-    void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
-    void            SetUser(USER_PTR u) { user = u; }
-    USER_PTR        GetUser() const { return user; }
+                TRF_IP_AFTER(TraffCounterImpl & t, UserImpl * u)
+                    : PropertyNotifierBase<uint32_t>(),
+                      traffCnt(t),
+                      user(u)
+                {}
+                TRF_IP_AFTER(const TRF_IP_AFTER & rvalue)
+                    : PropertyNotifierBase<uint32_t>(),
+                      traffCnt(rvalue.traffCnt),
+                      user(rvalue.user)
+                {}
+    void        Notify(const uint32_t & oldValue, const uint32_t & newValue);
+    void        SetUser(UserImpl * u) { user = u; }
+    UserImpl * GetUser() const { return user; }
 private:
-    TRAFFCOUNTER_IMPL & traffCnt;
-    USER_PTR            user;
+    TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue);
+
+    TraffCounterImpl & traffCnt;
+    UserImpl * user;
 };
+
+using UserImplPtr = UserImpl*;
 //-----------------------------------------------------------------------------
-class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
+class ADD_USER_NONIFIER: public NotifierBase<UserImplPtr> {
 public:
-            ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
-                NOTIFIER_BASE<USER_PTR>(),
+            explicit ADD_USER_NONIFIER(TraffCounterImpl & t) :
+                NotifierBase<UserImplPtr>(),
                 traffCnt(t)
             {}
     virtual ~ADD_USER_NONIFIER() {}
-    void    Notify(const USER_PTR & user);
+    void    Notify(const UserImplPtr & user);
+
 private:
-    TRAFFCOUNTER_IMPL & traffCnt;
+    ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
+    ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
+
+    TraffCounterImpl & traffCnt;
 };
 //-----------------------------------------------------------------------------
-class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
+class DEL_USER_NONIFIER: public NotifierBase<UserImplPtr> {
 public:
-            DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
-                NOTIFIER_BASE<USER_PTR>(),
+            explicit DEL_USER_NONIFIER(TraffCounterImpl & t) :
+                NotifierBase<UserImplPtr>(),
                 traffCnt(t)
             {}
     virtual ~DEL_USER_NONIFIER() {}
-    void    Notify(const USER_PTR & user);
+    void    Notify(const UserImplPtr & user);
+
 private:
-    TRAFFCOUNTER_IMPL & traffCnt;
+    DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
+    DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
+
+    TraffCounterImpl & traffCnt;
 };
 //-----------------------------------------------------------------------------
-class TRAFFCOUNTER : 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 * users, const TARIFFS * tariffs, const std::string & rulesFileName);
-    ~TRAFFCOUNTER_IMPL();
+class TraffCounterImpl : public TraffCounter {
+    friend class ADD_USER_NONIFIER;
+    friend class DEL_USER_NONIFIER;
+    friend class TRF_IP_BEFORE;
+    friend class TRF_IP_AFTER;
+    public:
+        TraffCounterImpl(UsersImpl * users, const std::string & rulesFileName);
+        ~TraffCounterImpl();
 
-    void        SetRulesFile(const std::string & rulesFileName);
+        int         Reload();
+        int         Start();
+        int         Stop();
 
-    int         Reload();
-    int         Start();
-    int         Stop();
+        void        process(const RawPacket & rawPacket) override;
+        void        SetMonitorDir(const std::string & monitorDir);
 
-    void        Process(const RAW_PACKET & rawPacket);
-    void        SetMonitorDir(const std::string & monitorDir);
+        size_t      rulesCount() const override { return rules.size(); }
 
-private:
-    bool        ParseAddress(const char * ta, RULE * rule) const;
-    uint32_t    CalcMask(uint32_t msk) const;
-    void        FreeRules();
-    void        PrintRule(RULE rule) const;
-    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_PTR user);
-    void        DelUser(uint32_t uip);
-    void        SetUserNotifiers(USER_PTR user);
-    void        UnSetUserNotifiers(USER_PTR user);
+        void        AddUser(UserImpl * user);
+        void        DelUser(uint32_t uip);
+        void        SetUserNotifiers(UserImpl * user);
+        void        UnSetUserNotifiers(UserImpl * user);
 
-    typedef std::list<RULE>::iterator rule_iter;
-    typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA>::iterator pp_iter;
-    typedef std::multimap<uint32_t, pp_iter>::iterator ip2p_iter;
-    typedef std::multimap<uint32_t, pp_iter>::const_iterator ip2p_citer;
+        typedef std::list<Rule>::iterator rule_iter;
 
-    std::list<RULE>          rules;
+        std::list<Rule>          rules;
 
-    std::map<RAW_PACKET, PACKET_EXTRA_DATA> packets; // Packets tree
+        typedef std::map<RawPacket, PacketExtraData> Packets;
+        typedef Packets::iterator pp_iter;
+        typedef std::multimap<uint32_t, pp_iter> Index;
+        typedef Index::iterator ip2p_iter;
+        typedef Index::const_iterator ip2p_citer;
 
-    std::multimap<uint32_t, pp_iter> ip2packets; // IP-to-Packet index
+        Packets packets; // Packets tree
 
-    std::string              dirName[DIR_NUM + 1];
+        Index ip2packets; // IP-to-Packet index
 
-    STG_LOGGER &             WriteServLog;
-    std::string              rulesFileName;
+        std::string              dirName[DIR_NUM + 1];
 
-    std::string              monitorDir;
-    bool                     monitoring;
+        Logger &             WriteServLog;
+        std::string              rulesFileName;
 
-    USERS *                  users;
+        std::string              monitorDir;
+        bool                     monitoring;
+        time_t                   touchTimeP;
 
-    bool                     running;
-    bool                     stopped;
-    pthread_mutex_t          mutex;
-    pthread_t                thread;
+        UsersImpl *             users;
 
-    std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
-    std::list<TRF_IP_AFTER>  ipAfterNotifiers;
+        bool                     stopped;
+        std::mutex               m_mutex;
+        std::jthread             m_thread;
 
-    ADD_USER_NONIFIER        addUserNotifier;
-    DEL_USER_NONIFIER        delUserNotifier;
+        std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
+        std::list<TRF_IP_AFTER>  ipAfterNotifiers;
+
+        ADD_USER_NONIFIER        addUserNotifier;
+        DEL_USER_NONIFIER        delUserNotifier;
 };
 //-----------------------------------------------------------------------------
 inline
@@ -232,7 +244,7 @@ void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
 if (!oldValue)
     return;
 
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, oldValue);
+EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::DelUser, oldValue);
 }
 //-----------------------------------------------------------------------------
 inline
@@ -242,20 +254,20 @@ void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
 if (!newValue)
     return;
 
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::AddUser, user);
+EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::AddUser, user);
 }
 //-----------------------------------------------------------------------------
 inline
-void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
+void ADD_USER_NONIFIER::Notify(const UserImplPtr & user)
 {
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::SetUserNotifiers, user);
+EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::SetUserNotifiers, user);
 }
 //-----------------------------------------------------------------------------
 inline
-void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
+void DEL_USER_NONIFIER::Notify(const UserImplPtr & user)
 {
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::UnSetUserNotifiers, user);
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, user->GetCurrIP());
+EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::UnSetUserNotifiers, user);
+EVENT_LOOP::instance().Enqueue(traffCnt, &TraffCounterImpl::DelUser, user->GetCurrIP());
 }
 //-----------------------------------------------------------------------------
-#endif //TRAFFCOUNTER_H
+}