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"
48 #include "async_pool.h"
50 #define FLUSH_TIME (10)
51 #define REMOVE_TIME (31)
53 using STG::TraffCounterImpl;
54 using STG::TRF_IP_BEFORE;
55 using STG::TRF_IP_AFTER;
57 namespace AsyncPoolST = STG::AsyncPoolST;
59 const char protoName[PROTOMAX][8] =
60 {"TCP", "UDP", "ICMP", "TCP_UDP", "ALL"};
64 tcp = 0, udp, icmp, tcp_udp, all
67 //-----------------------------------------------------------------------------
68 TraffCounterImpl::TraffCounterImpl(UsersImpl * u, const std::string & fn)
69 : WriteServLog(Logger::get()),
72 touchTimeP(stgTime - MONITOR_TIME_DELAY_SEC),
76 for (int i = 0; i < DIR_NUM; i++)
77 strprintf(&dirName[i], "DIR%d", i);
79 dirName[DIR_NUM] = "NULL";
81 m_onAddUserConn = users->onUserImplAdd([this](auto user){
82 AsyncPoolST::enqueue([this, user](){ SetUserNotifiers(user); });
84 m_onDelUserConn = users->onUserImplDel([this](auto user){
85 AsyncPoolST::enqueue([this, user](){ UnSetUserNotifiers(user); });
86 AsyncPoolST::enqueue([this, user](){ DelUser(user->GetCurrIP()); });
89 //-----------------------------------------------------------------------------
90 TraffCounterImpl::~TraffCounterImpl()
93 //-----------------------------------------------------------------------------
94 int TraffCounterImpl::Start()
96 std::lock_guard<std::mutex> lock(m_mutex);
103 printfd(__FILE__, "TraffCounterImpl::Start() - Cannot read rules\n");
104 WriteServLog("TraffCounter: Cannot read rules.");
108 printfd(__FILE__, "TraffCounter::Start()\n");
109 int h = users->OpenSearch();
110 assert(h && "USERS::OpenSearch is always correct");
113 while (users->SearchNext(h, &u) == 0)
115 users->CloseSearch(h);
117 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
120 //-----------------------------------------------------------------------------
121 int TraffCounterImpl::Stop()
126 m_thread.request_stop();
128 int h = users->OpenSearch();
129 assert(h && "USERS::OpenSearch is always correct");
132 while (users->SearchNext(h, &u) == 0)
133 UnSetUserNotifiers(u);
134 users->CloseSearch(h);
136 //5 seconds to thread stops itself
137 struct timespec ts = {0, 200000000};
138 for (int i = 0; i < 25 && !stopped; i++)
139 nanosleep(&ts, NULL);
149 printfd(__FILE__, "TraffCounter::Stop()\n");
153 //-----------------------------------------------------------------------------
154 void TraffCounterImpl::Run(std::stop_token token)
157 sigfillset(&signalSet);
158 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
163 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
164 struct timespec ts = {0, 500000000};
165 while (!token.stop_requested())
168 if (token.stop_requested())
174 if (monitoring && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
176 std::string monFile(monitorDir + "/traffcounter_r");
177 printfd(__FILE__, "Monitor=%d file TraffCounter %s\n", monitoring, monFile.c_str());
182 if (++c % FLUSH_TIME == 0)
188 //-----------------------------------------------------------------------------
189 void TraffCounterImpl::process(const RawPacket & rawPacket)
191 if (monitoring && (touchTimeP + MONITOR_TIME_DELAY_SEC <= stgTime))
193 std::string monFile = monitorDir + "/traffcounter_p";
194 printfd(__FILE__, "Monitor=%d file TraffCounter %s\n", monitoring, monFile.c_str());
195 touchTimeP = stgTime;
199 std::lock_guard<std::mutex> lock(m_mutex);
201 //printfd(__FILE__, "TraffCounter::Process()\n");
202 //TODO replace find with lower_bound.
204 // Searching a new packet in a tree.
205 pp_iter pi = packets.find(rawPacket);
207 // Packet found - update length and time
208 if (pi != packets.end())
210 pi->second.lenU += rawPacket.GetLen();
211 pi->second.lenD += rawPacket.GetLen();
212 pi->second.updateTime = stgTime;
213 /*printfd(__FILE__, "=============================\n");
214 printfd(__FILE__, "Packet found!\n");
215 printfd(__FILE__, "Version=%d\n", rawPacket.GetIPVersion());
216 printfd(__FILE__, "HeaderLen=%d\n", rawPacket.GetHeaderLen());
217 printfd(__FILE__, "PacketLen=%d\n", rawPacket.GetLen());
218 printfd(__FILE__, "SIP=%s\n", inet_ntostring(rawPacket.GetSrcIP()).c_str());
219 printfd(__FILE__, "DIP=%s\n", inet_ntostring(rawPacket.GetDstIP()).c_str());
220 printfd(__FILE__, "src port=%d\n", rawPacket.GetSrcPort());
221 printfd(__FILE__, "pst port=%d\n", rawPacket.GetDstPort());
222 printfd(__FILE__, "len=%d\n", rawPacket.GetLen());
223 printfd(__FILE__, "proto=%d\n", rawPacket.GetProto());
224 printfd(__FILE__, "PacketDirU=%d\n", pi->second.dirU);
225 printfd(__FILE__, "PacketDirD=%d\n", pi->second.dirD);
226 printfd(__FILE__, "=============================\n");*/
232 // Packet not found - add new packet
234 ed.updateTime = stgTime;
235 ed.flushTime = stgTime;
238 userU is that whose user_ip == packet_ip_src
239 userD is that whose user_ip == packet_ip_dst
242 uint32_t ipU = rawPacket.GetSrcIP();
243 uint32_t ipD = rawPacket.GetDstIP();
245 // Searching users with such IP
246 if (users->FindByIPIdx(ipU, &ed.userU) == 0)
248 ed.userUPresent = true;
251 if (users->FindByIPIdx(ipD, &ed.userD) == 0)
253 ed.userDPresent = true;
256 if (ed.userUPresent ||
259 DeterminateDir(rawPacket, &ed.dirU, &ed.dirD);
261 ed.lenD = ed.lenU = rawPacket.GetLen();
263 //TODO use result of lower_bound to inserting new record
265 // Adding packet to a tree.
266 std::pair<pp_iter, bool> insertResult = packets.insert(std::make_pair(rawPacket, ed));
267 pp_iter newPacket = insertResult.first;
269 // Adding packet reference to an IP index.
270 ip2packets.insert(std::make_pair(ipU, newPacket));
271 ip2packets.insert(std::make_pair(ipD, newPacket));
274 //-----------------------------------------------------------------------------
275 void TraffCounterImpl::FlushAndRemove()
277 std::lock_guard<std::mutex> lock(m_mutex);
279 Packets::size_type oldPacketsSize = packets.size();
280 Index::size_type oldIp2packetsSize = ip2packets.size();
283 pi = packets.begin();
285 ip2packets.erase(ip2packets.begin(), ip2packets.end());
286 while (pi != packets.end())
289 if (stgTime - pi->second.flushTime > FLUSH_TIME)
291 if (pi->second.userUPresent)
293 //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);
296 if (pi->second.dirU < DIR_NUM)
298 #ifdef TRAFF_STAT_WITH_PORTS
299 pi->second.userU->AddTraffStatU(pi->second.dirU,
300 pi->first.GetDstIP(),
301 pi->first.GetDstPort(),
304 pi->second.userU->AddTraffStatU(pi->second.dirU,
305 pi->first.GetDstIP(),
311 pi->second.flushTime = stgTime;
314 if (pi->second.userDPresent)
316 //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);
319 if (pi->second.dirD < DIR_NUM)
321 #ifdef TRAFF_STAT_WITH_PORTS
322 pi->second.userD->AddTraffStatD(pi->second.dirD,
323 pi->first.GetSrcIP(),
324 pi->first.GetSrcPort(),
327 pi->second.userD->AddTraffStatD(pi->second.dirD,
328 pi->first.GetSrcIP(),
334 pi->second.flushTime = stgTime;
338 if (stgTime - pi->second.updateTime < REMOVE_TIME)
340 std::pair<pp_iter, bool> res = newPackets.insert(*pi);
343 ip2packets.insert(std::make_pair(pi->first.GetSrcIP(), res.first));
344 ip2packets.insert(std::make_pair(pi->first.GetDstIP(), res.first));
349 swap(packets, newPackets);
350 printfd(__FILE__, "FlushAndRemove() packets: %d(rem %d) ip2packets: %d(rem %d)\n",
352 oldPacketsSize - packets.size(),
354 oldIp2packetsSize - ip2packets.size());
357 //-----------------------------------------------------------------------------
358 void TraffCounterImpl::AddUser(UserImpl * user)
360 printfd(__FILE__, "AddUser: %s\n", user->GetLogin().c_str());
361 uint32_t uip = user->GetCurrIP();
362 std::pair<ip2p_iter, ip2p_iter> pi;
364 std::lock_guard<std::mutex> lock(m_mutex);
365 // Find all packets with IP belongs to this user
366 pi = ip2packets.equal_range(uip);
368 while (pi.first != pi.second)
370 if (pi.first->second->first.GetSrcIP() == uip)
372 assert((!pi.first->second->second.userUPresent ||
373 pi.first->second->second.userU == user) &&
374 "U user present but it's not current user");
376 pi.first->second->second.lenU = 0;
377 pi.first->second->second.userU = user;
378 pi.first->second->second.userUPresent = true;
381 if (pi.first->second->first.GetDstIP() == uip)
383 assert((!pi.first->second->second.userDPresent ||
384 pi.first->second->second.userD == user) &&
385 "D user present but it's not current user");
387 pi.first->second->second.lenD = 0;
388 pi.first->second->second.userD = user;
389 pi.first->second->second.userDPresent = true;
395 //-----------------------------------------------------------------------------
396 void TraffCounterImpl::DelUser(uint32_t uip)
398 printfd(__FILE__, "DelUser: %s \n", inet_ntostring(uip).c_str());
399 std::pair<ip2p_iter, ip2p_iter> pi;
401 std::lock_guard<std::mutex> lock(m_mutex);
402 pi = ip2packets.equal_range(uip);
404 while (pi.first != pi.second)
406 if (pi.first->second->first.GetSrcIP() == uip)
408 if (pi.first->second->second.dirU < DIR_NUM && pi.first->second->second.userUPresent)
410 #ifdef TRAFF_STAT_WITH_PORTS
411 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
412 pi.first->second->first.GetDstIP(),
413 pi.first->second->first.GetDstPort(),
414 pi.first->second->second.lenU);
416 pi.first->second->second.userU->AddTraffStatU(pi.first->second->second.dirU,
417 pi.first->second->first.GetDstIP(),
418 pi.first->second->second.lenU);
421 pi.first->second->second.userUPresent = false;
424 if (pi.first->second->first.GetDstIP() == uip)
426 if (pi.first->second->second.dirD < DIR_NUM && pi.first->second->second.userDPresent)
428 #ifdef TRAFF_STAT_WITH_PORTS
429 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
430 pi.first->second->first.GetSrcIP(),
431 pi.first->second->first.GetSrcPort(),
432 pi.first->second->second.lenD);
434 pi.first->second->second.userD->AddTraffStatD(pi.first->second->second.dirD,
435 pi.first->second->first.GetSrcIP(),
436 pi.first->second->second.lenD);
440 pi.first->second->second.userDPresent = false;
446 ip2packets.erase(pi.first, pi.second);
448 //-----------------------------------------------------------------------------
449 void TraffCounterImpl::SetUserNotifiers(UserImpl * user)
451 // Adding user. Adding notifiers to user.
452 TRF_IP_BEFORE ipBNotifier(*this, user);
453 ipBeforeNotifiers.push_front(ipBNotifier);
454 user->AddCurrIPBeforeNotifier(&(*ipBeforeNotifiers.begin()));
456 TRF_IP_AFTER ipANotifier(*this, user);
457 ipAfterNotifiers.push_front(ipANotifier);
458 user->AddCurrIPAfterNotifier(&(*ipAfterNotifiers.begin()));
460 //-----------------------------------------------------------------------------
461 void TraffCounterImpl::UnSetUserNotifiers(UserImpl * user)
463 // Removing user. Removing notifiers from user.
464 std::list<TRF_IP_BEFORE>::iterator bi;
465 std::list<TRF_IP_AFTER>::iterator ai;
467 bi = ipBeforeNotifiers.begin();
468 while (bi != ipBeforeNotifiers.end())
470 if (user->GetLogin() == bi->GetUser()->GetLogin())
472 user->DelCurrIPBeforeNotifier(&(*bi));
473 ipBeforeNotifiers.erase(bi);
479 ai = ipAfterNotifiers.begin();
480 while (ai != ipAfterNotifiers.end())
482 if (user->GetLogin() == ai->GetUser()->GetLogin())
484 user->DelCurrIPAfterNotifier(&(*ai));
485 ipAfterNotifiers.erase(ai);
491 //-----------------------------------------------------------------------------
492 void TraffCounterImpl::DeterminateDir(const RawPacket & packet,
493 int * dirU, // Direction for incoming packet
494 int * dirD) const // Direction for outgoing packet
496 bool addrMatchU = false;
497 bool portMatchU = false;
498 bool addrMatchD = false;
499 bool portMatchD = false;
500 bool foundU = false; // Was rule for U found ?
501 bool foundD = false; // Was rule for D found ?
502 //printfd(__FILE__, "foundU=%d, foundD=%d\n", foundU, foundD);
504 enum { ICMP_RPOTO = 1, TCP_PROTO = 6, UDP_PROTO = 17 };
506 std::list<Rule>::const_iterator ln;
509 while (ln != rules.end())
522 portMatchU = (packet.GetProto() == ICMP_RPOTO);
526 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
527 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
531 if (packet.GetProto() == TCP_PROTO)
532 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
536 if (packet.GetProto() == UDP_PROTO)
537 portMatchU = (packet.GetDstPort() >= ln->port1) && (packet.GetDstPort() <= ln->port2);
541 printfd(__FILE__, "Error! Incorrect rule!\n");
545 addrMatchU = (packet.GetDstIP() & ln->mask) == ln->ip;
547 if (!foundU && addrMatchU && portMatchU)
551 //printfd(__FILE__, "Up rule ok! %d\n", ln->dir);
567 portMatchD = (packet.GetProto() == ICMP_RPOTO);
571 if (packet.GetProto() == TCP_PROTO || packet.GetProto() == UDP_PROTO)
572 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
576 if (packet.GetProto() == TCP_PROTO)
577 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
581 if (packet.GetProto() == UDP_PROTO)
582 portMatchD = (packet.GetSrcPort() >= ln->port1) && (packet.GetSrcPort() <= ln->port2);
586 printfd(__FILE__, "Error! Incorrect rule!\n");
590 addrMatchD = (packet.GetSrcIP() & ln->mask) == ln->ip;
592 if (!foundD && addrMatchD && portMatchD)
596 //printfd(__FILE__, "Down rule ok! %d\n", ln->dir);
601 } //while (ln != rules.end())
609 //-----------------------------------------------------------------------------
610 bool TraffCounterImpl::ReadRules(bool test)
612 //printfd(__FILE__, "TraffCounter::ReadRules()\n");
617 char tp[100]; // protocol
618 char ta[100]; // address
619 char td[100]; // target direction
622 f = fopen(rulesFileName.c_str(), "rt");
626 printfd(__FILE__, "TraffCounterImpl::ReadRules() - File '%s' cannot be opened.\n", rulesFileName.c_str());
627 WriteServLog("File '%s' cannot be oppened.", rulesFileName.c_str());
631 while (fgets(str, 1023, f))
634 if (str[strspn(str," \t")] == '#' || str[strspn(str," \t")] == '\n')
639 r = sscanf(str,"%99s %99s %99s", tp, ta, td);
642 printfd(__FILE__, "TraffCounterImpl::ReadRules() - Error in file '%s' at line %d. There must be 3 parameters.\n", rulesFileName.c_str(), lineNumber);
643 WriteServLog("Error in file '%s' at line %d. There must be 3 parameters.", rulesFileName.c_str(), lineNumber);
651 for (uint8_t i = 0; i < PROTOMAX; i++)
653 if (strcasecmp(tp, protoName[i]) == 0)
657 for (uint32_t i = 0; i < DIR_NUM + 1; i++)
659 if (td == dirName[i])
663 if (rul.dir == 0xff || rul.proto == 0xff)
665 printfd(__FILE__, "TraffCounterImpl::ReadRules() - Error in file '%s' at line %d.\n", rulesFileName.c_str(), lineNumber);
666 WriteServLog("Error in file %s. Line %d.",
667 rulesFileName.c_str(), lineNumber);
672 if (ParseAddress(ta, &rul) != 0)
674 printfd(__FILE__, "TraffCounterImpl::ReadRules() - Error in file '%s' at line %d. Error in adress.\n", rulesFileName.c_str(), lineNumber);
675 WriteServLog("Error in file %s. Error in adress. Line %d.",
676 rulesFileName.c_str(), lineNumber);
681 rules.push_back(rul);
686 // Adding lastest rule: ALL 0.0.0.0/0 NULL
687 rul.dir = DIR_NUM; //NULL
688 rul.ip = 0; //0.0.0.0
695 rules.push_back(rul);
699 //-----------------------------------------------------------------------------
700 int TraffCounterImpl::Reload()
702 std::lock_guard<std::mutex> lock(m_mutex);
706 printfd(__FILE__, "TraffCounterImpl::Reload() - Failed to reload rules.\n");
707 WriteServLog("TraffCounter: Cannot reload rules. Errors found.");
713 printfd(__FILE__, "TraffCounterImpl::Reload() - Reloaded rules successfully.\n");
714 WriteServLog("TraffCounter: Reloaded rules successfully.");
717 //-----------------------------------------------------------------------------
718 bool TraffCounterImpl::ParseAddress(const char * ta, Rule * rule) const
720 char addr[50], mask[20], port1[20], port2[20], ports[40];
722 size_t len = strlen(ta);
725 memset(addr, 0, sizeof(addr));
726 for (i = 0; i < len; i++)
728 if (ta[i] == '/' || ta[i] == ':')
766 if (!(rule->proto == tcp || rule->proto == udp || rule->proto == tcp_udp))
768 printfd(__FILE__, "TraffCounterImpl::ParseAddress() - No ports specified for this protocol.\n");
769 WriteServLog("No ports specified for this protocol.");
774 ports[i - p] = ta[i];
780 strcpy(ports, "0-65535");
787 if ((sss = strchr(ports, '-')) != NULL)
789 strncpy(port1, ports, int(sss-ports));
790 port1[int(sss - ports)] = 0;
791 strcpy(port2, sss + 1);
795 strcpy(port1, ports);
796 strcpy(port2, ports);
799 // Convert strings to mask, ports and IP
800 uint16_t prt1, prt2, msk;
801 struct in_addr ipaddr;
804 msk = static_cast<uint16_t>(strtol(mask, &res, 10));
808 prt1 = static_cast<uint16_t>(strtol(port1, &res, 10));
812 prt2 = static_cast<uint16_t>(strtol(port2, &res, 10));
816 int r = inet_aton(addr, &ipaddr);
820 rule->ip = ipaddr.s_addr;
821 rule->mask = CalcMask(msk);
823 if ((ipaddr.s_addr & rule->mask) != ipaddr.s_addr)
825 printfd(__FILE__, "TraffCounterImpl::ParseAddress() - Address does'n match mask.\n");
826 WriteServLog("Address does'n match mask.");
835 //-----------------------------------------------------------------------------
836 uint32_t TraffCounterImpl::CalcMask(uint32_t msk) const
838 if (msk >= 32) return 0xFFffFFff;
839 if (msk == 0) return 0;
840 return htonl(0xFFffFFff << (32 - msk));
842 //---------------------------------------------------------------------------
843 void TraffCounterImpl::FreeRules()
847 //-----------------------------------------------------------------------------
848 void TraffCounterImpl::SetMonitorDir(const std::string & dir)
851 monitoring = !monitorDir.empty();
853 //-----------------------------------------------------------------------------
854 void TRF_IP_BEFORE::notify(const uint32_t & oldValue, const uint32_t &)
856 // User changes his address. Remove old IP
860 AsyncPoolST::enqueue([this, oldValue](){ traffCnt.DelUser(oldValue); });
862 //-----------------------------------------------------------------------------
863 void TRF_IP_AFTER::notify(const uint32_t &, const uint32_t & newValue)
865 // User changes his address. Add new IP
869 AsyncPoolST::enqueue([this](){ traffCnt.AddUser(user); });
871 //-----------------------------------------------------------------------------