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 "stg/notifer.h"
28 #include "user_impl.h"
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wshadow"
37 #include <jthread.hpp>
38 #pragma GCC diagnostic pop
48 //-----------------------------------------------------------------------------
51 uint32_t mask; // Network mask
52 uint16_t port1; // Min port
53 uint16_t port2; // Max port
54 uint8_t proto; // Protocol
55 uint32_t dir; // Direction
57 //-----------------------------------------------------------------------------
58 struct PacketExtraData {
72 time_t flushTime; // Last flush time
73 time_t updateTime; // Last update time
74 UserImpl * userU; // Uploader
75 bool userUPresent; // Uploader is registered user
76 UserImpl * userD; // Downloader
77 bool userDPresent; // Downloader is registered user
78 int dirU; // Upload direction
79 int dirD; // Download direction
80 uint32_t lenU; // Upload length
81 uint32_t lenD; // Download length
83 //-----------------------------------------------------------------------------
84 class TraffCounterImpl;
85 //-----------------------------------------------------------------------------
86 class TRF_IP_BEFORE: public PropertyNotifierBase<uint32_t> {
88 TRF_IP_BEFORE(TraffCounterImpl & t, UserImpl * u)
89 : PropertyNotifierBase<uint32_t>(),
93 TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue)
94 : PropertyNotifierBase<uint32_t>(),
95 traffCnt(rvalue.traffCnt),
98 void notify(const uint32_t & oldValue, const uint32_t & newValue) override;
99 void SetUser(UserImpl * u) { user = u; }
100 UserImpl * GetUser() const { return user; }
103 TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue);
105 TraffCounterImpl & traffCnt;
108 //-----------------------------------------------------------------------------
109 class TRF_IP_AFTER: public PropertyNotifierBase<uint32_t> {
111 TRF_IP_AFTER(TraffCounterImpl & t, UserImpl * u)
112 : PropertyNotifierBase<uint32_t>(),
116 TRF_IP_AFTER(const TRF_IP_AFTER & rvalue)
117 : PropertyNotifierBase<uint32_t>(),
118 traffCnt(rvalue.traffCnt),
121 void notify(const uint32_t & oldValue, const uint32_t & newValue) override;
122 void SetUser(UserImpl * u) { user = u; }
123 UserImpl * GetUser() const { return user; }
125 TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue);
127 TraffCounterImpl & traffCnt;
131 using UserImplPtr = UserImpl*;
132 //-----------------------------------------------------------------------------
133 class TraffCounterImpl : public TraffCounter {
134 friend class TRF_IP_BEFORE;
135 friend class TRF_IP_AFTER;
137 TraffCounterImpl(UsersImpl * users, const std::string & rulesFileName);
144 void process(const RawPacket & rawPacket) override;
145 void SetMonitorDir(const std::string & monitorDir);
147 size_t rulesCount() const override { return rules.size(); }
150 bool ParseAddress(const char * ta, Rule * rule) const;
151 uint32_t CalcMask(uint32_t msk) const;
153 bool ReadRules(bool test = false);
155 void Run(std::stop_token token);
157 void DeterminateDir(const RawPacket & packet,
158 int * dirU, // Direction for upload
159 int * dirD) const; // Direction for download
161 void FlushAndRemove();
163 void AddUser(UserImpl * user);
164 void DelUser(uint32_t uip);
165 void SetUserNotifiers(UserImpl * user);
166 void UnSetUserNotifiers(UserImpl * user);
168 typedef std::list<Rule>::iterator rule_iter;
170 std::list<Rule> rules;
172 typedef std::map<RawPacket, PacketExtraData> Packets;
173 typedef Packets::iterator pp_iter;
174 typedef std::multimap<uint32_t, pp_iter> Index;
175 typedef Index::iterator ip2p_iter;
176 typedef Index::const_iterator ip2p_citer;
178 Packets packets; // Packets tree
180 Index ip2packets; // IP-to-Packet index
182 std::string dirName[DIR_NUM + 1];
184 Logger & WriteServLog;
185 std::string rulesFileName;
187 std::string monitorDir;
195 std::jthread m_thread;
197 ScopedConnection m_onAddUserConn;
198 ScopedConnection m_onDelUserConn;
200 std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
201 std::list<TRF_IP_AFTER> ipAfterNotifiers;
205 //-----------------------------------------------------------------------------