From 597f6f31e3801612273886481381df509d8bcd12 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Fri, 16 Sep 2011 20:44:06 +0300 Subject: [PATCH] Block all signals in threads other than main --- projects/rscriptd/listener.cpp | 8 + projects/sgauth/web.cpp | 15 +- projects/sgauthstress/proto.cpp | 5 + projects/stargazer/eventloop.cpp | 5 + .../authorization/inetaccess/inetaccess.cpp | 8 + .../plugins/capture/cap_nf/cap_nf.cpp | 8 + .../capture/divert_freebsd/divert_cap.cpp | 6 +- .../capture/ether_freebsd/ether_cap.cpp | 19 +- .../plugins/capture/ether_linux/ether_cap.cpp | 17 +- .../plugins/capture/ipq_linux/ipq_cap.cpp | 12 +- .../configuration/rpcconfig/rpcconfig.cpp | 5 + .../configuration/sgconfig/stgconfig.cpp | 4 + .../stargazer/plugins/other/ping/ping.cpp | 6 +- .../stargazer/plugins/other/radius/radius.cpp | 4 + .../plugins/other/rscript/rscript.cpp | 4 + .../stargazer/plugins/other/smux/smux.cpp | 4 + projects/stargazer/stg_timer.cpp | 9 +- projects/stargazer/traffcounter_impl.cpp | 4 + projects/stargazer/user_impl.cpp | 2 +- projects/stargazer/users_impl.cpp | 5 + stglibs/ia.lib/ia.cpp | 4 + stglibs/pinger.lib/pinger.cpp | 426 +++++++++--------- 22 files changed, 345 insertions(+), 235 deletions(-) diff --git a/projects/rscriptd/listener.cpp b/projects/rscriptd/listener.cpp index d4f2dc93..73042674 100644 --- a/projects/rscriptd/listener.cpp +++ b/projects/rscriptd/listener.cpp @@ -165,6 +165,10 @@ return false; //----------------------------------------------------------------------------- void * LISTENER::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + LISTENER * listener = static_cast(d); listener->Runner(); @@ -186,6 +190,10 @@ receiverStopped = true; //----------------------------------------------------------------------------- void * LISTENER::RunProcessor(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + LISTENER * listener = static_cast(d); listener->ProcessorRunner(); diff --git a/projects/sgauth/web.cpp b/projects/sgauth/web.cpp index ab169cc4..61cd2b58 100644 --- a/projects/sgauth/web.cpp +++ b/projects/sgauth/web.cpp @@ -23,11 +23,13 @@ $Date: 2010/03/15 12:58:17 $ */ -#include -#include -#include #include +#include +#include +#include +#include + #include "stg/common.h" #include "stg/ia.h" #include "web.h" @@ -42,10 +44,15 @@ extern IA_CLIENT_PROT * clnp; //--------------------------------------------------------------------------- #ifndef WIN32 void * RunWeb(void *) +{ +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + #else unsigned long WINAPI RunWeb(void *) -#endif { +#endif while (1) web->Run(); return NULL; diff --git a/projects/sgauthstress/proto.cpp b/projects/sgauthstress/proto.cpp index 2e97fd2a..dc2bf08c 100644 --- a/projects/sgauthstress/proto.cpp +++ b/projects/sgauthstress/proto.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -77,6 +78,10 @@ pthread_mutex_destroy(&mutex); void * PROTO::Runner(void * data) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + PROTO * protoPtr = static_cast(data); protoPtr->Run(); return NULL; diff --git a/projects/stargazer/eventloop.cpp b/projects/stargazer/eventloop.cpp index dd17de9b..6cc59c94 100644 --- a/projects/stargazer/eventloop.cpp +++ b/projects/stargazer/eventloop.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -53,6 +54,10 @@ return NULL; void EVENT_LOOP::Runner() { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + _stopped = false; printfd(__FILE__, "EVENT_LOOP::Runner - Before start\n"); while (_running) diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index 4986080e..1eac0ab1 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -500,6 +500,10 @@ return 0; //----------------------------------------------------------------------------- void * AUTH_IA::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + AUTH_IA * ia = static_cast(d); ia->isRunningRun = true; @@ -525,6 +529,10 @@ return NULL; //----------------------------------------------------------------------------- void * AUTH_IA::RunTimeouter(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + AUTH_IA * ia = static_cast(d); ia->isRunningRunTimeouter = true; diff --git a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp index fb7d2cc5..51ba4954 100644 --- a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp +++ b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp @@ -244,6 +244,10 @@ return false; void * NF_CAP::RunUDP(void * c) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + NF_CAP * cap = static_cast(c); uint8_t buf[BUF_SIZE]; int res; @@ -286,6 +290,10 @@ return NULL; void * NF_CAP::RunTCP(void * c) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + NF_CAP * cap = static_cast(c); uint8_t buf[BUF_SIZE]; int res; diff --git a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp index 5927727e..9a9d1543 100644 --- a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp +++ b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp @@ -150,7 +150,11 @@ return 0; //----------------------------------------------------------------------------- void * DIVERT_CAP::Run(void * d) { -DIVERT_CAP * dc = (DIVERT_CAP *)d; +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + +DIVERT_CAP * dc = static_cast(d); dc->isRunning = true; char buffer[64]; diff --git a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp index 108c4e0d..5835d077 100644 --- a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp +++ b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp @@ -39,19 +39,20 @@ $Author: faust $ #include #include #include - -#include #include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include + #include "stg/common.h" #include "stg/raw_ip_packet.h" #include "stg/traffcounter.h" #include "stg/plugin_creator.h" + #include "ether_cap.h" //#define CAP_DEBUG 1 @@ -201,7 +202,11 @@ return 0; //----------------------------------------------------------------------------- void * BPF_CAP::Run(void * d) { -BPF_CAP * dc = (BPF_CAP *)d; +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + +BPF_CAP * dc = static_cast(d); dc->isRunning = true; uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96 diff --git a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp index 276c8df0..632c1846 100644 --- a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp +++ b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp @@ -32,22 +32,23 @@ $Date: 2009/12/13 13:45:13 $ #include #include #include -#include -#include -#include -#include #include #include #include -#include - #include #include +#include +#include +#include +#include +#include + #include "stg/common.h" #include "stg/raw_ip_packet.h" #include "stg/traffcounter.h" #include "stg/plugin_creator.h" + #include "ether_cap.h" //#define CAP_DEBUG 1 @@ -149,6 +150,10 @@ return 0; //----------------------------------------------------------------------------- void * ETHER_CAP::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + ETHER_CAP * dc = (ETHER_CAP *)d; dc->isRunning = true; diff --git a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp index 84939b4e..d0192e0a 100644 --- a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp +++ b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp @@ -18,14 +18,16 @@ * Author : Boris Mikhailenko */ -#include -#include #include #include +#include +#include + #include "stg/raw_ip_packet.h" #include "stg/traffcounter.h" #include "stg/plugin_creator.h" + #include "ipq_cap.h" extern "C" @@ -126,9 +128,13 @@ return 0; //----------------------------------------------------------------------------- void * IPQ_CAP::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + RAW_PACKET raw_packet; -IPQ_CAP * dc = (IPQ_CAP *)d; +IPQ_CAP * dc = static_cast(d); dc->isRunning = true; memset(&raw_packet, 0, sizeof(raw_packet)); raw_packet.dataLen = -1; diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp index 837c8649..d3fdba55 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp @@ -157,6 +157,7 @@ if (!stopped) } if (!stopped) { + running = true; printfd(__FILE__, "Failed to stop RPC thread\n"); errorStr = "Failed to stop RPC thread"; return -1; @@ -171,6 +172,10 @@ return 0; void * RPC_CONFIG::Run(void * rc) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + RPC_CONFIG * config = static_cast(rc); config->stopped = false; diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp index 0a65c0d9..3cd6b901 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp @@ -145,6 +145,10 @@ return 0; //----------------------------------------------------------------------------- void * STG_CONFIG::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + STG_CONFIG * stgConf = (STG_CONFIG *)d; stgConf->isRunning = true; diff --git a/projects/stargazer/plugins/other/ping/ping.cpp b/projects/stargazer/plugins/other/ping/ping.cpp index c8240679..f4755c23 100644 --- a/projects/stargazer/plugins/other/ping/ping.cpp +++ b/projects/stargazer/plugins/other/ping/ping.cpp @@ -167,7 +167,11 @@ return isRunning; //----------------------------------------------------------------------------- void * PING::Run(void * d) { -PING * ping = (PING *)d; +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + +PING * ping = static_cast(d); ping->isRunning = true; long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000; diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 6a02ce4f..c827330f 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -258,6 +258,10 @@ return 0; //----------------------------------------------------------------------------- void * RADIUS::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + RADIUS * rad = (RADIUS *)d; RAD_PACKET packet; diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index c1d66051..17598006 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -198,6 +198,10 @@ pthread_mutex_destroy(&mutex); //----------------------------------------------------------------------------- void * REMOTE_SCRIPT::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + REMOTE_SCRIPT * rs = static_cast(d); rs->isRunning = true; diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 61ed8026..5367e249 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -274,6 +274,10 @@ return 0; void * SMUX::Runner(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + SMUX * smux = static_cast(d); smux->Run(); diff --git a/projects/stargazer/stg_timer.cpp b/projects/stargazer/stg_timer.cpp index fff79a39..235c669e 100644 --- a/projects/stargazer/stg_timer.cpp +++ b/projects/stargazer/stg_timer.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "stg/common.h" @@ -58,6 +59,10 @@ switch (START_TIME) stgTime = time(NULL); #endif +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + nonstop = 1; isTimerRunning = true; while (nonstop) @@ -65,12 +70,10 @@ while (nonstop) #ifdef STG_TIMER_DEBUG struct timespec ts = {0, 1000000000 / TIME_SPEED}; nanosleep(&ts, NULL); - //usleep(1000000 / TIME_SPEED); stgTime++; #else struct timespec ts = {0, 500000000}; nanosleep(&ts, NULL); - //usleep(500000); stgTime = time(NULL); #endif } @@ -112,11 +115,9 @@ int stgUsleep(unsigned long t) #ifdef STG_TIMER_DEBUG struct timespec ts = {(t / TIME_SPEED) / 1000000, ((t / TIME_SPEED) % 1000000) * 1000}; return nanosleep(&ts, NULL); -//return usleep(t / TIME_SPEED); #else struct timespec ts = {t / 1000000, (t % 1000000) * 1000}; return nanosleep(&ts, NULL); -//return usleep(t); #endif } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/traffcounter_impl.cpp b/projects/stargazer/traffcounter_impl.cpp index ae7f1dfb..a02e3b6d 100644 --- a/projects/stargazer/traffcounter_impl.cpp +++ b/projects/stargazer/traffcounter_impl.cpp @@ -169,6 +169,10 @@ return 0; //----------------------------------------------------------------------------- void * TRAFFCOUNTER_IMPL::Run(void * data) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + TRAFFCOUNTER_IMPL * tc = static_cast(data); tc->stopped = false; int c = 0; diff --git a/projects/stargazer/user_impl.cpp b/projects/stargazer/user_impl.cpp index 6aca9cea..1278afa2 100644 --- a/projects/stargazer/user_impl.cpp +++ b/projects/stargazer/user_impl.cpp @@ -484,7 +484,7 @@ for (int i = 0; i < DIR_NUM; i++) enabledDirs[i] = dirs & (1 << i); } -if (authorizedBy.size()) +if (!authorizedBy.empty()) { if (currIP != ip) { diff --git a/projects/stargazer/users_impl.cpp b/projects/stargazer/users_impl.cpp index 19be5a41..61ee90ff 100644 --- a/projects/stargazer/users_impl.cpp +++ b/projects/stargazer/users_impl.cpp @@ -33,6 +33,7 @@ #endif #include + #include #include #include @@ -353,6 +354,10 @@ return 0; //----------------------------------------------------------------------------- void * USERS_IMPL::Run(void * d) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid()); USERS_IMPL * us = (USERS_IMPL*) d; diff --git a/stglibs/ia.lib/ia.cpp b/stglibs/ia.lib/ia.cpp index abecef09..81bd75fb 100644 --- a/stglibs/ia.lib/ia.cpp +++ b/stglibs/ia.lib/ia.cpp @@ -63,6 +63,10 @@ #include void * RunL(void * data) { +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + IA_CLIENT_PROT * c = (IA_CLIENT_PROT *)data; static int a = 0; diff --git a/stglibs/pinger.lib/pinger.cpp b/stglibs/pinger.lib/pinger.cpp index df3793ab..8e6370ed 100644 --- a/stglibs/pinger.lib/pinger.cpp +++ b/stglibs/pinger.lib/pinger.cpp @@ -1,23 +1,25 @@ -#include #include -#include #include #include #include #include #include #include -#include #include #include -#include -#include -#include -#include "pinger.h" +#include +#include +#include +#include +#include +#include + #include "stg/common.h" #include "stg/locker.h" +#include "pinger.h" + #ifdef STG_TIME extern volatile time_t stgTime; #endif @@ -40,320 +42,328 @@ STG_PINGER::STG_PINGER(time_t d) ipToDel(), mutex() { - pthread_mutex_init(&mutex, NULL); - memset(&pmSend, 0, sizeof(pmSend)); +pthread_mutex_init(&mutex, NULL); +memset(&pmSend, 0, sizeof(pmSend)); } //----------------------------------------------------------------------------- STG_PINGER::~STG_PINGER() { - pthread_mutex_destroy(&mutex); +pthread_mutex_destroy(&mutex); } //----------------------------------------------------------------------------- int STG_PINGER::Start() { - struct protoent *proto = NULL; - proto = getprotobyname("ICMP"); - sendSocket = socket(PF_INET, SOCK_RAW, proto->p_proto); - recvSocket = socket(PF_INET, SOCK_RAW, proto->p_proto); - nonstop = true; - pid = (int) getpid() % 65535; - if (sendSocket < 0 || recvSocket < 0) - { - errorStr = "Cannot create socket."; - return -1; - } +struct protoent *proto = NULL; +proto = getprotobyname("ICMP"); +sendSocket = socket(PF_INET, SOCK_RAW, proto->p_proto); +recvSocket = socket(PF_INET, SOCK_RAW, proto->p_proto); +nonstop = true; +pid = (int) getpid() % 65535; +if (sendSocket < 0 || recvSocket < 0) + { + errorStr = "Cannot create socket."; + return -1; + } - if (pthread_create(&sendThread, NULL, RunSendPing, this)) - { - errorStr = "Cannot create send thread."; - return -1; - } +if (pthread_create(&sendThread, NULL, RunSendPing, this)) + { + errorStr = "Cannot create send thread."; + return -1; + } - if (pthread_create(&recvThread, NULL, RunRecvPing, this)) - { - errorStr = "Cannot create recv thread."; - return -1; - } +if (pthread_create(&recvThread, NULL, RunRecvPing, this)) + { + errorStr = "Cannot create recv thread."; + return -1; + } - return 0; +return 0; } //----------------------------------------------------------------------------- int STG_PINGER::Stop() { - close(recvSocket); - nonstop = false; - if (isRunningRecver) +close(recvSocket); +nonstop = false; +if (isRunningRecver) + { + //5 seconds to thread stops itself + for (size_t i = 0; i < 25; i++) { - //5 seconds to thread stops itself - for (size_t i = 0; i < 25; i++) - { - if (i % 5 == 0) - SendPing(0x0100007f);//127.0.0.1 + if (i % 5 == 0) + SendPing(0x0100007f);//127.0.0.1 - if (!isRunningRecver) - break; + if (!isRunningRecver) + break; - struct timespec ts = {0, 200000000}; - nanosleep(&ts, NULL); - } + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); + } - //after 5 seconds waiting thread still running. now killing it - if (isRunningRecver) + //after 5 seconds waiting thread still running. now killing it + if (isRunningRecver) + { + if (pthread_kill(recvThread, SIGINT)) { - if (pthread_kill(recvThread, SIGINT)) - { - errorStr = "Cannot kill thread."; - return -1; - } + errorStr = "Cannot kill thread."; + return -1; } } + } - if (isRunningSender) +if (isRunningSender) + { + //5 seconds to thread stops itself + for (size_t i = 0; i < 25; i++) { - //5 seconds to thread stops itself - for (size_t i = 0; i < 25; i++) - { - if (!isRunningSender) - break; + if (!isRunningSender) + break; - struct timespec ts = {0, 200000000}; - nanosleep(&ts, NULL); - } + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); + } - //after 5 seconds waiting thread still running. now killing it - if (isRunningSender) + //after 5 seconds waiting thread still running. now killing it + if (isRunningSender) + { + if (pthread_kill(sendThread, SIGINT)) { - if (pthread_kill(sendThread, SIGINT)) - { - errorStr = "Cannot kill thread."; - return -1; - } + errorStr = "Cannot kill thread."; + return -1; } } + } - close(sendSocket); - return 0; +close(sendSocket); +return 0; } //----------------------------------------------------------------------------- void STG_PINGER::AddIP(uint32_t ip) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - ipToAdd.push_back(ip); +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +ipToAdd.push_back(ip); } //----------------------------------------------------------------------------- void STG_PINGER::DelIP(uint32_t ip) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - ipToDel.push_back(ip); +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +ipToDel.push_back(ip); } //----------------------------------------------------------------------------- void STG_PINGER::RealAddIP() - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); - std::list::iterator iter; - iter = ipToAdd.begin(); - while (iter != ipToAdd.end()) - { - pingIP.insert(std::make_pair(*iter, 0)); - ++iter; - } - ipToAdd.erase(ipToAdd.begin(), ipToAdd.end()); +std::list::iterator iter; +iter = ipToAdd.begin(); +while (iter != ipToAdd.end()) + { + pingIP.insert(std::make_pair(*iter, 0)); + ++iter; } +ipToAdd.erase(ipToAdd.begin(), ipToAdd.end()); +} //----------------------------------------------------------------------------- void STG_PINGER::RealDelIP() { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex, __FILE__, __LINE__); - std::list::iterator iter; - std::multimap::iterator treeIter; - iter = ipToDel.begin(); - while (iter != ipToDel.end()) - { - treeIter = pingIP.find(*iter); - if (treeIter != pingIP.end()) - pingIP.erase(treeIter); +std::list::iterator iter; +std::multimap::iterator treeIter; +iter = ipToDel.begin(); +while (iter != ipToDel.end()) + { + treeIter = pingIP.find(*iter); + if (treeIter != pingIP.end()) + pingIP.erase(treeIter); - ++iter; - } - ipToDel.erase(ipToDel.begin(), ipToDel.end()); + ++iter; + } +ipToDel.erase(ipToDel.begin(), ipToDel.end()); } //----------------------------------------------------------------------------- int STG_PINGER::GetPingIPNum() const { - return pingIP.size(); +return pingIP.size(); } //----------------------------------------------------------------------------- void STG_PINGER::PrintAllIP() { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - std::multimap::iterator iter; - iter = pingIP.begin(); - while (iter != pingIP.end()) - { - uint32_t ip = iter->first; - time_t t = iter->second; - std::string s; - x2str(t, s); - printf("ip = %s, time = %9s\n", inet_ntostring(ip).c_str(), s.c_str()); - ++iter; - } +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::multimap::iterator iter; +iter = pingIP.begin(); +while (iter != pingIP.end()) + { + uint32_t ip = iter->first; + time_t t = iter->second; + std::string s; + x2str(t, s); + printf("ip = %s, time = %9s\n", inet_ntostring(ip).c_str(), s.c_str()); + ++iter; + } } //----------------------------------------------------------------------------- int STG_PINGER::GetIPTime(uint32_t ip, time_t * t) const { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - std::multimap::const_iterator treeIter; +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +std::multimap::const_iterator treeIter; - treeIter = pingIP.find(ip); - if (treeIter == pingIP.end()) - return -1; +treeIter = pingIP.find(ip); +if (treeIter == pingIP.end()) + return -1; - *t = treeIter->second; - return 0; +*t = treeIter->second; +return 0; } //----------------------------------------------------------------------------- uint16_t STG_PINGER::PingCheckSum(void * data, int len) { - unsigned short * buf = (unsigned short *)data; - unsigned int sum = 0; - unsigned short result; +unsigned short * buf = (unsigned short *)data; +unsigned int sum = 0; +unsigned short result; - for ( sum = 0; len > 1; len -= 2 ) - sum += *buf++; +for ( sum = 0; len > 1; len -= 2 ) + sum += *buf++; - if ( len == 1 ) - sum += *(unsigned char*)buf; +if ( len == 1 ) + sum += *(unsigned char*)buf; - sum = (sum >> 16) + (sum & 0xFFFF); - sum += (sum >> 16); - result = ~sum; - return result; +sum = (sum >> 16) + (sum & 0xFFFF); +sum += (sum >> 16); +result = ~sum; +return result; } //----------------------------------------------------------------------------- int STG_PINGER::SendPing(uint32_t ip) { - struct sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = 0; - addr.sin_addr.s_addr = ip; +struct sockaddr_in addr; +memset(&addr, 0, sizeof(addr)); +addr.sin_family = AF_INET; +addr.sin_port = 0; +addr.sin_addr.s_addr = ip; - memset(&pmSend, 0, sizeof(pmSend)); - pmSend.hdr.type = ICMP_ECHO; - pmSend.hdr.un.echo.id = pid; - memcpy(pmSend.msg, &ip, sizeof(ip)); +memset(&pmSend, 0, sizeof(pmSend)); +pmSend.hdr.type = ICMP_ECHO; +pmSend.hdr.un.echo.id = pid; +memcpy(pmSend.msg, &ip, sizeof(ip)); - pmSend.hdr.checksum = PingCheckSum(&pmSend, sizeof(pmSend)); +pmSend.hdr.checksum = PingCheckSum(&pmSend, sizeof(pmSend)); - if (sendto(sendSocket, &pmSend, sizeof(pmSend), 0, (sockaddr *)&addr, sizeof(addr)) <= 0 ) - { - errorStr = "Send ping error: " + std::string(strerror(errno)); - return -1; - } +if (sendto(sendSocket, &pmSend, sizeof(pmSend), 0, (sockaddr *)&addr, sizeof(addr)) <= 0 ) + { + errorStr = "Send ping error: " + std::string(strerror(errno)); + return -1; + } - return 0; +return 0; } //----------------------------------------------------------------------------- uint32_t STG_PINGER::RecvPing() { - struct sockaddr_in addr; - uint32_t ipAddr = 0; +struct sockaddr_in addr; +uint32_t ipAddr = 0; - char buf[128]; - memset(buf, 0, sizeof(buf)); - int bytes; - socklen_t len = sizeof(addr); +char buf[128]; +memset(buf, 0, sizeof(buf)); +int bytes; +socklen_t len = sizeof(addr); - bytes = recvfrom(recvSocket, &buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len); - if (bytes > 0) - { - struct IP_HDR * ip = (struct IP_HDR *)buf; - struct ICMP_HDR *icmp = (struct ICMP_HDR *)(buf + ip->ihl * 4); +bytes = recvfrom(recvSocket, &buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len); +if (bytes > 0) + { + struct IP_HDR * ip = (struct IP_HDR *)buf; + struct ICMP_HDR *icmp = (struct ICMP_HDR *)(buf + ip->ihl * 4); - if (icmp->un.echo.id != pid) - return 0; + if (icmp->un.echo.id != pid) + return 0; - ipAddr = *(uint32_t*)(buf + sizeof(ICMP_HDR) + ip->ihl * 4); - } + ipAddr = *(uint32_t*)(buf + sizeof(ICMP_HDR) + ip->ihl * 4); + } - return ipAddr; +return ipAddr; } //----------------------------------------------------------------------------- void * STG_PINGER::RunSendPing(void * d) { - STG_PINGER * pinger = static_cast(d); +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); - pinger->isRunningSender = true; - time_t lastPing = 0; - while (pinger->nonstop) +STG_PINGER * pinger = static_cast(d); + +pinger->isRunningSender = true; +time_t lastPing = 0; +while (pinger->nonstop) + { + pinger->RealAddIP(); + pinger->RealDelIP(); + + std::multimap::iterator iter; + iter = pinger->pingIP.begin(); + while (iter != pinger->pingIP.end()) { - pinger->RealAddIP(); - pinger->RealDelIP(); + pinger->SendPing(iter->first); + ++iter; + } - std::multimap::iterator iter; - iter = pinger->pingIP.begin(); - while (iter != pinger->pingIP.end()) - { - pinger->SendPing(iter->first); - ++iter; - } + time_t currTime; - time_t currTime; + #ifdef STG_TIME + lastPing = stgTime; + currTime = stgTime; + #else + currTime = lastPing = time(NULL); + #endif + while (currTime - lastPing < pinger->delay && pinger->nonstop) + { #ifdef STG_TIME - lastPing = stgTime; currTime = stgTime; #else - currTime = lastPing = time(NULL); + currTime = time(NULL); #endif - - while (currTime - lastPing < pinger->delay && pinger->nonstop) - { - #ifdef STG_TIME - currTime = stgTime; - #else - currTime = time(NULL); - #endif - struct timespec ts = {0, 20000000}; - nanosleep(&ts, NULL); - } + struct timespec ts = {0, 20000000}; + nanosleep(&ts, NULL); } + } - pinger->isRunningSender = false; +pinger->isRunningSender = false; - return NULL; +return NULL; } //----------------------------------------------------------------------------- void * STG_PINGER::RunRecvPing(void * d) { - STG_PINGER * pinger = static_cast(d); +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); - pinger->isRunningRecver = true; +STG_PINGER * pinger = static_cast(d); - while (pinger->nonstop) - { - uint32_t ip = pinger->RecvPing(); +pinger->isRunningRecver = true; + +while (pinger->nonstop) + { + uint32_t ip = pinger->RecvPing(); - if (ip) + if (ip) + { + std::multimap::iterator treeIterUpper = pinger->pingIP.upper_bound(ip); + std::multimap::iterator treeIterLower = pinger->pingIP.lower_bound(ip); + while (treeIterUpper != treeIterLower) { - std::multimap::iterator treeIterUpper = pinger->pingIP.upper_bound(ip); - std::multimap::iterator treeIterLower = pinger->pingIP.lower_bound(ip); - while (treeIterUpper != treeIterLower) - { - #ifdef STG_TIME - treeIterLower->second = stgTime; - #else - treeIterLower->second = time(NULL); - #endif - ++treeIterLower; - } + #ifdef STG_TIME + treeIterLower->second = stgTime; + #else + treeIterLower->second = time(NULL); + #endif + ++treeIterLower; } - } - pinger->isRunningRecver = false; - return NULL; + + } +pinger->isRunningRecver = false; +return NULL; } //----------------------------------------------------------------------------- -- 2.43.2