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_H
29 #define TRAFFCOUNTER_H
38 #include "stg_logger.h"
39 #include "raw_ip_packet.h"
42 #include "noncopyable.h"
43 #include "eventloop.h"
47 //-----------------------------------------------------------------------------
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 PACKET_EXTRA_DATA
73 PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp)
74 : flushTime(pp.flushTime),
75 updateTime(pp.updateTime),
77 userUPresent(pp.userUPresent),
79 userDPresent(pp.userDPresent),
86 time_t flushTime; // Last flush time
87 time_t updateTime; // Last update time
88 user_iter userU; // Uploader
89 bool userUPresent; // Uploader is registered user
90 user_iter userD; // Downloader
91 bool userDPresent; // Downloader is registered user
92 int dirU; // Upload direction
93 int dirD; // Download direction
94 uint32_t lenU; // Upload length
95 uint32_t lenD; // Download length
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
100 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t>
103 TRF_IP_BEFORE(TRAFFCOUNTER & t, user_iter u)
104 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
108 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
109 void SetUser(user_iter u) { user = u; }
110 user_iter GetUser() { return user; }
113 TRAFFCOUNTER & traffCnt;
116 //-----------------------------------------------------------------------------
117 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t>
120 TRF_IP_AFTER(TRAFFCOUNTER & t, user_iter u)
121 : PROPERTY_NOTIFIER_BASE<uint32_t>(),
125 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
126 void SetUser(user_iter u) { user = u; }
127 user_iter GetUser() { return user; }
129 TRAFFCOUNTER & traffCnt;
132 //-----------------------------------------------------------------------------
133 class ADD_USER_NONIFIER: public NOTIFIER_BASE<user_iter>
136 ADD_USER_NONIFIER(TRAFFCOUNTER & t) :
137 NOTIFIER_BASE<user_iter>(),
139 virtual ~ADD_USER_NONIFIER(){};
140 void Notify(const user_iter & user);
142 TRAFFCOUNTER & traffCnt;
144 //-----------------------------------------------------------------------------
145 class DEL_USER_NONIFIER: public NOTIFIER_BASE<user_iter>
148 DEL_USER_NONIFIER(TRAFFCOUNTER & t) :
149 NOTIFIER_BASE<user_iter>(),
151 virtual ~DEL_USER_NONIFIER(){};
152 void Notify(const user_iter & user);
154 TRAFFCOUNTER & traffCnt;
156 //-----------------------------------------------------------------------------
157 class TRAFFCOUNTER : private NONCOPYABLE
159 friend class ADD_USER_NONIFIER;
160 friend class DEL_USER_NONIFIER;
161 friend class TRF_IP_BEFORE;
162 friend class TRF_IP_AFTER;
164 TRAFFCOUNTER(USERS * users, const TARIFFS * tariffs, const std::string & rulesFileName);
167 void SetRulesFile(const std::string & rulesFileName);
173 void Process(const RAW_PACKET & rawPacket);
174 void SetMonitorDir(const std::string & monitorDir);
177 bool ParseAddress(const char * ta, RULE * rule) const;
178 uint32_t CalcMask(uint32_t msk) const;
180 void PrintRule(RULE rule) const;
181 bool ReadRules(bool test = false);
183 static void * Run(void * data);
185 void DeterminateDir(const RAW_PACKET & packet,
186 int * dirU, // Direction for upload
187 int * dirD) const; // Direction for download
189 void FlushAndRemove();
191 void AddUser(user_iter user);
192 void DelUser(uint32_t uip);
193 void SetUserNotifiers(user_iter user);
194 void UnSetUserNotifiers(user_iter user);
196 std::list<RULE> rules;
197 typedef std::list<RULE>::iterator rule_iter;
199 std::map<RAW_PACKET, PACKET_EXTRA_DATA> packets; // Packets tree
200 typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA>::iterator pp_iter;
202 std::multimap<uint32_t, pp_iter> ip2packets; // IP-to-Packet index
204 typedef std::multimap<uint32_t, pp_iter>::iterator ip2p_iter;
205 typedef std::multimap<uint32_t, pp_iter>::const_iterator ip2p_citer;
207 std::string dirName[DIR_NUM + 1];
209 STG_LOGGER & WriteServLog;
210 std::string rulesFileName;
212 std::string monitorDir;
219 pthread_mutex_t mutex;
222 std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
223 std::list<TRF_IP_AFTER> ipAfterNotifiers;
225 ADD_USER_NONIFIER addUserNotifier;
226 DEL_USER_NONIFIER delUserNotifier;
228 //-----------------------------------------------------------------------------
230 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
232 // User changes his address. Remove old IP
236 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, oldValue);
238 //-----------------------------------------------------------------------------
240 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
242 // User changes his address. Add new IP
246 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::AddUser, user);
248 //-----------------------------------------------------------------------------
250 void ADD_USER_NONIFIER::Notify(const user_iter & user)
252 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::SetUserNotifiers, user);
254 //-----------------------------------------------------------------------------
256 void DEL_USER_NONIFIER::Notify(const user_iter & user)
258 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::UnSetUserNotifiers, user);
259 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, user->GetCurrIP());
261 //-----------------------------------------------------------------------------
262 #endif //TRAFFCOUNTER_H