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 $
34 #include "traffcounter.h"
36 #include "stg_locker.h"
38 #define FLUSH_TIME (10)
39 #define REMOVE_TIME (31)
41 const char protoName[PROTOMAX][8] =
42 {"TCP", "UDP", "ICMP", "TCP_UDP", "ALL"};
46 tcp = 0, udp, icmp, tcp_udp, all
49 //-----------------------------------------------------------------------------
50 TRAFFCOUNTER::TRAFFCOUNTER(USERS * u, const TARIFFS *, const string & fn)
51 : WriteServLog(GetStgLogger()),
57 addUserNotifier(*this),
58 delUserNotifier(*this)
60 for (int i = 0; i < DIR_NUM; i++)
61 strprintf(&dirName[i], "DIR%d", i);
63 dirName[DIR_NUM] = "NULL";
65 users->AddNotifierUserAdd(&addUserNotifier);
66 users->AddNotifierUserDel(&delUserNotifier);
68 pthread_mutex_init(&mutex, NULL);
70 //-----------------------------------------------------------------------------
71 TRAFFCOUNTER::~TRAFFCOUNTER()
73 pthread_mutex_destroy(&mutex);
75 //-----------------------------------------------------------------------------
76 int TRAFFCOUNTER::Start()
78 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
85 WriteServLog("TRAFFCOUNTER: Cannot read rules.");
89 printfd(__FILE__, "TRAFFCOUNTER::Start()\n");
90 int h = users->OpenSearch();
94 WriteServLog("TRAFFCOUNTER: Cannot get users.");
98 while (users->SearchNext(h, &u) == 0)
102 users->CloseSearch(h);
105 if (pthread_create(&thread, NULL, Run, this))
107 WriteServLog("TRAFFCOUNTER: Error: Cannot start thread!");
112 //-----------------------------------------------------------------------------
113 int TRAFFCOUNTER::Stop()
120 int h = users->OpenSearch();
123 WriteServLog("TRAFFCOUNTER: Fatal error: Cannot get users.");
128 while (users->SearchNext(h, &u) == 0)
130 UnSetUserNotifiers(u);
132 users->CloseSearch(h);
134 //5 seconds to thread stops itself
135 for (int i = 0; i < 25 && !stopped; i++)
140 //after 5 seconds waiting thread still running. now kill it
143 printfd(__FILE__, "kill TRAFFCOUNTER thread.\n");
144 if (pthread_kill(thread, SIGINT))
148 printfd(__FILE__, "TRAFFCOUNTER killed\n");
150 printfd(__FILE__, "TRAFFCOUNTER::Stop()\n");
154 //-----------------------------------------------------------------------------
155 void * TRAFFCOUNTER::Run(void * data)
157 TRAFFCOUNTER * tc = static_cast<TRAFFCOUNTER *>(data);
161 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
168 tc->FlushAndRemove();
172 if (tc->monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
174 string monFile(tc->monitorDir + "/traffcounter_r");
175 printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
177 TouchFile(monFile.c_str());
180 if (++c % FLUSH_TIME == 0)
181 tc->FlushAndRemove();
187 //-----------------------------------------------------------------------------
188 void TRAFFCOUNTER::Process(const RAW_PACKET & rawPacket)
193 static time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
195 if (monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
197 static string monFile = monitorDir + "/traffcounter_p";
198 printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", monitoring, monFile.c_str());
200 TouchFile(monFile.c_str());
203 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
205 //printfd(__FILE__, "TRAFFCOUNTER::Process()\n");
206 //TODO replace find with lower_bound.
208 // Searching a new packet in a tree.
209 pp_iter pi = packets.find(rawPacket);
211 // Packet found - update length and time
212 if (pi != packets.end())
214 pi->second.lenU += rawPacket.GetLen();
215 pi->second.lenD += rawPacket.GetLen();
216 pi->second.updateTime = stgTime;
217 /*printfd(__FILE__, "=============================\n");
218 printfd(__FILE__, "Packet found!\n");
219 printfd(__FILE__, "Version=%d\n", rawPacket.GetIPVersion());
220 printfd(__FILE__, "HeaderLen=%d\n", rawPacket.GetHeaderLen());
221 printfd(__FILE__, "PacketLen=%d\n", rawPacket.GetLen());
222 printfd(__FILE__, "SIP=%s\n", inet_ntostring(rawPacket.GetSrcIP()).c_str());
223 printfd(__FILE__, "DIP=%s\n", inet_ntostring(rawPacket.GetDstIP()).c_str());
224 printfd(__FILE__, "src port=%d\n", rawPacket.GetSrcPort());
225 printfd(__FILE__, "pst port=%d\n", rawPacket.GetDstPort());
226 printfd(__FILE__, "len=%d\n", rawPacket.GetLen());
227 printfd(__FILE__, "proto=%d\n", rawPacket.GetProto());
228 printfd(__FILE__, "PacketDirU=%d\n", pi->second.dirU);
229 printfd(__FILE__, "PacketDirD=%d\n", pi->second.dirD);
230 printfd(__FILE__, "=============================\n");*/
234 PACKET_EXTRA_DATA ed;
236 // Packet not found - add new packet
238 ed.updateTime = stgTime;
239 ed.flushTime = stgTime;
242 userU is that whose user_ip == packet_ip_src
243 userD is that whose user_ip == packet_ip_dst
246 uint32_t ipU = rawPacket.GetSrcIP();
247 uint32_t ipD = rawPacket.GetDstIP();
249 // Searching users with such IP
250 if (users->FindByIPIdx(ipU, &ed.userU) == 0)
252 ed.userUPresent = true;
255 if (users->FindByIPIdx(ipD, &ed.userD) == 0)
257 ed.userDPresent = true;
260 if (ed.userUPresent ||
263 DeterminateDir(rawPacket, &ed.dirU, &ed.dirD);
265 ed.lenD = ed.lenU = rawPacket.GetLen();
267 //TODO use result of lower_bound to inserting new record
269 // Adding packet to a tree.
270 pair<pp_iter, bool> insertResult = packets.insert(pair<RAW_PACKET,
271 PACKET_EXTRA_DATA>(rawPacket, ed));
272 pp_iter newPacket = insertResult.first;
274 // Adding packet reference to an IP index.
275 ip2packets.insert(pair<uint32_t, pp_iter>(ipU, newPacket));
276 ip2packets.insert(pair<uint32_t, pp_iter>(ipD, newPacket));
279 //-----------------------------------------------------------------------------
280 void TRAFFCOUNTER::FlushAndRemove()
282 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
284 int oldPacketsSize = packets.size();
285 int oldIp2packetsSize = ip2packets.size();
288 pi = packets.begin();
289 std::map<RAW_PACKET, PACKET_EXTRA_DATA> newPackets;
290 ip2packets.erase(ip2packets.begin(), ip2packets.end());
291 while (pi != packets.end())
294 if (stgTime - pi->second.flushTime > FLUSH_TIME)
296 if (pi->second.userUPresent)
298 //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);
301 if (pi->second.dirU < DIR_NUM)
303 #ifdef TRAFF_STAT_WITH_PORTS
304 pi->second.userU->AddTraffStatU(pi->second.dirU,
305 pi->first.GetDstIP(),
306 pi->first.GetDstPort(),
309 pi->second.userU->AddTraffStatU(pi->second.dirU,
310 pi->first.GetDstIP(),
316 pi->second.flushTime = stgTime;
319 if (pi->second.userDPresent)
321 //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);
324 if (pi->second.dirD < DIR_NUM)
326 #ifdef TRAFF_STAT_WITH_PORTS
327 pi->second.userD->AddTraffStatD(pi->second.dirD,
328 pi->first.GetSrcIP(),
329 pi->first.GetSrcPort(),
332 pi->second.userD->AddTraffStatD(pi->second.dirD,
333 pi->first.GetSrcIP(),
339 pi->second.flushTime = stgTime;
344 if (stgTime - pi->second.updateTime > REMOVE_TIME)
346 // Remove packet and references from ip2packets index
347 //printfd(__FILE__, "+++ Removing +++\n");
348 pair<ip2p_iter, ip2p_iter> be(
349 ip2packets.equal_range(pi->first.GetSrcIP()));
350 while (be.first != be.second)
352 // Have a reference to a packet?
353 if (be.first->second == pi)
355 ip2packets.erase(be.first++);
356 //printfd(__FILE__, "Remove U from ip2packets %s\n", inet_ntostring(pi->first.GetSrcIP()).c_str());
364 //printfd(__FILE__, "-------------------\n");
365 be = ip2packets.equal_range(pi->first.GetDstIP());
366 while (be.first != be.second)
368 // Have a reference to a packet?
369 if (be.first->second == pi)
371 ip2packets.erase(be.first++);
372 //printfd(__FILE__, "Remove D from ip2packets %s\n", inet_ntostring(pi->first.GetDstIP()).c_str());
379 //printfd(__FILE__, "Remove packet\n");
386 if (stgTime - pi->second.updateTime < REMOVE_TIME)
388 pair<pp_iter, bool> res = newPackets.insert(*pi);
391 ip2packets.insert(make_pair(pi->first.GetSrcIP(), res.first));
392 ip2packets.insert(make_pair(pi->first.GetDstIP(), res.first));
397 swap(packets, newPackets);
398 printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n",
400 oldPacketsSize - packets.size(),
402 oldIp2packetsSize - ip2packets.size());
405 //-----------------------------------------------------------------------------
406 void TRAFFCOUNTER::AddUser(user_iter user)
408 printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str());
409 uint32_t uip = user->GetCurrIP();
410 pair<ip2p_iter, ip2p_iter> pi;
412 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
413 // Find all packets with IP belongs to this user
414 pi = ip2packets.equal_range(uip);
416 while (pi.first != pi.second)
418 if (pi.first->second->first.GetSrcIP() == uip)
420 assert((!pi.first->second->second.userUPresent ||
421 pi.first->second->second.userU == user) &&
422 "U user present but it's not current user");
424 pi.first->second->second.lenU = 0;
425 pi.first->second->second.userU = user;
426 pi.first->second->second.userUPresent = true;
429 if (pi.first->second->first.GetDstIP() == uip)
431 assert((!pi.first->second->second.userDPresent ||
432 pi.first->second->second.userD == user) &&
433 "D user present but it's not current user");
435 pi.first->second->second.lenD = 0;
436 pi.first->second->second.userD = user;
437 pi.first->second->second.userDPresent = true;
443 //-----------------------------------------------------------------------------
444 void TRAFFCOUNTER::DelUser(uint32_t uip)
446 printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
447 pair<ip2p_iter, ip2p_iter> pi;
449 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
450 pi = ip2packets.equal_range(uip);
452 while (pi.first != pi.second)
454 if (pi.first->second->first.GetSrcIP() == uip)
456 if (pi.first->second->second.dirU < DIR_NUM && pi.first->second->second.userUPresent)
458 #ifdef TRAFF_STAT_WITH_PORTS
459 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
460 pi.first->second->first.GetDstIP(),
461 pi.first->second->first.GetDstPort(),
462 pi.first->second->second.lenU);
464 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
465 pi.first->second->first.GetDstIP(),
466 pi.first->second->second.lenU);
469 pi.first->second->second.userUPresent = false;
472 if (pi.first->second->first.GetDstIP() == uip)
474 if (pi.first->second->second.dirD < DIR_NUM && pi.first->second->second.userDPresent)
476 #ifdef TRAFF_STAT_WITH_PORTS
477 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
478 pi.first->second->first.GetSrcIP(),
479 pi.first->second->first.GetSrcPort(),
480 pi.first->second->second.lenD);
482 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
483 pi.first->second->first.GetSrcIP(),
484 pi.first->second->second.lenD);
488 pi.first->second->second.userDPresent = false;
494 ip2packets.erase(pi.first, pi.second);
496 //-----------------------------------------------------------------------------
497 void TRAFFCOUNTER::SetUserNotifiers(user_iter user)
499 // Adding user. Adding notifiers to user.
500 TRF_IP_BEFORE ipBNotifier(*this, user);
501 ipBeforeNotifiers.push_front(ipBNotifier);
502 user->AddCurrIPBeforeNotifier(&(*ipBeforeNotifiers.begin()));
504 TRF_IP_AFTER ipANotifier(*this, user);
505 ipAfterNotifiers.push_front(ipANotifier);
506 user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
508 //-----------------------------------------------------------------------------
509 void TRAFFCOUNTER::UnSetUserNotifiers(user_iter user)
511 // Removing user. Removing notifiers from user.
512 list<TRF_IP_BEFORE>::iterator bi;
513 list<TRF_IP_AFTER>::iterator ai;
515 bi = ipBeforeNotifiers.begin();
516 while (bi != ipBeforeNotifiers.end())
518 if (user->GetLogin() == bi->GetUser()->GetLogin())
520 user->DelCurrIPBeforeNotifier(&(*bi));
521 ipBeforeNotifiers.erase(bi);
527 ai = ipAfterNotifiers.begin();
528 while (ai != ipAfterNotifiers.end())
530 if (user->GetLogin() == ai->GetUser()->GetLogin())
532 user->DelCurrIPAfterNotifier(&(*ai));
533 ipAfterNotifiers.erase(ai);
539 //-----------------------------------------------------------------------------
540 void TRAFFCOUNTER::DeterminateDir(const RAW_PACKET & packet,
541 int * dirU, // Direction for incoming packet
542 int * dirD) const // Direction for outgoing packet
548 bool foundU = false; // Was rule for U found ?
549 bool foundD = false; // Was rule for D found ?
550 //printfd(__FILE__, "foundU=%d, foundD=%d\n", foundU, foundD);
552 enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
554 list<RULE>::const_iterator ln;
557 while (ln != rules.end())
571 portMatchU = (packet.GetProto() == ICMP_RPOTO);
575 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
576 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
580 if (packet.GetProto() == TCP_PROTO)
581 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
585 if (packet.GetProto() == UDP_PROTO)
586 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
590 printfd(__FILE__, "Error! Incorrect rule!\n");
594 addrMatchU = (packet.GetDstIP() & ln->mask) == ln->ip;
596 if (!foundU && addrMatchU && portMatchU)
600 //printfd(__FILE__, "Up rule ok! %d\n", ln->dir);
601 //PrintRule(ln->rule);
618 portMatchD = (packet.GetProto() == ICMP_RPOTO);
622 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
623 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
627 if (packet.GetProto() == TCP_PROTO)
628 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
632 if (packet.GetProto() == UDP_PROTO)
633 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
637 printfd(__FILE__, "Error! Incorrect rule!\n");
641 addrMatchD = (packet.GetSrcIP() & ln->mask) == ln->ip;
643 if (!foundD && addrMatchD && portMatchD)
647 //printfd(__FILE__, "Down rule ok! %d\n", ln->dir);
648 //PrintRule(ln->rule);
653 } //while (ln != rules.end())
663 //-----------------------------------------------------------------------------
664 void TRAFFCOUNTER::SetRulesFile(const string & fn)
668 //-----------------------------------------------------------------------------
669 bool TRAFFCOUNTER::ReadRules(bool test)
671 //printfd(__FILE__, "TRAFFCOUNTER::ReadRules()\n");
676 char tp[100]; // protocol
677 char ta[100]; // address
678 char td[100]; // target direction
681 f = fopen(rulesFileName.c_str(), "rt");
685 WriteServLog("File %s cannot be oppened.", rulesFileName.c_str());
689 while (fgets(str, 1023, f))
692 if (str[strspn(str," \t")] == '#' || str[strspn(str," \t")] == '\n')
697 r = sscanf(str,"%s %s %s", tp, ta, td);
700 WriteServLog("Error in file %s. There must be 3 parameters. Line %d.", rulesFileName.c_str(), lineNumber);
708 for (int i = 0; i < PROTOMAX; i++)
710 if (strcasecmp(tp, protoName[i]) == 0)
714 for (int i = 0; i < DIR_NUM + 1; i++)
716 if (td == dirName[i])
720 if (rul.dir == 0xff || rul.proto == 0xff)
722 WriteServLog("Error in file %s. Line %d.",
723 rulesFileName.c_str(), lineNumber);
728 if (ParseAddress(ta, &rul) != 0)
730 WriteServLog("Error in file %s. Error in adress. Line %d.",
731 rulesFileName.c_str(), lineNumber);
736 rules.push_back(rul);
742 // Adding lastest rule: ALL 0.0.0.0/0 NULL
743 rul.dir = DIR_NUM; //NULL
744 rul.ip = 0; //0.0.0.0
751 rules.push_back(rul);
757 //-----------------------------------------------------------------------------
758 int TRAFFCOUNTER::Reload()
760 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
764 WriteServLog("TRAFFCOUNTER: Cannot reload rules. Errors found.");
770 WriteServLog("TRAFFCOUNTER: Reload rules successfull.");
773 //-----------------------------------------------------------------------------
774 bool TRAFFCOUNTER::ParseAddress(const char * ta, RULE * rule) const
776 char addr[50], mask[20], port1[20], port2[20], ports[40];
778 int len = strlen(ta);
781 memset(addr, 0, sizeof(addr));
782 for (i = 0; i < len; i++)
784 if (ta[i] == '/' || ta[i] == ':')
822 if (!(rule->proto == tcp || rule->proto == udp || rule->proto == tcp_udp))
824 WriteServLog("No ports specified for this protocol.");
829 ports[i - p] = ta[i];
835 strcpy(ports, "0-65535");
842 if ((sss = strchr(ports, '-')) != NULL)
844 strncpy(port1, ports, int(sss-ports));
845 port1[int(sss - ports)] = 0;
846 strcpy(port2, sss + 1);
850 strcpy(port1, ports);
851 strcpy(port2, ports);
854 // Convert strings to mask, ports and IP
859 msk = strtol(mask, &res, 10);
863 prt1 = strtol(port1, &res, 10);
867 prt2 = strtol(port2, &res, 10);
871 int r = inet_aton(addr, (struct in_addr*)&ip);
876 rule->mask = CalcMask(msk);
878 //printfd(__FILE__, "msk=%d mask=%08X mask=%08X\n", msk, rule->mask, (0xFFffFFff << (32 - msk)));
880 if ((ip & rule->mask) != ip)
882 WriteServLog("Address does'n match mask.");
891 //-----------------------------------------------------------------------------
892 uint32_t TRAFFCOUNTER::CalcMask(uint32_t msk) const
894 if (msk >= 32) return 0xFFffFFff;
895 if (msk == 0) return 0;
896 return htonl(0xFFffFFff << (32 - msk));
898 //---------------------------------------------------------------------------
899 void TRAFFCOUNTER::FreeRules()
903 //-----------------------------------------------------------------------------
904 void TRAFFCOUNTER::PrintRule(RULE rule) const
906 printf("%15s ", inet_ntostring(rule.ip).c_str());
907 printf("mask=%08X ", rule.mask);
908 printf("port1=%5d ", rule.port1);
909 printf("port2=%5d ", rule.port2);
928 printf("dir=%d \n", rule.dir);
931 //-----------------------------------------------------------------------------
932 void TRAFFCOUNTER::SetMonitorDir(const string & monitorDir)
934 TRAFFCOUNTER::monitorDir = monitorDir;
935 monitoring = (monitorDir != "");
937 //-----------------------------------------------------------------------------