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"
29 #include "eventloop.h"
30 #include "user_impl.h"
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wshadow"
39 #include <jthread.hpp>
40 #pragma GCC diagnostic pop
50 //-----------------------------------------------------------------------------
53 uint32_t mask; // Network mask
54 uint16_t port1; // Min port
55 uint16_t port2; // Max port
56 uint8_t proto; // Protocol
57 uint32_t dir; // Direction
59 //-----------------------------------------------------------------------------
60 struct PacketExtraData {
74 time_t flushTime; // Last flush time
75 time_t updateTime; // Last update time
76 UserImpl * userU; // Uploader
77 bool userUPresent; // Uploader is registered user
78 UserImpl * userD; // Downloader
79 bool userDPresent; // Downloader is registered user
80 int dirU; // Upload direction
81 int dirD; // Download direction
82 uint32_t lenU; // Upload length
83 uint32_t lenD; // Download length
85 //-----------------------------------------------------------------------------
86 class TraffCounterImpl;
87 //-----------------------------------------------------------------------------
88 class TRF_IP_BEFORE: public PropertyNotifierBase<uint32_t> {
90 TRF_IP_BEFORE(TraffCounterImpl & t, UserImpl * u)
91 : PropertyNotifierBase<uint32_t>(),
95 TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue)
96 : PropertyNotifierBase<uint32_t>(),
97 traffCnt(rvalue.traffCnt),
100 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
101 void SetUser(UserImpl * u) { user = u; }
102 UserImpl * GetUser() const { return user; }
105 TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue);
107 TraffCounterImpl & traffCnt;
110 //-----------------------------------------------------------------------------
111 class TRF_IP_AFTER: public PropertyNotifierBase<uint32_t> {
113 TRF_IP_AFTER(TraffCounterImpl & t, UserImpl * u)
114 : PropertyNotifierBase<uint32_t>(),
118 TRF_IP_AFTER(const TRF_IP_AFTER & rvalue)
119 : PropertyNotifierBase<uint32_t>(),
120 traffCnt(rvalue.traffCnt),
123 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
124 void SetUser(UserImpl * u) { user = u; }
125 UserImpl * GetUser() const { return user; }
127 TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue);
129 TraffCounterImpl & traffCnt;
133 using UserImplPtr = UserImpl*;
134 //-----------------------------------------------------------------------------
135 class ADD_USER_NONIFIER: public NotifierBase<UserImplPtr> {
137 explicit ADD_USER_NONIFIER(TraffCounterImpl & t) :
138 NotifierBase<UserImplPtr>(),
141 virtual ~ADD_USER_NONIFIER() {}
142 void Notify(const UserImplPtr & user);
145 ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
146 ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
148 TraffCounterImpl & traffCnt;
150 //-----------------------------------------------------------------------------
151 class DEL_USER_NONIFIER: public NotifierBase<UserImplPtr> {
153 explicit DEL_USER_NONIFIER(TraffCounterImpl & t) :
154 NotifierBase<UserImplPtr>(),
157 virtual ~DEL_USER_NONIFIER() {}
158 void Notify(const UserImplPtr & user);
161 DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
162 DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
164 TraffCounterImpl & traffCnt;
166 //-----------------------------------------------------------------------------
167 class TraffCounterImpl : public TraffCounter {
168 friend class ADD_USER_NONIFIER;
169 friend class DEL_USER_NONIFIER;
170 friend class TRF_IP_BEFORE;
171 friend class TRF_IP_AFTER;
173 TraffCounterImpl(UsersImpl * users, const std::string & rulesFileName);
180 void process(const RawPacket & rawPacket) override;
181 void SetMonitorDir(const std::string & monitorDir);
183 size_t rulesCount() const override { return rules.size(); }
186 bool ParseAddress(const char * ta, Rule * rule) const;
187 uint32_t CalcMask(uint32_t msk) const;
189 bool ReadRules(bool test = false);
191 void Run(std::stop_token token);
193 void DeterminateDir(const RawPacket & packet,
194 int * dirU, // Direction for upload
195 int * dirD) const; // Direction for download
197 void FlushAndRemove();
199 void AddUser(UserImpl * user);
200 void DelUser(uint32_t uip);
201 void SetUserNotifiers(UserImpl * user);
202 void UnSetUserNotifiers(UserImpl * user);
204 typedef std::list<Rule>::iterator rule_iter;
206 std::list<Rule> rules;
208 typedef std::map<RawPacket, PacketExtraData> Packets;
209 typedef Packets::iterator pp_iter;
210 typedef std::multimap<uint32_t, pp_iter> Index;
211 typedef Index::iterator ip2p_iter;
212 typedef Index::const_iterator ip2p_citer;
214 Packets packets; // Packets tree
216 Index ip2packets; // IP-to-Packet index
218 std::string dirName[DIR_NUM + 1];
220 Logger & WriteServLog;
221 std::string rulesFileName;
223 std::string monitorDir;
231 std::jthread m_thread;
233 std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
234 std::list<TRF_IP_AFTER> ipAfterNotifiers;
236 ADD_USER_NONIFIER addUserNotifier;
237 DEL_USER_NONIFIER delUserNotifier;
239 //-----------------------------------------------------------------------------
241 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
243 // User changes his address. Remove old IP
247 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::DelUser, oldValue);
249 //-----------------------------------------------------------------------------
251 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
253 // User changes his address. Add new IP
257 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::AddUser, user);
259 //-----------------------------------------------------------------------------
261 void ADD_USER_NONIFIER::Notify(const UserImplPtr & user)
263 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::SetUserNotifiers, user);
265 //-----------------------------------------------------------------------------
267 void DEL_USER_NONIFIER::Notify(const UserImplPtr & user)
269 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::UnSetUserNotifiers, user);
270 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::DelUser, user->GetCurrIP());
272 //-----------------------------------------------------------------------------