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>
23 #include "stg/traffcounter.h"
24 #include "stg/logger.h"
25 #include "stg/raw_ip_packet.h"
26 #include "stg/subscriptions.h"
27 #include "user_impl.h"
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wshadow"
37 #include <jthread.hpp>
38 #pragma GCC diagnostic pop
49 //-----------------------------------------------------------------------------
52 uint32_t mask; // Network mask
53 uint16_t port1; // Min port
54 uint16_t port2; // Max port
55 uint8_t proto; // Protocol
56 uint32_t dir; // Direction
58 //-----------------------------------------------------------------------------
59 struct PacketExtraData {
73 time_t flushTime; // Last flush time
74 time_t updateTime; // Last update time
75 UserImpl * userU; // Uploader
76 bool userUPresent; // Uploader is registered user
77 UserImpl * userD; // Downloader
78 bool userDPresent; // Downloader is registered user
79 int dirU; // Upload direction
80 int dirD; // Download direction
81 uint32_t lenU; // Upload length
82 uint32_t lenD; // Download length
84 //-----------------------------------------------------------------------------
85 class TraffCounterImpl : public TraffCounter {
87 TraffCounterImpl(UsersImpl * users, const std::string & rulesFileName);
94 void process(const RawPacket & rawPacket) override;
95 void SetMonitorDir(const std::string & monitorDir);
97 size_t rulesCount() const override { return rules.size(); }
100 bool ParseAddress(const char * ta, Rule * rule) const;
101 uint32_t CalcMask(uint32_t msk) const;
103 bool ReadRules(bool test = false);
105 void Run(std::stop_token token);
107 void DeterminateDir(const RawPacket & packet,
108 int * dirU, // Direction for upload
109 int * dirD) const; // Direction for download
111 void FlushAndRemove();
113 void AddUser(UserImpl * user);
114 void DelUser(uint32_t uip);
115 void SetUserNotifiers(UserImpl* user);
116 void UnSetUserNotifiers(UserImpl* user);
118 typedef std::list<Rule>::iterator rule_iter;
120 std::list<Rule> rules;
122 typedef std::map<RawPacket, PacketExtraData> Packets;
123 typedef Packets::iterator pp_iter;
124 typedef std::multimap<uint32_t, pp_iter> Index;
125 typedef Index::iterator ip2p_iter;
126 typedef Index::const_iterator ip2p_citer;
128 Packets packets; // Packets tree
130 Index ip2packets; // IP-to-Packet index
132 std::string dirName[DIR_NUM + 1];
134 Logger & WriteServLog;
135 std::string rulesFileName;
137 std::string monitorDir;
145 std::jthread m_thread;
147 ScopedConnection m_onAddUserConn;
148 ScopedConnection m_onDelUserConn;
150 using OnIPConns = std::tuple<int, ScopedConnection, ScopedConnection>;
151 std::vector<OnIPConns> m_onIPConns;
152 void beforeIPChange(uint32_t oldVal);
153 void afterIPChange(UserImpl* user, uint32_t newVal);
157 //-----------------------------------------------------------------------------