4 #include <sys/socket.h>
5 #include <netinet/in.h>
18 #include "stg/common.h"
19 #include "stg/locker.h"
21 #include "stg/pinger.h"
24 extern volatile time_t stgTime;
27 //-----------------------------------------------------------------------------
28 STG_PINGER::STG_PINGER(time_t d)
31 isRunningRecver(false),
32 isRunningSender(false),
45 pthread_mutex_init(&mutex, NULL);
46 memset(&pmSend, 0, sizeof(pmSend));
48 //-----------------------------------------------------------------------------
49 STG_PINGER::~STG_PINGER()
51 pthread_mutex_destroy(&mutex);
53 //-----------------------------------------------------------------------------
54 int STG_PINGER::Start()
56 struct protoent *proto = NULL;
57 proto = getprotobyname("ICMP");
58 sendSocket = socket(PF_INET, SOCK_RAW, proto->p_proto);
59 recvSocket = socket(PF_INET, SOCK_RAW, proto->p_proto);
61 pid = (int) getpid() % 65535;
62 if (sendSocket < 0 || recvSocket < 0)
64 errorStr = "Cannot create socket.";
68 if (pthread_create(&sendThread, NULL, RunSendPing, this))
70 errorStr = "Cannot create send thread.";
74 if (pthread_create(&recvThread, NULL, RunRecvPing, this))
76 errorStr = "Cannot create recv thread.";
82 //-----------------------------------------------------------------------------
83 int STG_PINGER::Stop()
89 //5 seconds to thread stops itself
90 for (size_t i = 0; i < 25; i++)
93 SendPing(0x0100007f);//127.0.0.1
98 struct timespec ts = {0, 200000000};
105 //5 seconds to thread stops itself
106 for (size_t i = 0; i < 25; i++)
108 if (!isRunningSender)
111 struct timespec ts = {0, 200000000};
112 nanosleep(&ts, NULL);
118 if (isRunningSender || isRunningRecver)
123 //-----------------------------------------------------------------------------
124 void STG_PINGER::AddIP(uint32_t ip)
126 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
127 ipToAdd.push_back(ip);
129 //-----------------------------------------------------------------------------
130 void STG_PINGER::DelIP(uint32_t ip)
132 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
133 ipToDel.push_back(ip);
135 //-----------------------------------------------------------------------------
136 void STG_PINGER::RealAddIP()
138 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
140 std::list<uint32_t>::iterator iter;
141 iter = ipToAdd.begin();
142 while (iter != ipToAdd.end())
144 pingIP.insert(std::make_pair(*iter, 0));
147 ipToAdd.erase(ipToAdd.begin(), ipToAdd.end());
149 //-----------------------------------------------------------------------------
150 void STG_PINGER::RealDelIP()
152 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
154 std::list<uint32_t>::iterator iter;
155 std::multimap<uint32_t, time_t>::iterator treeIter;
156 iter = ipToDel.begin();
157 while (iter != ipToDel.end())
159 treeIter = pingIP.find(*iter);
160 if (treeIter != pingIP.end())
161 pingIP.erase(treeIter);
165 ipToDel.erase(ipToDel.begin(), ipToDel.end());
167 //-----------------------------------------------------------------------------
168 int STG_PINGER::GetPingIPNum() const
170 return pingIP.size();
172 //-----------------------------------------------------------------------------
173 void STG_PINGER::PrintAllIP()
175 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
176 std::multimap<uint32_t, time_t>::iterator iter;
177 iter = pingIP.begin();
178 while (iter != pingIP.end())
180 uint32_t ip = iter->first;
181 time_t t = iter->second;
184 printf("ip = %s, time = %9s\n", inet_ntostring(ip).c_str(), s.c_str());
189 //-----------------------------------------------------------------------------
190 int STG_PINGER::GetIPTime(uint32_t ip, time_t * t) const
192 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
193 std::multimap<uint32_t, time_t>::const_iterator treeIter;
195 treeIter = pingIP.find(ip);
196 if (treeIter == pingIP.end())
199 *t = treeIter->second;
202 //-----------------------------------------------------------------------------
203 uint16_t STG_PINGER::PingCheckSum(void * data, int len)
205 unsigned short * buf = (unsigned short *)data;
206 unsigned int sum = 0;
207 unsigned short result;
209 for ( sum = 0; len > 1; len -= 2 )
213 sum += *(unsigned char*)buf;
215 sum = (sum >> 16) + (sum & 0xFFFF);
220 //-----------------------------------------------------------------------------
221 int STG_PINGER::SendPing(uint32_t ip)
223 struct sockaddr_in addr;
224 memset(&addr, 0, sizeof(addr));
225 addr.sin_family = AF_INET;
227 addr.sin_addr.s_addr = ip;
229 memset(&pmSend, 0, sizeof(pmSend));
230 pmSend.hdr.type = ICMP_ECHO;
231 pmSend.hdr.un.echo.id = pid;
232 memcpy(pmSend.msg, &ip, sizeof(ip));
234 pmSend.hdr.checksum = PingCheckSum(&pmSend, sizeof(pmSend));
236 if (sendto(sendSocket, &pmSend, sizeof(pmSend), 0, (sockaddr *)&addr, sizeof(addr)) <= 0 )
238 errorStr = "Send ping error: " + std::string(strerror(errno));
245 //-----------------------------------------------------------------------------
246 uint32_t STG_PINGER::RecvPing()
248 struct sockaddr_in addr;
252 memset(buf, 0, sizeof(buf));
254 socklen_t len = sizeof(addr);
256 bytes = recvfrom(recvSocket, &buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len);
259 struct IP_HDR * ip = (struct IP_HDR *)buf;
260 struct ICMP_HDR *icmp = (struct ICMP_HDR *)(buf + ip->ihl * 4);
262 if (icmp->un.echo.id != pid)
265 ipAddr = *(uint32_t*)(buf + sizeof(ICMP_HDR) + ip->ihl * 4);
270 //-----------------------------------------------------------------------------
271 void * STG_PINGER::RunSendPing(void * d)
274 sigfillset(&signalSet);
275 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
277 STG_PINGER * pinger = static_cast<STG_PINGER *>(d);
279 pinger->isRunningSender = true;
281 while (pinger->nonstop)
286 std::multimap<uint32_t, time_t>::iterator iter;
287 iter = pinger->pingIP.begin();
288 while (iter != pinger->pingIP.end())
290 pinger->SendPing(iter->first);
300 currTime = lastPing = time(NULL);
303 while (currTime - lastPing < pinger->delay && pinger->nonstop)
308 currTime = time(NULL);
310 struct timespec ts = {0, 20000000};
311 nanosleep(&ts, NULL);
315 pinger->isRunningSender = false;
319 //-----------------------------------------------------------------------------
320 void * STG_PINGER::RunRecvPing(void * d)
323 sigfillset(&signalSet);
324 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
326 STG_PINGER * pinger = static_cast<STG_PINGER *>(d);
328 pinger->isRunningRecver = true;
330 while (pinger->nonstop)
332 uint32_t ip = pinger->RecvPing();
336 std::multimap<uint32_t, time_t>::iterator treeIterUpper = pinger->pingIP.upper_bound(ip);
337 std::multimap<uint32_t, time_t>::iterator treeIterLower = pinger->pingIP.lower_bound(ip);
338 while (treeIterUpper != treeIterLower)
341 treeIterLower->second = stgTime;
343 treeIterLower->second = time(NULL);
350 pinger->isRunningRecver = false;
353 //-----------------------------------------------------------------------------