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/noncopyable.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 ADD_USER_NONIFIER: public NotifierBase<UserImplPtr> {
135 explicit ADD_USER_NONIFIER(TraffCounterImpl & t) :
136 NotifierBase<UserImplPtr>(),
139 virtual ~ADD_USER_NONIFIER() {}
140 void notify(const UserImplPtr & user) override;
143 ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
144 ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
146 TraffCounterImpl & traffCnt;
148 //-----------------------------------------------------------------------------
149 class DEL_USER_NONIFIER: public NotifierBase<UserImplPtr> {
151 explicit DEL_USER_NONIFIER(TraffCounterImpl & t) :
152 NotifierBase<UserImplPtr>(),
155 virtual ~DEL_USER_NONIFIER() {}
156 void notify(const UserImplPtr & user) override;
159 DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
160 DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
162 TraffCounterImpl & traffCnt;
164 //-----------------------------------------------------------------------------
165 class TraffCounterImpl : public TraffCounter {
166 friend class ADD_USER_NONIFIER;
167 friend class DEL_USER_NONIFIER;
168 friend class TRF_IP_BEFORE;
169 friend class TRF_IP_AFTER;
171 TraffCounterImpl(UsersImpl * users, const std::string & rulesFileName);
178 void process(const RawPacket & rawPacket) override;
179 void SetMonitorDir(const std::string & monitorDir);
181 size_t rulesCount() const override { return rules.size(); }
184 bool ParseAddress(const char * ta, Rule * rule) const;
185 uint32_t CalcMask(uint32_t msk) const;
187 bool ReadRules(bool test = false);
189 void Run(std::stop_token token);
191 void DeterminateDir(const RawPacket & packet,
192 int * dirU, // Direction for upload
193 int * dirD) const; // Direction for download
195 void FlushAndRemove();
197 void AddUser(UserImpl * user);
198 void DelUser(uint32_t uip);
199 void SetUserNotifiers(UserImpl * user);
200 void UnSetUserNotifiers(UserImpl * user);
202 typedef std::list<Rule>::iterator rule_iter;
204 std::list<Rule> rules;
206 typedef std::map<RawPacket, PacketExtraData> Packets;
207 typedef Packets::iterator pp_iter;
208 typedef std::multimap<uint32_t, pp_iter> Index;
209 typedef Index::iterator ip2p_iter;
210 typedef Index::const_iterator ip2p_citer;
212 Packets packets; // Packets tree
214 Index ip2packets; // IP-to-Packet index
216 std::string dirName[DIR_NUM + 1];
218 Logger & WriteServLog;
219 std::string rulesFileName;
221 std::string monitorDir;
229 std::jthread m_thread;
231 std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
232 std::list<TRF_IP_AFTER> ipAfterNotifiers;
234 ADD_USER_NONIFIER addUserNotifier;
235 DEL_USER_NONIFIER delUserNotifier;
239 //-----------------------------------------------------------------------------