From 5cf7604389d8846f9c1c21dbb6915f7639659c4c Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 4 Apr 2011 15:19:05 +0300 Subject: [PATCH 1/1] Traffcounter divided into interface and implementation --- include/traffcounter.h | 31 +++++ projects/stargazer/Makefile | 2 +- ...traffcounter.cpp => traffcounter_impl.cpp} | 56 ++++----- .../{traffcounter.h => traffcounter_impl.h} | 109 +++++++++--------- 4 files changed, 113 insertions(+), 85 deletions(-) create mode 100644 include/traffcounter.h rename projects/stargazer/{traffcounter.cpp => traffcounter_impl.cpp} (94%) rename projects/stargazer/{traffcounter.h => traffcounter_impl.h} (78%) diff --git a/include/traffcounter.h b/include/traffcounter.h new file mode 100644 index 00000000..1962ee7a --- /dev/null +++ b/include/traffcounter.h @@ -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 + */ + +#ifndef TRAFFCOUNTER_H +#define TRAFFCOUNTER_H + +#include "raw_ip_packet.h" + +class TRAFFCOUNTER { +public: + virtual void Process(const RAW_PACKET & rawPacket) = 0; +}; + +#endif diff --git a/projects/stargazer/Makefile b/projects/stargazer/Makefile index 9f7dd869..5aa4687f 100644 --- a/projects/stargazer/Makefile +++ b/projects/stargazer/Makefile @@ -17,7 +17,7 @@ SRCS = ./admin_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 diff --git a/projects/stargazer/traffcounter.cpp b/projects/stargazer/traffcounter_impl.cpp similarity index 94% rename from projects/stargazer/traffcounter.cpp rename to projects/stargazer/traffcounter_impl.cpp index dd672f37..f410b109 100644 --- a/projects/stargazer/traffcounter.cpp +++ b/projects/stargazer/traffcounter_impl.cpp @@ -35,9 +35,9 @@ #include #include -#include // Functions fopen and similar +#include // fopen and similar -#include "traffcounter.h" +#include "traffcounter_impl.h" #include "common.h" #include "stg_locker.h" #include "stg_timer.h" @@ -49,12 +49,12 @@ const char protoName[PROTOMAX][8] = {"TCP", "UDP", "ICMP", "TCP_UDP", "ALL"}; enum protoNum - { - tcp = 0, udp, icmp, tcp_udp, all - }; +{ +tcp = 0, udp, icmp, tcp_udp, all +}; //----------------------------------------------------------------------------- -TRAFFCOUNTER::TRAFFCOUNTER(USERS * u, const TARIFFS *, const std::string & fn) +TRAFFCOUNTER_IMPL::TRAFFCOUNTER_IMPL(USERS * u, const TARIFFS *, const std::string & fn) : WriteServLog(GetStgLogger()), rulesFileName(fn), monitoring(false), @@ -75,12 +75,12 @@ users->AddNotifierUserDel(&delUserNotifier); pthread_mutex_init(&mutex, NULL); } //----------------------------------------------------------------------------- -TRAFFCOUNTER::~TRAFFCOUNTER() +TRAFFCOUNTER_IMPL::~TRAFFCOUNTER_IMPL() { pthread_mutex_destroy(&mutex); } //----------------------------------------------------------------------------- -int TRAFFCOUNTER::Start() +int TRAFFCOUNTER_IMPL::Start() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -117,7 +117,7 @@ if (pthread_create(&thread, NULL, Run, this)) return 0; } //----------------------------------------------------------------------------- -int TRAFFCOUNTER::Stop() +int TRAFFCOUNTER_IMPL::Stop() { if (stopped) return 0; @@ -160,9 +160,9 @@ printfd(__FILE__, "TRAFFCOUNTER::Stop()\n"); return 0; } //----------------------------------------------------------------------------- -void * TRAFFCOUNTER::Run(void * data) +void * TRAFFCOUNTER_IMPL::Run(void * data) { -TRAFFCOUNTER * tc = static_cast(data); +TRAFFCOUNTER_IMPL * tc = static_cast(data); tc->stopped = false; int c = 0; @@ -193,7 +193,7 @@ tc->stopped = true; return NULL; } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::Process(const RAW_PACKET & rawPacket) +void TRAFFCOUNTER_IMPL::Process(const RAW_PACKET & rawPacket) { if (!running) return; @@ -284,7 +284,7 @@ if (ed.userUPresent || } } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::FlushAndRemove() +void TRAFFCOUNTER_IMPL::FlushAndRemove() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -410,7 +410,7 @@ printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::AddUser(USER_PTR user) +void TRAFFCOUNTER_IMPL::AddUser(USER_PTR user) { printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str()); uint32_t uip = user->GetCurrIP(); @@ -448,7 +448,7 @@ while (pi.first != pi.second) } } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::DelUser(uint32_t uip) +void TRAFFCOUNTER_IMPL::DelUser(uint32_t uip) { printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str()); std::pair pi; @@ -501,7 +501,7 @@ while (pi.first != pi.second) ip2packets.erase(pi.first, pi.second); } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::SetUserNotifiers(USER_PTR user) +void TRAFFCOUNTER_IMPL::SetUserNotifiers(USER_PTR user) { // Adding user. Adding notifiers to user. TRF_IP_BEFORE ipBNotifier(*this, user); @@ -513,7 +513,7 @@ ipAfterNotifiers.push_front(ipANotifier); user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin())); } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::UnSetUserNotifiers(USER_PTR user) +void TRAFFCOUNTER_IMPL::UnSetUserNotifiers(USER_PTR user) { // Removing user. Removing notifiers from user. std::list::iterator bi; @@ -544,9 +544,9 @@ while (ai != ipAfterNotifiers.end()) } } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::DeterminateDir(const RAW_PACKET & packet, - int * dirU, // Direction for incoming packet - int * dirD) const // Direction for outgoing packet +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; @@ -668,12 +668,12 @@ if (!foundD) return; }; //----------------------------------------------------------------------------- -void TRAFFCOUNTER::SetRulesFile(const std::string & fn) +void TRAFFCOUNTER_IMPL::SetRulesFile(const std::string & fn) { rulesFileName = fn; } //----------------------------------------------------------------------------- -bool TRAFFCOUNTER::ReadRules(bool test) +bool TRAFFCOUNTER_IMPL::ReadRules(bool test) { //printfd(__FILE__, "TRAFFCOUNTER::ReadRules()\n"); @@ -762,7 +762,7 @@ if (!test) return false; } //----------------------------------------------------------------------------- -int TRAFFCOUNTER::Reload() +int TRAFFCOUNTER_IMPL::Reload() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -778,7 +778,7 @@ WriteServLog("TRAFFCOUNTER: Reload rules successfull."); return 0; } //----------------------------------------------------------------------------- -bool TRAFFCOUNTER::ParseAddress(const char * ta, RULE * rule) const +bool TRAFFCOUNTER_IMPL::ParseAddress(const char * ta, RULE * rule) const { char addr[50], mask[20], port1[20], port2[20], ports[40]; @@ -896,19 +896,19 @@ rule->port2 = prt2; return false; } //----------------------------------------------------------------------------- -uint32_t TRAFFCOUNTER::CalcMask(uint32_t msk) const +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::FreeRules() +void TRAFFCOUNTER_IMPL::FreeRules() { rules.clear(); } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::PrintRule(RULE rule) const +void TRAFFCOUNTER_IMPL::PrintRule(RULE rule) const { printf("%15s ", inet_ntostring(rule.ip).c_str()); printf("mask=%08X ", rule.mask); @@ -936,7 +936,7 @@ printf("dir=%d \n", rule.dir); return; } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::SetMonitorDir(const std::string & monitorDir) +void TRAFFCOUNTER_IMPL::SetMonitorDir(const std::string & monitorDir) { TRAFFCOUNTER::monitorDir = monitorDir; monitoring = (monitorDir != ""); diff --git a/projects/stargazer/traffcounter.h b/projects/stargazer/traffcounter_impl.h similarity index 78% rename from projects/stargazer/traffcounter.h rename to projects/stargazer/traffcounter_impl.h index d6a56881..eb476239 100644 --- a/projects/stargazer/traffcounter.h +++ b/projects/stargazer/traffcounter_impl.h @@ -25,15 +25,17 @@ */ -#ifndef TRAFFCOUNTER_H -#define TRAFFCOUNTER_H +#ifndef TRAFFCOUNTER_IMPL_H +#define TRAFFCOUNTER_IMPL_H #include + #include #include #include #include +#include "traffcounter.h" #include "os_int.h" #include "stg_logger.h" #include "raw_ip_packet.h" @@ -47,8 +49,7 @@ class TARIFFS; //----------------------------------------------------------------------------- -struct RULE -{ +struct RULE { uint32_t ip; // IP uint32_t mask; // Network mask uint16_t port1; // Min port @@ -57,8 +58,7 @@ uint8_t proto; // Protocol uint32_t dir; // Direction }; //----------------------------------------------------------------------------- -struct PACKET_EXTRA_DATA -{ +struct PACKET_EXTRA_DATA { PACKET_EXTRA_DATA() : flushTime(0), updateTime(0), @@ -70,7 +70,7 @@ PACKET_EXTRA_DATA() dirD(DIR_NUM), lenU(0), lenD(0) -{}; +{} PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp) : flushTime(pp.flushTime), @@ -83,7 +83,7 @@ PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp) dirD(pp.dirD), lenU(pp.lenU), lenD(pp.lenD) -{}; +{} time_t flushTime; // Last flush time time_t updateTime; // Last update time @@ -97,74 +97,71 @@ uint32_t lenU; // Upload length uint32_t lenD; // Download length }; //----------------------------------------------------------------------------- -class TRAFFCOUNTER; +class TRAFFCOUNTER_IMPL; //----------------------------------------------------------------------------- -class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE -{ +class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE { public: - TRF_IP_BEFORE(TRAFFCOUNTER & t, USER_PTR u) + TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_PTR u) : PROPERTY_NOTIFIER_BASE(), 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; + TRAFFCOUNTER_IMPL & traffCnt; + USER_PTR user; }; //----------------------------------------------------------------------------- -class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE -{ +class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE { public: - TRF_IP_AFTER(TRAFFCOUNTER & t, USER_PTR u) + TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_PTR u) : PROPERTY_NOTIFIER_BASE(), 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; + TRAFFCOUNTER_IMPL & traffCnt; + USER_PTR user; }; //----------------------------------------------------------------------------- -class ADD_USER_NONIFIER: public NOTIFIER_BASE -{ +class ADD_USER_NONIFIER: public NOTIFIER_BASE { public: - ADD_USER_NONIFIER(TRAFFCOUNTER & t) : + ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) : NOTIFIER_BASE(), - traffCnt(t) {}; - virtual ~ADD_USER_NONIFIER(){}; + traffCnt(t) + {} + virtual ~ADD_USER_NONIFIER() {} void Notify(const USER_PTR & user); private: - TRAFFCOUNTER & traffCnt; + TRAFFCOUNTER_IMPL & traffCnt; }; //----------------------------------------------------------------------------- -class DEL_USER_NONIFIER: public NOTIFIER_BASE -{ +class DEL_USER_NONIFIER: public NOTIFIER_BASE { public: - DEL_USER_NONIFIER(TRAFFCOUNTER & t) : + DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) : NOTIFIER_BASE(), - traffCnt(t) {}; - virtual ~DEL_USER_NONIFIER(){}; + traffCnt(t) + {} + virtual ~DEL_USER_NONIFIER() {} void Notify(const USER_PTR & user); private: - TRAFFCOUNTER & traffCnt; + TRAFFCOUNTER_IMPL & traffCnt; }; //----------------------------------------------------------------------------- -class TRAFFCOUNTER : private NONCOPYABLE -{ +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(USERS * users, const TARIFFS * tariffs, const std::string & rulesFileName); - ~TRAFFCOUNTER(); + TRAFFCOUNTER_IMPL(USERS * users, const TARIFFS * tariffs, const std::string & rulesFileName); + ~TRAFFCOUNTER_IMPL(); void SetRulesFile(const std::string & rulesFileName); @@ -195,37 +192,37 @@ private: void SetUserNotifiers(USER_PTR user); void UnSetUserNotifiers(USER_PTR user); - std::list rules; typedef std::list::iterator rule_iter; + typedef std::map::iterator pp_iter; + typedef std::multimap::iterator ip2p_iter; + typedef std::multimap::const_iterator ip2p_citer; + + std::list rules; std::map packets; // Packets tree - typedef std::map::iterator pp_iter; std::multimap ip2packets; // IP-to-Packet index - typedef std::multimap::iterator ip2p_iter; - typedef std::multimap::const_iterator ip2p_citer; - - std::string dirName[DIR_NUM + 1]; + std::string dirName[DIR_NUM + 1]; - STG_LOGGER & WriteServLog; - std::string rulesFileName; + STG_LOGGER & WriteServLog; + std::string rulesFileName; - std::string monitorDir; - bool monitoring; + std::string monitorDir; + bool monitoring; - USERS * users; + USERS * users; - bool running; - bool stopped; - pthread_mutex_t mutex; - pthread_t thread; + bool running; + bool stopped; + pthread_mutex_t mutex; + pthread_t thread; - std::list ipBeforeNotifiers; - std::list ipAfterNotifiers; + std::list ipBeforeNotifiers; + std::list ipAfterNotifiers; - ADD_USER_NONIFIER addUserNotifier; - DEL_USER_NONIFIER delUserNotifier; + ADD_USER_NONIFIER addUserNotifier; + DEL_USER_NONIFIER delUserNotifier; }; //----------------------------------------------------------------------------- inline -- 2.44.2