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.
 
   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.
 
  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
 
  18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
 
  23  $Date: 2009/04/10 14:15:46 $
 
  28 #ifndef TRAFFCOUNTER_H
 
  29 #define TRAFFCOUNTER_H
 
  32 #include <netinet/ip.h>
 
  40         #error "You need either stdint.h or inttypes.h to compile this!"
 
  50 #include "rules_finder.h"
 
  51 #include "tc_packets.h"
 
  52 #include "user_tc_iface.h"
 
  53 #include "capturer_tc_iface.h"
 
  55 #define PACKET_TIMEOUT 300
 
  60     class TRAFFCOUNTER : public IUSER_TC, public ICAPTURER_TC
 
  66         void            SetRules(const RULES & data);
 
  72         void            AddPacket(const iphdr & ipHdr, uint16_t sport, uint16_t dport);
 
  75         void            AddIP(uint32_t ip);
 
  76         void            DeleteIP(uint32_t ip, TRAFF_DATA * traff);
 
  77         void            GetIP(uint32_t ip, TRAFF_DATA * traff);
 
  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
 
  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.
 
  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; };
 
  95         static void *   Run(void * data);
 
  97         void            Process(const PENDING_PACKET & p);
 
  99         RULES_FINDER rulesFinder;
 
 102          * SESSION_INDEX: ip -> SESSION_ITER
 
 103          * SESSIONS: SESSION_ID -> SESSION_DATA
 
 104          *                -> SESSION_INDEX (saddr)
 
 105          *                -> SESSION_INDEX (daddr)
 
 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;
 
 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.
 
 115         typedef std::multimap<uint32_t, SESSION_ITER> SESSION_INDEX;
 
 116         typedef SESSION_INDEX::iterator INDEX_ITER;
 
 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.
 
 123         struct SESSION_FULL_DATA : public SESSION_DATA
 
 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
 
 130         std::list<PENDING_PACKET> pendingPackets;
 
 132         SESSIONS sessions; // A map with sessions data
 
 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"
 
 139         uint64_t cacheMisses;
 
 140         uint64_t pendingCount;
 
 143         SESSION_INDEX ip2sessions; // IP index for sessions data
 
 146          * A sorted vector of allowed/disallowed ips
 
 148         std::vector<uint32_t> ips;
 
 149         typedef std::vector<uint32_t>::iterator IP_ITER;
 
 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
 
 165 #endif //TRAFFCOUNTER_H