]> git.stg.codes - stg.git/blob - projects/stargazer/traffcounter_impl.h
Remove 'stg' prefixes from headers and add it to libraries
[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/users.h"
43 #include "stg/noncopyable.h"
44 #include "actions.h"
45 #include "eventloop.h"
46
47 #define PROTOMAX    (5)
48
49 class TARIFFS;
50
51 //-----------------------------------------------------------------------------
52 struct RULE {
53 uint32_t    ip;             // IP
54 uint32_t    mask;           // Network mask
55 uint16_t    port1;          // Min port
56 uint16_t    port2;          // Max port
57 uint8_t     proto;          // Protocol
58 uint32_t    dir;            // Direction
59 };
60 //-----------------------------------------------------------------------------
61 struct PACKET_EXTRA_DATA {
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_IMPL;
101 //-----------------------------------------------------------------------------
102 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
103 public:
104                     TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_PTR u)
105                         : PROPERTY_NOTIFIER_BASE<uint32_t>(),
106                           traffCnt(t),
107                           user(u)
108                     {}
109     void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
110     void            SetUser(USER_PTR u) { user = u; }
111     USER_PTR        GetUser() const { return user; }
112
113 private:
114     TRAFFCOUNTER_IMPL & traffCnt;
115     USER_PTR            user;
116 };
117 //-----------------------------------------------------------------------------
118 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
119 public:
120                     TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_PTR u)
121                         : PROPERTY_NOTIFIER_BASE<uint32_t>(),
122                           traffCnt(t),
123                           user(u)
124                     {}
125     void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
126     void            SetUser(USER_PTR u) { user = u; }
127     USER_PTR        GetUser() const { return user; }
128 private:
129     TRAFFCOUNTER_IMPL & traffCnt;
130     USER_PTR            user;
131 };
132 //-----------------------------------------------------------------------------
133 class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
134 public:
135             ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
136                 NOTIFIER_BASE<USER_PTR>(),
137                 traffCnt(t)
138             {}
139     virtual ~ADD_USER_NONIFIER() {}
140     void    Notify(const USER_PTR & user);
141 private:
142     TRAFFCOUNTER_IMPL & traffCnt;
143 };
144 //-----------------------------------------------------------------------------
145 class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
146 public:
147             DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
148                 NOTIFIER_BASE<USER_PTR>(),
149                 traffCnt(t)
150             {}
151     virtual ~DEL_USER_NONIFIER() {}
152     void    Notify(const USER_PTR & user);
153 private:
154     TRAFFCOUNTER_IMPL & traffCnt;
155 };
156 //-----------------------------------------------------------------------------
157 class TRAFFCOUNTER_IMPL : public TRAFFCOUNTER, private NONCOPYABLE {
158 friend class ADD_USER_NONIFIER;
159 friend class DEL_USER_NONIFIER;
160 friend class TRF_IP_BEFORE;
161 friend class TRF_IP_AFTER;
162 public:
163     TRAFFCOUNTER_IMPL(USERS * users, const TARIFFS * tariffs, const std::string & rulesFileName);
164     ~TRAFFCOUNTER_IMPL();
165
166     void        SetRulesFile(const std::string & rulesFileName);
167
168     int         Reload();
169     int         Start();
170     int         Stop();
171
172     void        Process(const RAW_PACKET & rawPacket);
173     void        SetMonitorDir(const std::string & monitorDir);
174
175 private:
176     bool        ParseAddress(const char * ta, RULE * rule) const;
177     uint32_t    CalcMask(uint32_t msk) const;
178     void        FreeRules();
179     void        PrintRule(RULE rule) const;
180     bool        ReadRules(bool test = false);
181
182     static void * Run(void * data);
183
184     void        DeterminateDir(const RAW_PACKET & packet,
185                                int * dirU, // Direction for upload
186                                int * dirD) const; // Direction for download
187
188     void        FlushAndRemove();
189
190     void        AddUser(USER_PTR user);
191     void        DelUser(uint32_t uip);
192     void        SetUserNotifiers(USER_PTR user);
193     void        UnSetUserNotifiers(USER_PTR user);
194
195     typedef std::list<RULE>::iterator rule_iter;
196     typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA>::iterator pp_iter;
197     typedef std::multimap<uint32_t, pp_iter>::iterator ip2p_iter;
198     typedef std::multimap<uint32_t, pp_iter>::const_iterator ip2p_citer;
199
200     std::list<RULE>          rules;
201
202     std::map<RAW_PACKET, PACKET_EXTRA_DATA> packets; // Packets tree
203
204     std::multimap<uint32_t, pp_iter> ip2packets; // IP-to-Packet index
205
206     std::string              dirName[DIR_NUM + 1];
207
208     STG_LOGGER &             WriteServLog;
209     std::string              rulesFileName;
210
211     std::string              monitorDir;
212     bool                     monitoring;
213
214     USERS *                  users;
215
216     bool                     running;
217     bool                     stopped;
218     pthread_mutex_t          mutex;
219     pthread_t                thread;
220
221     std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
222     std::list<TRF_IP_AFTER>  ipAfterNotifiers;
223
224     ADD_USER_NONIFIER        addUserNotifier;
225     DEL_USER_NONIFIER        delUserNotifier;
226 };
227 //-----------------------------------------------------------------------------
228 inline
229 void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
230 {
231 // User changes his address. Remove old IP
232 if (!oldValue)
233     return;
234
235 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, oldValue);
236 }
237 //-----------------------------------------------------------------------------
238 inline
239 void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
240 {
241 // User changes his address. Add new IP
242 if (!newValue)
243     return;
244
245 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::AddUser, user);
246 }
247 //-----------------------------------------------------------------------------
248 inline
249 void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
250 {
251 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user);
252 }
253 //-----------------------------------------------------------------------------
254 inline
255 void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
256 {
257 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user);
258 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP());
259 }
260 //-----------------------------------------------------------------------------
261 #endif //TRAFFCOUNTER_H