2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
27 $Date: 2010/11/03 11:28:07 $
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
39 #include <cstdio> // fopen and similar
40 #include <cstdlib> // strtol
42 #include "stg/common.h"
43 #include "stg/locker.h"
44 #include "traffcounter_impl.h"
45 #include "stg_timer.h"
46 #include "users_impl.h"
48 #define FLUSH_TIME (10)
49 #define REMOVE_TIME (31)
51 const char protoName[PROTOMAX][8] =
52 {"TCP", "UDP", "ICMP", "TCP_UDP", "ALL"};
56 tcp = 0, udp, icmp, tcp_udp, all
59 //-----------------------------------------------------------------------------
60 TRAFFCOUNTER_IMPL::TRAFFCOUNTER_IMPL(USERS_IMPL * u, const std::string & fn)
66 WriteServLog(GetStgLogger()),
77 addUserNotifier(*this),
78 delUserNotifier(*this)
80 for (int i = 0; i < DIR_NUM; i++)
81 strprintf(&dirName[i], "DIR%d", i);
83 dirName[DIR_NUM] = "NULL";
85 users->AddNotifierUserAdd(&addUserNotifier);
86 users->AddNotifierUserDel(&delUserNotifier);
88 pthread_mutex_init(&mutex, NULL);
90 //-----------------------------------------------------------------------------
91 TRAFFCOUNTER_IMPL::~TRAFFCOUNTER_IMPL()
93 pthread_mutex_destroy(&mutex);
95 //-----------------------------------------------------------------------------
96 int TRAFFCOUNTER_IMPL::Start()
98 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
105 printfd(__FILE__, "TRAFFCOUNTER_IMPL::Start() - Cannot read rules\n");
106 WriteServLog("TRAFFCOUNTER: Cannot read rules.");
110 printfd(__FILE__, "TRAFFCOUNTER::Start()\n");
111 int h = users->OpenSearch();
112 assert(h && "USERS::OpenSearch is always correct");
115 while (users->SearchNext(h, &u) == 0)
119 users->CloseSearch(h);
122 if (pthread_create(&thread, NULL, Run, this))
124 printfd(__FILE__, "TRAFFCOUNTER_IMPL::Start() - Cannot start thread\n");
125 WriteServLog("TRAFFCOUNTER: Error: Cannot start thread.");
130 //-----------------------------------------------------------------------------
131 int TRAFFCOUNTER_IMPL::Stop()
138 int h = users->OpenSearch();
139 assert(h && "USERS::OpenSearch is always correct");
142 while (users->SearchNext(h, &u) == 0)
144 UnSetUserNotifiers(u);
146 users->CloseSearch(h);
148 //5 seconds to thread stops itself
149 struct timespec ts = {0, 200000000};
150 for (int i = 0; i < 25 && !stopped; i++)
152 nanosleep(&ts, NULL);
155 //after 5 seconds waiting thread still running. now kill it
158 printfd(__FILE__, "kill TRAFFCOUNTER thread.\n");
159 if (pthread_kill(thread, SIGINT))
163 printfd(__FILE__, "TRAFFCOUNTER killed\n");
165 printfd(__FILE__, "TRAFFCOUNTER::Stop()\n");
169 //-----------------------------------------------------------------------------
170 void * TRAFFCOUNTER_IMPL::Run(void * data)
173 sigfillset(&signalSet);
174 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
176 TRAFFCOUNTER_IMPL * tc = static_cast<TRAFFCOUNTER_IMPL *>(data);
180 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
181 struct timespec ts = {0, 500000000};
187 tc->FlushAndRemove();
191 if (tc->monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
193 std::string monFile(tc->monitorDir + "/traffcounter_r");
194 printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
196 TouchFile(monFile.c_str());
199 if (++c % FLUSH_TIME == 0)
200 tc->FlushAndRemove();
206 //-----------------------------------------------------------------------------
207 void TRAFFCOUNTER_IMPL::Process(const RAW_PACKET & rawPacket)
212 static time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
214 if (monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
216 static std::string monFile = monitorDir + "/traffcounter_p";
217 printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", monitoring, monFile.c_str());
219 TouchFile(monFile.c_str());
222 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
224 //printfd(__FILE__, "TRAFFCOUNTER::Process()\n");
225 //TODO replace find with lower_bound.
227 // Searching a new packet in a tree.
228 pp_iter pi = packets.find(rawPacket);
230 // Packet found - update length and time
231 if (pi != packets.end())
233 pi->second.lenU += rawPacket.GetLen();
234 pi->second.lenD += rawPacket.GetLen();
235 pi->second.updateTime = stgTime;
236 /*printfd(__FILE__, "=============================\n");
237 printfd(__FILE__, "Packet found!\n");
238 printfd(__FILE__, "Version=%d\n", rawPacket.GetIPVersion());
239 printfd(__FILE__, "HeaderLen=%d\n", rawPacket.GetHeaderLen());
240 printfd(__FILE__, "PacketLen=%d\n", rawPacket.GetLen());
241 printfd(__FILE__, "SIP=%s\n", inet_ntostring(rawPacket.GetSrcIP()).c_str());
242 printfd(__FILE__, "DIP=%s\n", inet_ntostring(rawPacket.GetDstIP()).c_str());
243 printfd(__FILE__, "src port=%d\n", rawPacket.GetSrcPort());
244 printfd(__FILE__, "pst port=%d\n", rawPacket.GetDstPort());
245 printfd(__FILE__, "len=%d\n", rawPacket.GetLen());
246 printfd(__FILE__, "proto=%d\n", rawPacket.GetProto());
247 printfd(__FILE__, "PacketDirU=%d\n", pi->second.dirU);
248 printfd(__FILE__, "PacketDirD=%d\n", pi->second.dirD);
249 printfd(__FILE__, "=============================\n");*/
253 PACKET_EXTRA_DATA ed;
255 // Packet not found - add new packet
257 ed.updateTime = stgTime;
258 ed.flushTime = stgTime;
261 userU is that whose user_ip == packet_ip_src
262 userD is that whose user_ip == packet_ip_dst
265 uint32_t ipU = rawPacket.GetSrcIP();
266 uint32_t ipD = rawPacket.GetDstIP();
268 // Searching users with such IP
269 if (users->FindByIPIdx(ipU, &ed.userU) == 0)
271 ed.userUPresent = true;
274 if (users->FindByIPIdx(ipD, &ed.userD) == 0)
276 ed.userDPresent = true;
279 if (ed.userUPresent ||
282 DeterminateDir(rawPacket, &ed.dirU, &ed.dirD);
284 ed.lenD = ed.lenU = rawPacket.GetLen();
286 //TODO use result of lower_bound to inserting new record
288 // Adding packet to a tree.
289 std::pair<pp_iter, bool> insertResult = packets.insert(std::make_pair(rawPacket, ed));
290 pp_iter newPacket = insertResult.first;
292 // Adding packet reference to an IP index.
293 ip2packets.insert(std::make_pair(ipU, newPacket));
294 ip2packets.insert(std::make_pair(ipD, newPacket));
297 //-----------------------------------------------------------------------------
298 void TRAFFCOUNTER_IMPL::FlushAndRemove()
300 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
302 int oldPacketsSize = packets.size();
303 int oldIp2packetsSize = ip2packets.size();
306 pi = packets.begin();
307 std::map<RAW_PACKET, PACKET_EXTRA_DATA> newPackets;
308 ip2packets.erase(ip2packets.begin(), ip2packets.end());
309 while (pi != packets.end())
312 if (stgTime - pi->second.flushTime > FLUSH_TIME)
314 if (pi->second.userUPresent)
316 //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);
319 if (pi->second.dirU < DIR_NUM)
321 #ifdef TRAFF_STAT_WITH_PORTS
322 pi->second.userU->AddTraffStatU(pi->second.dirU,
323 pi->first.GetDstIP(),
324 pi->first.GetDstPort(),
327 pi->second.userU->AddTraffStatU(pi->second.dirU,
328 pi->first.GetDstIP(),
334 pi->second.flushTime = stgTime;
337 if (pi->second.userDPresent)
339 //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);
342 if (pi->second.dirD < DIR_NUM)
344 #ifdef TRAFF_STAT_WITH_PORTS
345 pi->second.userD->AddTraffStatD(pi->second.dirD,
346 pi->first.GetSrcIP(),
347 pi->first.GetSrcPort(),
350 pi->second.userD->AddTraffStatD(pi->second.dirD,
351 pi->first.GetSrcIP(),
357 pi->second.flushTime = stgTime;
361 if (stgTime - pi->second.updateTime < REMOVE_TIME)
363 std::pair<pp_iter, bool> res = newPackets.insert(*pi);
366 ip2packets.insert(std::make_pair(pi->first.GetSrcIP(), res.first));
367 ip2packets.insert(std::make_pair(pi->first.GetDstIP(), res.first));
372 swap(packets, newPackets);
373 printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n",
375 oldPacketsSize - packets.size(),
377 oldIp2packetsSize - ip2packets.size());
380 //-----------------------------------------------------------------------------
381 void TRAFFCOUNTER_IMPL::AddUser(USER_IMPL * user)
383 printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str());
384 uint32_t uip = user->GetCurrIP();
385 std::pair<ip2p_iter, ip2p_iter> pi;
387 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
388 // Find all packets with IP belongs to this user
389 pi = ip2packets.equal_range(uip);
391 while (pi.first != pi.second)
393 if (pi.first->second->first.GetSrcIP() == uip)
395 assert((!pi.first->second->second.userUPresent ||
396 pi.first->second->second.userU == user) &&
397 "U user present but it's not current user");
399 pi.first->second->second.lenU = 0;
400 pi.first->second->second.userU = user;
401 pi.first->second->second.userUPresent = true;
404 if (pi.first->second->first.GetDstIP() == uip)
406 assert((!pi.first->second->second.userDPresent ||
407 pi.first->second->second.userD == user) &&
408 "D user present but it's not current user");
410 pi.first->second->second.lenD = 0;
411 pi.first->second->second.userD = user;
412 pi.first->second->second.userDPresent = true;
418 //-----------------------------------------------------------------------------
419 void TRAFFCOUNTER_IMPL::DelUser(uint32_t uip)
421 printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
422 std::pair<ip2p_iter, ip2p_iter> pi;
424 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
425 pi = ip2packets.equal_range(uip);
427 while (pi.first != pi.second)
429 if (pi.first->second->first.GetSrcIP() == uip)
431 if (pi.first->second->second.dirU < DIR_NUM && pi.first->second->second.userUPresent)
433 #ifdef TRAFF_STAT_WITH_PORTS
434 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
435 pi.first->second->first.GetDstIP(),
436 pi.first->second->first.GetDstPort(),
437 pi.first->second->second.lenU);
439 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
440 pi.first->second->first.GetDstIP(),
441 pi.first->second->second.lenU);
444 pi.first->second->second.userUPresent = false;
447 if (pi.first->second->first.GetDstIP() == uip)
449 if (pi.first->second->second.dirD < DIR_NUM && pi.first->second->second.userDPresent)
451 #ifdef TRAFF_STAT_WITH_PORTS
452 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
453 pi.first->second->first.GetSrcIP(),
454 pi.first->second->first.GetSrcPort(),
455 pi.first->second->second.lenD);
457 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
458 pi.first->second->first.GetSrcIP(),
459 pi.first->second->second.lenD);
463 pi.first->second->second.userDPresent = false;
469 ip2packets.erase(pi.first, pi.second);
471 //-----------------------------------------------------------------------------
472 void TRAFFCOUNTER_IMPL::SetUserNotifiers(USER_IMPL * user)
474 // Adding user. Adding notifiers to user.
475 TRF_IP_BEFORE ipBNotifier(*this, user);
476 ipBeforeNotifiers.push_front(ipBNotifier);
477 user->AddCurrIPBeforeNotifier(&(*ipBeforeNotifiers.begin()));
479 TRF_IP_AFTER ipANotifier(*this, user);
480 ipAfterNotifiers.push_front(ipANotifier);
481 user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
483 //-----------------------------------------------------------------------------
484 void TRAFFCOUNTER_IMPL::UnSetUserNotifiers(USER_IMPL * user)
486 // Removing user. Removing notifiers from user.
487 std::list<TRF_IP_BEFORE>::iterator bi;
488 std::list<TRF_IP_AFTER>::iterator ai;
490 bi = ipBeforeNotifiers.begin();
491 while (bi != ipBeforeNotifiers.end())
493 if (user->GetLogin() == bi->GetUser()->GetLogin())
495 user->DelCurrIPBeforeNotifier(&(*bi));
496 ipBeforeNotifiers.erase(bi);
502 ai = ipAfterNotifiers.begin();
503 while (ai != ipAfterNotifiers.end())
505 if (user->GetLogin() == ai->GetUser()->GetLogin())
507 user->DelCurrIPAfterNotifier(&(*ai));
508 ipAfterNotifiers.erase(ai);
514 //-----------------------------------------------------------------------------
515 void TRAFFCOUNTER_IMPL::DeterminateDir(const RAW_PACKET & packet,
516 int * dirU, // Direction for incoming packet
517 int * dirD) const // Direction for outgoing packet
523 bool foundU = false; // Was rule for U found ?
524 bool foundD = false; // Was rule for D found ?
525 //printfd(__FILE__, "foundU=%d, foundD=%d\n", foundU, foundD);
527 enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
529 std::list<RULE>::const_iterator ln;
532 while (ln != rules.end())
546 portMatchU = (packet.GetProto() == ICMP_RPOTO);
550 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
551 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
555 if (packet.GetProto() == TCP_PROTO)
556 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
560 if (packet.GetProto() == UDP_PROTO)
561 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
565 printfd(__FILE__, "Error! Incorrect rule!\n");
569 addrMatchU = (packet.GetDstIP() & ln->mask) == ln->ip;
571 if (!foundU && addrMatchU && portMatchU)
575 //printfd(__FILE__, "Up rule ok! %d\n", ln->dir);
576 //PrintRule(ln->rule);
593 portMatchD = (packet.GetProto() == ICMP_RPOTO);
597 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
598 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
602 if (packet.GetProto() == TCP_PROTO)
603 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
607 if (packet.GetProto() == UDP_PROTO)
608 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
612 printfd(__FILE__, "Error! Incorrect rule!\n");
616 addrMatchD = (packet.GetSrcIP() & ln->mask) == ln->ip;
618 if (!foundD && addrMatchD && portMatchD)
622 //printfd(__FILE__, "Down rule ok! %d\n", ln->dir);
623 //PrintRule(ln->rule);
628 } //while (ln != rules.end())
638 //-----------------------------------------------------------------------------
639 void TRAFFCOUNTER_IMPL::SetRulesFile(const std::string & fn)
643 //-----------------------------------------------------------------------------
644 bool TRAFFCOUNTER_IMPL::ReadRules(bool test)
646 //printfd(__FILE__, "TRAFFCOUNTER::ReadRules()\n");
651 char tp[100]; // protocol
652 char ta[100]; // address
653 char td[100]; // target direction
656 f = fopen(rulesFileName.c_str(), "rt");
660 printfd(__FILE__, "TRAFFCOUNTER_IMPL::ReadRules() - File '%s' cannot be opened.\n", rulesFileName.c_str());
661 WriteServLog("File '%s' cannot be oppened.", rulesFileName.c_str());
665 while (fgets(str, 1023, f))
668 if (str[strspn(str," \t")] == '#' || str[strspn(str," \t")] == '\n')
673 r = sscanf(str,"%s %s %s", tp, ta, td);
676 printfd(__FILE__, "TRAFFCOUNTER_IMPL::ReadRules() - Error in file '%s' at line %d. There must be 3 parameters.\n", rulesFileName.c_str(), lineNumber);
677 WriteServLog("Error in file '%s' at line %d. There must be 3 parameters.", rulesFileName.c_str(), lineNumber);
685 for (int i = 0; i < PROTOMAX; i++)
687 if (strcasecmp(tp, protoName[i]) == 0)
691 for (int i = 0; i < DIR_NUM + 1; i++)
693 if (td == dirName[i])
697 if (rul.dir == 0xff || rul.proto == 0xff)
699 printfd(__FILE__, "TRAFFCOUNTER_IMPL::ReadRules() - Error in file '%s' at line %d.\n", rulesFileName.c_str(), lineNumber);
700 WriteServLog("Error in file %s. Line %d.",
701 rulesFileName.c_str(), lineNumber);
706 if (ParseAddress(ta, &rul) != 0)
708 printfd(__FILE__, "TRAFFCOUNTER_IMPL::ReadRules() - Error in file '%s' at line %d. Error in adress.\n", rulesFileName.c_str(), lineNumber);
709 WriteServLog("Error in file %s. Error in adress. Line %d.",
710 rulesFileName.c_str(), lineNumber);
715 rules.push_back(rul);
721 // Adding lastest rule: ALL 0.0.0.0/0 NULL
722 rul.dir = DIR_NUM; //NULL
723 rul.ip = 0; //0.0.0.0
730 rules.push_back(rul);
736 //-----------------------------------------------------------------------------
737 int TRAFFCOUNTER_IMPL::Reload()
739 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
743 printfd(__FILE__, "TRAFFCOUNTER_IMPL::Reload() - Failed to reload rules.\n");
744 WriteServLog("TRAFFCOUNTER: Cannot reload rules. Errors found.");
750 printfd(__FILE__, "TRAFFCOUNTER_IMPL::Reload() - Reload rules successfull.\n");
751 WriteServLog("TRAFFCOUNTER: Reload rules successfull.");
754 //-----------------------------------------------------------------------------
755 bool TRAFFCOUNTER_IMPL::ParseAddress(const char * ta, RULE * rule) const
757 char addr[50], mask[20], port1[20], port2[20], ports[40];
759 int len = strlen(ta);
762 memset(addr, 0, sizeof(addr));
763 for (i = 0; i < len; i++)
765 if (ta[i] == '/' || ta[i] == ':')
803 if (!(rule->proto == tcp || rule->proto == udp || rule->proto == tcp_udp))
805 printfd(__FILE__, "TRAFFCOUNTER_IMPL::ParseAddress() - No ports specified for this protocol.\n");
806 WriteServLog("No ports specified for this protocol.");
811 ports[i - p] = ta[i];
817 strcpy(ports, "0-65535");
824 if ((sss = strchr(ports, '-')) != NULL)
826 strncpy(port1, ports, int(sss-ports));
827 port1[int(sss - ports)] = 0;
828 strcpy(port2, sss + 1);
832 strcpy(port1, ports);
833 strcpy(port2, ports);
836 // Convert strings to mask, ports and IP
841 msk = strtol(mask, &res, 10);
845 prt1 = strtol(port1, &res, 10);
849 prt2 = strtol(port2, &res, 10);
853 int r = inet_aton(addr, (struct in_addr*)&ip);
858 rule->mask = CalcMask(msk);
860 //printfd(__FILE__, "msk=%d mask=%08X mask=%08X\n", msk, rule->mask, (0xFFffFFff << (32 - msk)));
862 if ((ip & rule->mask) != ip)
864 printfd(__FILE__, "TRAFFCOUNTER_IMPL::ParseAddress() - Address does'n match mask.\n");
865 WriteServLog("Address does'n match mask.");
874 //-----------------------------------------------------------------------------
875 uint32_t TRAFFCOUNTER_IMPL::CalcMask(uint32_t msk) const
877 if (msk >= 32) return 0xFFffFFff;
878 if (msk == 0) return 0;
879 return htonl(0xFFffFFff << (32 - msk));
881 //---------------------------------------------------------------------------
882 void TRAFFCOUNTER_IMPL::FreeRules()
886 //-----------------------------------------------------------------------------
887 void TRAFFCOUNTER_IMPL::PrintRule(RULE rule) const
889 printf("%15s ", inet_ntostring(rule.ip).c_str());
890 printf("mask=%08X ", rule.mask);
891 printf("port1=%5d ", rule.port1);
892 printf("port2=%5d ", rule.port2);
911 printf("dir=%d \n", rule.dir);
914 //-----------------------------------------------------------------------------
915 void TRAFFCOUNTER_IMPL::SetMonitorDir(const std::string & monitorDir)
917 TRAFFCOUNTER_IMPL::monitorDir = monitorDir;
918 monitoring = (monitorDir != "");
920 //-----------------------------------------------------------------------------