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 time_t flushTime; // Last flush time
77 time_t updateTime; // Last update time
78 USER_IMPL * userU; // Uploader
79 bool userUPresent; // Uploader is registered user
80 USER_IMPL * userD; // Downloader
81 bool userDPresent; // Downloader is registered user
82 int dirU; // Upload direction
83 int dirD; // Download direction
84 uint32_t lenU; // Upload length
85 uint32_t lenD; // Download length
87 //-----------------------------------------------------------------------------
88 class TRAFFCOUNTER_IMPL;
89 //-----------------------------------------------------------------------------
90 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
92 TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
93 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
97 TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue)
98 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
99 traffCnt(rvalue.traffCnt),
102 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
103 void SetUser(USER_IMPL * u) { user = u; }
104 USER_IMPL * GetUser() const { return user; }
107 TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue);
109 TRAFFCOUNTER_IMPL & traffCnt;
112 //-----------------------------------------------------------------------------
113 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
115 TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
116 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
120 TRF_IP_AFTER(const TRF_IP_AFTER & rvalue)
121 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
122 traffCnt(rvalue.traffCnt),
125 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
126 void SetUser(USER_IMPL * u) { user = u; }
127 USER_IMPL * GetUser() const { return user; }
129 TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue);
131 TRAFFCOUNTER_IMPL & traffCnt;
134 //-----------------------------------------------------------------------------
135 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
137 explicit ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
138 NOTIFIER_BASE<USER_IMPL_PTR>(),
141 virtual ~ADD_USER_NONIFIER() {}
142 void Notify(const USER_IMPL_PTR & user);
145 ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
146 ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
148 TRAFFCOUNTER_IMPL & traffCnt;
150 //-----------------------------------------------------------------------------
151 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
153 explicit DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
154 NOTIFIER_BASE<USER_IMPL_PTR>(),
157 virtual ~DEL_USER_NONIFIER() {}
158 void Notify(const USER_IMPL_PTR & user);
161 DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
162 DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
164 TRAFFCOUNTER_IMPL & traffCnt;
166 //-----------------------------------------------------------------------------
167 class TRAFFCOUNTER_IMPL : public TRAFFCOUNTER, private NONCOPYABLE {
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 TRAFFCOUNTER_IMPL(USERS_IMPL * users, const std::string & rulesFileName);
174 ~TRAFFCOUNTER_IMPL();
180 void Process(const RAW_PACKET & rawPacket);
181 void SetMonitorDir(const std::string & monitorDir);
183 size_t RulesCount() const { return rules.size(); }
186 TRAFFCOUNTER_IMPL(const TRAFFCOUNTER_IMPL &);
187 TRAFFCOUNTER_IMPL & operator=(const TRAFFCOUNTER_IMPL &);
189 bool ParseAddress(const char * ta, RULE * rule) const;
190 uint32_t CalcMask(uint32_t msk) const;
192 bool ReadRules(bool test = false);
194 static void * Run(void * data);
196 void DeterminateDir(const RAW_PACKET & packet,
197 int * dirU, // Direction for upload
198 int * dirD) const; // Direction for download
200 void FlushAndRemove();
202 void AddUser(USER_IMPL * user);
203 void DelUser(uint32_t uip);
204 void SetUserNotifiers(USER_IMPL * user);
205 void UnSetUserNotifiers(USER_IMPL * user);
207 typedef std::list<RULE>::iterator rule_iter;
209 std::list<RULE> rules;
211 typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA> Packets;
212 typedef Packets::iterator pp_iter;
213 typedef std::multimap<uint32_t, pp_iter> Index;
214 typedef Index::iterator ip2p_iter;
215 typedef Index::const_iterator ip2p_citer;
217 Packets packets; // Packets tree
219 Index ip2packets; // IP-to-Packet index
221 std::string dirName[DIR_NUM + 1];
223 STG_LOGGER & WriteServLog;
224 std::string rulesFileName;
226 std::string monitorDir;
234 pthread_mutex_t mutex;
237 std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
238 std::list<TRF_IP_AFTER> ipAfterNotifiers;
240 ADD_USER_NONIFIER addUserNotifier;
241 DEL_USER_NONIFIER delUserNotifier;
243 //-----------------------------------------------------------------------------
245 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
247 // User changes his address. Remove old IP
251 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, oldValue);
253 //-----------------------------------------------------------------------------
255 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
257 // User changes his address. Add new IP
261 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::AddUser, user);
263 //-----------------------------------------------------------------------------
265 void ADD_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
267 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user);
269 //-----------------------------------------------------------------------------
271 void DEL_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
273 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user);
274 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP());
276 //-----------------------------------------------------------------------------
277 #endif //TRAFFCOUNTER_H