6 #include <sys/socket.h>
7 #include <netinet/in.h>
18 #include "stg/common.h"
19 #include "stg/locker.h"
22 extern volatile time_t stgTime;
25 //-----------------------------------------------------------------------------
26 STG_PINGER::STG_PINGER(time_t d)
29 isRunningRecver(false),
30 isRunningSender(false),
43 pthread_mutex_init(&mutex, NULL);
44 memset(&pmSend, 0, sizeof(pmSend));
46 //-----------------------------------------------------------------------------
47 STG_PINGER::~STG_PINGER()
49 pthread_mutex_destroy(&mutex);
51 //-----------------------------------------------------------------------------
52 int STG_PINGER::Start()
54 struct protoent *proto = NULL;
55 proto = getprotobyname("ICMP");
56 sendSocket = socket(PF_INET, SOCK_RAW, proto->p_proto);
57 recvSocket = socket(PF_INET, SOCK_RAW, proto->p_proto);
59 pid = (int) getpid() % 65535;
60 if (sendSocket < 0 || recvSocket < 0)
62 errorStr = "Cannot create socket.";
66 if (pthread_create(&sendThread, NULL, RunSendPing, this))
68 errorStr = "Cannot create send thread.";
72 if (pthread_create(&recvThread, NULL, RunRecvPing, this))
74 errorStr = "Cannot create recv thread.";
80 //-----------------------------------------------------------------------------
81 int STG_PINGER::Stop()
87 //5 seconds to thread stops itself
88 for (size_t i = 0; i < 25; i++)
91 SendPing(0x0100007f);//127.0.0.1
99 //after 5 seconds waiting thread still running. now killing it
102 if (pthread_kill(recvThread, SIGINT))
104 errorStr = "Cannot kill thread.";
112 //5 seconds to thread stops itself
113 for (size_t i = 0; i < 25; i++)
115 if (!isRunningSender)
121 //after 5 seconds waiting thread still running. now killing it
124 if (pthread_kill(sendThread, SIGINT))
126 errorStr = "Cannot kill thread.";
135 //-----------------------------------------------------------------------------
136 void STG_PINGER::AddIP(uint32_t ip)
138 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
139 ipToAdd.push_back(ip);
141 //-----------------------------------------------------------------------------
142 void STG_PINGER::DelIP(uint32_t ip)
144 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
145 ipToDel.push_back(ip);
147 //-----------------------------------------------------------------------------
148 void STG_PINGER::RealAddIP()
150 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
152 std::list<uint32_t>::iterator iter;
153 iter = ipToAdd.begin();
154 while (iter != ipToAdd.end())
156 pingIP.insert(std::make_pair(*iter, 0));
159 ipToAdd.erase(ipToAdd.begin(), ipToAdd.end());
161 //-----------------------------------------------------------------------------
162 void STG_PINGER::RealDelIP()
164 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
166 std::list<uint32_t>::iterator iter;
167 std::multimap<uint32_t, time_t>::iterator treeIter;
168 iter = ipToDel.begin();
169 while (iter != ipToDel.end())
171 treeIter = pingIP.find(*iter);
172 if (treeIter != pingIP.end())
173 pingIP.erase(treeIter);
177 ipToDel.erase(ipToDel.begin(), ipToDel.end());
179 //-----------------------------------------------------------------------------
180 int STG_PINGER::GetPingIPNum() const
182 return pingIP.size();
184 //-----------------------------------------------------------------------------
185 void STG_PINGER::PrintAllIP()
187 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
188 std::multimap<uint32_t, time_t>::iterator iter;
189 iter = pingIP.begin();
190 while (iter != pingIP.end())
192 uint32_t ip = iter->first;
193 time_t t = iter->second;
196 printf("ip = %s, time = %9s\n", inet_ntostring(ip).c_str(), s.c_str());
201 //-----------------------------------------------------------------------------
202 int STG_PINGER::GetIPTime(uint32_t ip, time_t * t) const
204 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
205 std::multimap<uint32_t, time_t>::const_iterator treeIter;
207 treeIter = pingIP.find(ip);
208 if (treeIter == pingIP.end())
211 *t = treeIter->second;
214 //-----------------------------------------------------------------------------
215 uint16_t STG_PINGER::PingCheckSum(void * data, int len)
217 unsigned short * buf = (unsigned short *)data;
218 unsigned int sum = 0;
219 unsigned short result;
221 for ( sum = 0; len > 1; len -= 2 )
225 sum += *(unsigned char*)buf;
227 sum = (sum >> 16) + (sum & 0xFFFF);
232 //-----------------------------------------------------------------------------
233 int STG_PINGER::SendPing(uint32_t ip)
235 struct sockaddr_in addr;
236 memset(&addr, 0, sizeof(addr));
237 addr.sin_family = AF_INET;
239 addr.sin_addr.s_addr = ip;
241 memset(&pmSend, 0, sizeof(pmSend));
242 pmSend.hdr.type = ICMP_ECHO;
243 pmSend.hdr.un.echo.id = pid;
244 memcpy(pmSend.msg, &ip, sizeof(ip));
246 pmSend.hdr.checksum = PingCheckSum(&pmSend, sizeof(pmSend));
248 if (sendto(sendSocket, &pmSend, sizeof(pmSend), 0, (sockaddr *)&addr, sizeof(addr)) <= 0 )
250 errorStr = "Send ping error: " + std::string(strerror(errno));
257 //-----------------------------------------------------------------------------
258 uint32_t STG_PINGER::RecvPing()
260 struct sockaddr_in addr;
264 memset(buf, 0, sizeof(buf));
266 socklen_t len = sizeof(addr);
268 bytes = recvfrom(recvSocket, &buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len);
271 struct IP_HDR * ip = (struct IP_HDR *)buf;
272 struct ICMP_HDR *icmp = (struct ICMP_HDR *)(buf + ip->ihl * 4);
274 if (icmp->un.echo.id != pid)
277 ipAddr = *(uint32_t*)(buf + sizeof(ICMP_HDR) + ip->ihl * 4);
282 //-----------------------------------------------------------------------------
283 void * STG_PINGER::RunSendPing(void * d)
285 STG_PINGER * pinger = static_cast<STG_PINGER *>(d);
287 pinger->isRunningSender = true;
289 while (pinger->nonstop)
294 std::multimap<uint32_t, time_t>::iterator iter;
295 iter = pinger->pingIP.begin();
296 while (iter != pinger->pingIP.end())
298 pinger->SendPing(iter->first);
308 currTime = lastPing = time(NULL);
311 while (currTime - lastPing < pinger->delay && pinger->nonstop)
316 currTime = time(NULL);
322 pinger->isRunningSender = false;
326 //-----------------------------------------------------------------------------
327 void * STG_PINGER::RunRecvPing(void * d)
329 STG_PINGER * pinger = static_cast<STG_PINGER *>(d);
331 pinger->isRunningRecver = true;
333 while (pinger->nonstop)
335 uint32_t ip = pinger->RecvPing();
339 std::multimap<uint32_t, time_t>::iterator treeIterUpper = pinger->pingIP.upper_bound(ip);
340 std::multimap<uint32_t, time_t>::iterator treeIterLower = pinger->pingIP.lower_bound(ip);
341 while (treeIterUpper != treeIterLower)
344 treeIterLower->second = stgTime;
346 treeIterLower->second = time(NULL);
353 pinger->isRunningRecver = false;
356 //-----------------------------------------------------------------------------