]> git.stg.codes - stg.git/blob - stargazer/traffcounter_impl.h
Update travis dist.
[stg.git] / 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 "stg/traffcounter.h"
32 #include "stg/logger.h"
33 #include "stg/raw_ip_packet.h"
34 #include "stg/noncopyable.h"
35 #include "stg/notifer.h"
36 #include "actions.h"
37 #include "eventloop.h"
38 #include "user_impl.h"
39
40 #include <ctime>
41 #include <list>
42 #include <map>
43 #include <string>
44 #include <cstdint>
45
46 #include <pthread.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 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
86 };
87 //-----------------------------------------------------------------------------
88 class TRAFFCOUNTER_IMPL;
89 //-----------------------------------------------------------------------------
90 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
91 public:
92                 TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
93                     : PROPERTY_NOTIFIER_BASE<uint32_t>(),
94                       traffCnt(t),
95                       user(u)
96                 {}
97                 TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue)
98                     : PROPERTY_NOTIFIER_BASE<uint32_t>(),
99                       traffCnt(rvalue.traffCnt),
100                       user(rvalue.user)
101                 {}
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; }
105
106 private:
107     TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue);
108
109     TRAFFCOUNTER_IMPL & traffCnt;
110     USER_IMPL * user;
111 };
112 //-----------------------------------------------------------------------------
113 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
114 public:
115                 TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
116                     : PROPERTY_NOTIFIER_BASE<uint32_t>(),
117                       traffCnt(t),
118                       user(u)
119                 {}
120                 TRF_IP_AFTER(const TRF_IP_AFTER & rvalue)
121                     : PROPERTY_NOTIFIER_BASE<uint32_t>(),
122                       traffCnt(rvalue.traffCnt),
123                       user(rvalue.user)
124                 {}
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; }
128 private:
129     TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue);
130
131     TRAFFCOUNTER_IMPL & traffCnt;
132     USER_IMPL * user;
133 };
134 //-----------------------------------------------------------------------------
135 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
136 public:
137             explicit ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
138                 NOTIFIER_BASE<USER_IMPL_PTR>(),
139                 traffCnt(t)
140             {}
141     virtual ~ADD_USER_NONIFIER() {}
142     void    Notify(const USER_IMPL_PTR & user);
143
144 private:
145     ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
146     ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
147
148     TRAFFCOUNTER_IMPL & traffCnt;
149 };
150 //-----------------------------------------------------------------------------
151 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
152 public:
153             explicit DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
154                 NOTIFIER_BASE<USER_IMPL_PTR>(),
155                 traffCnt(t)
156             {}
157     virtual ~DEL_USER_NONIFIER() {}
158     void    Notify(const USER_IMPL_PTR & user);
159
160 private:
161     DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
162     DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
163
164     TRAFFCOUNTER_IMPL & traffCnt;
165 };
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;
172 public:
173     TRAFFCOUNTER_IMPL(USERS_IMPL * users, const std::string & rulesFileName);
174     ~TRAFFCOUNTER_IMPL();
175
176     int         Reload();
177     int         Start();
178     int         Stop();
179
180     void        Process(const RAW_PACKET & rawPacket);
181     void        SetMonitorDir(const std::string & monitorDir);
182
183     size_t      RulesCount() const { return rules.size(); }
184
185 private:
186     TRAFFCOUNTER_IMPL(const TRAFFCOUNTER_IMPL &);
187     TRAFFCOUNTER_IMPL & operator=(const TRAFFCOUNTER_IMPL &);
188
189     bool        ParseAddress(const char * ta, RULE * rule) const;
190     uint32_t    CalcMask(uint32_t msk) const;
191     void        FreeRules();
192     bool        ReadRules(bool test = false);
193
194     static void * Run(void * data);
195
196     void        DeterminateDir(const RAW_PACKET & packet,
197                                int * dirU, // Direction for upload
198                                int * dirD) const; // Direction for download
199
200     void        FlushAndRemove();
201
202     void        AddUser(USER_IMPL * user);
203     void        DelUser(uint32_t uip);
204     void        SetUserNotifiers(USER_IMPL * user);
205     void        UnSetUserNotifiers(USER_IMPL * user);
206
207     typedef std::list<RULE>::iterator rule_iter;
208
209     std::list<RULE>          rules;
210
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;
216
217     Packets packets; // Packets tree
218
219     Index ip2packets; // IP-to-Packet index
220
221     std::string              dirName[DIR_NUM + 1];
222
223     STG_LOGGER &             WriteServLog;
224     std::string              rulesFileName;
225
226     std::string              monitorDir;
227     bool                     monitoring;
228     time_t                   touchTimeP;
229
230     USERS_IMPL *             users;
231
232     bool                     running;
233     bool                     stopped;
234     pthread_mutex_t          mutex;
235     pthread_t                thread;
236
237     std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
238     std::list<TRF_IP_AFTER>  ipAfterNotifiers;
239
240     ADD_USER_NONIFIER        addUserNotifier;
241     DEL_USER_NONIFIER        delUserNotifier;
242 };
243 //-----------------------------------------------------------------------------
244 inline
245 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
246 {
247 // User changes his address. Remove old IP
248 if (!oldValue)
249     return;
250
251 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, oldValue);
252 }
253 //-----------------------------------------------------------------------------
254 inline
255 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
256 {
257 // User changes his address. Add new IP
258 if (!newValue)
259     return;
260
261 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::AddUser, user);
262 }
263 //-----------------------------------------------------------------------------
264 inline
265 void ADD_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
266 {
267 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user);
268 }
269 //-----------------------------------------------------------------------------
270 inline
271 void DEL_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
272 {
273 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user);
274 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP());
275 }
276 //-----------------------------------------------------------------------------
277 #endif //TRAFFCOUNTER_H