]> git.stg.codes - stg.git/blob - projects/stargazer/traffcounter_impl.h
Replace updating lastActivityTime via property with pure assignement
[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     void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
111     void            SetUser(USER_IMPL * u) { user = u; }
112     USER_IMPL *     GetUser() const { return user; }
113
114 private:
115     TRAFFCOUNTER_IMPL & traffCnt;
116     USER_IMPL *         user;
117 };
118 //-----------------------------------------------------------------------------
119 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
120 public:
121                     TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
122                         : PROPERTY_NOTIFIER_BASE<uint32_t>(),
123                           traffCnt(t),
124                           user(u)
125                     {}
126     void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
127     void            SetUser(USER_IMPL * u) { user = u; }
128     USER_IMPL *     GetUser() const { return user; }
129 private:
130     TRAFFCOUNTER_IMPL & traffCnt;
131     USER_IMPL *         user;
132 };
133 //-----------------------------------------------------------------------------
134 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
135 public:
136             ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
137                 NOTIFIER_BASE<USER_IMPL_PTR>(),
138                 traffCnt(t)
139             {}
140     virtual ~ADD_USER_NONIFIER() {}
141     void    Notify(const USER_IMPL_PTR & user);
142 private:
143     TRAFFCOUNTER_IMPL & traffCnt;
144 };
145 //-----------------------------------------------------------------------------
146 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
147 public:
148             DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
149                 NOTIFIER_BASE<USER_IMPL_PTR>(),
150                 traffCnt(t)
151             {}
152     virtual ~DEL_USER_NONIFIER() {}
153     void    Notify(const USER_IMPL_PTR & user);
154 private:
155     TRAFFCOUNTER_IMPL & traffCnt;
156 };
157 //-----------------------------------------------------------------------------
158 class TRAFFCOUNTER_IMPL : public 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;
163 public:
164     TRAFFCOUNTER_IMPL(USERS_IMPL * users, const std::string & rulesFileName);
165     ~TRAFFCOUNTER_IMPL();
166
167     void        SetRulesFile(const std::string & rulesFileName);
168
169     int         Reload();
170     int         Start();
171     int         Stop();
172
173     void        Process(const RAW_PACKET & rawPacket);
174     void        SetMonitorDir(const std::string & monitorDir);
175
176 private:
177     bool        ParseAddress(const char * ta, RULE * rule) const;
178     uint32_t    CalcMask(uint32_t msk) const;
179     void        FreeRules();
180     void        PrintRule(RULE rule) const;
181     bool        ReadRules(bool test = false);
182
183     static void * Run(void * data);
184
185     void        DeterminateDir(const RAW_PACKET & packet,
186                                int * dirU, // Direction for upload
187                                int * dirD) const; // Direction for download
188
189     void        FlushAndRemove();
190
191     void        AddUser(USER_IMPL * user);
192     void        DelUser(uint32_t uip);
193     void        SetUserNotifiers(USER_IMPL * user);
194     void        UnSetUserNotifiers(USER_IMPL * user);
195
196     typedef std::list<RULE>::iterator rule_iter;
197     typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA>::iterator pp_iter;
198     typedef std::multimap<uint32_t, pp_iter>::iterator ip2p_iter;
199     typedef std::multimap<uint32_t, pp_iter>::const_iterator ip2p_citer;
200
201     std::list<RULE>          rules;
202
203     std::map<RAW_PACKET, PACKET_EXTRA_DATA> packets; // Packets tree
204
205     std::multimap<uint32_t, pp_iter> ip2packets; // IP-to-Packet index
206
207     std::string              dirName[DIR_NUM + 1];
208
209     STG_LOGGER &             WriteServLog;
210     std::string              rulesFileName;
211
212     std::string              monitorDir;
213     bool                     monitoring;
214
215     USERS_IMPL *             users;
216
217     bool                     running;
218     bool                     stopped;
219     pthread_mutex_t          mutex;
220     pthread_t                thread;
221
222     std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
223     std::list<TRF_IP_AFTER>  ipAfterNotifiers;
224
225     ADD_USER_NONIFIER        addUserNotifier;
226     DEL_USER_NONIFIER        delUserNotifier;
227 };
228 //-----------------------------------------------------------------------------
229 inline
230 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
231 {
232 // User changes his address. Remove old IP
233 if (!oldValue)
234     return;
235
236 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, oldValue);
237 }
238 //-----------------------------------------------------------------------------
239 inline
240 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
241 {
242 // User changes his address. Add new IP
243 if (!newValue)
244     return;
245
246 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::AddUser, user);
247 }
248 //-----------------------------------------------------------------------------
249 inline
250 void ADD_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
251 {
252 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user);
253 }
254 //-----------------------------------------------------------------------------
255 inline
256 void DEL_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
257 {
258 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user);
259 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP());
260 }
261 //-----------------------------------------------------------------------------
262 #endif //TRAFFCOUNTER_H