1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
#include "../stdafx.h"
#include "kICMP.h"
CICMP::_mechanismus CICMP::m_mechanismus = CICMP::_icmpUndetermined;
BOOL CICMPDll::Initialize()
{
if(m_hICMP!=INVALID_HANDLE_VALUE || m_hICMPDLL)
Deinitialize();
m_hICMPDLL = ::LoadLibraryEx("ICMP",NULL,0);
if(!m_hICMPDLL)
return FALSE;
*(FARPROC*)&m_icmpCF = ::GetProcAddress(m_hICMPDLL,"IcmpCreateFile");
*(FARPROC*)&m_icmpSE = ::GetProcAddress(m_hICMPDLL,"IcmpSendEcho");
*(FARPROC*)&m_icmpCH = ::GetProcAddress(m_hICMPDLL,"IcmpCloseHandle");
if(!(m_icmpCF && m_icmpSE && m_icmpCH)){
Deinitialize(); return FALSE;
}
m_hICMP = (*m_icmpCF)();
if(m_hICMP==INVALID_HANDLE_VALUE){
Deinitialize(); return FALSE;
}
TRACE0("ICMP-DLL Initialized\n");
return TRUE;
}
void CICMPDll::Deinitialize()
{
if(m_hICMPDLL){
if(m_hICMP!=INVALID_HANDLE_VALUE && m_icmpCH)
(*m_icmpCH)(m_hICMP);
::FreeLibrary(m_hICMPDLL); m_hICMPDLL = NULL;
m_icmpCF = NULL;
m_icmpSE = NULL;
m_icmpCH = NULL;
}
m_hICMP=INVALID_HANDLE_VALUE;
if(m_sizeOut && m_bsOut){
delete m_bsOut;
m_bsOut = NULL; m_sizeOut = 0;
}
if(m_sizeIn && m_bsIn){
delete m_bsIn;
m_bsIn = NULL; m_sizeIn = 0;
}
}
LONG CICMPDll::Ping(const in_addr host,const UINT packetSize,
const UINT timeOut,LPINT pStatus)
{
if(!(m_hICMP && m_hICMPDLL && m_icmpSE)){
if(pStatus)
(*pStatus) = icmpNotInitialized;
return -1;
}
VERIFY(AdjustBuffers(packetSize));
IPINFO ipi;
memset(&ipi,0,sizeof(ipi));
ipi.Ttl = 30;
for(UINT tmp=0;tmp<packetSize;tmp++)
m_bsOut[tmp]=tmp&0xFF;
LPICMPECHO pRep = (LPICMPECHO)m_bsIn;
pRep->Status = 0xFFFFFFFFl;
if((*m_icmpSE)(m_hICMP,host.s_addr,m_bsOut,packetSize,
&ipi,pRep,m_sizeIn,timeOut))
TRACE0("ICMP-SendEcho succeeded\n");
else
TRACE0("ICMP-SendEcho failed\n");
LONG lrv = -1;
INT rv = ipUnknown;
switch(pRep->Status){
case IP_SUCCESS:
lrv = pRep->RTTime; rv = ipSuccess;
break;
case IP_BUF_TOO_SMALL: rv = ipBuffTooSmall; break;
case IP_DEST_NET_UNREACHABLE: rv = ipDestNetUnreachable; break;
case IP_DEST_HOST_UNREACHABLE: rv = ipDestHostUnreachable; break;
case IP_DEST_PROT_UNREACHABLE: rv = ipDestProtUnreachable; break;
case IP_DEST_PORT_UNREACHABLE: rv = ipDestPortUnreachable; break;
case IP_NO_RESOURCES: rv = ipNoResources; break;
case IP_BAD_OPTION: rv = ipBadOption; break;
case IP_HW_ERROR: rv = ipHWError; break;
case IP_PACKET_TOO_BIG: rv = ipPacketTooBig; break;
case IP_REQ_TIMED_OUT: rv = ipTimeOut; break;
case IP_BAD_REQ: rv = ipBadRequest; break;
case IP_BAD_ROUTE: rv = ipBadRoute; break;
case IP_TTL_EXPIRED_TRANSIT: rv = ipTTLExpiredInTransit; break;
case IP_TTL_EXPIRED_REASSEM: rv = ipTTLExpiredInReasm; break;
case IP_PARAM_PROBLEM: rv = ipParamProblem; break;
case IP_SOURCE_QUENCH: rv = ipSourceQuench; break;
case IP_OPTION_TOO_BIG: rv = ipOptionTooBig; break;
case IP_BAD_DESTINATION: rv = ipBadDest; break;
}
if(pStatus)
(*pStatus)=rv;
return lrv;
}
BOOL CICMPDll::AdjustBuffers(UINT packetSize)
{
if(!packetSize)
packetSize=1;
if(packetSize>m_sizeOut){
if(m_sizeOut && m_bsOut)
delete m_bsOut;
m_bsOut = new BYTE[m_sizeOut=packetSize];
if(!m_bsOut)
return FALSE;
}
UINT sin = sizeof(ICMPECHO)+SIZE_ICMP_HDR+packetSize;
if(sin>m_sizeIn){
if(m_sizeIn && m_bsIn)
delete m_bsIn;
m_bsIn = new BYTE[m_sizeIn=sin];
if(!m_bsIn)
return FALSE;
}
return TRUE;
}
WORD CICMPWS::m_icmpSeq = 0;
BOOL CICMPWS::Initialize()
{
if(m_socket!=INVALID_SOCKET)
Deinitialize();
m_socket = socket(AF_INET,SOCK_RAW,1/*ICMP*/);
if(m_socket==INVALID_SOCKET)
return FALSE;
TRACE0("ICMP-WS Initialized\n");
return TRUE;
}
void CICMPWS::Deinitialize()
{
if(m_socket!=INVALID_SOCKET){
closesocket(m_socket);
m_socket=INVALID_SOCKET;
}
if(m_sizeOut && m_bsOut){
delete m_bsOut;
m_bsOut = NULL; m_sizeOut = 0;
}
if(m_sizeIn && m_bsIn){
delete m_bsIn;
m_bsIn = NULL; m_sizeIn = 0;
}
}
LONG CICMPWS::Ping(const in_addr host,const UINT packetSize,
const UINT timeOut,LPINT pStatus)
{
if(m_socket==INVALID_SOCKET){
if(pStatus)
(*pStatus)=icmpNotInitialized;
}
VERIFY(AdjustBuffers(packetSize));
icmp* pPacket = (icmp*)m_bsOut;
memset(pPacket,0,m_sizeOut);
pPacket->icmp_type = ICMP_ECHO;
pPacket->icmp_seq = m_icmpSeq++;
pPacket->icmp_id = (WORD)(::GetCurrentThreadId()&0xFFFF);
for(UINT tmp=0;tmp<packetSize;tmp++)
pPacket->icmp_data[tmp]=tmp&0xFF;
pPacket->icmp_cksum = cksum(pPacket,SIZE_ICMP_HDR+packetSize);
sockaddr_in to;
memset(&to,0,sizeof(to));
to.sin_addr.s_addr = host.s_addr;
to.sin_family = AF_INET;
if(sendto(m_socket,(char*)pPacket,SIZE_ICMP_HDR+packetSize,0,
(SOCKADDR*)&to,sizeof(to)) != (int)(SIZE_ICMP_HDR+packetSize)){
TRACE1("sendto: %lu\n",WSAGetLastError());
if(pStatus)
(*pStatus)=icmpSocketError;
return -1;
}
DWORD sentTime = ::GetTickCount();
sockaddr_in from;
memset(&from,0,sizeof(from));
from.sin_family=AF_INET;
from.sin_addr.s_addr=INADDR_ANY;
fd_set fds;
FD_ZERO(&fds);
FD_SET(m_socket,&fds);
long lrv = -1;
INT rv = ipTimeOut;
for(;;){
DWORD ct = ::GetTickCount();
if((ct-sentTime)>=timeOut){
TRACE0("Timeout\n");
break;
}
timeval tv = {
(timeOut-ct+sentTime)/1000,
(timeOut-ct+sentTime)%1000
}; // tv_sec, tv_usec (secs,microsecs)
if(!select(m_socket,&fds,NULL,NULL,&tv)){
TRACE1("select: %d\n",WSAGetLastError());
break;
}
DWORD rtime = ::GetTickCount();
ASSERT(FD_ISSET(m_socket,&fds));
int fl = sizeof(from);
int rb = recvfrom(m_socket,(char*)m_bsIn,m_sizeIn,0,(SOCKADDR*)&from,&fl);
ip* pIP = (ip*)m_bsIn;
icmp* pICMP = (icmp*)&m_bsIn[sizeof(ip)];
if(pICMP->icmp_id!=pPacket->icmp_id)
continue;
if(pICMP->icmp_seq!=pPacket->icmp_seq)
continue;
if(from.sin_addr.s_addr!=host.s_addr)
continue;
if(pICMP->icmp_type==ICMP_ECHOREPLY){
lrv=rtime-sentTime;
rv=ipSuccess;
break;
}
rv = ipUnknown; // ***
break;
}
if(pStatus)
(*pStatus)=rv;
return lrv;
}
BOOL CICMPWS::AdjustBuffers(UINT packetSize)
{
if(!packetSize)
packetSize=0;
UINT osize = packetSize+SIZE_ICMP_HDR;
if(m_sizeOut<osize){
if(m_sizeOut && m_bsOut)
delete m_bsOut;
m_bsOut = new BYTE[m_sizeOut=osize];
if(!m_bsOut)
return FALSE;
}
UINT isize = osize+sizeof(ip);
if(m_sizeIn<isize){
if(m_sizeIn && m_bsIn)
delete m_bsIn;
m_bsIn = new BYTE[m_sizeIn=isize];
if(!m_bsIn)
return FALSE;
}
return TRUE;
}
WORD CICMPWS::cksum(LPVOID data,int count)
{
long lSum = 0;
WORD *pData = (WORD*)data;
while(count>0){
if(count>1){
lSum+=*(pData++);
count-=2;
}else{
lSum+=((WORD)*(BYTE*)pData)&0xFF;
count--;
}
}
lSum = (lSum&0xFFFF)+(lSum>>16);
lSum += (lSum>>16);
return (~lSum)&0xFFFF;
}
CICMP* CICMP::CreateICMP()
{
if(m_mechanismus==_icmpUndetermined)
GuessMechanismus();
switch(m_mechanismus){
case _icmpWinsock:
return new CICMPWS;
break;
case _icmpDLL:
return new CICMPDll;
break;
}
return NULL;
}
void CICMP::GuessMechanismus()
{
m_mechanismus=_icmpUndetermined;
SOCKET testSocket = socket(AF_INET,SOCK_RAW,1);
if(testSocket!=INVALID_SOCKET){
closesocket(testSocket);
m_mechanismus=_icmpWinsock;
}else{
HINSTANCE hICMP = ::LoadLibraryEx("ICMP",NULL,0);
if(!hICMP)
return;
BOOL isThere = (
::GetProcAddress(hICMP,"IcmpCreateFile")
&& ::GetProcAddress(hICMP,"IcmpSendEcho")
&& ::GetProcAddress(hICMP,"IcmpCloseHandle")
);
::FreeLibrary(hICMP);
if(isThere)
m_mechanismus=_icmpDLL;
}
}
|