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 $Date: 2010/04/22 12:57:46 $
28 #ifndef TRAFFCOUNTER_IMPL_H
29 #define TRAFFCOUNTER_IMPL_H
31 #include "stg/traffcounter.h"
32 #include "stg/os_int.h"
33 #include "stg/logger.h"
34 #include "stg/raw_ip_packet.h"
35 #include "stg/noncopyable.h"
36 #include "stg/notifer.h"
38 #include "eventloop.h"
39 #include "user_impl.h"
52 //-----------------------------------------------------------------------------
55 uint32_t mask; // Network mask
56 uint16_t port1; // Min port
57 uint16_t port2; // Max port
58 uint8_t proto; // Protocol
59 uint32_t dir; // Direction
61 //-----------------------------------------------------------------------------
62 struct PACKET_EXTRA_DATA {
76 PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp)
77 : flushTime(pp.flushTime),
78 updateTime(pp.updateTime),
80 userUPresent(pp.userUPresent),
82 userDPresent(pp.userDPresent),
89 time_t flushTime; // Last flush time
90 time_t updateTime; // Last update time
91 USER_IMPL * userU; // Uploader
92 bool userUPresent; // Uploader is registered user
93 USER_IMPL * userD; // Downloader
94 bool userDPresent; // Downloader is registered user
95 int dirU; // Upload direction
96 int dirD; // Download direction
97 uint32_t lenU; // Upload length
98 uint32_t lenD; // Download length
100 //-----------------------------------------------------------------------------
101 class TRAFFCOUNTER_IMPL;
102 //-----------------------------------------------------------------------------
103 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
105 TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
106 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
110 TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue)
111 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
112 traffCnt(rvalue.traffCnt),
115 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
116 void SetUser(USER_IMPL * u) { user = u; }
117 USER_IMPL * GetUser() const { return user; }
120 TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue);
122 TRAFFCOUNTER_IMPL & traffCnt;
125 //-----------------------------------------------------------------------------
126 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
128 TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
129 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
133 TRF_IP_AFTER(const TRF_IP_AFTER & rvalue)
134 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
135 traffCnt(rvalue.traffCnt),
138 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
139 void SetUser(USER_IMPL * u) { user = u; }
140 USER_IMPL * GetUser() const { return user; }
142 TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue);
144 TRAFFCOUNTER_IMPL & traffCnt;
147 //-----------------------------------------------------------------------------
148 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
150 explicit ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
151 NOTIFIER_BASE<USER_IMPL_PTR>(),
154 virtual ~ADD_USER_NONIFIER() {}
155 void Notify(const USER_IMPL_PTR & user);
158 ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
159 ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
161 TRAFFCOUNTER_IMPL & traffCnt;
163 //-----------------------------------------------------------------------------
164 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
166 explicit DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
167 NOTIFIER_BASE<USER_IMPL_PTR>(),
170 virtual ~DEL_USER_NONIFIER() {}
171 void Notify(const USER_IMPL_PTR & user);
174 DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
175 DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
177 TRAFFCOUNTER_IMPL & traffCnt;
179 //-----------------------------------------------------------------------------
180 class TRAFFCOUNTER_IMPL : public TRAFFCOUNTER, private NONCOPYABLE {
181 friend class ADD_USER_NONIFIER;
182 friend class DEL_USER_NONIFIER;
183 friend class TRF_IP_BEFORE;
184 friend class TRF_IP_AFTER;
186 TRAFFCOUNTER_IMPL(USERS_IMPL * users, const std::string & rulesFileName);
187 ~TRAFFCOUNTER_IMPL();
193 void Process(const RAW_PACKET & rawPacket);
194 void SetMonitorDir(const std::string & monitorDir);
196 size_t RulesCount() const { return rules.size(); }
199 TRAFFCOUNTER_IMPL(const TRAFFCOUNTER_IMPL & rvalue);
200 TRAFFCOUNTER_IMPL & operator=(const TRAFFCOUNTER_IMPL & rvalue);
202 bool ParseAddress(const char * ta, RULE * rule) const;
203 uint32_t CalcMask(uint32_t msk) const;
205 bool ReadRules(bool test = false);
207 static void * Run(void * data);
209 void DeterminateDir(const RAW_PACKET & packet,
210 int * dirU, // Direction for upload
211 int * dirD) const; // Direction for download
213 void FlushAndRemove();
215 void AddUser(USER_IMPL * user);
216 void DelUser(uint32_t uip);
217 void SetUserNotifiers(USER_IMPL * user);
218 void UnSetUserNotifiers(USER_IMPL * user);
220 typedef std::list<RULE>::iterator rule_iter;
222 std::list<RULE> rules;
224 typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA> Packets;
225 typedef Packets::iterator pp_iter;
226 typedef std::multimap<uint32_t, pp_iter> Index;
227 typedef Index::iterator ip2p_iter;
228 typedef Index::const_iterator ip2p_citer;
230 Packets packets; // Packets tree
232 Index ip2packets; // IP-to-Packet index
234 std::string dirName[DIR_NUM + 1];
236 STG_LOGGER & WriteServLog;
237 std::string rulesFileName;
239 std::string monitorDir;
247 pthread_mutex_t mutex;
250 std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
251 std::list<TRF_IP_AFTER> ipAfterNotifiers;
253 ADD_USER_NONIFIER addUserNotifier;
254 DEL_USER_NONIFIER delUserNotifier;
256 //-----------------------------------------------------------------------------
258 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
260 // User changes his address. Remove old IP
264 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, oldValue);
266 //-----------------------------------------------------------------------------
268 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
270 // User changes his address. Add new IP
274 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::AddUser, user);
276 //-----------------------------------------------------------------------------
278 void ADD_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
280 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user);
282 //-----------------------------------------------------------------------------
284 void DEL_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
286 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user);
287 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP());
289 //-----------------------------------------------------------------------------
290 #endif //TRAFFCOUNTER_H