X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/641204dfbdb9fc870cdd2e7f9e3169a44693e7bf..f5bfdf06e2e8f2e6f479cc6b477a6c7c4a69f965:/projects/stargazer/traffcounter.cpp diff --git a/projects/stargazer/traffcounter.cpp b/projects/stargazer/traffcounter.cpp index 54014167..dd672f37 100644 --- a/projects/stargazer/traffcounter.cpp +++ b/projects/stargazer/traffcounter.cpp @@ -28,12 +28,19 @@ $Author: faust $ */ +/* inet_aton */ +#include +#include +#include + #include #include +#include // 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) @@ -47,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), @@ -88,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."); @@ -124,7 +131,7 @@ if (!h) return -1; } -user_iter u; +USER_PTR u; while (users->SearchNext(h, &u) == 0) { UnSetUserNotifiers(u); @@ -132,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 @@ -159,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(); @@ -171,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()); @@ -194,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()); @@ -267,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)); } } //----------------------------------------------------------------------------- @@ -287,7 +294,7 @@ int oldIp2packetsSize = ip2packets.size(); pp_iter pi; pi = packets.begin(); std::map newPackets; -std::multimap newIP2Packets; +ip2packets.erase(ip2packets.begin(), ip2packets.end()); while (pi != packets.end()) { //Flushing @@ -385,17 +392,16 @@ 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) { - newIP2Packets.insert(make_pair(pi->first.GetSrcIP(), res.first)); - newIP2Packets.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; } swap(packets, newPackets); -swap(ip2packets, newIP2Packets); printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n", packets.size(), oldPacketsSize - packets.size(), @@ -404,13 +410,13 @@ 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(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex, __FILE__, __LINE__); // Find all packets with IP belongs to this user pi = ip2packets.equal_range(uip); @@ -445,9 +451,9 @@ 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(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex, __FILE__, __LINE__); pi = ip2packets.equal_range(uip); while (pi.first != pi.second) @@ -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; } @@ -699,6 +705,7 @@ while (fgets(str, 1023, f)) if (r != 3) { WriteServLog("Error in file %s. There must be 3 parameters. Line %d.", rulesFileName.c_str(), lineNumber); + fclose(f); return true; } @@ -721,6 +728,7 @@ while (fgets(str, 1023, f)) { WriteServLog("Error in file %s. Line %d.", rulesFileName.c_str(), lineNumber); + fclose(f); return true; } @@ -728,6 +736,7 @@ while (fgets(str, 1023, f)) { WriteServLog("Error in file %s. Error in adress. Line %d.", rulesFileName.c_str(), lineNumber); + fclose(f); return true; } if (!test) @@ -927,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 != "");