]> git.stg.codes - stg.git/blob - projects/traffcounter/traffcounter.h
Добавление исходников
[stg.git] / projects / traffcounter / 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 : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.3 $
23  $Date: 2009/04/10 14:15:46 $
24  $Author: faust $
25  */
26
27
28 #ifndef TRAFFCOUNTER_H
29 #define TRAFFCOUNTER_H
30
31 #include <pthread.h>
32 #include <netinet/ip.h>
33
34 #ifdef HAVE_STDINT
35     #include <stdint.h>
36 #else
37     #ifdef HAVE_INTTYPES
38         #include <inttypes.h>
39     #else
40         #error "You need either stdint.h or inttypes.h to compile this!"
41     #endif
42 #endif
43
44 #include <ctime>
45 #include <list>
46 #include <vector>
47 #include <map>
48
49 #include "rules.h"
50 #include "rules_finder.h"
51 #include "tc_packets.h"
52 #include "user_tc_iface.h"
53 #include "capturer_tc_iface.h"
54
55 #define PACKET_TIMEOUT 300
56
57 namespace STG
58 {
59
60     class TRAFFCOUNTER : public IUSER_TC, public ICAPTURER_TC
61     {
62     public:
63         TRAFFCOUNTER();
64         ~TRAFFCOUNTER();
65
66         void            SetRules(const RULES & data);
67
68         bool            Start();
69         bool            Stop();
70
71         // Capturer API
72         void            AddPacket(const iphdr & ipHdr, uint16_t sport, uint16_t dport);
73
74         // User API
75         void            AddIP(uint32_t ip);
76         void            DeleteIP(uint32_t ip, TRAFF_DATA * traff);
77         void            GetIP(uint32_t ip, TRAFF_DATA * traff);
78
79         /*
80          * Stream quality represents a "scatterness" of data stream
81          * When sessions represend a large amount of information - it's a good
82          * stream. Most of common-use protocols (HTTP, FTP, etc.) shows a good
83          * stream quality.
84          * When there are a lot of packet that creates a new streams - it's a
85          * bad stream. p2p traffic has a bias to show a bad stream quality.
86          */
87         double          StreamQuality() const;
88         uint64_t        PendingCount() const { return pendingCount; };
89         uint64_t        SessionsCount() const { return sessions.size(); };
90         uint64_t        IndexesCount() const { return ip2sessions.size(); };
91         uint64_t        CacheHits() const { return cacheHits; };
92         uint64_t        CacheMisses() const { return cacheMisses; };
93
94     private:
95         static void *   Run(void * data);
96
97         void            Process(const PENDING_PACKET & p);
98
99         RULES_FINDER rulesFinder;
100
101         /*
102          * SESSION_INDEX: ip -> SESSION_ITER
103          * SESSIONS: SESSION_ID -> SESSION_DATA
104          *                -> SESSION_INDEX (saddr)
105          *                -> SESSION_INDEX (daddr)
106          */
107         struct SESSION_FULL_DATA; // Forward declaration
108         typedef std::map<SESSION_ID, SESSION_FULL_DATA, SESSION_LESS> SESSIONS;
109         typedef SESSIONS::iterator SESSION_ITER;
110         /*
111          *  This structure is used to take a fast session access by IP
112          * Normally, one IP can reffer multiple sessions. For each data stream there
113          * are 2 sessions: incoming data and outgoing data.
114          */
115         typedef std::multimap<uint32_t, SESSION_ITER> SESSION_INDEX;
116         typedef SESSION_INDEX::iterator INDEX_ITER;
117         /*
118          *  Append session meta-information with back-indexes
119          * In process of removing IP from TRAFFCOUNTER we need to remove indexes of
120          * sessions, reffered by this IP. To prevent slow searching by index tree we
121          * use 2 back-references: for source and destination IP.
122          */
123         struct SESSION_FULL_DATA : public SESSION_DATA
124         {
125             INDEX_ITER sIdx; // Back reference for fast index removing
126             INDEX_ITER dIdx; // Back reference for fast index removing
127             int        refCount; // Reference count for packet removing
128         };
129
130         std::list<PENDING_PACKET> pendingPackets;
131
132         SESSIONS sessions; // A map with sessions data
133
134         /*
135          * When pending packet appends a session - it's a "cache hit"
136          * When pending packet creates a new session - it's a "cache miss"
137          */
138         uint64_t cacheHits;
139         uint64_t cacheMisses;
140         uint64_t pendingCount;
141         uint64_t maxPending;
142
143         SESSION_INDEX ip2sessions; // IP index for sessions data
144
145         /*
146          * A sorted vector of allowed/disallowed ips
147          */
148         std::vector<uint32_t> ips;
149         typedef std::vector<uint32_t>::iterator IP_ITER;
150
151         bool        stopped;
152         bool        running;
153
154         mutable pthread_mutex_t         sessionMutex; // For sessions
155         mutable pthread_mutex_t         pendingMutex; // For pendinPackets
156         mutable pthread_cond_t          pendingCond;  //
157         mutable pthread_mutex_t         ipMutex;      // For ip list
158         mutable pthread_mutex_t         rulesMutex;   // For rules list
159         pthread_t               thread;
160
161     };
162
163 }
164
165 #endif //TRAFFCOUNTER_H