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
38 #include "traffcounter.h"
40 #include "stg_logger.h"
41 #include "raw_ip_packet.h"
44 #include "noncopyable.h"
45 #include "eventloop.h"
51 //-----------------------------------------------------------------------------
54 uint32_t mask; // Network mask
55 uint16_t port1; // Min port
56 uint16_t port2; // Max port
57 uint8_t proto; // Protocol
58 uint32_t dir; // Direction
60 //-----------------------------------------------------------------------------
61 struct PACKET_EXTRA_DATA {
75 PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp)
76 : flushTime(pp.flushTime),
77 updateTime(pp.updateTime),
79 userUPresent(pp.userUPresent),
81 userDPresent(pp.userDPresent),
88 time_t flushTime; // Last flush time
89 time_t updateTime; // Last update time
90 USER_PTR userU; // Uploader
91 bool userUPresent; // Uploader is registered user
92 USER_PTR userD; // Downloader
93 bool userDPresent; // Downloader is registered user
94 int dirU; // Upload direction
95 int dirD; // Download direction
96 uint32_t lenU; // Upload length
97 uint32_t lenD; // Download length
99 //-----------------------------------------------------------------------------
100 class TRAFFCOUNTER_IMPL;
101 //-----------------------------------------------------------------------------
102 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
104 TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_PTR u)
105 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
109 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
110 void SetUser(USER_PTR u) { user = u; }
111 USER_PTR GetUser() const { return user; }
114 TRAFFCOUNTER_IMPL & traffCnt;
117 //-----------------------------------------------------------------------------
118 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
120 TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_PTR u)
121 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
125 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
126 void SetUser(USER_PTR u) { user = u; }
127 USER_PTR GetUser() const { return user; }
129 TRAFFCOUNTER_IMPL & traffCnt;
132 //-----------------------------------------------------------------------------
133 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
135 ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
136 NOTIFIER_BASE<USER_PTR>(),
139 virtual ~ADD_USER_NONIFIER() {}
140 void Notify(const USER_PTR & user);
142 TRAFFCOUNTER_IMPL & traffCnt;
144 //-----------------------------------------------------------------------------
145 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
147 DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
148 NOTIFIER_BASE<USER_PTR>(),
151 virtual ~DEL_USER_NONIFIER() {}
152 void Notify(const USER_PTR & user);
154 TRAFFCOUNTER_IMPL & traffCnt;
156 //-----------------------------------------------------------------------------
157 class TRAFFCOUNTER_IMPL : public TRAFFCOUNTER, private NONCOPYABLE {
158 friend class ADD_USER_NONIFIER;
159 friend class DEL_USER_NONIFIER;
160 friend class TRF_IP_BEFORE;
161 friend class TRF_IP_AFTER;
163 TRAFFCOUNTER_IMPL(USERS * users, const TARIFFS * tariffs, const std::string & rulesFileName);
164 ~TRAFFCOUNTER_IMPL();
166 void SetRulesFile(const std::string & rulesFileName);
172 void Process(const RAW_PACKET & rawPacket);
173 void SetMonitorDir(const std::string & monitorDir);
176 bool ParseAddress(const char * ta, RULE * rule) const;
177 uint32_t CalcMask(uint32_t msk) const;
179 void PrintRule(RULE rule) const;
180 bool ReadRules(bool test = false);
182 static void * Run(void * data);
184 void DeterminateDir(const RAW_PACKET & packet,
185 int * dirU, // Direction for upload
186 int * dirD) const; // Direction for download
188 void FlushAndRemove();
190 void AddUser(USER_PTR user);
191 void DelUser(uint32_t uip);
192 void SetUserNotifiers(USER_PTR user);
193 void UnSetUserNotifiers(USER_PTR user);
195 typedef std::list<RULE>::iterator rule_iter;
196 typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA>::iterator pp_iter;
197 typedef std::multimap<uint32_t, pp_iter>::iterator ip2p_iter;
198 typedef std::multimap<uint32_t, pp_iter>::const_iterator ip2p_citer;
200 std::list<RULE> rules;
202 std::map<RAW_PACKET, PACKET_EXTRA_DATA> packets; // Packets tree
204 std::multimap<uint32_t, pp_iter> ip2packets; // IP-to-Packet index
206 std::string dirName[DIR_NUM + 1];
208 STG_LOGGER & WriteServLog;
209 std::string rulesFileName;
211 std::string monitorDir;
218 pthread_mutex_t mutex;
221 std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
222 std::list<TRF_IP_AFTER> ipAfterNotifiers;
224 ADD_USER_NONIFIER addUserNotifier;
225 DEL_USER_NONIFIER delUserNotifier;
227 //-----------------------------------------------------------------------------
229 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
231 // User changes his address. Remove old IP
235 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, oldValue);
237 //-----------------------------------------------------------------------------
239 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
241 // User changes his address. Add new IP
245 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::AddUser, user);
247 //-----------------------------------------------------------------------------
249 void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
251 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user);
253 //-----------------------------------------------------------------------------
255 void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
257 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user);
258 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP());
260 //-----------------------------------------------------------------------------
261 #endif //TRAFFCOUNTER_H