]> git.stg.codes - stg.git/blob - projects/stargazer/traffcounter.h
Cosmetic changes in settings.[h|cpp]
[stg.git] / projects / stargazer / traffcounter.h
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.23 $
23  $Date: 2010/04/22 12:57:46 $
24  $Author: faust $
25  */
26
27
28 #ifndef TRAFFCOUNTER_H
29 #define TRAFFCOUNTER_H
30
31 #include <pthread.h>
32 #include <ctime>
33 #include <list>
34 #include <map>
35 #include <string>
36
37 #include "os_int.h"
38 #include "stg_logger.h"
39 #include "raw_ip_packet.h"
40 #include "users.h"
41 #include "actions.h"
42 #include "noncopyable.h"
43 #include "eventloop.h"
44
45 #define PROTOMAX    (5)
46
47 class TARIFFS;
48
49 //-----------------------------------------------------------------------------
50 struct RULE
51 {
52 uint32_t    ip;             // IP
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
58 };
59 //-----------------------------------------------------------------------------
60 struct PACKET_EXTRA_DATA
61 {
62 PACKET_EXTRA_DATA()
63     : flushTime(0),
64       updateTime(0),
65       userU(),
66       userUPresent(false),
67       userD(),
68       userDPresent(false),
69       dirU(DIR_NUM),
70       dirD(DIR_NUM),
71       lenU(0),
72       lenD(0)
73 {};
74
75 PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp)
76     : flushTime(pp.flushTime),
77       updateTime(pp.updateTime),
78       userU(pp.userU),
79       userUPresent(pp.userUPresent),
80       userD(pp.userD),
81       userDPresent(pp.userDPresent),
82       dirU(pp.dirU),
83       dirD(pp.dirD),
84       lenU(pp.lenU),
85       lenD(pp.lenD)
86 {};
87
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
98 };
99 //-----------------------------------------------------------------------------
100 class TRAFFCOUNTER;
101 //-----------------------------------------------------------------------------
102 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t>
103 {
104 public:
105                     TRF_IP_BEFORE(TRAFFCOUNTER & t, USER_PTR u)
106                         : PROPERTY_NOTIFIER_BASE<uint32_t>(),
107                           traffCnt(t),
108                           user(u)
109                     {};
110     void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
111     void            SetUser(USER_PTR u) { user = u; }
112     USER_PTR        GetUser() const { return user; }
113
114 private:
115     TRAFFCOUNTER &  traffCnt;
116     USER_PTR        user;
117 };
118 //-----------------------------------------------------------------------------
119 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t>
120 {
121 public:
122                     TRF_IP_AFTER(TRAFFCOUNTER & t, USER_PTR u)
123                         : PROPERTY_NOTIFIER_BASE<uint32_t>(),
124                           traffCnt(t),
125                           user(u)
126                     {};
127     void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
128     void            SetUser(USER_PTR u) { user = u; }
129     USER_PTR        GetUser() const { return user; }
130 private:
131     TRAFFCOUNTER &  traffCnt;
132     USER_PTR        user;
133 };
134 //-----------------------------------------------------------------------------
135 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR>
136 {
137 public:
138             ADD_USER_NONIFIER(TRAFFCOUNTER & t) :
139                 NOTIFIER_BASE<USER_PTR>(),
140                 traffCnt(t) {};
141     virtual ~ADD_USER_NONIFIER(){};
142     void    Notify(const USER_PTR & user);
143 private:
144     TRAFFCOUNTER & traffCnt;
145 };
146 //-----------------------------------------------------------------------------
147 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR>
148 {
149 public:
150             DEL_USER_NONIFIER(TRAFFCOUNTER & t) :
151                 NOTIFIER_BASE<USER_PTR>(),
152                 traffCnt(t) {};
153     virtual ~DEL_USER_NONIFIER(){};
154     void    Notify(const USER_PTR & user);
155 private:
156     TRAFFCOUNTER & traffCnt;
157 };
158 //-----------------------------------------------------------------------------
159 class TRAFFCOUNTER : private NONCOPYABLE
160 {
161 friend class ADD_USER_NONIFIER;
162 friend class DEL_USER_NONIFIER;
163 friend class TRF_IP_BEFORE;
164 friend class TRF_IP_AFTER;
165 public:
166     TRAFFCOUNTER(USERS * users, const TARIFFS * tariffs, const std::string & rulesFileName);
167     ~TRAFFCOUNTER();
168
169     void        SetRulesFile(const std::string & rulesFileName);
170
171     int         Reload();
172     int         Start();
173     int         Stop();
174
175     void        Process(const RAW_PACKET & rawPacket);
176     void        SetMonitorDir(const std::string & monitorDir);
177
178 private:
179     bool        ParseAddress(const char * ta, RULE * rule) const;
180     uint32_t    CalcMask(uint32_t msk) const;
181     void        FreeRules();
182     void        PrintRule(RULE rule) const;
183     bool        ReadRules(bool test = false);
184
185     static void * Run(void * data);
186
187     void        DeterminateDir(const RAW_PACKET & packet,
188                                int * dirU, // Direction for upload
189                                int * dirD) const; // Direction for download
190
191     void        FlushAndRemove();
192
193     void        AddUser(USER_PTR user);
194     void        DelUser(uint32_t uip);
195     void        SetUserNotifiers(USER_PTR user);
196     void        UnSetUserNotifiers(USER_PTR user);
197
198     std::list<RULE>  rules;
199     typedef std::list<RULE>::iterator rule_iter;
200
201     std::map<RAW_PACKET, PACKET_EXTRA_DATA> packets; // Packets tree
202     typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA>::iterator pp_iter;
203
204     std::multimap<uint32_t, pp_iter> ip2packets; // IP-to-Packet index
205
206     typedef std::multimap<uint32_t, pp_iter>::iterator ip2p_iter;
207     typedef std::multimap<uint32_t, pp_iter>::const_iterator ip2p_citer;
208
209     std::string dirName[DIR_NUM + 1];
210
211     STG_LOGGER & WriteServLog;
212     std::string rulesFileName;
213
214     std::string monitorDir;
215     bool        monitoring;
216
217     USERS *     users;
218
219     bool        running;
220     bool        stopped;
221     pthread_mutex_t         mutex;
222     pthread_t               thread;
223
224     std::list<TRF_IP_BEFORE>     ipBeforeNotifiers;
225     std::list<TRF_IP_AFTER>      ipAfterNotifiers;
226
227     ADD_USER_NONIFIER       addUserNotifier;
228     DEL_USER_NONIFIER       delUserNotifier;
229 };
230 //-----------------------------------------------------------------------------
231 inline
232 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
233 {
234 // User changes his address. Remove old IP
235 if (!oldValue)
236     return;
237
238 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, oldValue);
239 }
240 //-----------------------------------------------------------------------------
241 inline
242 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
243 {
244 // User changes his address. Add new IP
245 if (!newValue)
246     return;
247
248 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::AddUser, user);
249 }
250 //-----------------------------------------------------------------------------
251 inline
252 void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
253 {
254 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::SetUserNotifiers, user);
255 }
256 //-----------------------------------------------------------------------------
257 inline
258 void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
259 {
260 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::UnSetUserNotifiers, user);
261 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, user->GetCurrIP());
262 }
263 //-----------------------------------------------------------------------------
264 #endif //TRAFFCOUNTER_H