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/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
38 #include <cstdio> // fopen and similar
40 #include "traffcounter_impl.h"
42 #include "stg_locker.h"
43 #include "stg_timer.h"
45 #define FLUSH_TIME (10)
46 #define REMOVE_TIME (31)
48 const char protoName[PROTOMAX][8] =
49 {"TCP", "UDP", "ICMP", "TCP_UDP", "ALL"};
53 tcp = 0, udp, icmp, tcp_udp, all
56 //-----------------------------------------------------------------------------
57 TRAFFCOUNTER_IMPL::TRAFFCOUNTER_IMPL(USERS * u, const TARIFFS *, const std::string & fn)
58 : WriteServLog(GetStgLogger()),
64 addUserNotifier(*this),
65 delUserNotifier(*this)
67 for (int i = 0; i < DIR_NUM; i++)
68 strprintf(&dirName[i], "DIR%d", i);
70 dirName[DIR_NUM] = "NULL";
72 users->AddNotifierUserAdd(&addUserNotifier);
73 users->AddNotifierUserDel(&delUserNotifier);
75 pthread_mutex_init(&mutex, NULL);
77 //-----------------------------------------------------------------------------
78 TRAFFCOUNTER_IMPL::~TRAFFCOUNTER_IMPL()
80 pthread_mutex_destroy(&mutex);
82 //-----------------------------------------------------------------------------
83 int TRAFFCOUNTER_IMPL::Start()
85 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
92 WriteServLog("TRAFFCOUNTER: Cannot read rules.");
96 printfd(__FILE__, "TRAFFCOUNTER::Start()\n");
97 int h = users->OpenSearch();
101 WriteServLog("TRAFFCOUNTER: Cannot get users.");
105 while (users->SearchNext(h, &u) == 0)
109 users->CloseSearch(h);
112 if (pthread_create(&thread, NULL, Run, this))
114 WriteServLog("TRAFFCOUNTER: Error: Cannot start thread!");
119 //-----------------------------------------------------------------------------
120 int TRAFFCOUNTER_IMPL::Stop()
127 int h = users->OpenSearch();
130 WriteServLog("TRAFFCOUNTER: Fatal error: Cannot get users.");
135 while (users->SearchNext(h, &u) == 0)
137 UnSetUserNotifiers(u);
139 users->CloseSearch(h);
141 //5 seconds to thread stops itself
142 struct timespec ts = {0, 200000000};
143 for (int i = 0; i < 25 && !stopped; i++)
145 nanosleep(&ts, NULL);
148 //after 5 seconds waiting thread still running. now kill it
151 printfd(__FILE__, "kill TRAFFCOUNTER thread.\n");
152 if (pthread_kill(thread, SIGINT))
156 printfd(__FILE__, "TRAFFCOUNTER killed\n");
158 printfd(__FILE__, "TRAFFCOUNTER::Stop()\n");
162 //-----------------------------------------------------------------------------
163 void * TRAFFCOUNTER_IMPL::Run(void * data)
165 TRAFFCOUNTER_IMPL * tc = static_cast<TRAFFCOUNTER_IMPL *>(data);
169 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
170 struct timespec ts = {0, 500000000};
176 tc->FlushAndRemove();
180 if (tc->monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
182 std::string monFile(tc->monitorDir + "/traffcounter_r");
183 printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
185 TouchFile(monFile.c_str());
188 if (++c % FLUSH_TIME == 0)
189 tc->FlushAndRemove();
195 //-----------------------------------------------------------------------------
196 void TRAFFCOUNTER_IMPL::Process(const RAW_PACKET & rawPacket)
201 static time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
203 if (monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
205 static std::string monFile = monitorDir + "/traffcounter_p";
206 printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", monitoring, monFile.c_str());
208 TouchFile(monFile.c_str());
211 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
213 //printfd(__FILE__, "TRAFFCOUNTER::Process()\n");
214 //TODO replace find with lower_bound.
216 // Searching a new packet in a tree.
217 pp_iter pi = packets.find(rawPacket);
219 // Packet found - update length and time
220 if (pi != packets.end())
222 pi->second.lenU += rawPacket.GetLen();
223 pi->second.lenD += rawPacket.GetLen();
224 pi->second.updateTime = stgTime;
225 /*printfd(__FILE__, "=============================\n");
226 printfd(__FILE__, "Packet found!\n");
227 printfd(__FILE__, "Version=%d\n", rawPacket.GetIPVersion());
228 printfd(__FILE__, "HeaderLen=%d\n", rawPacket.GetHeaderLen());
229 printfd(__FILE__, "PacketLen=%d\n", rawPacket.GetLen());
230 printfd(__FILE__, "SIP=%s\n", inet_ntostring(rawPacket.GetSrcIP()).c_str());
231 printfd(__FILE__, "DIP=%s\n", inet_ntostring(rawPacket.GetDstIP()).c_str());
232 printfd(__FILE__, "src port=%d\n", rawPacket.GetSrcPort());
233 printfd(__FILE__, "pst port=%d\n", rawPacket.GetDstPort());
234 printfd(__FILE__, "len=%d\n", rawPacket.GetLen());
235 printfd(__FILE__, "proto=%d\n", rawPacket.GetProto());
236 printfd(__FILE__, "PacketDirU=%d\n", pi->second.dirU);
237 printfd(__FILE__, "PacketDirD=%d\n", pi->second.dirD);
238 printfd(__FILE__, "=============================\n");*/
242 PACKET_EXTRA_DATA ed;
244 // Packet not found - add new packet
246 ed.updateTime = stgTime;
247 ed.flushTime = stgTime;
250 userU is that whose user_ip == packet_ip_src
251 userD is that whose user_ip == packet_ip_dst
254 uint32_t ipU = rawPacket.GetSrcIP();
255 uint32_t ipD = rawPacket.GetDstIP();
257 // Searching users with such IP
258 if (users->FindByIPIdx(ipU, &ed.userU) == 0)
260 ed.userUPresent = true;
263 if (users->FindByIPIdx(ipD, &ed.userD) == 0)
265 ed.userDPresent = true;
268 if (ed.userUPresent ||
271 DeterminateDir(rawPacket, &ed.dirU, &ed.dirD);
273 ed.lenD = ed.lenU = rawPacket.GetLen();
275 //TODO use result of lower_bound to inserting new record
277 // Adding packet to a tree.
278 std::pair<pp_iter, bool> insertResult = packets.insert(std::make_pair(rawPacket, ed));
279 pp_iter newPacket = insertResult.first;
281 // Adding packet reference to an IP index.
282 ip2packets.insert(std::make_pair(ipU, newPacket));
283 ip2packets.insert(std::make_pair(ipD, newPacket));
286 //-----------------------------------------------------------------------------
287 void TRAFFCOUNTER_IMPL::FlushAndRemove()
289 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
291 int oldPacketsSize = packets.size();
292 int oldIp2packetsSize = ip2packets.size();
295 pi = packets.begin();
296 std::map<RAW_PACKET, PACKET_EXTRA_DATA> newPackets;
297 ip2packets.erase(ip2packets.begin(), ip2packets.end());
298 while (pi != packets.end())
301 if (stgTime - pi->second.flushTime > FLUSH_TIME)
303 if (pi->second.userUPresent)
305 //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);
308 if (pi->second.dirU < DIR_NUM)
310 #ifdef TRAFF_STAT_WITH_PORTS
311 pi->second.userU->AddTraffStatU(pi->second.dirU,
312 pi->first.GetDstIP(),
313 pi->first.GetDstPort(),
316 pi->second.userU->AddTraffStatU(pi->second.dirU,
317 pi->first.GetDstIP(),
323 pi->second.flushTime = stgTime;
326 if (pi->second.userDPresent)
328 //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);
331 if (pi->second.dirD < DIR_NUM)
333 #ifdef TRAFF_STAT_WITH_PORTS
334 pi->second.userD->AddTraffStatD(pi->second.dirD,
335 pi->first.GetSrcIP(),
336 pi->first.GetSrcPort(),
339 pi->second.userD->AddTraffStatD(pi->second.dirD,
340 pi->first.GetSrcIP(),
346 pi->second.flushTime = stgTime;
351 if (stgTime - pi->second.updateTime > REMOVE_TIME)
353 // Remove packet and references from ip2packets index
354 //printfd(__FILE__, "+++ Removing +++\n");
355 pair<ip2p_iter, ip2p_iter> be(
356 ip2packets.equal_range(pi->first.GetSrcIP()));
357 while (be.first != be.second)
359 // Have a reference to a packet?
360 if (be.first->second == pi)
362 ip2packets.erase(be.first++);
363 //printfd(__FILE__, "Remove U from ip2packets %s\n", inet_ntostring(pi->first.GetSrcIP()).c_str());
371 //printfd(__FILE__, "-------------------\n");
372 be = ip2packets.equal_range(pi->first.GetDstIP());
373 while (be.first != be.second)
375 // Have a reference to a packet?
376 if (be.first->second == pi)
378 ip2packets.erase(be.first++);
379 //printfd(__FILE__, "Remove D from ip2packets %s\n", inet_ntostring(pi->first.GetDstIP()).c_str());
386 //printfd(__FILE__, "Remove packet\n");
393 if (stgTime - pi->second.updateTime < REMOVE_TIME)
395 std::pair<pp_iter, bool> res = newPackets.insert(*pi);
398 ip2packets.insert(std::make_pair(pi->first.GetSrcIP(), res.first));
399 ip2packets.insert(std::make_pair(pi->first.GetDstIP(), res.first));
404 swap(packets, newPackets);
405 printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n",
407 oldPacketsSize - packets.size(),
409 oldIp2packetsSize - ip2packets.size());
412 //-----------------------------------------------------------------------------
413 void TRAFFCOUNTER_IMPL::AddUser(USER_PTR user)
415 printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str());
416 uint32_t uip = user->GetCurrIP();
417 std::pair<ip2p_iter, ip2p_iter> pi;
419 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
420 // Find all packets with IP belongs to this user
421 pi = ip2packets.equal_range(uip);
423 while (pi.first != pi.second)
425 if (pi.first->second->first.GetSrcIP() == uip)
427 assert((!pi.first->second->second.userUPresent ||
428 pi.first->second->second.userU == user) &&
429 "U user present but it's not current user");
431 pi.first->second->second.lenU = 0;
432 pi.first->second->second.userU = user;
433 pi.first->second->second.userUPresent = true;
436 if (pi.first->second->first.GetDstIP() == uip)
438 assert((!pi.first->second->second.userDPresent ||
439 pi.first->second->second.userD == user) &&
440 "D user present but it's not current user");
442 pi.first->second->second.lenD = 0;
443 pi.first->second->second.userD = user;
444 pi.first->second->second.userDPresent = true;
450 //-----------------------------------------------------------------------------
451 void TRAFFCOUNTER_IMPL::DelUser(uint32_t uip)
453 printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
454 std::pair<ip2p_iter, ip2p_iter> pi;
456 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
457 pi = ip2packets.equal_range(uip);
459 while (pi.first != pi.second)
461 if (pi.first->second->first.GetSrcIP() == uip)
463 if (pi.first->second->second.dirU < DIR_NUM && pi.first->second->second.userUPresent)
465 #ifdef TRAFF_STAT_WITH_PORTS
466 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
467 pi.first->second->first.GetDstIP(),
468 pi.first->second->first.GetDstPort(),
469 pi.first->second->second.lenU);
471 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
472 pi.first->second->first.GetDstIP(),
473 pi.first->second->second.lenU);
476 pi.first->second->second.userUPresent = false;
479 if (pi.first->second->first.GetDstIP() == uip)
481 if (pi.first->second->second.dirD < DIR_NUM && pi.first->second->second.userDPresent)
483 #ifdef TRAFF_STAT_WITH_PORTS
484 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
485 pi.first->second->first.GetSrcIP(),
486 pi.first->second->first.GetSrcPort(),
487 pi.first->second->second.lenD);
489 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
490 pi.first->second->first.GetSrcIP(),
491 pi.first->second->second.lenD);
495 pi.first->second->second.userDPresent = false;
501 ip2packets.erase(pi.first, pi.second);
503 //-----------------------------------------------------------------------------
504 void TRAFFCOUNTER_IMPL::SetUserNotifiers(USER_PTR user)
506 // Adding user. Adding notifiers to user.
507 TRF_IP_BEFORE ipBNotifier(*this, user);
508 ipBeforeNotifiers.push_front(ipBNotifier);
509 user->AddCurrIPBeforeNotifier(&(*ipBeforeNotifiers.begin()));
511 TRF_IP_AFTER ipANotifier(*this, user);
512 ipAfterNotifiers.push_front(ipANotifier);
513 user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
515 //-----------------------------------------------------------------------------
516 void TRAFFCOUNTER_IMPL::UnSetUserNotifiers(USER_PTR user)
518 // Removing user. Removing notifiers from user.
519 std::list<TRF_IP_BEFORE>::iterator bi;
520 std::list<TRF_IP_AFTER>::iterator ai;
522 bi = ipBeforeNotifiers.begin();
523 while (bi != ipBeforeNotifiers.end())
525 if (user->GetLogin() == bi->GetUser()->GetLogin())
527 user->DelCurrIPBeforeNotifier(&(*bi));
528 ipBeforeNotifiers.erase(bi);
534 ai = ipAfterNotifiers.begin();
535 while (ai != ipAfterNotifiers.end())
537 if (user->GetLogin() == ai->GetUser()->GetLogin())
539 user->DelCurrIPAfterNotifier(&(*ai));
540 ipAfterNotifiers.erase(ai);
546 //-----------------------------------------------------------------------------
547 void TRAFFCOUNTER_IMPL::DeterminateDir(const RAW_PACKET & packet,
548 int * dirU, // Direction for incoming packet
549 int * dirD) const // Direction for outgoing packet
555 bool foundU = false; // Was rule for U found ?
556 bool foundD = false; // Was rule for D found ?
557 //printfd(__FILE__, "foundU=%d, foundD=%d\n", foundU, foundD);
559 enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
561 std::list<RULE>::const_iterator ln;
564 while (ln != rules.end())
578 portMatchU = (packet.GetProto() == ICMP_RPOTO);
582 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
583 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
587 if (packet.GetProto() == TCP_PROTO)
588 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
592 if (packet.GetProto() == UDP_PROTO)
593 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
597 printfd(__FILE__, "Error! Incorrect rule!\n");
601 addrMatchU = (packet.GetDstIP() & ln->mask) == ln->ip;
603 if (!foundU && addrMatchU && portMatchU)
607 //printfd(__FILE__, "Up rule ok! %d\n", ln->dir);
608 //PrintRule(ln->rule);
625 portMatchD = (packet.GetProto() == ICMP_RPOTO);
629 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
630 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
634 if (packet.GetProto() == TCP_PROTO)
635 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
639 if (packet.GetProto() == UDP_PROTO)
640 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
644 printfd(__FILE__, "Error! Incorrect rule!\n");
648 addrMatchD = (packet.GetSrcIP() & ln->mask) == ln->ip;
650 if (!foundD && addrMatchD && portMatchD)
654 //printfd(__FILE__, "Down rule ok! %d\n", ln->dir);
655 //PrintRule(ln->rule);
660 } //while (ln != rules.end())
670 //-----------------------------------------------------------------------------
671 void TRAFFCOUNTER_IMPL::SetRulesFile(const std::string & fn)
675 //-----------------------------------------------------------------------------
676 bool TRAFFCOUNTER_IMPL::ReadRules(bool test)
678 //printfd(__FILE__, "TRAFFCOUNTER::ReadRules()\n");
683 char tp[100]; // protocol
684 char ta[100]; // address
685 char td[100]; // target direction
688 f = fopen(rulesFileName.c_str(), "rt");
692 WriteServLog("File %s cannot be oppened.", rulesFileName.c_str());
696 while (fgets(str, 1023, f))
699 if (str[strspn(str," \t")] == '#' || str[strspn(str," \t")] == '\n')
704 r = sscanf(str,"%s %s %s", tp, ta, td);
707 WriteServLog("Error in file %s. There must be 3 parameters. Line %d.", rulesFileName.c_str(), lineNumber);
715 for (int i = 0; i < PROTOMAX; i++)
717 if (strcasecmp(tp, protoName[i]) == 0)
721 for (int i = 0; i < DIR_NUM + 1; i++)
723 if (td == dirName[i])
727 if (rul.dir == 0xff || rul.proto == 0xff)
729 WriteServLog("Error in file %s. Line %d.",
730 rulesFileName.c_str(), lineNumber);
735 if (ParseAddress(ta, &rul) != 0)
737 WriteServLog("Error in file %s. Error in adress. Line %d.",
738 rulesFileName.c_str(), lineNumber);
743 rules.push_back(rul);
749 // Adding lastest rule: ALL 0.0.0.0/0 NULL
750 rul.dir = DIR_NUM; //NULL
751 rul.ip = 0; //0.0.0.0
758 rules.push_back(rul);
764 //-----------------------------------------------------------------------------
765 int TRAFFCOUNTER_IMPL::Reload()
767 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
771 WriteServLog("TRAFFCOUNTER: Cannot reload rules. Errors found.");
777 WriteServLog("TRAFFCOUNTER: Reload rules successfull.");
780 //-----------------------------------------------------------------------------
781 bool TRAFFCOUNTER_IMPL::ParseAddress(const char * ta, RULE * rule) const
783 char addr[50], mask[20], port1[20], port2[20], ports[40];
785 int len = strlen(ta);
788 memset(addr, 0, sizeof(addr));
789 for (i = 0; i < len; i++)
791 if (ta[i] == '/' || ta[i] == ':')
829 if (!(rule->proto == tcp || rule->proto == udp || rule->proto == tcp_udp))
831 WriteServLog("No ports specified for this protocol.");
836 ports[i - p] = ta[i];
842 strcpy(ports, "0-65535");
849 if ((sss = strchr(ports, '-')) != NULL)
851 strncpy(port1, ports, int(sss-ports));
852 port1[int(sss - ports)] = 0;
853 strcpy(port2, sss + 1);
857 strcpy(port1, ports);
858 strcpy(port2, ports);
861 // Convert strings to mask, ports and IP
866 msk = strtol(mask, &res, 10);
870 prt1 = strtol(port1, &res, 10);
874 prt2 = strtol(port2, &res, 10);
878 int r = inet_aton(addr, (struct in_addr*)&ip);
883 rule->mask = CalcMask(msk);
885 //printfd(__FILE__, "msk=%d mask=%08X mask=%08X\n", msk, rule->mask, (0xFFffFFff << (32 - msk)));
887 if ((ip & rule->mask) != ip)
889 WriteServLog("Address does'n match mask.");
898 //-----------------------------------------------------------------------------
899 uint32_t TRAFFCOUNTER_IMPL::CalcMask(uint32_t msk) const
901 if (msk >= 32) return 0xFFffFFff;
902 if (msk == 0) return 0;
903 return htonl(0xFFffFFff << (32 - msk));
905 //---------------------------------------------------------------------------
906 void TRAFFCOUNTER_IMPL::FreeRules()
910 //-----------------------------------------------------------------------------
911 void TRAFFCOUNTER_IMPL::PrintRule(RULE rule) const
913 printf("%15s ", inet_ntostring(rule.ip).c_str());
914 printf("mask=%08X ", rule.mask);
915 printf("port1=%5d ", rule.port1);
916 printf("port2=%5d ", rule.port2);
935 printf("dir=%d \n", rule.dir);
938 //-----------------------------------------------------------------------------
939 void TRAFFCOUNTER_IMPL::SetMonitorDir(const std::string & monitorDir)
941 TRAFFCOUNTER_IMPL::monitorDir = monitorDir;
942 monitoring = (monitorDir != "");
944 //-----------------------------------------------------------------------------