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 "stg/const.h" // MONITOR_TIME_DELAY_SEC
45 #include "traffcounter_impl.h"
46 #include "stg_timer.h"
47 #include "users_impl.h"
49 #define FLUSH_TIME (10)
50 #define REMOVE_TIME (31)
52 using STG::TraffCounterImpl;
54 const char protoName[PROTOMAX][8] =
55 {"TCP", "UDP", "ICMP", "TCP_UDP", "ALL"};
59 tcp = 0, udp, icmp, tcp_udp, all
62 //-----------------------------------------------------------------------------
63 TraffCounterImpl::TraffCounterImpl(UsersImpl * u, const std::string & fn)
64 : WriteServLog(Logger::get()),
67 touchTimeP(stgTime - MONITOR_TIME_DELAY_SEC),
71 addUserNotifier(*this),
72 delUserNotifier(*this)
74 for (int i = 0; i < DIR_NUM; i++)
75 strprintf(&dirName[i], "DIR%d", i);
77 dirName[DIR_NUM] = "NULL";
79 users->AddNotifierUserAdd(&addUserNotifier);
80 users->AddNotifierUserDel(&delUserNotifier);
82 pthread_mutex_init(&mutex, NULL);
84 //-----------------------------------------------------------------------------
85 TraffCounterImpl::~TraffCounterImpl()
87 pthread_mutex_destroy(&mutex);
89 //-----------------------------------------------------------------------------
90 int TraffCounterImpl::Start()
92 STG_LOCKER lock(&mutex);
99 printfd(__FILE__, "TraffCounterImpl::Start() - Cannot read rules\n");
100 WriteServLog("TraffCounter: Cannot read rules.");
104 printfd(__FILE__, "TraffCounter::Start()\n");
105 int h = users->OpenSearch();
106 assert(h && "USERS::OpenSearch is always correct");
109 while (users->SearchNext(h, &u) == 0)
111 users->CloseSearch(h);
114 if (pthread_create(&thread, NULL, Run, this))
116 printfd(__FILE__, "TraffCounterImpl::Start() - Cannot start thread\n");
117 WriteServLog("TraffCounter: Error: Cannot start thread.");
122 //-----------------------------------------------------------------------------
123 int TraffCounterImpl::Stop()
130 int h = users->OpenSearch();
131 assert(h && "USERS::OpenSearch is always correct");
134 while (users->SearchNext(h, &u) == 0)
135 UnSetUserNotifiers(u);
136 users->CloseSearch(h);
138 //5 seconds to thread stops itself
139 struct timespec ts = {0, 200000000};
140 for (int i = 0; i < 25 && !stopped; i++)
142 nanosleep(&ts, NULL);
148 printfd(__FILE__, "TraffCounter::Stop()\n");
152 //-----------------------------------------------------------------------------
153 void * TraffCounterImpl::Run(void * data)
156 sigfillset(&signalSet);
157 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
159 TraffCounterImpl * tc = static_cast<TraffCounterImpl *>(data);
163 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
164 struct timespec ts = {0, 500000000};
170 tc->FlushAndRemove();
174 if (tc->monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
176 std::string monFile(tc->monitorDir + "/traffcounter_r");
177 printfd(__FILE__, "Monitor=%d file TraffCounter %s\n", tc->monitoring, monFile.c_str());
182 if (++c % FLUSH_TIME == 0)
183 tc->FlushAndRemove();
189 //-----------------------------------------------------------------------------
190 void TraffCounterImpl::process(const RawPacket & rawPacket)
195 if (monitoring && (touchTimeP + MONITOR_TIME_DELAY_SEC <= stgTime))
197 std::string monFile = monitorDir + "/traffcounter_p";
198 printfd(__FILE__, "Monitor=%d file TraffCounter %s\n", monitoring, monFile.c_str());
199 touchTimeP = stgTime;
203 STG_LOCKER lock(&mutex);
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");*/
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 std::pair<pp_iter, bool> insertResult = packets.insert(std::make_pair(rawPacket, ed));
271 pp_iter newPacket = insertResult.first;
273 // Adding packet reference to an IP index.
274 ip2packets.insert(std::make_pair(ipU, newPacket));
275 ip2packets.insert(std::make_pair(ipD, newPacket));
278 //-----------------------------------------------------------------------------
279 void TraffCounterImpl::FlushAndRemove()
281 STG_LOCKER lock(&mutex);
283 Packets::size_type oldPacketsSize = packets.size();
284 Index::size_type oldIp2packetsSize = ip2packets.size();
287 pi = packets.begin();
289 ip2packets.erase(ip2packets.begin(), ip2packets.end());
290 while (pi != packets.end())
293 if (stgTime - pi->second.flushTime > FLUSH_TIME)
295 if (pi->second.userUPresent)
297 //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);
300 if (pi->second.dirU < DIR_NUM)
302 #ifdef TRAFF_STAT_WITH_PORTS
303 pi->second.userU->AddTraffStatU(pi->second.dirU,
304 pi->first.GetDstIP(),
305 pi->first.GetDstPort(),
308 pi->second.userU->AddTraffStatU(pi->second.dirU,
309 pi->first.GetDstIP(),
315 pi->second.flushTime = stgTime;
318 if (pi->second.userDPresent)
320 //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);
323 if (pi->second.dirD < DIR_NUM)
325 #ifdef TRAFF_STAT_WITH_PORTS
326 pi->second.userD->AddTraffStatD(pi->second.dirD,
327 pi->first.GetSrcIP(),
328 pi->first.GetSrcPort(),
331 pi->second.userD->AddTraffStatD(pi->second.dirD,
332 pi->first.GetSrcIP(),
338 pi->second.flushTime = stgTime;
342 if (stgTime - pi->second.updateTime < REMOVE_TIME)
344 std::pair<pp_iter, bool> res = newPackets.insert(*pi);
347 ip2packets.insert(std::make_pair(pi->first.GetSrcIP(), res.first));
348 ip2packets.insert(std::make_pair(pi->first.GetDstIP(), res.first));
353 swap(packets, newPackets);
354 printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n",
356 oldPacketsSize - packets.size(),
358 oldIp2packetsSize - ip2packets.size());
361 //-----------------------------------------------------------------------------
362 void TraffCounterImpl::AddUser(UserImpl * user)
364 printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str());
365 uint32_t uip = user->GetCurrIP();
366 std::pair<ip2p_iter, ip2p_iter> pi;
368 STG_LOCKER lock(&mutex);
369 // Find all packets with IP belongs to this user
370 pi = ip2packets.equal_range(uip);
372 while (pi.first != pi.second)
374 if (pi.first->second->first.GetSrcIP() == uip)
376 assert((!pi.first->second->second.userUPresent ||
377 pi.first->second->second.userU == user) &&
378 "U user present but it's not current user");
380 pi.first->second->second.lenU = 0;
381 pi.first->second->second.userU = user;
382 pi.first->second->second.userUPresent = true;
385 if (pi.first->second->first.GetDstIP() == uip)
387 assert((!pi.first->second->second.userDPresent ||
388 pi.first->second->second.userD == user) &&
389 "D user present but it's not current user");
391 pi.first->second->second.lenD = 0;
392 pi.first->second->second.userD = user;
393 pi.first->second->second.userDPresent = true;
399 //-----------------------------------------------------------------------------
400 void TraffCounterImpl::DelUser(uint32_t uip)
402 printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
403 std::pair<ip2p_iter, ip2p_iter> pi;
405 STG_LOCKER lock(&mutex);
406 pi = ip2packets.equal_range(uip);
408 while (pi.first != pi.second)
410 if (pi.first->second->first.GetSrcIP() == uip)
412 if (pi.first->second->second.dirU < DIR_NUM && pi.first->second->second.userUPresent)
414 #ifdef TRAFF_STAT_WITH_PORTS
415 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
416 pi.first->second->first.GetDstIP(),
417 pi.first->second->first.GetDstPort(),
418 pi.first->second->second.lenU);
420 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
421 pi.first->second->first.GetDstIP(),
422 pi.first->second->second.lenU);
425 pi.first->second->second.userUPresent = false;
428 if (pi.first->second->first.GetDstIP() == uip)
430 if (pi.first->second->second.dirD < DIR_NUM && pi.first->second->second.userDPresent)
432 #ifdef TRAFF_STAT_WITH_PORTS
433 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
434 pi.first->second->first.GetSrcIP(),
435 pi.first->second->first.GetSrcPort(),
436 pi.first->second->second.lenD);
438 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
439 pi.first->second->first.GetSrcIP(),
440 pi.first->second->second.lenD);
444 pi.first->second->second.userDPresent = false;
450 ip2packets.erase(pi.first, pi.second);
452 //-----------------------------------------------------------------------------
453 void TraffCounterImpl::SetUserNotifiers(UserImpl * user)
455 // Adding user. Adding notifiers to user.
456 TRF_IP_BEFORE ipBNotifier(*this, user);
457 ipBeforeNotifiers.push_front(ipBNotifier);
458 user->AddCurrIPBeforeNotifier(&(*ipBeforeNotifiers.begin()));
460 TRF_IP_AFTER ipANotifier(*this, user);
461 ipAfterNotifiers.push_front(ipANotifier);
462 user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
464 //-----------------------------------------------------------------------------
465 void TraffCounterImpl::UnSetUserNotifiers(UserImpl * user)
467 // Removing user. Removing notifiers from user.
468 std::list<TRF_IP_BEFORE>::iterator bi;
469 std::list<TRF_IP_AFTER>::iterator ai;
471 bi = ipBeforeNotifiers.begin();
472 while (bi != ipBeforeNotifiers.end())
474 if (user->GetLogin() == bi->GetUser()->GetLogin())
476 user->DelCurrIPBeforeNotifier(&(*bi));
477 ipBeforeNotifiers.erase(bi);
483 ai = ipAfterNotifiers.begin();
484 while (ai != ipAfterNotifiers.end())
486 if (user->GetLogin() == ai->GetUser()->GetLogin())
488 user->DelCurrIPAfterNotifier(&(*ai));
489 ipAfterNotifiers.erase(ai);
495 //-----------------------------------------------------------------------------
496 void TraffCounterImpl::DeterminateDir(const RawPacket & packet,
497 int * dirU, // Direction for incoming packet
498 int * dirD) const // Direction for outgoing packet
500 bool addrMatchU = false;
501 bool portMatchU = false;
502 bool addrMatchD = false;
503 bool portMatchD = false;
504 bool foundU = false; // Was rule for U found ?
505 bool foundD = false; // Was rule for D found ?
506 //printfd(__FILE__, "foundU=%d, foundD=%d\n", foundU, foundD);
508 enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
510 std::list<Rule>::const_iterator ln;
513 while (ln != rules.end())
526 portMatchU = (packet.GetProto() == ICMP_RPOTO);
530 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
531 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
535 if (packet.GetProto() == TCP_PROTO)
536 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
540 if (packet.GetProto() == UDP_PROTO)
541 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
545 printfd(__FILE__, "Error! Incorrect rule!\n");
549 addrMatchU = (packet.GetDstIP() & ln->mask) == ln->ip;
551 if (!foundU && addrMatchU && portMatchU)
555 //printfd(__FILE__, "Up rule ok! %d\n", ln->dir);
571 portMatchD = (packet.GetProto() == ICMP_RPOTO);
575 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
576 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
580 if (packet.GetProto() == TCP_PROTO)
581 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
585 if (packet.GetProto() == UDP_PROTO)
586 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
590 printfd(__FILE__, "Error! Incorrect rule!\n");
594 addrMatchD = (packet.GetSrcIP() & ln->mask) == ln->ip;
596 if (!foundD && addrMatchD && portMatchD)
600 //printfd(__FILE__, "Down rule ok! %d\n", ln->dir);
605 } //while (ln != rules.end())
613 //-----------------------------------------------------------------------------
614 bool TraffCounterImpl::ReadRules(bool test)
616 //printfd(__FILE__, "TraffCounter::ReadRules()\n");
621 char tp[100]; // protocol
622 char ta[100]; // address
623 char td[100]; // target direction
626 f = fopen(rulesFileName.c_str(), "rt");
630 printfd(__FILE__, "TraffCounterImpl::ReadRules() - File '%s' cannot be opened.\n", rulesFileName.c_str());
631 WriteServLog("File '%s' cannot be oppened.", rulesFileName.c_str());
635 while (fgets(str, 1023, f))
638 if (str[strspn(str," \t")] == '#' || str[strspn(str," \t")] == '\n')
643 r = sscanf(str,"%99s %99s %99s", tp, ta, td);
646 printfd(__FILE__, "TraffCounterImpl::ReadRules() - Error in file '%s' at line %d. There must be 3 parameters.\n", rulesFileName.c_str(), lineNumber);
647 WriteServLog("Error in file '%s' at line %d. There must be 3 parameters.", rulesFileName.c_str(), lineNumber);
655 for (uint8_t i = 0; i < PROTOMAX; i++)
657 if (strcasecmp(tp, protoName[i]) == 0)
661 for (uint32_t i = 0; i < DIR_NUM + 1; i++)
663 if (td == dirName[i])
667 if (rul.dir == 0xff || rul.proto == 0xff)
669 printfd(__FILE__, "TraffCounterImpl::ReadRules() - Error in file '%s' at line %d.\n", rulesFileName.c_str(), lineNumber);
670 WriteServLog("Error in file %s. Line %d.",
671 rulesFileName.c_str(), lineNumber);
676 if (ParseAddress(ta, &rul) != 0)
678 printfd(__FILE__, "TraffCounterImpl::ReadRules() - Error in file '%s' at line %d. Error in adress.\n", rulesFileName.c_str(), lineNumber);
679 WriteServLog("Error in file %s. Error in adress. Line %d.",
680 rulesFileName.c_str(), lineNumber);
685 rules.push_back(rul);
690 // Adding lastest rule: ALL 0.0.0.0/0 NULL
691 rul.dir = DIR_NUM; //NULL
692 rul.ip = 0; //0.0.0.0
699 rules.push_back(rul);
703 //-----------------------------------------------------------------------------
704 int TraffCounterImpl::Reload()
706 STG_LOCKER lock(&mutex);
710 printfd(__FILE__, "TraffCounterImpl::Reload() - Failed to reload rules.\n");
711 WriteServLog("TraffCounter: Cannot reload rules. Errors found.");
717 printfd(__FILE__, "TraffCounterImpl::Reload() - Reloaded rules successfully.\n");
718 WriteServLog("TraffCounter: Reloaded rules successfully.");
721 //-----------------------------------------------------------------------------
722 bool TraffCounterImpl::ParseAddress(const char * ta, Rule * rule) const
724 char addr[50], mask[20], port1[20], port2[20], ports[40];
726 size_t len = strlen(ta);
729 memset(addr, 0, sizeof(addr));
730 for (i = 0; i < len; i++)
732 if (ta[i] == '/' || ta[i] == ':')
770 if (!(rule->proto == tcp || rule->proto == udp || rule->proto == tcp_udp))
772 printfd(__FILE__, "TraffCounterImpl::ParseAddress() - No ports specified for this protocol.\n");
773 WriteServLog("No ports specified for this protocol.");
778 ports[i - p] = ta[i];
784 strcpy(ports, "0-65535");
791 if ((sss = strchr(ports, '-')) != NULL)
793 strncpy(port1, ports, int(sss-ports));
794 port1[int(sss - ports)] = 0;
795 strcpy(port2, sss + 1);
799 strcpy(port1, ports);
800 strcpy(port2, ports);
803 // Convert strings to mask, ports and IP
804 uint16_t prt1, prt2, msk;
805 struct in_addr ipaddr;
808 msk = static_cast<uint16_t>(strtol(mask, &res, 10));
812 prt1 = static_cast<uint16_t>(strtol(port1, &res, 10));
816 prt2 = static_cast<uint16_t>(strtol(port2, &res, 10));
820 int r = inet_aton(addr, &ipaddr);
824 rule->ip = ipaddr.s_addr;
825 rule->mask = CalcMask(msk);
827 if ((ipaddr.s_addr & rule->mask) != ipaddr.s_addr)
829 printfd(__FILE__, "TraffCounterImpl::ParseAddress() - Address does'n match mask.\n");
830 WriteServLog("Address does'n match mask.");
839 //-----------------------------------------------------------------------------
840 uint32_t TraffCounterImpl::CalcMask(uint32_t msk) const
842 if (msk >= 32) return 0xFFffFFff;
843 if (msk == 0) return 0;
844 return htonl(0xFFffFFff << (32 - msk));
846 //---------------------------------------------------------------------------
847 void TraffCounterImpl::FreeRules()
851 //-----------------------------------------------------------------------------
852 void TraffCounterImpl::SetMonitorDir(const std::string & dir)
855 monitoring = !monitorDir.empty();
857 //-----------------------------------------------------------------------------