]> git.stg.codes - stg.git/blob - projects/stargazer/traffcounter_impl.h
Fixed credit.
[stg.git] / projects / stargazer / traffcounter_impl.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_IMPL_H
29 #define TRAFFCOUNTER_IMPL_H
30
31 #include <pthread.h>
32
33 #include <ctime>
34 #include <list>
35 #include <map>
36 #include <string>
37
38 #include "stg/traffcounter.h"
39 #include "stg/os_int.h"
40 #include "stg/logger.h"
41 #include "stg/raw_ip_packet.h"
42 #include "stg/noncopyable.h"
43 #include "stg/notifer.h"
44 #include "actions.h"
45 #include "eventloop.h"
46 #include "user_impl.h"
47
48 #define PROTOMAX    (5)
49
50 class USERS_IMPL;
51
52 //-----------------------------------------------------------------------------
53 struct RULE {
54 uint32_t    ip;             // IP
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
60 };
61 //-----------------------------------------------------------------------------
62 struct PACKET_EXTRA_DATA {
63 PACKET_EXTRA_DATA()
64     : flushTime(0),
65       updateTime(0),
66       userU(NULL),
67       userUPresent(false),
68       userD(NULL),
69       userDPresent(false),
70       dirU(DIR_NUM),
71       dirD(DIR_NUM),
72       lenU(0),
73       lenD(0)
74 {}
75
76 PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp)
77     : flushTime(pp.flushTime),
78       updateTime(pp.updateTime),
79       userU(pp.userU),
80       userUPresent(pp.userUPresent),
81       userD(pp.userD),
82       userDPresent(pp.userDPresent),
83       dirU(pp.dirU),
84       dirD(pp.dirD),
85       lenU(pp.lenU),
86       lenD(pp.lenD)
87 {}
88
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
99 };
100 //-----------------------------------------------------------------------------
101 class TRAFFCOUNTER_IMPL;
102 //-----------------------------------------------------------------------------
103 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
104 public:
105                 TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
106                     : PROPERTY_NOTIFIER_BASE<uint32_t>(),
107                       traffCnt(t),
108                       user(u)
109                 {}
110                 TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue)
111                     : PROPERTY_NOTIFIER_BASE<uint32_t>(),
112                       traffCnt(rvalue.traffCnt),
113                       user(rvalue.user)
114                 {}
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; }
118
119 private:
120     TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue);
121
122     TRAFFCOUNTER_IMPL & traffCnt;
123     USER_IMPL * user;
124 };
125 //-----------------------------------------------------------------------------
126 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
127 public:
128                 TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
129                     : PROPERTY_NOTIFIER_BASE<uint32_t>(),
130                       traffCnt(t),
131                       user(u)
132                 {}
133                 TRF_IP_AFTER(const TRF_IP_AFTER & rvalue)
134                     : PROPERTY_NOTIFIER_BASE<uint32_t>(),
135                       traffCnt(rvalue.traffCnt),
136                       user(rvalue.user)
137                 {}
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; }
141 private:
142     TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue);
143
144     TRAFFCOUNTER_IMPL & traffCnt;
145     USER_IMPL * user;
146 };
147 //-----------------------------------------------------------------------------
148 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
149 public:
150             ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
151                 NOTIFIER_BASE<USER_IMPL_PTR>(),
152                 traffCnt(t)
153             {}
154     virtual ~ADD_USER_NONIFIER() {}
155     void    Notify(const USER_IMPL_PTR & user);
156
157 private:
158     ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
159     ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
160
161     TRAFFCOUNTER_IMPL & traffCnt;
162 };
163 //-----------------------------------------------------------------------------
164 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
165 public:
166             DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
167                 NOTIFIER_BASE<USER_IMPL_PTR>(),
168                 traffCnt(t)
169             {}
170     virtual ~DEL_USER_NONIFIER() {}
171     void    Notify(const USER_IMPL_PTR & user);
172
173 private:
174     DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
175     DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
176
177     TRAFFCOUNTER_IMPL & traffCnt;
178 };
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;
185 public:
186     TRAFFCOUNTER_IMPL(USERS_IMPL * users, const std::string & rulesFileName);
187     ~TRAFFCOUNTER_IMPL();
188
189     void        SetRulesFile(const std::string & rulesFileName);
190
191     int         Reload();
192     int         Start();
193     int         Stop();
194
195     void        Process(const RAW_PACKET & rawPacket);
196     void        SetMonitorDir(const std::string & monitorDir);
197
198     size_t      RulesCount() const { return rules.size(); }
199
200 private:
201     TRAFFCOUNTER_IMPL(const TRAFFCOUNTER_IMPL & rvalue);
202     TRAFFCOUNTER_IMPL & operator=(const TRAFFCOUNTER_IMPL & rvalue);
203
204     bool        ParseAddress(const char * ta, RULE * rule) const;
205     uint32_t    CalcMask(uint32_t msk) const;
206     void        FreeRules();
207     void        PrintRule(RULE rule) const;
208     bool        ReadRules(bool test = false);
209
210     static void * Run(void * data);
211
212     void        DeterminateDir(const RAW_PACKET & packet,
213                                int * dirU, // Direction for upload
214                                int * dirD) const; // Direction for download
215
216     void        FlushAndRemove();
217
218     void        AddUser(USER_IMPL * user);
219     void        DelUser(uint32_t uip);
220     void        SetUserNotifiers(USER_IMPL * user);
221     void        UnSetUserNotifiers(USER_IMPL * user);
222
223     typedef std::list<RULE>::iterator rule_iter;
224
225     std::list<RULE>          rules;
226
227     typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA> Packets;
228     typedef Packets::iterator pp_iter;
229     typedef std::multimap<uint32_t, pp_iter> Index;
230     typedef Index::iterator ip2p_iter;
231     typedef Index::const_iterator ip2p_citer;
232
233     Packets packets; // Packets tree
234
235     Index ip2packets; // IP-to-Packet index
236
237     std::string              dirName[DIR_NUM + 1];
238
239     STG_LOGGER &             WriteServLog;
240     std::string              rulesFileName;
241
242     std::string              monitorDir;
243     bool                     monitoring;
244     time_t                   touchTimeP;
245
246     USERS_IMPL *             users;
247
248     bool                     running;
249     bool                     stopped;
250     pthread_mutex_t          mutex;
251     pthread_t                thread;
252
253     std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
254     std::list<TRF_IP_AFTER>  ipAfterNotifiers;
255
256     ADD_USER_NONIFIER        addUserNotifier;
257     DEL_USER_NONIFIER        delUserNotifier;
258 };
259 //-----------------------------------------------------------------------------
260 inline
261 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
262 {
263 // User changes his address. Remove old IP
264 if (!oldValue)
265     return;
266
267 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, oldValue);
268 }
269 //-----------------------------------------------------------------------------
270 inline
271 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
272 {
273 // User changes his address. Add new IP
274 if (!newValue)
275     return;
276
277 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::AddUser, user);
278 }
279 //-----------------------------------------------------------------------------
280 inline
281 void ADD_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
282 {
283 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user);
284 }
285 //-----------------------------------------------------------------------------
286 inline
287 void DEL_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
288 {
289 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user);
290 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP());
291 }
292 //-----------------------------------------------------------------------------
293 #endif //TRAFFCOUNTER_H