git.stg.codes
/
stg.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Catch exception via const referense, not a value
[stg.git]
/
projects
/
stargazer
/
traffcounter.cpp
diff --git
a/projects/stargazer/traffcounter.cpp
b/projects/stargazer/traffcounter.cpp
index 72c1295bbe1de231287eaed6d64734af6fa02c86..9697cde055b9b84b01ce0f3ba6bebf4f14e92bc4 100644
(file)
--- a/
projects/stargazer/traffcounter.cpp
+++ b/
projects/stargazer/traffcounter.cpp
@@
-28,12
+28,19
@@
$Author: faust $
*/
$Author: faust $
*/
+/* inet_aton */
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
#include <csignal>
#include <cassert>
#include <csignal>
#include <cassert>
+#include <cstdio> // Functions fopen and similar
#include "traffcounter.h"
#include "common.h"
#include "stg_locker.h"
#include "traffcounter.h"
#include "common.h"
#include "stg_locker.h"
+#include "stg_timer.h"
#define FLUSH_TIME (10)
#define REMOVE_TIME (31)
#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 st
d::st
ring & fn)
: WriteServLog(GetStgLogger()),
rulesFileName(fn),
monitoring(false),
: WriteServLog(GetStgLogger()),
rulesFileName(fn),
monitoring(false),
@@
-88,7
+95,7
@@
if (ReadRules())
printfd(__FILE__, "TRAFFCOUNTER::Start()\n");
int h = users->OpenSearch();
printfd(__FILE__, "TRAFFCOUNTER::Start()\n");
int h = users->OpenSearch();
-
user_iter
u;
+
USER_PTR
u;
if (!h)
{
WriteServLog("TRAFFCOUNTER: Cannot get users.");
if (!h)
{
WriteServLog("TRAFFCOUNTER: Cannot get users.");
@@
-124,7
+131,7
@@
if (!h)
return -1;
}
return -1;
}
-
user_iter
u;
+
USER_PTR
u;
while (users->SearchNext(h, &u) == 0)
{
UnSetUserNotifiers(u);
while (users->SearchNext(h, &u) == 0)
{
UnSetUserNotifiers(u);
@@
-171,7
+178,7
@@
while (tc->running)
if (tc->monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
{
if (tc->monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
{
- string monFile(tc->monitorDir + "/traffcounter_r");
+ st
d::st
ring monFile(tc->monitorDir + "/traffcounter_r");
printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
touchTime = stgTime;
TouchFile(monFile.c_str());
printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
touchTime = stgTime;
TouchFile(monFile.c_str());
@@
-194,7
+201,7
@@
static time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
if (monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
{
if (monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
{
- static string monFile = monitorDir + "/traffcounter_p";
+ static st
d::st
ring monFile = monitorDir + "/traffcounter_p";
printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", monitoring, monFile.c_str());
touchTime = stgTime;
TouchFile(monFile.c_str());
printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", monitoring, monFile.c_str());
touchTime = stgTime;
TouchFile(monFile.c_str());
@@
-267,13
+274,12
@@
if (ed.userUPresent ||
//TODO use result of lower_bound to inserting new record
// Adding packet to a tree.
//TODO use result of lower_bound to inserting new record
// Adding packet to a tree.
- pair<pp_iter, bool> insertResult = packets.insert(pair<RAW_PACKET,
- PACKET_EXTRA_DATA>(rawPacket, ed));
+ 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.
pp_iter newPacket = insertResult.first;
// Adding packet reference to an IP index.
- ip2packets.insert(
pair<uint32_t, pp_iter>
(ipU, newPacket));
- ip2packets.insert(
pair<uint32_t, pp_iter>
(ipD, newPacket));
+ ip2packets.insert(
std::make_pair
(ipU, newPacket));
+ ip2packets.insert(
std::make_pair
(ipD, newPacket));
}
}
//-----------------------------------------------------------------------------
}
}
//-----------------------------------------------------------------------------
@@
-385,11
+391,11
@@
while (pi != packets.end())
}*/
if (stgTime - pi->second.updateTime < REMOVE_TIME)
{
}*/
if (stgTime - pi->second.updateTime < REMOVE_TIME)
{
- pair<pp_iter, bool> res = newPackets.insert(*pi);
+
std::
pair<pp_iter, bool> res = newPackets.insert(*pi);
if (res.second)
{
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;
}
}
++pi;
@@
-403,11
+409,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();
{
printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str());
uint32_t uip = user->GetCurrIP();
-pair<ip2p_iter, ip2p_iter> pi;
+
std::
pair<ip2p_iter, ip2p_iter> pi;
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
// Find all packets with IP belongs to this user
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
// Find all packets with IP belongs to this user
@@
-444,7
+450,7
@@
while (pi.first != pi.second)
void TRAFFCOUNTER::DelUser(uint32_t uip)
{
printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
void TRAFFCOUNTER::DelUser(uint32_t uip)
{
printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
-pair<ip2p_iter, ip2p_iter> pi;
+
std::
pair<ip2p_iter, ip2p_iter> pi;
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
pi = ip2packets.equal_range(uip);
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
pi = ip2packets.equal_range(uip);
@@
-494,7
+500,7
@@
while (pi.first != pi.second)
ip2packets.erase(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);
{
// Adding user. Adding notifiers to user.
TRF_IP_BEFORE ipBNotifier(*this, user);
@@
-506,11
+512,11
@@
ipAfterNotifiers.push_front(ipANotifier);
user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
}
//-----------------------------------------------------------------------------
user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
}
//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::UnSetUserNotifiers(
user_iter
user)
+void TRAFFCOUNTER::UnSetUserNotifiers(
USER_PTR
user)
{
// Removing user. Removing notifiers from user.
{
// Removing user. Removing notifiers from user.
-list<TRF_IP_BEFORE>::iterator bi;
-list<TRF_IP_AFTER>::iterator ai;
+
std::
list<TRF_IP_BEFORE>::iterator bi;
+
std::
list<TRF_IP_AFTER>::iterator ai;
bi = ipBeforeNotifiers.begin();
while (bi != ipBeforeNotifiers.end())
bi = ipBeforeNotifiers.begin();
while (bi != ipBeforeNotifiers.end())
@@
-551,7
+557,7
@@
bool foundD = false; // Was rule for D found ?
enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
-list<RULE>::const_iterator ln;
+
std::
list<RULE>::const_iterator ln;
ln = rules.begin();
while (ln != rules.end())
ln = rules.begin();
while (ln != rules.end())
@@
-661,7
+667,7
@@
if (!foundD)
return;
};
//-----------------------------------------------------------------------------
return;
};
//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::SetRulesFile(const string & fn)
+void TRAFFCOUNTER::SetRulesFile(const st
d::st
ring & fn)
{
rulesFileName = fn;
}
{
rulesFileName = fn;
}
@@
-698,6
+704,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);
if (r != 3)
{
WriteServLog("Error in file %s. There must be 3 parameters. Line %d.", rulesFileName.c_str(), lineNumber);
+ fclose(f);
return true;
}
return true;
}
@@
-720,6
+727,7
@@
while (fgets(str, 1023, f))
{
WriteServLog("Error in file %s. Line %d.",
rulesFileName.c_str(), lineNumber);
{
WriteServLog("Error in file %s. Line %d.",
rulesFileName.c_str(), lineNumber);
+ fclose(f);
return true;
}
return true;
}
@@
-727,6
+735,7
@@
while (fgets(str, 1023, f))
{
WriteServLog("Error in file %s. Error in adress. Line %d.",
rulesFileName.c_str(), lineNumber);
{
WriteServLog("Error in file %s. Error in adress. Line %d.",
rulesFileName.c_str(), lineNumber);
+ fclose(f);
return true;
}
if (!test)
return true;
}
if (!test)
@@
-926,7
+935,7
@@
printf("dir=%d \n", rule.dir);
return;
}
//-----------------------------------------------------------------------------
return;
}
//-----------------------------------------------------------------------------
-void TRAFFCOUNTER::SetMonitorDir(const string & monitorDir)
+void TRAFFCOUNTER::SetMonitorDir(const st
d::st
ring & monitorDir)
{
TRAFFCOUNTER::monitorDir = monitorDir;
monitoring = (monitorDir != "");
{
TRAFFCOUNTER::monitorDir = monitorDir;
monitoring = (monitorDir != "");