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 std::multimap<uint32_t, pp_iter> newIP2Packets;
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 newIP2Packets.insert(make_pair(pi->first.GetSrcIP(), res.first));
392 newIP2Packets.insert(make_pair(pi->first.GetDstIP(), res.first));
397 swap(packets, newPackets);
398 swap(ip2packets, newIP2Packets);
399 printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n",
401 oldPacketsSize - packets.size(),
403 oldIp2packetsSize - ip2packets.size());
406 //-----------------------------------------------------------------------------
407 void TRAFFCOUNTER::AddUser(user_iter user)
409 printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str());
410 uint32_t uip = user->GetCurrIP();
411 pair<ip2p_iter, ip2p_iter> pi;
413 STG_LOCKER(&mutex, __FILE__, __LINE__);
414 // Find all packets with IP belongs to this user
415 pi = ip2packets.equal_range(uip);
417 while (pi.first != pi.second)
419 if (pi.first->second->first.GetSrcIP() == uip)
421 assert((!pi.first->second->second.userUPresent ||
422 pi.first->second->second.userU == user) &&
423 "U user present but it's not current user");
425 pi.first->second->second.lenU = 0;
426 pi.first->second->second.userU = user;
427 pi.first->second->second.userUPresent = true;
430 if (pi.first->second->first.GetDstIP() == uip)
432 assert((!pi.first->second->second.userDPresent ||
433 pi.first->second->second.userD == user) &&
434 "D user present but it's not current user");
436 pi.first->second->second.lenD = 0;
437 pi.first->second->second.userD = user;
438 pi.first->second->second.userDPresent = true;
444 //-----------------------------------------------------------------------------
445 void TRAFFCOUNTER::DelUser(uint32_t uip)
447 printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
448 pair<ip2p_iter, ip2p_iter> pi;
450 STG_LOCKER(&mutex, __FILE__, __LINE__);
451 pi = ip2packets.equal_range(uip);
453 while (pi.first != pi.second)
455 if (pi.first->second->first.GetSrcIP() == uip)
457 if (pi.first->second->second.dirU < DIR_NUM && pi.first->second->second.userUPresent)
459 #ifdef TRAFF_STAT_WITH_PORTS
460 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
461 pi.first->second->first.GetDstIP(),
462 pi.first->second->first.GetDstPort(),
463 pi.first->second->second.lenU);
465 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
466 pi.first->second->first.GetDstIP(),
467 pi.first->second->second.lenU);
470 pi.first->second->second.userUPresent = false;
473 if (pi.first->second->first.GetDstIP() == uip)
475 if (pi.first->second->second.dirD < DIR_NUM && pi.first->second->second.userDPresent)
477 #ifdef TRAFF_STAT_WITH_PORTS
478 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
479 pi.first->second->first.GetSrcIP(),
480 pi.first->second->first.GetSrcPort(),
481 pi.first->second->second.lenD);
483 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
484 pi.first->second->first.GetSrcIP(),
485 pi.first->second->second.lenD);
489 pi.first->second->second.userDPresent = false;
495 ip2packets.erase(pi.first, pi.second);
497 //-----------------------------------------------------------------------------
498 void TRAFFCOUNTER::SetUserNotifiers(user_iter user)
500 // Adding user. Adding notifiers to user.
501 TRF_IP_BEFORE ipBNotifier(*this, user);
502 ipBeforeNotifiers.push_front(ipBNotifier);
503 user->AddCurrIPBeforeNotifier(&(*ipBeforeNotifiers.begin()));
505 TRF_IP_AFTER ipANotifier(*this, user);
506 ipAfterNotifiers.push_front(ipANotifier);
507 user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
509 //-----------------------------------------------------------------------------
510 void TRAFFCOUNTER::UnSetUserNotifiers(user_iter user)
512 // Removing user. Removing notifiers from user.
513 list<TRF_IP_BEFORE>::iterator bi;
514 list<TRF_IP_AFTER>::iterator ai;
516 bi = ipBeforeNotifiers.begin();
517 while (bi != ipBeforeNotifiers.end())
519 if (user->GetLogin() == bi->GetUser()->GetLogin())
521 user->DelCurrIPBeforeNotifier(&(*bi));
522 ipBeforeNotifiers.erase(bi);
528 ai = ipAfterNotifiers.begin();
529 while (ai != ipAfterNotifiers.end())
531 if (user->GetLogin() == ai->GetUser()->GetLogin())
533 user->DelCurrIPAfterNotifier(&(*ai));
534 ipAfterNotifiers.erase(ai);
540 //-----------------------------------------------------------------------------
541 void TRAFFCOUNTER::DeterminateDir(const RAW_PACKET & packet,
542 int * dirU, // Direction for incoming packet
543 int * dirD) const // Direction for outgoing packet
549 bool foundU = false; // Was rule for U found ?
550 bool foundD = false; // Was rule for D found ?
551 //printfd(__FILE__, "foundU=%d, foundD=%d\n", foundU, foundD);
553 enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
555 list<RULE>::const_iterator ln;
558 while (ln != rules.end())
572 portMatchU = (packet.GetProto() == ICMP_RPOTO);
576 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
577 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
581 if (packet.GetProto() == TCP_PROTO)
582 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
586 if (packet.GetProto() == UDP_PROTO)
587 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
591 printfd(__FILE__, "Error! Incorrect rule!\n");
595 addrMatchU = (packet.GetDstIP() & ln->mask) == ln->ip;
597 if (!foundU && addrMatchU && portMatchU)
601 //printfd(__FILE__, "Up rule ok! %d\n", ln->dir);
602 //PrintRule(ln->rule);
619 portMatchD = (packet.GetProto() == ICMP_RPOTO);
623 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
624 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
628 if (packet.GetProto() == TCP_PROTO)
629 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
633 if (packet.GetProto() == UDP_PROTO)
634 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
638 printfd(__FILE__, "Error! Incorrect rule!\n");
642 addrMatchD = (packet.GetSrcIP() & ln->mask) == ln->ip;
644 if (!foundD && addrMatchD && portMatchD)
648 //printfd(__FILE__, "Down rule ok! %d\n", ln->dir);
649 //PrintRule(ln->rule);
654 } //while (ln != rules.end())
664 //-----------------------------------------------------------------------------
665 void TRAFFCOUNTER::SetRulesFile(const string & fn)
669 //-----------------------------------------------------------------------------
670 bool TRAFFCOUNTER::ReadRules(bool test)
672 //printfd(__FILE__, "TRAFFCOUNTER::ReadRules()\n");
677 char tp[100]; // protocol
678 char ta[100]; // address
679 char td[100]; // target direction
682 f = fopen(rulesFileName.c_str(), "rt");
686 WriteServLog("File %s cannot be oppened.", rulesFileName.c_str());
690 while (fgets(str, 1023, f))
693 if (str[strspn(str," \t")] == '#' || str[strspn(str," \t")] == '\n')
698 r = sscanf(str,"%s %s %s", tp, ta, td);
701 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);
727 if (ParseAddress(ta, &rul) != 0)
729 WriteServLog("Error in file %s. Error in adress. Line %d.",
730 rulesFileName.c_str(), lineNumber);
734 rules.push_back(rul);
740 // Adding lastest rule: ALL 0.0.0.0/0 NULL
741 rul.dir = DIR_NUM; //NULL
742 rul.ip = 0; //0.0.0.0
749 rules.push_back(rul);
755 //-----------------------------------------------------------------------------
756 int TRAFFCOUNTER::Reload()
758 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
762 WriteServLog("TRAFFCOUNTER: Cannot reload rules. Errors found.");
768 WriteServLog("TRAFFCOUNTER: Reload rules successfull.");
771 //-----------------------------------------------------------------------------
772 bool TRAFFCOUNTER::ParseAddress(const char * ta, RULE * rule) const
774 char addr[50], mask[20], port1[20], port2[20], ports[40];
776 int len = strlen(ta);
779 memset(addr, 0, sizeof(addr));
780 for (i = 0; i < len; i++)
782 if (ta[i] == '/' || ta[i] == ':')
820 if (!(rule->proto == tcp || rule->proto == udp || rule->proto == tcp_udp))
822 WriteServLog("No ports specified for this protocol.");
827 ports[i - p] = ta[i];
833 strcpy(ports, "0-65535");
840 if ((sss = strchr(ports, '-')) != NULL)
842 strncpy(port1, ports, int(sss-ports));
843 port1[int(sss - ports)] = 0;
844 strcpy(port2, sss + 1);
848 strcpy(port1, ports);
849 strcpy(port2, ports);
852 // Convert strings to mask, ports and IP
857 msk = strtol(mask, &res, 10);
861 prt1 = strtol(port1, &res, 10);
865 prt2 = strtol(port2, &res, 10);
869 int r = inet_aton(addr, (struct in_addr*)&ip);
874 rule->mask = CalcMask(msk);
876 //printfd(__FILE__, "msk=%d mask=%08X mask=%08X\n", msk, rule->mask, (0xFFffFFff << (32 - msk)));
878 if ((ip & rule->mask) != ip)
880 WriteServLog("Address does'n match mask.");
889 //-----------------------------------------------------------------------------
890 uint32_t TRAFFCOUNTER::CalcMask(uint32_t msk) const
892 if (msk >= 32) return 0xFFffFFff;
893 if (msk == 0) return 0;
894 return htonl(0xFFffFFff << (32 - msk));
896 //---------------------------------------------------------------------------
897 void TRAFFCOUNTER::FreeRules()
901 //-----------------------------------------------------------------------------
902 void TRAFFCOUNTER::PrintRule(RULE rule) const
904 printf("%15s ", inet_ntostring(rule.ip).c_str());
905 printf("mask=%08X ", rule.mask);
906 printf("port1=%5d ", rule.port1);
907 printf("port2=%5d ", rule.port2);
926 printf("dir=%d \n", rule.dir);
929 //-----------------------------------------------------------------------------
930 void TRAFFCOUNTER::SetMonitorDir(const string & monitorDir)
932 TRAFFCOUNTER::monitorDir = monitorDir;
933 monitoring = (monitorDir != "");
935 //-----------------------------------------------------------------------------