X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/78e14695fc1b743f225a5aa30ca98784bce576c1..e1c08c2c174c75d7c5cd2ec5d0b974c6f240ba7e:/projects/stargazer/traffcounter.cpp diff --git a/projects/stargazer/traffcounter.cpp b/projects/stargazer/traffcounter.cpp index 1d088f48..dd672f37 100644 --- a/projects/stargazer/traffcounter.cpp +++ b/projects/stargazer/traffcounter.cpp @@ -28,6 +28,11 @@ $Author: faust $ */ +/* inet_aton */ +#include +#include +#include + #include #include #include // Functions fopen and similar @@ -35,6 +40,7 @@ #include "traffcounter.h" #include "common.h" #include "stg_locker.h" +#include "stg_timer.h" #define FLUSH_TIME (10) #define REMOVE_TIME (31) @@ -48,7 +54,7 @@ enum protoNum }; //----------------------------------------------------------------------------- -TRAFFCOUNTER::TRAFFCOUNTER(USERS * u, const TARIFFS *, const string & fn) +TRAFFCOUNTER::TRAFFCOUNTER(USERS * u, const TARIFFS *, const std::string & fn) : WriteServLog(GetStgLogger()), rulesFileName(fn), monitoring(false), @@ -89,7 +95,7 @@ if (ReadRules()) printfd(__FILE__, "TRAFFCOUNTER::Start()\n"); int h = users->OpenSearch(); -user_iter u; +USER_PTR u; if (!h) { WriteServLog("TRAFFCOUNTER: Cannot get users."); @@ -125,7 +131,7 @@ if (!h) return -1; } -user_iter u; +USER_PTR u; while (users->SearchNext(h, &u) == 0) { UnSetUserNotifiers(u); @@ -133,9 +139,10 @@ while (users->SearchNext(h, &u) == 0) users->CloseSearch(h); //5 seconds to thread stops itself +struct timespec ts = {0, 200000000}; for (int i = 0; i < 25 && !stopped; i++) { - usleep(200000); + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now kill it @@ -160,10 +167,10 @@ tc->stopped = false; int c = 0; time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC; - +struct timespec ts = {0, 500000000}; while (tc->running) { - usleep(500000); + nanosleep(&ts, 0); if (!tc->running) { tc->FlushAndRemove(); @@ -172,7 +179,7 @@ while (tc->running) if (tc->monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime)) { - string monFile(tc->monitorDir + "/traffcounter_r"); + 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()); @@ -195,7 +202,7 @@ static time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC; if (monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime)) { - static string monFile = monitorDir + "/traffcounter_p"; + 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()); @@ -268,13 +275,12 @@ if (ed.userUPresent || //TODO use result of lower_bound to inserting new record // Adding packet to a tree. - pair insertResult = packets.insert(pair(rawPacket, ed)); + std::pair insertResult = packets.insert(std::make_pair(rawPacket, ed)); pp_iter newPacket = insertResult.first; // Adding packet reference to an IP index. - ip2packets.insert(pair(ipU, newPacket)); - ip2packets.insert(pair(ipD, newPacket)); + ip2packets.insert(std::make_pair(ipU, newPacket)); + ip2packets.insert(std::make_pair(ipD, newPacket)); } } //----------------------------------------------------------------------------- @@ -386,11 +392,11 @@ while (pi != packets.end()) }*/ if (stgTime - pi->second.updateTime < REMOVE_TIME) { - pair res = newPackets.insert(*pi); + std::pair res = newPackets.insert(*pi); if (res.second) { - ip2packets.insert(make_pair(pi->first.GetSrcIP(), res.first)); - ip2packets.insert(make_pair(pi->first.GetDstIP(), res.first)); + ip2packets.insert(std::make_pair(pi->first.GetSrcIP(), res.first)); + ip2packets.insert(std::make_pair(pi->first.GetDstIP(), res.first)); } } ++pi; @@ -404,11 +410,11 @@ printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::AddUser(user_iter user) +void TRAFFCOUNTER::AddUser(USER_PTR user) { printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str()); uint32_t uip = user->GetCurrIP(); -pair pi; +std::pair pi; STG_LOCKER lock(&mutex, __FILE__, __LINE__); // Find all packets with IP belongs to this user @@ -445,7 +451,7 @@ while (pi.first != pi.second) void TRAFFCOUNTER::DelUser(uint32_t uip) { printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str()); -pair pi; +std::pair pi; STG_LOCKER lock(&mutex, __FILE__, __LINE__); pi = ip2packets.equal_range(uip); @@ -495,7 +501,7 @@ while (pi.first != pi.second) ip2packets.erase(pi.first, pi.second); } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::SetUserNotifiers(user_iter user) +void TRAFFCOUNTER::SetUserNotifiers(USER_PTR user) { // Adding user. Adding notifiers to user. TRF_IP_BEFORE ipBNotifier(*this, user); @@ -507,11 +513,11 @@ ipAfterNotifiers.push_front(ipANotifier); user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin())); } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::UnSetUserNotifiers(user_iter user) +void TRAFFCOUNTER::UnSetUserNotifiers(USER_PTR user) { // Removing user. Removing notifiers from user. -list::iterator bi; -list::iterator ai; +std::list::iterator bi; +std::list::iterator ai; bi = ipBeforeNotifiers.begin(); while (bi != ipBeforeNotifiers.end()) @@ -552,7 +558,7 @@ bool foundD = false; // Was rule for D found ? enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 }; -list::const_iterator ln; +std::list::const_iterator ln; ln = rules.begin(); while (ln != rules.end()) @@ -662,7 +668,7 @@ if (!foundD) return; }; //----------------------------------------------------------------------------- -void TRAFFCOUNTER::SetRulesFile(const string & fn) +void TRAFFCOUNTER::SetRulesFile(const std::string & fn) { rulesFileName = fn; } @@ -930,7 +936,7 @@ printf("dir=%d \n", rule.dir); return; } //----------------------------------------------------------------------------- -void TRAFFCOUNTER::SetMonitorDir(const string & monitorDir) +void TRAFFCOUNTER::SetMonitorDir(const std::string & monitorDir) { TRAFFCOUNTER::monitorDir = monitorDir; monitoring = (monitorDir != "");