]> git.stg.codes - stg.git/commitdiff
Traffcounter divided into interface and implementation
authorMaxim Mamontov <faust.madf@gmail.com>
Mon, 4 Apr 2011 12:19:05 +0000 (15:19 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Mon, 4 Apr 2011 12:19:05 +0000 (15:19 +0300)
include/traffcounter.h [new file with mode: 0644]
projects/stargazer/Makefile
projects/stargazer/traffcounter.cpp [deleted file]
projects/stargazer/traffcounter.h [deleted file]
projects/stargazer/traffcounter_impl.cpp [new file with mode: 0644]
projects/stargazer/traffcounter_impl.h [new file with mode: 0644]

diff --git a/include/traffcounter.h b/include/traffcounter.h
new file mode 100644 (file)
index 0000000..1962ee7
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ *
+ *    You should have received a copy of the GNU General Public License
+ *    along with this program; if not, write to the Free Software
+ *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ *    Author : maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef TRAFFCOUNTER_H
+#define TRAFFCOUNTER_H
+
+#include "raw_ip_packet.h"
+
+class TRAFFCOUNTER {
+public:
+    virtual void Process(const RAW_PACKET & rawPacket) = 0;
+};
+
+#endif
index 9f7dd8692eaceb6443ed0865e888d743861416d4..5aa4687fb8b63cb2c96aaae2c707ad0cf8ece7c6 100644 (file)
@@ -17,7 +17,7 @@ SRCS = ./admin_impl.cpp \
        ./store_loader.cpp \
        ./tariff_impl.cpp \
        ./tariffs_impl.cpp \
        ./store_loader.cpp \
        ./tariff_impl.cpp \
        ./tariffs_impl.cpp \
-       ./traffcounter.cpp \
+       ./traffcounter_impl.cpp \
        ./user_impl.cpp \
        ./user_property.cpp \
        ./users_impl.cpp
        ./user_impl.cpp \
        ./user_property.cpp \
        ./users_impl.cpp
diff --git a/projects/stargazer/traffcounter.cpp b/projects/stargazer/traffcounter.cpp
deleted file mode 100644 (file)
index dd672f3..0000000
+++ /dev/null
@@ -1,944 +0,0 @@
-/*
- *    This program is free software; you can redistribute it and/or modify
- *    it under the terms of the GNU General Public License as published by
- *    the Free Software Foundation; either version 2 of the License, or
- *    (at your option) any later version.
- *
- *    This program is distributed in the hope that it will be useful,
- *    but WITHOUT ANY WARRANTY; without even the implied warranty of
- *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *    GNU General Public License for more details.
- *
- *    You should have received a copy of the GNU General Public License
- *    along with this program; if not, write to the Free Software
- *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-/*
- *    Date: 27.10.2002
- */
-
-/*
- *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
- */
-
-/*
- $Revision: 1.58 $
- $Date: 2010/11/03 11:28:07 $
- $Author: faust $
- */
-
-/* inet_aton */
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-#include <csignal>
-#include <cassert>
-#include <cstdio> // Functions fopen and similar
-
-#include "traffcounter.h"
-#include "common.h"
-#include "stg_locker.h"
-#include "stg_timer.h"
-
-#define FLUSH_TIME  (10)
-#define REMOVE_TIME  (31)
-
-const char protoName[PROTOMAX][8] =
-{"TCP", "UDP", "ICMP", "TCP_UDP", "ALL"};
-
-enum protoNum
-    {
-    tcp = 0, udp, icmp, tcp_udp, all
-    };
-
-//-----------------------------------------------------------------------------
-TRAFFCOUNTER::TRAFFCOUNTER(USERS * u, const TARIFFS *, const std::string & fn)
-    : WriteServLog(GetStgLogger()),
-      rulesFileName(fn),
-      monitoring(false),
-      users(u),
-      running(false),
-      stopped(true),
-      addUserNotifier(*this),
-      delUserNotifier(*this)
-{
-for (int i = 0; i < DIR_NUM; i++)
-    strprintf(&dirName[i], "DIR%d", i);
-
-dirName[DIR_NUM] = "NULL";
-
-users->AddNotifierUserAdd(&addUserNotifier);
-users->AddNotifierUserDel(&delUserNotifier);
-
-pthread_mutex_init(&mutex, NULL);
-}
-//-----------------------------------------------------------------------------
-TRAFFCOUNTER::~TRAFFCOUNTER()
-{
-pthread_mutex_destroy(&mutex);
-}
-//-----------------------------------------------------------------------------
-int TRAFFCOUNTER::Start()
-{
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-
-if (!stopped)
-    return 0;
-
-if (ReadRules())
-    {
-    WriteServLog("TRAFFCOUNTER: Cannot read rules.");
-    return -1;
-    }
-
-printfd(__FILE__, "TRAFFCOUNTER::Start()\n");
-int h = users->OpenSearch();
-USER_PTR u;
-if (!h)
-    {
-    WriteServLog("TRAFFCOUNTER: Cannot get users.");
-    return -1;
-    }
-
-while (users->SearchNext(h, &u) == 0)
-    {
-    SetUserNotifiers(u);
-    }
-users->CloseSearch(h);
-
-running = true;
-if (pthread_create(&thread, NULL, Run, this))
-    {
-    WriteServLog("TRAFFCOUNTER: Error: Cannot start thread!");
-    return -1;
-    }
-return 0;
-}
-//-----------------------------------------------------------------------------
-int TRAFFCOUNTER::Stop()
-{
-if (stopped)
-    return 0;
-
-running = false;
-
-int h = users->OpenSearch();
-if (!h)
-    {
-    WriteServLog("TRAFFCOUNTER: Fatal error: Cannot get users.");
-    return -1;
-    }
-
-USER_PTR u;
-while (users->SearchNext(h, &u) == 0)
-    {
-    UnSetUserNotifiers(u);
-    }
-users->CloseSearch(h);
-
-//5 seconds to thread stops itself
-struct timespec ts = {0, 200000000};
-for (int i = 0; i < 25 && !stopped; i++)
-    {
-    nanosleep(&ts, NULL);
-    }
-
-//after 5 seconds waiting thread still running. now kill it
-if (!stopped)
-    {
-    printfd(__FILE__, "kill TRAFFCOUNTER thread.\n");
-    if (pthread_kill(thread, SIGINT))
-        {
-        return -1;
-        }
-    printfd(__FILE__, "TRAFFCOUNTER killed\n");
-    }
-printfd(__FILE__, "TRAFFCOUNTER::Stop()\n");
-
-return 0;
-}
-//-----------------------------------------------------------------------------
-void * TRAFFCOUNTER::Run(void * data)
-{
-TRAFFCOUNTER * tc = static_cast<TRAFFCOUNTER *>(data);
-tc->stopped = false;
-int c = 0;
-
-time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
-struct timespec ts = {0, 500000000};
-while (tc->running)
-    {
-    nanosleep(&ts, 0);
-    if (!tc->running)
-        {
-        tc->FlushAndRemove();
-        break;
-        }
-
-    if (tc->monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
-        {
-        std::string monFile(tc->monitorDir + "/traffcounter_r");
-        printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
-        touchTime = stgTime;
-        TouchFile(monFile.c_str());
-        }
-
-    if (++c % FLUSH_TIME == 0)
-        tc->FlushAndRemove();
-    }
-
-tc->stopped = true;
-return NULL;
-}
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::Process(const RAW_PACKET & rawPacket)
-{
-if (!running)
-    return;
-
-static time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
-
-if (monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
-    {
-    static std::string monFile = monitorDir + "/traffcounter_p";
-    printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", monitoring, monFile.c_str());
-    touchTime = stgTime;
-    TouchFile(monFile.c_str());
-    }
-
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-
-//printfd(__FILE__, "TRAFFCOUNTER::Process()\n");
-//TODO replace find with lower_bound.
-
-// Searching a new packet in a tree.
-pp_iter pi = packets.find(rawPacket);
-
-// Packet found - update length and time
-if (pi != packets.end())
-    {
-    pi->second.lenU += rawPacket.GetLen();
-    pi->second.lenD += rawPacket.GetLen();
-    pi->second.updateTime = stgTime;
-    /*printfd(__FILE__, "=============================\n");
-    printfd(__FILE__, "Packet found!\n");
-    printfd(__FILE__, "Version=%d\n", rawPacket.GetIPVersion());
-    printfd(__FILE__, "HeaderLen=%d\n", rawPacket.GetHeaderLen());
-    printfd(__FILE__, "PacketLen=%d\n", rawPacket.GetLen());
-    printfd(__FILE__, "SIP=%s\n", inet_ntostring(rawPacket.GetSrcIP()).c_str());
-    printfd(__FILE__, "DIP=%s\n", inet_ntostring(rawPacket.GetDstIP()).c_str());
-    printfd(__FILE__, "src port=%d\n", rawPacket.GetSrcPort());
-    printfd(__FILE__, "pst port=%d\n", rawPacket.GetDstPort());
-    printfd(__FILE__, "len=%d\n", rawPacket.GetLen());
-    printfd(__FILE__, "proto=%d\n", rawPacket.GetProto());
-    printfd(__FILE__, "PacketDirU=%d\n", pi->second.dirU);
-    printfd(__FILE__, "PacketDirD=%d\n", pi->second.dirD);
-    printfd(__FILE__, "=============================\n");*/
-    return;
-    }
-
-PACKET_EXTRA_DATA ed;
-
-// Packet not found - add new packet
-
-ed.updateTime = stgTime;
-ed.flushTime = stgTime;
-
-/*
- userU is that whose user_ip == packet_ip_src
- userD is that whose user_ip == packet_ip_dst
- */
-
-uint32_t ipU = rawPacket.GetSrcIP();
-uint32_t ipD = rawPacket.GetDstIP();
-
-// Searching users with such IP
-if (users->FindByIPIdx(ipU, &ed.userU) == 0)
-    {
-    ed.userUPresent = true;
-    }
-
-if (users->FindByIPIdx(ipD, &ed.userD) == 0)
-    {
-    ed.userDPresent = true;
-    }
-
-if (ed.userUPresent ||
-    ed.userDPresent)
-    {
-    DeterminateDir(rawPacket, &ed.dirU, &ed.dirD);
-
-    ed.lenD = ed.lenU = rawPacket.GetLen();
-
-    //TODO use result of lower_bound to inserting new record
-
-    // Adding packet to a tree.
-    std::pair<pp_iter, bool> insertResult = packets.insert(std::make_pair(rawPacket, ed));
-    pp_iter newPacket = insertResult.first;
-
-    // Adding packet reference to an IP index.
-    ip2packets.insert(std::make_pair(ipU, newPacket));
-    ip2packets.insert(std::make_pair(ipD, newPacket));
-    }
-}
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::FlushAndRemove()
-{
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-
-int oldPacketsSize = packets.size();
-int oldIp2packetsSize = ip2packets.size();
-
-pp_iter pi;
-pi = packets.begin();
-std::map<RAW_PACKET, PACKET_EXTRA_DATA> newPackets;
-ip2packets.erase(ip2packets.begin(), ip2packets.end());
-while (pi != packets.end())
-    {
-    //Flushing
-    if (stgTime - pi->second.flushTime > FLUSH_TIME)
-        {
-        if (pi->second.userUPresent)
-            {
-            //printfd(__FILE__, "+++ Flushing U user %s (%s:%d) of length %d\n", pi->second.userU->GetLogin().c_str(), inet_ntostring(pi->first.GetSrcIP()).c_str(), pi->first.GetSrcPort(), pi->second.lenU);
-
-            // Add stat
-            if (pi->second.dirU < DIR_NUM)
-                {
-                #ifdef TRAFF_STAT_WITH_PORTS
-                pi->second.userU->AddTraffStatU(pi->second.dirU,
-                                                pi->first.GetDstIP(),
-                                                pi->first.GetDstPort(),
-                                                pi->second.lenU);
-                #else
-                pi->second.userU->AddTraffStatU(pi->second.dirU,
-                                                pi->first.GetDstIP(),
-                                                pi->second.lenU);
-                #endif
-                }
-
-            pi->second.lenU = 0;
-            pi->second.flushTime = stgTime;
-            }
-
-        if (pi->second.userDPresent)
-            {
-            //printfd(__FILE__, "+++ Flushing D user %s (%s:%d) of length %d\n", pi->second.userD->GetLogin().c_str(), inet_ntostring(pi->first.GetDstIP()).c_str(), pi->first.GetDstPort(), pi->second.lenD);
-
-            // Add stat
-            if (pi->second.dirD < DIR_NUM)
-                {
-                #ifdef TRAFF_STAT_WITH_PORTS
-                pi->second.userD->AddTraffStatD(pi->second.dirD,
-                                                pi->first.GetSrcIP(),
-                                                pi->first.GetSrcPort(),
-                                                pi->second.lenD);
-                #else
-                pi->second.userD->AddTraffStatD(pi->second.dirD,
-                                                pi->first.GetSrcIP(),
-                                                pi->second.lenD);
-                #endif
-                }
-
-            pi->second.lenD = 0;
-            pi->second.flushTime = stgTime;
-            }
-        }
-
-    /*//Removing
-    if (stgTime - pi->second.updateTime > REMOVE_TIME)
-        {
-        // Remove packet and references from ip2packets index
-        //printfd(__FILE__, "+++ Removing +++\n");
-        pair<ip2p_iter, ip2p_iter> be(
-                ip2packets.equal_range(pi->first.GetSrcIP()));
-        while (be.first != be.second)
-            {
-            // Have a reference to a packet?
-            if (be.first->second == pi)
-                {
-                ip2packets.erase(be.first++);
-                //printfd(__FILE__, "Remove U from ip2packets %s\n", inet_ntostring(pi->first.GetSrcIP()).c_str());
-                }
-            else
-                {
-                ++be.first;
-                }
-            }
-
-        //printfd(__FILE__, "-------------------\n");
-        be = ip2packets.equal_range(pi->first.GetDstIP());
-        while (be.first != be.second)
-            {
-            // Have a reference to a packet?
-            if (be.first->second == pi)
-                {
-                ip2packets.erase(be.first++);
-                //printfd(__FILE__, "Remove D from ip2packets %s\n", inet_ntostring(pi->first.GetDstIP()).c_str());
-                }
-            else
-                {
-                ++be.first;
-                }
-            }
-        //printfd(__FILE__, "Remove packet\n");
-        packets.erase(pi++);
-        }
-    else
-        {
-        ++pi;
-        }*/
-    if (stgTime - pi->second.updateTime < REMOVE_TIME)
-        {
-        std::pair<pp_iter, bool> res = newPackets.insert(*pi);
-        if (res.second)
-            {
-            ip2packets.insert(std::make_pair(pi->first.GetSrcIP(), res.first));
-            ip2packets.insert(std::make_pair(pi->first.GetDstIP(), res.first));
-            }
-        }
-    ++pi;
-    }
-swap(packets, newPackets);
-printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n",
-        packets.size(),
-        oldPacketsSize - packets.size(),
-        ip2packets.size(),
-        oldIp2packetsSize - ip2packets.size());
-
-}
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::AddUser(USER_PTR user)
-{
-printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str());
-uint32_t uip = user->GetCurrIP();
-std::pair<ip2p_iter, ip2p_iter> pi;
-
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-// Find all packets with IP belongs to this user
-pi = ip2packets.equal_range(uip);
-
-while (pi.first != pi.second)
-    {
-    if (pi.first->second->first.GetSrcIP() == uip)
-        {
-        assert((!pi.first->second->second.userUPresent || 
-                 pi.first->second->second.userU == user) &&
-               "U user present but it's not current user");
-
-        pi.first->second->second.lenU = 0;
-        pi.first->second->second.userU = user;
-        pi.first->second->second.userUPresent = true;
-        }
-
-    if (pi.first->second->first.GetDstIP() == uip)
-        {
-        assert((!pi.first->second->second.userDPresent || 
-                 pi.first->second->second.userD == user) &&
-               "D user present but it's not current user");
-
-        pi.first->second->second.lenD = 0;
-        pi.first->second->second.userD = user;
-        pi.first->second->second.userDPresent = true;
-        }
-
-    ++pi.first;
-    }
-}
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::DelUser(uint32_t uip)
-{
-printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
-std::pair<ip2p_iter, ip2p_iter> pi;
-
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-pi = ip2packets.equal_range(uip);
-
-while (pi.first != pi.second)
-    {
-    if (pi.first->second->first.GetSrcIP() == uip)
-        {
-        if (pi.first->second->second.dirU < DIR_NUM && pi.first->second->second.userUPresent)
-            {
-            #ifdef TRAFF_STAT_WITH_PORTS
-            pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
-                                                          pi.first->second->first.GetDstIP(),
-                                                          pi.first->second->first.GetDstPort(),
-                                                          pi.first->second->second.lenU);
-            #else
-            pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
-                                                          pi.first->second->first.GetDstIP(),
-                                                          pi.first->second->second.lenU);
-            #endif
-            }
-        pi.first->second->second.userUPresent = false;
-        }
-
-    if (pi.first->second->first.GetDstIP() == uip)
-        {
-        if (pi.first->second->second.dirD < DIR_NUM && pi.first->second->second.userDPresent)
-            {
-            #ifdef TRAFF_STAT_WITH_PORTS
-            pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
-                                                          pi.first->second->first.GetSrcIP(),
-                                                          pi.first->second->first.GetSrcPort(),
-                                                          pi.first->second->second.lenD);
-            #else
-            pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
-                                                          pi.first->second->first.GetSrcIP(),
-                                                          pi.first->second->second.lenD);
-            #endif
-            }
-
-        pi.first->second->second.userDPresent = false;
-        }
-
-    ++pi.first;
-    }
-
-ip2packets.erase(pi.first, pi.second);
-}
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::SetUserNotifiers(USER_PTR user)
-{
-// Adding user. Adding notifiers to user.
-TRF_IP_BEFORE ipBNotifier(*this, user);
-ipBeforeNotifiers.push_front(ipBNotifier);
-user->AddCurrIPBeforeNotifier(&(*ipBeforeNotifiers.begin()));
-
-TRF_IP_AFTER ipANotifier(*this, user);
-ipAfterNotifiers.push_front(ipANotifier);
-user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
-}
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::UnSetUserNotifiers(USER_PTR user)
-{
-// Removing user. Removing notifiers from user.
-std::list<TRF_IP_BEFORE>::iterator bi;
-std::list<TRF_IP_AFTER>::iterator ai;
-
-bi = ipBeforeNotifiers.begin();
-while (bi != ipBeforeNotifiers.end())
-    {
-    if (user->GetLogin() == bi->GetUser()->GetLogin())
-        {
-        user->DelCurrIPBeforeNotifier(&(*bi));
-        ipBeforeNotifiers.erase(bi);
-        break;
-        }
-    ++bi;
-    }
-
-ai = ipAfterNotifiers.begin();
-while (ai != ipAfterNotifiers.end())
-    {
-    if (user->GetLogin() == ai->GetUser()->GetLogin())
-        {
-        user->DelCurrIPAfterNotifier(&(*ai));
-        ipAfterNotifiers.erase(ai);
-        break;
-        }
-    ++ai;
-    }
-}
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::DeterminateDir(const RAW_PACKET & packet,
-                                 int * dirU, // Direction for incoming packet
-                                 int * dirD) const // Direction for outgoing packet
-{
-bool addrMatchU;
-bool portMatchU;
-bool addrMatchD;
-bool portMatchD;
-bool foundU = false; // Was rule for U found ?
-bool foundD = false; // Was rule for D found ?
-//printfd(__FILE__, "foundU=%d, foundD=%d\n", foundU, foundD);
-
-enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
-
-std::list<RULE>::const_iterator ln;
-ln = rules.begin();
-
-while (ln != rules.end())
-    {
-    if (!foundU)
-        {
-        addrMatchU = false;
-        portMatchU = false;
-
-        switch (ln->proto)
-            {
-            case all:
-                portMatchU = true;
-                break;
-
-            case icmp:
-                portMatchU = (packet.GetProto() == ICMP_RPOTO);
-                break;
-
-            case tcp_udp:
-                if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
-                    portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
-                break;
-
-            case tcp:
-                if (packet.GetProto() == TCP_PROTO)
-                    portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
-                break;
-
-            case udp:
-                if (packet.GetProto() == UDP_PROTO)
-                    portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
-                break;
-
-            default:
-                printfd(__FILE__, "Error! Incorrect rule!\n");
-                break;
-            }
-
-        addrMatchU = (packet.GetDstIP() & ln->mask) == ln->ip;
-
-        if (!foundU && addrMatchU && portMatchU)
-            {
-            foundU = true;
-            *dirU = ln->dir;
-            //printfd(__FILE__, "Up rule ok! %d\n", ln->dir);
-            //PrintRule(ln->rule);
-            }
-
-        } //if (!foundU)
-
-    if (!foundD)
-        {
-        addrMatchD = false;
-        portMatchD = false;
-
-        switch (ln->proto)
-            {
-            case all:
-                portMatchD = true;
-                break;
-
-            case icmp:
-                portMatchD = (packet.GetProto() == ICMP_RPOTO);
-                break;
-
-            case tcp_udp:
-                if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
-                    portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
-                break;
-
-            case tcp:
-                if (packet.GetProto() == TCP_PROTO)
-                    portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
-                break;
-
-            case udp:
-                if (packet.GetProto() == UDP_PROTO)
-                    portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
-                break;
-
-            default:
-                printfd(__FILE__, "Error! Incorrect rule!\n");
-                break;
-            }
-
-        addrMatchD = (packet.GetSrcIP() & ln->mask) == ln->ip;
-
-        if (!foundD && addrMatchD && portMatchD)
-            {
-            foundD = true;
-            *dirD = ln->dir;
-            //printfd(__FILE__, "Down rule ok! %d\n", ln->dir);
-            //PrintRule(ln->rule);
-            }
-        } //if (!foundD)
-
-    ++ln;
-    }   //while (ln != rules.end())
-
-if (!foundU)
-    *dirU = DIR_NUM;
-
-if (!foundD)
-    *dirD = DIR_NUM;
-
-return;
-};
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::SetRulesFile(const std::string & fn)
-{
-rulesFileName = fn;
-}
-//-----------------------------------------------------------------------------
-bool TRAFFCOUNTER::ReadRules(bool test)
-{
-//printfd(__FILE__, "TRAFFCOUNTER::ReadRules()\n");
-
-RULE rul;
-FILE * f;
-char str[1024];
-char tp[100];   // protocol
-char ta[100];   // address
-char td[100];   // target direction
-int r;
-int lineNumber = 0;
-f = fopen(rulesFileName.c_str(), "rt");
-
-if (!f)
-    {
-    WriteServLog("File %s cannot be oppened.", rulesFileName.c_str());
-    return true;
-    }
-
-while (fgets(str, 1023, f))
-    {
-    lineNumber++;
-    if (str[strspn(str," \t")] == '#' || str[strspn(str," \t")] == '\n')
-        {
-        continue;
-        }
-
-    r = sscanf(str,"%s %s %s", tp, ta, td);
-    if (r != 3)
-        {
-        WriteServLog("Error in file %s. There must be 3 parameters. Line %d.", rulesFileName.c_str(), lineNumber);
-        fclose(f);
-        return true;
-        }
-
-    rul.proto = 0xff;
-    rul.dir = 0xff;
-
-    for (int i = 0; i < PROTOMAX; i++)
-        {
-        if (strcasecmp(tp, protoName[i]) == 0)
-            rul.proto = i;
-        }
-
-    for (int i = 0; i < DIR_NUM + 1; i++)
-        {
-        if (td == dirName[i])
-            rul.dir = i;
-        }
-
-    if (rul.dir == 0xff || rul.proto == 0xff)
-        {
-        WriteServLog("Error in file %s. Line %d.",
-                     rulesFileName.c_str(), lineNumber);
-        fclose(f);
-        return true;
-        }
-
-    if (ParseAddress(ta, &rul) != 0)
-        {
-        WriteServLog("Error in file %s. Error in adress. Line %d.",
-                     rulesFileName.c_str(), lineNumber);
-        fclose(f);
-        return true;
-        }
-    if (!test)
-        rules.push_back(rul);
-    //PrintRule(rul);
-    }
-
-fclose(f);
-
-// Adding lastest rule: ALL 0.0.0.0/0 NULL
-rul.dir = DIR_NUM; //NULL
-rul.ip = 0;  //0.0.0.0
-rul.mask = 0;
-rul.port1 = 0;
-rul.port2 = 65535;
-rul.proto = all;
-
-if (!test)
-    rules.push_back(rul);
-
-//PrintRule(rul);
-
-return false;
-}
-//-----------------------------------------------------------------------------
-int TRAFFCOUNTER::Reload()
-{
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-
-if (ReadRules(true))
-    {
-    WriteServLog("TRAFFCOUNTER: Cannot reload rules. Errors found.");
-    return -1;
-    }
-
-FreeRules();
-ReadRules();
-WriteServLog("TRAFFCOUNTER: Reload rules successfull.");
-return 0;
-}
-//-----------------------------------------------------------------------------
-bool TRAFFCOUNTER::ParseAddress(const char * ta, RULE * rule) const
-{
-char addr[50], mask[20], port1[20], port2[20], ports[40];
-
-int len = strlen(ta);
-char n = 0;
-int i, p;
-memset(addr, 0, sizeof(addr));
-for (i = 0; i < len; i++)
-    {
-    if (ta[i] == '/' || ta[i] == ':')
-        {
-        addr[i] = 0;
-        n = ta[i];
-        break;
-        }
-    addr[i] = ta[i];
-    n = 0;
-    }
-addr[i + 1] = 0;
-p = i + 1;
-
-if (n == '/')
-    {
-    // mask
-    for (; i < len; i++)
-        {
-        if (ta[i] == ':')
-            {
-            mask[i - p] = 0;
-            n = ':';
-            break;
-            }
-        mask[i - p] = ta[i];
-        }
-    mask[i - p] = 0;
-    }
-else
-    {
-    strcpy(mask, "32");
-    }
-
-p = i + 1;
-i++;
-
-if (n == ':')
-    {
-    // port
-    if (!(rule->proto == tcp || rule->proto == udp || rule->proto == tcp_udp))
-        {
-        WriteServLog("No ports specified for this protocol.");
-        return true;
-        }
-
-    for (; i < len; i++)
-        ports[i - p] = ta[i];
-
-    ports[i - p] = 0;
-    }
-else
-    {
-    strcpy(ports, "0-65535");
-    }
-
-char *sss;
-char pts[100];
-strcpy(pts, ports);
-
-if ((sss = strchr(ports, '-')) != NULL)
-    {
-    strncpy(port1, ports, int(sss-ports));
-    port1[int(sss - ports)] = 0;
-    strcpy(port2, sss + 1);
-    }
-else
-    {
-    strcpy(port1, ports);
-    strcpy(port2, ports);
-    }
-
-// Convert strings to mask, ports and IP
-int prt1, prt2, msk;
-unsigned ip;
-char *res;
-
-msk = strtol(mask, &res, 10);
-if (*res != 0)
-    return true;
-
-prt1 = strtol(port1, &res, 10);
-if (*res != 0)
-    return true;
-
-prt2 = strtol(port2, &res, 10);
-if (*res != 0)
-    return true;
-
-int r = inet_aton(addr, (struct in_addr*)&ip);
-if (r == 0)
-    return true;
-
-rule->ip = ip;
-rule->mask = CalcMask(msk);
-//msk = 1;
-//printfd(__FILE__, "msk=%d mask=%08X   mask=%08X\n", msk, rule->mask, (0xFFffFFff << (32 - msk)));
-
-if ((ip & rule->mask) != ip)
-    {
-    WriteServLog("Address does'n match mask.");
-    return true;
-    }
-
-rule->port1 = prt1;
-rule->port2 = prt2;
-
-return false;
-}
-//-----------------------------------------------------------------------------
-uint32_t TRAFFCOUNTER::CalcMask(uint32_t msk) const
-{
-if (msk >= 32) return 0xFFffFFff;
-if (msk == 0) return 0;
-return htonl(0xFFffFFff << (32 - msk));
-}
-//---------------------------------------------------------------------------
-void TRAFFCOUNTER::FreeRules()
-{
-rules.clear();
-}
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::PrintRule(RULE rule) const
-{
-printf("%15s   ", inet_ntostring(rule.ip).c_str());
-printf("mask=%08X ", rule.mask);
-printf("port1=%5d ", rule.port1);
-printf("port2=%5d ", rule.port2);
-switch (rule.proto)
-    {
-    case 0:
-        printf("TCP     ");
-        break;
-    case 1:
-        printf("UDP     ");
-        break;
-    case 2:
-        printf("ICMP    ");
-        break;
-    case 3:
-        printf("TCP_UDP ");
-        break;
-    case 4:
-        printf("ALL     ");
-        break;
-    }
-printf("dir=%d \n", rule.dir);
-return;
-}
-//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::SetMonitorDir(const std::string & monitorDir)
-{
-TRAFFCOUNTER::monitorDir = monitorDir;
-monitoring = (monitorDir != "");
-}
-//-----------------------------------------------------------------------------
diff --git a/projects/stargazer/traffcounter.h b/projects/stargazer/traffcounter.h
deleted file mode 100644 (file)
index d6a5688..0000000
+++ /dev/null
@@ -1,264 +0,0 @@
-/*
- *    This program is free software; you can redistribute it and/or modify
- *    it under the terms of the GNU General Public License as published by
- *    the Free Software Foundation; either version 2 of the License, or
- *    (at your option) any later version.
- *
- *    This program is distributed in the hope that it will be useful,
- *    but WITHOUT ANY WARRANTY; without even the implied warranty of
- *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *    GNU General Public License for more details.
- *
- *    You should have received a copy of the GNU General Public License
- *    along with this program; if not, write to the Free Software
- *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-/*
- *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
- */
-
- /*
- $Revision: 1.23 $
- $Date: 2010/04/22 12:57:46 $
- $Author: faust $
- */
-
-
-#ifndef TRAFFCOUNTER_H
-#define TRAFFCOUNTER_H
-
-#include <pthread.h>
-#include <ctime>
-#include <list>
-#include <map>
-#include <string>
-
-#include "os_int.h"
-#include "stg_logger.h"
-#include "raw_ip_packet.h"
-#include "users.h"
-#include "actions.h"
-#include "noncopyable.h"
-#include "eventloop.h"
-
-#define PROTOMAX    (5)
-
-class TARIFFS;
-
-//-----------------------------------------------------------------------------
-struct RULE
-{
-uint32_t    ip;             // IP
-uint32_t    mask;           // Network mask
-uint16_t    port1;          // Min port
-uint16_t    port2;          // Max port
-uint8_t     proto;          // Protocol
-uint32_t    dir;            // Direction
-};
-//-----------------------------------------------------------------------------
-struct PACKET_EXTRA_DATA
-{
-PACKET_EXTRA_DATA()
-    : flushTime(0),
-      updateTime(0),
-      userU(),
-      userUPresent(false),
-      userD(),
-      userDPresent(false),
-      dirU(DIR_NUM),
-      dirD(DIR_NUM),
-      lenU(0),
-      lenD(0)
-{};
-
-PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp)
-    : flushTime(pp.flushTime),
-      updateTime(pp.updateTime),
-      userU(pp.userU),
-      userUPresent(pp.userUPresent),
-      userD(pp.userD),
-      userDPresent(pp.userDPresent),
-      dirU(pp.dirU),
-      dirD(pp.dirD),
-      lenU(pp.lenU),
-      lenD(pp.lenD)
-{};
-
-time_t      flushTime;          // Last flush time
-time_t      updateTime;         // Last update time
-USER_PTR    userU;              // Uploader
-bool        userUPresent;       // Uploader is registered user
-USER_PTR    userD;              // Downloader
-bool        userDPresent;       // Downloader is registered user
-int         dirU;               // Upload direction
-int         dirD;               // Download direction
-uint32_t    lenU;               // Upload length
-uint32_t    lenD;               // Download length
-};
-//-----------------------------------------------------------------------------
-class TRAFFCOUNTER;
-//-----------------------------------------------------------------------------
-class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t>
-{
-public:
-                    TRF_IP_BEFORE(TRAFFCOUNTER & t, USER_PTR u)
-                        : PROPERTY_NOTIFIER_BASE<uint32_t>(),
-                          traffCnt(t),
-                          user(u)
-                    {};
-    void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
-    void            SetUser(USER_PTR u) { user = u; }
-    USER_PTR        GetUser() const { return user; }
-
-private:
-    TRAFFCOUNTER &  traffCnt;
-    USER_PTR        user;
-};
-//-----------------------------------------------------------------------------
-class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t>
-{
-public:
-                    TRF_IP_AFTER(TRAFFCOUNTER & t, USER_PTR u)
-                        : PROPERTY_NOTIFIER_BASE<uint32_t>(),
-                          traffCnt(t),
-                          user(u)
-                    {};
-    void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
-    void            SetUser(USER_PTR u) { user = u; }
-    USER_PTR        GetUser() const { return user; }
-private:
-    TRAFFCOUNTER &  traffCnt;
-    USER_PTR        user;
-};
-//-----------------------------------------------------------------------------
-class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR>
-{
-public:
-            ADD_USER_NONIFIER(TRAFFCOUNTER & t) :
-                NOTIFIER_BASE<USER_PTR>(),
-                traffCnt(t) {};
-    virtual ~ADD_USER_NONIFIER(){};
-    void    Notify(const USER_PTR & user);
-private:
-    TRAFFCOUNTER & traffCnt;
-};
-//-----------------------------------------------------------------------------
-class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR>
-{
-public:
-            DEL_USER_NONIFIER(TRAFFCOUNTER & t) :
-                NOTIFIER_BASE<USER_PTR>(),
-                traffCnt(t) {};
-    virtual ~DEL_USER_NONIFIER(){};
-    void    Notify(const USER_PTR & user);
-private:
-    TRAFFCOUNTER & traffCnt;
-};
-//-----------------------------------------------------------------------------
-class TRAFFCOUNTER : private NONCOPYABLE
-{
-friend class ADD_USER_NONIFIER;
-friend class DEL_USER_NONIFIER;
-friend class TRF_IP_BEFORE;
-friend class TRF_IP_AFTER;
-public:
-    TRAFFCOUNTER(USERS * users, const TARIFFS * tariffs, const std::string & rulesFileName);
-    ~TRAFFCOUNTER();
-
-    void        SetRulesFile(const std::string & rulesFileName);
-
-    int         Reload();
-    int         Start();
-    int         Stop();
-
-    void        Process(const RAW_PACKET & rawPacket);
-    void        SetMonitorDir(const std::string & monitorDir);
-
-private:
-    bool        ParseAddress(const char * ta, RULE * rule) const;
-    uint32_t    CalcMask(uint32_t msk) const;
-    void        FreeRules();
-    void        PrintRule(RULE rule) const;
-    bool        ReadRules(bool test = false);
-
-    static void * Run(void * data);
-
-    void        DeterminateDir(const RAW_PACKET & packet,
-                               int * dirU, // Direction for upload
-                               int * dirD) const; // Direction for download
-
-    void        FlushAndRemove();
-
-    void        AddUser(USER_PTR user);
-    void        DelUser(uint32_t uip);
-    void        SetUserNotifiers(USER_PTR user);
-    void        UnSetUserNotifiers(USER_PTR user);
-
-    std::list<RULE>  rules;
-    typedef std::list<RULE>::iterator rule_iter;
-
-    std::map<RAW_PACKET, PACKET_EXTRA_DATA> packets; // Packets tree
-    typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA>::iterator pp_iter;
-
-    std::multimap<uint32_t, pp_iter> ip2packets; // IP-to-Packet index
-
-    typedef std::multimap<uint32_t, pp_iter>::iterator ip2p_iter;
-    typedef std::multimap<uint32_t, pp_iter>::const_iterator ip2p_citer;
-
-    std::string dirName[DIR_NUM + 1];
-
-    STG_LOGGER & WriteServLog;
-    std::string rulesFileName;
-
-    std::string monitorDir;
-    bool        monitoring;
-
-    USERS *     users;
-
-    bool        running;
-    bool        stopped;
-    pthread_mutex_t         mutex;
-    pthread_t               thread;
-
-    std::list<TRF_IP_BEFORE>     ipBeforeNotifiers;
-    std::list<TRF_IP_AFTER>      ipAfterNotifiers;
-
-    ADD_USER_NONIFIER       addUserNotifier;
-    DEL_USER_NONIFIER       delUserNotifier;
-};
-//-----------------------------------------------------------------------------
-inline
-void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
-{
-// User changes his address. Remove old IP
-if (!oldValue)
-    return;
-
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, oldValue);
-}
-//-----------------------------------------------------------------------------
-inline
-void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
-{
-// User changes his address. Add new IP
-if (!newValue)
-    return;
-
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::AddUser, user);
-}
-//-----------------------------------------------------------------------------
-inline
-void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
-{
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::SetUserNotifiers, user);
-}
-//-----------------------------------------------------------------------------
-inline
-void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
-{
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::UnSetUserNotifiers, user);
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, user->GetCurrIP());
-}
-//-----------------------------------------------------------------------------
-#endif //TRAFFCOUNTER_H
diff --git a/projects/stargazer/traffcounter_impl.cpp b/projects/stargazer/traffcounter_impl.cpp
new file mode 100644 (file)
index 0000000..f410b10
--- /dev/null
@@ -0,0 +1,944 @@
+/*
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ *
+ *    You should have received a copy of the GNU General Public License
+ *    along with this program; if not, write to the Free Software
+ *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ *    Date: 27.10.2002
+ */
+
+/*
+ *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
+ */
+
+/*
+ $Revision: 1.58 $
+ $Date: 2010/11/03 11:28:07 $
+ $Author: faust $
+ */
+
+/* inet_aton */
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <csignal>
+#include <cassert>
+#include <cstdio> // fopen and similar
+
+#include "traffcounter_impl.h"
+#include "common.h"
+#include "stg_locker.h"
+#include "stg_timer.h"
+
+#define FLUSH_TIME  (10)
+#define REMOVE_TIME  (31)
+
+const char protoName[PROTOMAX][8] =
+{"TCP", "UDP", "ICMP", "TCP_UDP", "ALL"};
+
+enum protoNum
+{
+tcp = 0, udp, icmp, tcp_udp, all
+};
+
+//-----------------------------------------------------------------------------
+TRAFFCOUNTER_IMPL::TRAFFCOUNTER_IMPL(USERS * u, const TARIFFS *, const std::string & fn)
+    : WriteServLog(GetStgLogger()),
+      rulesFileName(fn),
+      monitoring(false),
+      users(u),
+      running(false),
+      stopped(true),
+      addUserNotifier(*this),
+      delUserNotifier(*this)
+{
+for (int i = 0; i < DIR_NUM; i++)
+    strprintf(&dirName[i], "DIR%d", i);
+
+dirName[DIR_NUM] = "NULL";
+
+users->AddNotifierUserAdd(&addUserNotifier);
+users->AddNotifierUserDel(&delUserNotifier);
+
+pthread_mutex_init(&mutex, NULL);
+}
+//-----------------------------------------------------------------------------
+TRAFFCOUNTER_IMPL::~TRAFFCOUNTER_IMPL()
+{
+pthread_mutex_destroy(&mutex);
+}
+//-----------------------------------------------------------------------------
+int TRAFFCOUNTER_IMPL::Start()
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+
+if (!stopped)
+    return 0;
+
+if (ReadRules())
+    {
+    WriteServLog("TRAFFCOUNTER: Cannot read rules.");
+    return -1;
+    }
+
+printfd(__FILE__, "TRAFFCOUNTER::Start()\n");
+int h = users->OpenSearch();
+USER_PTR u;
+if (!h)
+    {
+    WriteServLog("TRAFFCOUNTER: Cannot get users.");
+    return -1;
+    }
+
+while (users->SearchNext(h, &u) == 0)
+    {
+    SetUserNotifiers(u);
+    }
+users->CloseSearch(h);
+
+running = true;
+if (pthread_create(&thread, NULL, Run, this))
+    {
+    WriteServLog("TRAFFCOUNTER: Error: Cannot start thread!");
+    return -1;
+    }
+return 0;
+}
+//-----------------------------------------------------------------------------
+int TRAFFCOUNTER_IMPL::Stop()
+{
+if (stopped)
+    return 0;
+
+running = false;
+
+int h = users->OpenSearch();
+if (!h)
+    {
+    WriteServLog("TRAFFCOUNTER: Fatal error: Cannot get users.");
+    return -1;
+    }
+
+USER_PTR u;
+while (users->SearchNext(h, &u) == 0)
+    {
+    UnSetUserNotifiers(u);
+    }
+users->CloseSearch(h);
+
+//5 seconds to thread stops itself
+struct timespec ts = {0, 200000000};
+for (int i = 0; i < 25 && !stopped; i++)
+    {
+    nanosleep(&ts, NULL);
+    }
+
+//after 5 seconds waiting thread still running. now kill it
+if (!stopped)
+    {
+    printfd(__FILE__, "kill TRAFFCOUNTER thread.\n");
+    if (pthread_kill(thread, SIGINT))
+        {
+        return -1;
+        }
+    printfd(__FILE__, "TRAFFCOUNTER killed\n");
+    }
+printfd(__FILE__, "TRAFFCOUNTER::Stop()\n");
+
+return 0;
+}
+//-----------------------------------------------------------------------------
+void * TRAFFCOUNTER_IMPL::Run(void * data)
+{
+TRAFFCOUNTER_IMPL * tc = static_cast<TRAFFCOUNTER_IMPL *>(data);
+tc->stopped = false;
+int c = 0;
+
+time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
+struct timespec ts = {0, 500000000};
+while (tc->running)
+    {
+    nanosleep(&ts, 0);
+    if (!tc->running)
+        {
+        tc->FlushAndRemove();
+        break;
+        }
+
+    if (tc->monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
+        {
+        std::string monFile(tc->monitorDir + "/traffcounter_r");
+        printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
+        touchTime = stgTime;
+        TouchFile(monFile.c_str());
+        }
+
+    if (++c % FLUSH_TIME == 0)
+        tc->FlushAndRemove();
+    }
+
+tc->stopped = true;
+return NULL;
+}
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::Process(const RAW_PACKET & rawPacket)
+{
+if (!running)
+    return;
+
+static time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
+
+if (monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
+    {
+    static std::string monFile = monitorDir + "/traffcounter_p";
+    printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", monitoring, monFile.c_str());
+    touchTime = stgTime;
+    TouchFile(monFile.c_str());
+    }
+
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+
+//printfd(__FILE__, "TRAFFCOUNTER::Process()\n");
+//TODO replace find with lower_bound.
+
+// Searching a new packet in a tree.
+pp_iter pi = packets.find(rawPacket);
+
+// Packet found - update length and time
+if (pi != packets.end())
+    {
+    pi->second.lenU += rawPacket.GetLen();
+    pi->second.lenD += rawPacket.GetLen();
+    pi->second.updateTime = stgTime;
+    /*printfd(__FILE__, "=============================\n");
+    printfd(__FILE__, "Packet found!\n");
+    printfd(__FILE__, "Version=%d\n", rawPacket.GetIPVersion());
+    printfd(__FILE__, "HeaderLen=%d\n", rawPacket.GetHeaderLen());
+    printfd(__FILE__, "PacketLen=%d\n", rawPacket.GetLen());
+    printfd(__FILE__, "SIP=%s\n", inet_ntostring(rawPacket.GetSrcIP()).c_str());
+    printfd(__FILE__, "DIP=%s\n", inet_ntostring(rawPacket.GetDstIP()).c_str());
+    printfd(__FILE__, "src port=%d\n", rawPacket.GetSrcPort());
+    printfd(__FILE__, "pst port=%d\n", rawPacket.GetDstPort());
+    printfd(__FILE__, "len=%d\n", rawPacket.GetLen());
+    printfd(__FILE__, "proto=%d\n", rawPacket.GetProto());
+    printfd(__FILE__, "PacketDirU=%d\n", pi->second.dirU);
+    printfd(__FILE__, "PacketDirD=%d\n", pi->second.dirD);
+    printfd(__FILE__, "=============================\n");*/
+    return;
+    }
+
+PACKET_EXTRA_DATA ed;
+
+// Packet not found - add new packet
+
+ed.updateTime = stgTime;
+ed.flushTime = stgTime;
+
+/*
+ userU is that whose user_ip == packet_ip_src
+ userD is that whose user_ip == packet_ip_dst
+ */
+
+uint32_t ipU = rawPacket.GetSrcIP();
+uint32_t ipD = rawPacket.GetDstIP();
+
+// Searching users with such IP
+if (users->FindByIPIdx(ipU, &ed.userU) == 0)
+    {
+    ed.userUPresent = true;
+    }
+
+if (users->FindByIPIdx(ipD, &ed.userD) == 0)
+    {
+    ed.userDPresent = true;
+    }
+
+if (ed.userUPresent ||
+    ed.userDPresent)
+    {
+    DeterminateDir(rawPacket, &ed.dirU, &ed.dirD);
+
+    ed.lenD = ed.lenU = rawPacket.GetLen();
+
+    //TODO use result of lower_bound to inserting new record
+
+    // Adding packet to a tree.
+    std::pair<pp_iter, bool> insertResult = packets.insert(std::make_pair(rawPacket, ed));
+    pp_iter newPacket = insertResult.first;
+
+    // Adding packet reference to an IP index.
+    ip2packets.insert(std::make_pair(ipU, newPacket));
+    ip2packets.insert(std::make_pair(ipD, newPacket));
+    }
+}
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::FlushAndRemove()
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+
+int oldPacketsSize = packets.size();
+int oldIp2packetsSize = ip2packets.size();
+
+pp_iter pi;
+pi = packets.begin();
+std::map<RAW_PACKET, PACKET_EXTRA_DATA> newPackets;
+ip2packets.erase(ip2packets.begin(), ip2packets.end());
+while (pi != packets.end())
+    {
+    //Flushing
+    if (stgTime - pi->second.flushTime > FLUSH_TIME)
+        {
+        if (pi->second.userUPresent)
+            {
+            //printfd(__FILE__, "+++ Flushing U user %s (%s:%d) of length %d\n", pi->second.userU->GetLogin().c_str(), inet_ntostring(pi->first.GetSrcIP()).c_str(), pi->first.GetSrcPort(), pi->second.lenU);
+
+            // Add stat
+            if (pi->second.dirU < DIR_NUM)
+                {
+                #ifdef TRAFF_STAT_WITH_PORTS
+                pi->second.userU->AddTraffStatU(pi->second.dirU,
+                                                pi->first.GetDstIP(),
+                                                pi->first.GetDstPort(),
+                                                pi->second.lenU);
+                #else
+                pi->second.userU->AddTraffStatU(pi->second.dirU,
+                                                pi->first.GetDstIP(),
+                                                pi->second.lenU);
+                #endif
+                }
+
+            pi->second.lenU = 0;
+            pi->second.flushTime = stgTime;
+            }
+
+        if (pi->second.userDPresent)
+            {
+            //printfd(__FILE__, "+++ Flushing D user %s (%s:%d) of length %d\n", pi->second.userD->GetLogin().c_str(), inet_ntostring(pi->first.GetDstIP()).c_str(), pi->first.GetDstPort(), pi->second.lenD);
+
+            // Add stat
+            if (pi->second.dirD < DIR_NUM)
+                {
+                #ifdef TRAFF_STAT_WITH_PORTS
+                pi->second.userD->AddTraffStatD(pi->second.dirD,
+                                                pi->first.GetSrcIP(),
+                                                pi->first.GetSrcPort(),
+                                                pi->second.lenD);
+                #else
+                pi->second.userD->AddTraffStatD(pi->second.dirD,
+                                                pi->first.GetSrcIP(),
+                                                pi->second.lenD);
+                #endif
+                }
+
+            pi->second.lenD = 0;
+            pi->second.flushTime = stgTime;
+            }
+        }
+
+    /*//Removing
+    if (stgTime - pi->second.updateTime > REMOVE_TIME)
+        {
+        // Remove packet and references from ip2packets index
+        //printfd(__FILE__, "+++ Removing +++\n");
+        pair<ip2p_iter, ip2p_iter> be(
+                ip2packets.equal_range(pi->first.GetSrcIP()));
+        while (be.first != be.second)
+            {
+            // Have a reference to a packet?
+            if (be.first->second == pi)
+                {
+                ip2packets.erase(be.first++);
+                //printfd(__FILE__, "Remove U from ip2packets %s\n", inet_ntostring(pi->first.GetSrcIP()).c_str());
+                }
+            else
+                {
+                ++be.first;
+                }
+            }
+
+        //printfd(__FILE__, "-------------------\n");
+        be = ip2packets.equal_range(pi->first.GetDstIP());
+        while (be.first != be.second)
+            {
+            // Have a reference to a packet?
+            if (be.first->second == pi)
+                {
+                ip2packets.erase(be.first++);
+                //printfd(__FILE__, "Remove D from ip2packets %s\n", inet_ntostring(pi->first.GetDstIP()).c_str());
+                }
+            else
+                {
+                ++be.first;
+                }
+            }
+        //printfd(__FILE__, "Remove packet\n");
+        packets.erase(pi++);
+        }
+    else
+        {
+        ++pi;
+        }*/
+    if (stgTime - pi->second.updateTime < REMOVE_TIME)
+        {
+        std::pair<pp_iter, bool> res = newPackets.insert(*pi);
+        if (res.second)
+            {
+            ip2packets.insert(std::make_pair(pi->first.GetSrcIP(), res.first));
+            ip2packets.insert(std::make_pair(pi->first.GetDstIP(), res.first));
+            }
+        }
+    ++pi;
+    }
+swap(packets, newPackets);
+printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n",
+        packets.size(),
+        oldPacketsSize - packets.size(),
+        ip2packets.size(),
+        oldIp2packetsSize - ip2packets.size());
+
+}
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::AddUser(USER_PTR user)
+{
+printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str());
+uint32_t uip = user->GetCurrIP();
+std::pair<ip2p_iter, ip2p_iter> pi;
+
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+// Find all packets with IP belongs to this user
+pi = ip2packets.equal_range(uip);
+
+while (pi.first != pi.second)
+    {
+    if (pi.first->second->first.GetSrcIP() == uip)
+        {
+        assert((!pi.first->second->second.userUPresent || 
+                 pi.first->second->second.userU == user) &&
+               "U user present but it's not current user");
+
+        pi.first->second->second.lenU = 0;
+        pi.first->second->second.userU = user;
+        pi.first->second->second.userUPresent = true;
+        }
+
+    if (pi.first->second->first.GetDstIP() == uip)
+        {
+        assert((!pi.first->second->second.userDPresent || 
+                 pi.first->second->second.userD == user) &&
+               "D user present but it's not current user");
+
+        pi.first->second->second.lenD = 0;
+        pi.first->second->second.userD = user;
+        pi.first->second->second.userDPresent = true;
+        }
+
+    ++pi.first;
+    }
+}
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::DelUser(uint32_t uip)
+{
+printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
+std::pair<ip2p_iter, ip2p_iter> pi;
+
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+pi = ip2packets.equal_range(uip);
+
+while (pi.first != pi.second)
+    {
+    if (pi.first->second->first.GetSrcIP() == uip)
+        {
+        if (pi.first->second->second.dirU < DIR_NUM && pi.first->second->second.userUPresent)
+            {
+            #ifdef TRAFF_STAT_WITH_PORTS
+            pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
+                                                          pi.first->second->first.GetDstIP(),
+                                                          pi.first->second->first.GetDstPort(),
+                                                          pi.first->second->second.lenU);
+            #else
+            pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
+                                                          pi.first->second->first.GetDstIP(),
+                                                          pi.first->second->second.lenU);
+            #endif
+            }
+        pi.first->second->second.userUPresent = false;
+        }
+
+    if (pi.first->second->first.GetDstIP() == uip)
+        {
+        if (pi.first->second->second.dirD < DIR_NUM && pi.first->second->second.userDPresent)
+            {
+            #ifdef TRAFF_STAT_WITH_PORTS
+            pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
+                                                          pi.first->second->first.GetSrcIP(),
+                                                          pi.first->second->first.GetSrcPort(),
+                                                          pi.first->second->second.lenD);
+            #else
+            pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
+                                                          pi.first->second->first.GetSrcIP(),
+                                                          pi.first->second->second.lenD);
+            #endif
+            }
+
+        pi.first->second->second.userDPresent = false;
+        }
+
+    ++pi.first;
+    }
+
+ip2packets.erase(pi.first, pi.second);
+}
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::SetUserNotifiers(USER_PTR user)
+{
+// Adding user. Adding notifiers to user.
+TRF_IP_BEFORE ipBNotifier(*this, user);
+ipBeforeNotifiers.push_front(ipBNotifier);
+user->AddCurrIPBeforeNotifier(&(*ipBeforeNotifiers.begin()));
+
+TRF_IP_AFTER ipANotifier(*this, user);
+ipAfterNotifiers.push_front(ipANotifier);
+user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
+}
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::UnSetUserNotifiers(USER_PTR user)
+{
+// Removing user. Removing notifiers from user.
+std::list<TRF_IP_BEFORE>::iterator bi;
+std::list<TRF_IP_AFTER>::iterator ai;
+
+bi = ipBeforeNotifiers.begin();
+while (bi != ipBeforeNotifiers.end())
+    {
+    if (user->GetLogin() == bi->GetUser()->GetLogin())
+        {
+        user->DelCurrIPBeforeNotifier(&(*bi));
+        ipBeforeNotifiers.erase(bi);
+        break;
+        }
+    ++bi;
+    }
+
+ai = ipAfterNotifiers.begin();
+while (ai != ipAfterNotifiers.end())
+    {
+    if (user->GetLogin() == ai->GetUser()->GetLogin())
+        {
+        user->DelCurrIPAfterNotifier(&(*ai));
+        ipAfterNotifiers.erase(ai);
+        break;
+        }
+    ++ai;
+    }
+}
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::DeterminateDir(const RAW_PACKET & packet,
+                                       int * dirU, // Direction for incoming packet
+                                       int * dirD) const // Direction for outgoing packet
+{
+bool addrMatchU;
+bool portMatchU;
+bool addrMatchD;
+bool portMatchD;
+bool foundU = false; // Was rule for U found ?
+bool foundD = false; // Was rule for D found ?
+//printfd(__FILE__, "foundU=%d, foundD=%d\n", foundU, foundD);
+
+enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
+
+std::list<RULE>::const_iterator ln;
+ln = rules.begin();
+
+while (ln != rules.end())
+    {
+    if (!foundU)
+        {
+        addrMatchU = false;
+        portMatchU = false;
+
+        switch (ln->proto)
+            {
+            case all:
+                portMatchU = true;
+                break;
+
+            case icmp:
+                portMatchU = (packet.GetProto() == ICMP_RPOTO);
+                break;
+
+            case tcp_udp:
+                if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
+                    portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
+                break;
+
+            case tcp:
+                if (packet.GetProto() == TCP_PROTO)
+                    portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
+                break;
+
+            case udp:
+                if (packet.GetProto() == UDP_PROTO)
+                    portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
+                break;
+
+            default:
+                printfd(__FILE__, "Error! Incorrect rule!\n");
+                break;
+            }
+
+        addrMatchU = (packet.GetDstIP() & ln->mask) == ln->ip;
+
+        if (!foundU && addrMatchU && portMatchU)
+            {
+            foundU = true;
+            *dirU = ln->dir;
+            //printfd(__FILE__, "Up rule ok! %d\n", ln->dir);
+            //PrintRule(ln->rule);
+            }
+
+        } //if (!foundU)
+
+    if (!foundD)
+        {
+        addrMatchD = false;
+        portMatchD = false;
+
+        switch (ln->proto)
+            {
+            case all:
+                portMatchD = true;
+                break;
+
+            case icmp:
+                portMatchD = (packet.GetProto() == ICMP_RPOTO);
+                break;
+
+            case tcp_udp:
+                if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
+                    portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
+                break;
+
+            case tcp:
+                if (packet.GetProto() == TCP_PROTO)
+                    portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
+                break;
+
+            case udp:
+                if (packet.GetProto() == UDP_PROTO)
+                    portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
+                break;
+
+            default:
+                printfd(__FILE__, "Error! Incorrect rule!\n");
+                break;
+            }
+
+        addrMatchD = (packet.GetSrcIP() & ln->mask) == ln->ip;
+
+        if (!foundD && addrMatchD && portMatchD)
+            {
+            foundD = true;
+            *dirD = ln->dir;
+            //printfd(__FILE__, "Down rule ok! %d\n", ln->dir);
+            //PrintRule(ln->rule);
+            }
+        } //if (!foundD)
+
+    ++ln;
+    }   //while (ln != rules.end())
+
+if (!foundU)
+    *dirU = DIR_NUM;
+
+if (!foundD)
+    *dirD = DIR_NUM;
+
+return;
+};
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::SetRulesFile(const std::string & fn)
+{
+rulesFileName = fn;
+}
+//-----------------------------------------------------------------------------
+bool TRAFFCOUNTER_IMPL::ReadRules(bool test)
+{
+//printfd(__FILE__, "TRAFFCOUNTER::ReadRules()\n");
+
+RULE rul;
+FILE * f;
+char str[1024];
+char tp[100];   // protocol
+char ta[100];   // address
+char td[100];   // target direction
+int r;
+int lineNumber = 0;
+f = fopen(rulesFileName.c_str(), "rt");
+
+if (!f)
+    {
+    WriteServLog("File %s cannot be oppened.", rulesFileName.c_str());
+    return true;
+    }
+
+while (fgets(str, 1023, f))
+    {
+    lineNumber++;
+    if (str[strspn(str," \t")] == '#' || str[strspn(str," \t")] == '\n')
+        {
+        continue;
+        }
+
+    r = sscanf(str,"%s %s %s", tp, ta, td);
+    if (r != 3)
+        {
+        WriteServLog("Error in file %s. There must be 3 parameters. Line %d.", rulesFileName.c_str(), lineNumber);
+        fclose(f);
+        return true;
+        }
+
+    rul.proto = 0xff;
+    rul.dir = 0xff;
+
+    for (int i = 0; i < PROTOMAX; i++)
+        {
+        if (strcasecmp(tp, protoName[i]) == 0)
+            rul.proto = i;
+        }
+
+    for (int i = 0; i < DIR_NUM + 1; i++)
+        {
+        if (td == dirName[i])
+            rul.dir = i;
+        }
+
+    if (rul.dir == 0xff || rul.proto == 0xff)
+        {
+        WriteServLog("Error in file %s. Line %d.",
+                     rulesFileName.c_str(), lineNumber);
+        fclose(f);
+        return true;
+        }
+
+    if (ParseAddress(ta, &rul) != 0)
+        {
+        WriteServLog("Error in file %s. Error in adress. Line %d.",
+                     rulesFileName.c_str(), lineNumber);
+        fclose(f);
+        return true;
+        }
+    if (!test)
+        rules.push_back(rul);
+    //PrintRule(rul);
+    }
+
+fclose(f);
+
+// Adding lastest rule: ALL 0.0.0.0/0 NULL
+rul.dir = DIR_NUM; //NULL
+rul.ip = 0;  //0.0.0.0
+rul.mask = 0;
+rul.port1 = 0;
+rul.port2 = 65535;
+rul.proto = all;
+
+if (!test)
+    rules.push_back(rul);
+
+//PrintRule(rul);
+
+return false;
+}
+//-----------------------------------------------------------------------------
+int TRAFFCOUNTER_IMPL::Reload()
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+
+if (ReadRules(true))
+    {
+    WriteServLog("TRAFFCOUNTER: Cannot reload rules. Errors found.");
+    return -1;
+    }
+
+FreeRules();
+ReadRules();
+WriteServLog("TRAFFCOUNTER: Reload rules successfull.");
+return 0;
+}
+//-----------------------------------------------------------------------------
+bool TRAFFCOUNTER_IMPL::ParseAddress(const char * ta, RULE * rule) const
+{
+char addr[50], mask[20], port1[20], port2[20], ports[40];
+
+int len = strlen(ta);
+char n = 0;
+int i, p;
+memset(addr, 0, sizeof(addr));
+for (i = 0; i < len; i++)
+    {
+    if (ta[i] == '/' || ta[i] == ':')
+        {
+        addr[i] = 0;
+        n = ta[i];
+        break;
+        }
+    addr[i] = ta[i];
+    n = 0;
+    }
+addr[i + 1] = 0;
+p = i + 1;
+
+if (n == '/')
+    {
+    // mask
+    for (; i < len; i++)
+        {
+        if (ta[i] == ':')
+            {
+            mask[i - p] = 0;
+            n = ':';
+            break;
+            }
+        mask[i - p] = ta[i];
+        }
+    mask[i - p] = 0;
+    }
+else
+    {
+    strcpy(mask, "32");
+    }
+
+p = i + 1;
+i++;
+
+if (n == ':')
+    {
+    // port
+    if (!(rule->proto == tcp || rule->proto == udp || rule->proto == tcp_udp))
+        {
+        WriteServLog("No ports specified for this protocol.");
+        return true;
+        }
+
+    for (; i < len; i++)
+        ports[i - p] = ta[i];
+
+    ports[i - p] = 0;
+    }
+else
+    {
+    strcpy(ports, "0-65535");
+    }
+
+char *sss;
+char pts[100];
+strcpy(pts, ports);
+
+if ((sss = strchr(ports, '-')) != NULL)
+    {
+    strncpy(port1, ports, int(sss-ports));
+    port1[int(sss - ports)] = 0;
+    strcpy(port2, sss + 1);
+    }
+else
+    {
+    strcpy(port1, ports);
+    strcpy(port2, ports);
+    }
+
+// Convert strings to mask, ports and IP
+int prt1, prt2, msk;
+unsigned ip;
+char *res;
+
+msk = strtol(mask, &res, 10);
+if (*res != 0)
+    return true;
+
+prt1 = strtol(port1, &res, 10);
+if (*res != 0)
+    return true;
+
+prt2 = strtol(port2, &res, 10);
+if (*res != 0)
+    return true;
+
+int r = inet_aton(addr, (struct in_addr*)&ip);
+if (r == 0)
+    return true;
+
+rule->ip = ip;
+rule->mask = CalcMask(msk);
+//msk = 1;
+//printfd(__FILE__, "msk=%d mask=%08X   mask=%08X\n", msk, rule->mask, (0xFFffFFff << (32 - msk)));
+
+if ((ip & rule->mask) != ip)
+    {
+    WriteServLog("Address does'n match mask.");
+    return true;
+    }
+
+rule->port1 = prt1;
+rule->port2 = prt2;
+
+return false;
+}
+//-----------------------------------------------------------------------------
+uint32_t TRAFFCOUNTER_IMPL::CalcMask(uint32_t msk) const
+{
+if (msk >= 32) return 0xFFffFFff;
+if (msk == 0) return 0;
+return htonl(0xFFffFFff << (32 - msk));
+}
+//---------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::FreeRules()
+{
+rules.clear();
+}
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::PrintRule(RULE rule) const
+{
+printf("%15s   ", inet_ntostring(rule.ip).c_str());
+printf("mask=%08X ", rule.mask);
+printf("port1=%5d ", rule.port1);
+printf("port2=%5d ", rule.port2);
+switch (rule.proto)
+    {
+    case 0:
+        printf("TCP     ");
+        break;
+    case 1:
+        printf("UDP     ");
+        break;
+    case 2:
+        printf("ICMP    ");
+        break;
+    case 3:
+        printf("TCP_UDP ");
+        break;
+    case 4:
+        printf("ALL     ");
+        break;
+    }
+printf("dir=%d \n", rule.dir);
+return;
+}
+//-----------------------------------------------------------------------------
+void TRAFFCOUNTER_IMPL::SetMonitorDir(const std::string & monitorDir)
+{
+TRAFFCOUNTER::monitorDir = monitorDir;
+monitoring = (monitorDir != "");
+}
+//-----------------------------------------------------------------------------
diff --git a/projects/stargazer/traffcounter_impl.h b/projects/stargazer/traffcounter_impl.h
new file mode 100644 (file)
index 0000000..eb47623
--- /dev/null
@@ -0,0 +1,261 @@
+/*
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ *
+ *    You should have received a copy of the GNU General Public License
+ *    along with this program; if not, write to the Free Software
+ *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
+ */
+
+ /*
+ $Revision: 1.23 $
+ $Date: 2010/04/22 12:57:46 $
+ $Author: faust $
+ */
+
+
+#ifndef TRAFFCOUNTER_IMPL_H
+#define TRAFFCOUNTER_IMPL_H
+
+#include <pthread.h>
+
+#include <ctime>
+#include <list>
+#include <map>
+#include <string>
+
+#include "traffcounter.h"
+#include "os_int.h"
+#include "stg_logger.h"
+#include "raw_ip_packet.h"
+#include "users.h"
+#include "actions.h"
+#include "noncopyable.h"
+#include "eventloop.h"
+
+#define PROTOMAX    (5)
+
+class TARIFFS;
+
+//-----------------------------------------------------------------------------
+struct RULE {
+uint32_t    ip;             // IP
+uint32_t    mask;           // Network mask
+uint16_t    port1;          // Min port
+uint16_t    port2;          // Max port
+uint8_t     proto;          // Protocol
+uint32_t    dir;            // Direction
+};
+//-----------------------------------------------------------------------------
+struct PACKET_EXTRA_DATA {
+PACKET_EXTRA_DATA()
+    : flushTime(0),
+      updateTime(0),
+      userU(),
+      userUPresent(false),
+      userD(),
+      userDPresent(false),
+      dirU(DIR_NUM),
+      dirD(DIR_NUM),
+      lenU(0),
+      lenD(0)
+{}
+
+PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp)
+    : flushTime(pp.flushTime),
+      updateTime(pp.updateTime),
+      userU(pp.userU),
+      userUPresent(pp.userUPresent),
+      userD(pp.userD),
+      userDPresent(pp.userDPresent),
+      dirU(pp.dirU),
+      dirD(pp.dirD),
+      lenU(pp.lenU),
+      lenD(pp.lenD)
+{}
+
+time_t      flushTime;          // Last flush time
+time_t      updateTime;         // Last update time
+USER_PTR    userU;              // Uploader
+bool        userUPresent;       // Uploader is registered user
+USER_PTR    userD;              // Downloader
+bool        userDPresent;       // Downloader is registered user
+int         dirU;               // Upload direction
+int         dirD;               // Download direction
+uint32_t    lenU;               // Upload length
+uint32_t    lenD;               // Download length
+};
+//-----------------------------------------------------------------------------
+class TRAFFCOUNTER_IMPL;
+//-----------------------------------------------------------------------------
+class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
+public:
+                    TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_PTR u)
+                        : PROPERTY_NOTIFIER_BASE<uint32_t>(),
+                          traffCnt(t),
+                          user(u)
+                    {}
+    void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
+    void            SetUser(USER_PTR u) { user = u; }
+    USER_PTR        GetUser() const { return user; }
+
+private:
+    TRAFFCOUNTER_IMPL & traffCnt;
+    USER_PTR            user;
+};
+//-----------------------------------------------------------------------------
+class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
+public:
+                    TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_PTR u)
+                        : PROPERTY_NOTIFIER_BASE<uint32_t>(),
+                          traffCnt(t),
+                          user(u)
+                    {}
+    void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
+    void            SetUser(USER_PTR u) { user = u; }
+    USER_PTR        GetUser() const { return user; }
+private:
+    TRAFFCOUNTER_IMPL & traffCnt;
+    USER_PTR            user;
+};
+//-----------------------------------------------------------------------------
+class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
+public:
+            ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
+                NOTIFIER_BASE<USER_PTR>(),
+                traffCnt(t)
+            {}
+    virtual ~ADD_USER_NONIFIER() {}
+    void    Notify(const USER_PTR & user);
+private:
+    TRAFFCOUNTER_IMPL & traffCnt;
+};
+//-----------------------------------------------------------------------------
+class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
+public:
+            DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
+                NOTIFIER_BASE<USER_PTR>(),
+                traffCnt(t)
+            {}
+    virtual ~DEL_USER_NONIFIER() {}
+    void    Notify(const USER_PTR & user);
+private:
+    TRAFFCOUNTER_IMPL & traffCnt;
+};
+//-----------------------------------------------------------------------------
+class TRAFFCOUNTER : public TRAFFCOUNTER, private NONCOPYABLE {
+friend class ADD_USER_NONIFIER;
+friend class DEL_USER_NONIFIER;
+friend class TRF_IP_BEFORE;
+friend class TRF_IP_AFTER;
+public:
+    TRAFFCOUNTER_IMPL(USERS * users, const TARIFFS * tariffs, const std::string & rulesFileName);
+    ~TRAFFCOUNTER_IMPL();
+
+    void        SetRulesFile(const std::string & rulesFileName);
+
+    int         Reload();
+    int         Start();
+    int         Stop();
+
+    void        Process(const RAW_PACKET & rawPacket);
+    void        SetMonitorDir(const std::string & monitorDir);
+
+private:
+    bool        ParseAddress(const char * ta, RULE * rule) const;
+    uint32_t    CalcMask(uint32_t msk) const;
+    void        FreeRules();
+    void        PrintRule(RULE rule) const;
+    bool        ReadRules(bool test = false);
+
+    static void * Run(void * data);
+
+    void        DeterminateDir(const RAW_PACKET & packet,
+                               int * dirU, // Direction for upload
+                               int * dirD) const; // Direction for download
+
+    void        FlushAndRemove();
+
+    void        AddUser(USER_PTR user);
+    void        DelUser(uint32_t uip);
+    void        SetUserNotifiers(USER_PTR user);
+    void        UnSetUserNotifiers(USER_PTR user);
+
+    typedef std::list<RULE>::iterator rule_iter;
+    typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA>::iterator pp_iter;
+    typedef std::multimap<uint32_t, pp_iter>::iterator ip2p_iter;
+    typedef std::multimap<uint32_t, pp_iter>::const_iterator ip2p_citer;
+
+    std::list<RULE>          rules;
+
+    std::map<RAW_PACKET, PACKET_EXTRA_DATA> packets; // Packets tree
+
+    std::multimap<uint32_t, pp_iter> ip2packets; // IP-to-Packet index
+
+    std::string              dirName[DIR_NUM + 1];
+
+    STG_LOGGER &             WriteServLog;
+    std::string              rulesFileName;
+
+    std::string              monitorDir;
+    bool                     monitoring;
+
+    USERS *                  users;
+
+    bool                     running;
+    bool                     stopped;
+    pthread_mutex_t          mutex;
+    pthread_t                thread;
+
+    std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
+    std::list<TRF_IP_AFTER>  ipAfterNotifiers;
+
+    ADD_USER_NONIFIER        addUserNotifier;
+    DEL_USER_NONIFIER        delUserNotifier;
+};
+//-----------------------------------------------------------------------------
+inline
+void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
+{
+// User changes his address. Remove old IP
+if (!oldValue)
+    return;
+
+EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, oldValue);
+}
+//-----------------------------------------------------------------------------
+inline
+void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
+{
+// User changes his address. Add new IP
+if (!newValue)
+    return;
+
+EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::AddUser, user);
+}
+//-----------------------------------------------------------------------------
+inline
+void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
+{
+EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::SetUserNotifiers, user);
+}
+//-----------------------------------------------------------------------------
+inline
+void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
+{
+EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::UnSetUserNotifiers, user);
+EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER::DelUser, user->GetCurrIP());
+}
+//-----------------------------------------------------------------------------
+#endif //TRAFFCOUNTER_H