-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinMTRNet.cpp
455 lines (388 loc) · 12.1 KB
/
WinMTRNet.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//*****************************************************************************
// FILE: WinMTRNet.cpp
//
//*****************************************************************************
#include "WinMTRGlobal.h"
#include "WinMTRNet.h"
#include "WinMTRDialog.h"
#include <iostream>
#include <sstream>
#define TRACE_MSG(msg) \
{ \
std::ostringstream dbg_msg(std::ostringstream::out); \
dbg_msg << msg << std::endl; \
OutputDebugString(dbg_msg.str().c_str()); \
}
#define IPFLAG_DONT_FRAGMENT 0x02
#define MAX_HOPS 30
struct trace_thread {
int address;
WinMTRNet *winmtr;
int ttl;
};
struct dns_resolver_thread {
int index;
WinMTRNet *winmtr;
};
void TraceThread(void *p);
void DnsResolverThread(void *p);
WinMTRNet::WinMTRNet(WinMTRDialog *wp) {
ghMutex = CreateMutex(NULL, FALSE, NULL);
tracing=false;
initialized = false;
wmtrdlg = wp;
WSADATA wsaData;
if( WSAStartup(MAKEWORD(2, 2), &wsaData) ) {
AfxMessageBox("Failed initializing windows sockets library!");
return;
}
hICMP_DLL = LoadLibrary(_T("ICMP.DLL"));
if (hICMP_DLL == 0) {
AfxMessageBox("Failed: Unable to locate ICMP.DLL!");
return;
}
/*
* Get pointers to ICMP.DLL functions
*/
lpfnIcmpCreateFile = (LPFNICMPCREATEFILE)GetProcAddress(hICMP_DLL,"IcmpCreateFile");
lpfnIcmpCloseHandle = (LPFNICMPCLOSEHANDLE)GetProcAddress(hICMP_DLL,"IcmpCloseHandle");
lpfnIcmpSendEcho = (LPFNICMPSENDECHO)GetProcAddress(hICMP_DLL,"IcmpSendEcho");
if ((!lpfnIcmpCreateFile) || (!lpfnIcmpCloseHandle) || (!lpfnIcmpSendEcho)) {
AfxMessageBox("Wrong ICMP.DLL system library !");
return;
}
/*
* IcmpCreateFile() - Open the ping service
*/
hICMP = (HANDLE) lpfnIcmpCreateFile();
if (hICMP == INVALID_HANDLE_VALUE) {
AfxMessageBox("Error in ICMP.DLL !");
return;
}
ResetHops();
initialized = true;
return;
}
WinMTRNet::~WinMTRNet()
{
if(initialized) {
/*
* IcmpCloseHandle - Close the ICMP handle
*/
lpfnIcmpCloseHandle(hICMP);
// Shut down...
FreeLibrary(hICMP_DLL);
WSACleanup();
CloseHandle(ghMutex);
}
}
void WinMTRNet::ResetHops()
{
for(int i = 0; i < MaxHost;i++) {
host[i].addr = 0;
host[i].xmit = 0;
host[i].returned = 0;
host[i].total = 0;
host[i].last = 0;
host[i].best = 0;
host[i].worst = 0;
memset(host[i].name,0,sizeof(host[i].name));
}
}
void WinMTRNet::DoTrace(int address)
{
HANDLE hThreads[MAX_HOPS];
tracing = true;
ResetHops();
last_remote_addr = address;
// one thread per TTL value
for(int i = 0; i < MAX_HOPS; i++) {
trace_thread *current = new trace_thread;
current->address = address;
current->winmtr = this;
current->ttl = i + 1;
hThreads[i] = (HANDLE)_beginthread(TraceThread, 0 , current);
}
WaitForMultipleObjects(MAX_HOPS, hThreads, TRUE, INFINITE);
}
void WinMTRNet::StopTrace()
{
tracing = false;
}
void TraceThread(void *p)
{
trace_thread* current = (trace_thread*)p;
WinMTRNet *wmtrnet = current->winmtr;
TRACE_MSG("Threaad with TTL=" << current->ttl << " started.");
IPINFO stIPInfo, *lpstIPInfo;
DWORD dwReplyCount;
char achReqData[8192];
int nDataLen = wmtrnet->wmtrdlg->pingsize;
char achRepData[sizeof(ICMPECHO) + 8192];
/*
* Init IPInfo structure
*/
lpstIPInfo = &stIPInfo;
stIPInfo.Ttl = current->ttl;
stIPInfo.Tos = 0;
stIPInfo.Flags = IPFLAG_DONT_FRAGMENT;
stIPInfo.OptionsSize = 0;
stIPInfo.OptionsData = NULL;
for (int i=0; i<nDataLen; i++) achReqData[i] = 32; //whitespaces
while(wmtrnet->tracing) {
// For some strange reason, ICMP API is not filling the TTL for icmp echo reply
// Check if the current thread should be closed
if( current->ttl > wmtrnet->GetMax() ) break;
// NOTE: some servers does not respond back everytime, if TTL expires in transit; e.g. :
// ping -n 20 -w 5000 -l 64 -i 7 www.chinapost.com.tw -> less that half of the replies are coming back from 219.80.240.93
// but if we are pinging ping -n 20 -w 5000 -l 64 219.80.240.93 we have 0% loss
// A resolution would be:
// - as soon as we get a hop, we start pinging directly that hop, with a greater TTL
// - a drawback would be that, some servers are configured to reply for TTL transit expire, but not to ping requests, so,
// for these servers we'll have 100% loss
dwReplyCount = wmtrnet->lpfnIcmpSendEcho(wmtrnet->hICMP, current->address, achReqData, nDataLen, lpstIPInfo, achRepData, sizeof(achRepData), ECHO_REPLY_TIMEOUT);
PICMPECHO icmp_echo_reply = (PICMPECHO)achRepData;
wmtrnet->AddXmit(current->ttl - 1);
if (dwReplyCount != 0) {
TRACE_MSG("TTL " << current->ttl << " reply TTL " << icmp_echo_reply->Options.Ttl << " Status " << icmp_echo_reply->Status << " Reply count " << dwReplyCount);
switch(icmp_echo_reply->Status) {
case IP_SUCCESS:
case IP_TTL_EXPIRED_TRANSIT:
wmtrnet->SetLast(current->ttl - 1, icmp_echo_reply->RoundTripTime);
wmtrnet->SetBest(current->ttl - 1, icmp_echo_reply->RoundTripTime);
wmtrnet->SetWorst(current->ttl - 1, icmp_echo_reply->RoundTripTime);
wmtrnet->AddReturned(current->ttl - 1);
wmtrnet->SetAddr(current->ttl - 1, icmp_echo_reply->Address);
break;
case IP_BUF_TOO_SMALL:
wmtrnet->SetName(current->ttl - 1, "Reply buffer too small.");
break;
case IP_DEST_NET_UNREACHABLE:
wmtrnet->SetName(current->ttl - 1, "Destination network unreachable.");
break;
case IP_DEST_HOST_UNREACHABLE:
wmtrnet->SetName(current->ttl - 1, "Destination host unreachable.");
break;
case IP_DEST_PROT_UNREACHABLE:
wmtrnet->SetName(current->ttl - 1, "Destination protocol unreachable.");
break;
case IP_DEST_PORT_UNREACHABLE:
wmtrnet->SetName(current->ttl - 1, "Destination port unreachable.");
break;
case IP_NO_RESOURCES:
wmtrnet->SetName(current->ttl - 1, "Insufficient IP resources were available.");
break;
case IP_BAD_OPTION:
wmtrnet->SetName(current->ttl - 1, "Bad IP option was specified.");
break;
case IP_HW_ERROR:
wmtrnet->SetName(current->ttl - 1, "Hardware error occurred.");
break;
case IP_PACKET_TOO_BIG:
wmtrnet->SetName(current->ttl - 1, "Packet was too big.");
break;
case IP_REQ_TIMED_OUT:
wmtrnet->SetName(current->ttl - 1, "Request timed out.");
break;
case IP_BAD_REQ:
wmtrnet->SetName(current->ttl - 1, "Bad request.");
break;
case IP_BAD_ROUTE:
wmtrnet->SetName(current->ttl - 1, "Bad route.");
break;
case IP_TTL_EXPIRED_REASSEM:
wmtrnet->SetName(current->ttl - 1, "The time to live expired during fragment reassembly.");
break;
case IP_PARAM_PROBLEM:
wmtrnet->SetName(current->ttl - 1, "Parameter problem.");
break;
case IP_SOURCE_QUENCH:
wmtrnet->SetName(current->ttl - 1, "Datagrams are arriving too fast to be processed and datagrams may have been discarded.");
break;
case IP_OPTION_TOO_BIG:
wmtrnet->SetName(current->ttl - 1, "An IP option was too big.");
break;
case IP_BAD_DESTINATION:
wmtrnet->SetName(current->ttl - 1, "Bad destination.");
break;
case IP_GENERAL_FAILURE:
wmtrnet->SetName(current->ttl - 1, "General failure.");
break;
default:
wmtrnet->SetName(current->ttl - 1, "General failure.");
}
if(wmtrnet->wmtrdlg->interval * 1000 > icmp_echo_reply->RoundTripTime)
Sleep(wmtrnet->wmtrdlg->interval * 1000 - icmp_echo_reply->RoundTripTime);
}
} /* end ping loop */
TRACE_MSG("Thread with TTL=" << current->ttl << " stopped.");
delete p;
_endthread();
}
int WinMTRNet::GetAddr(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
int addr = ntohl(host[at].addr);
ReleaseMutex(ghMutex);
return addr;
}
int WinMTRNet::GetName(int at, char *n)
{
WaitForSingleObject(ghMutex, INFINITE);
if(!strcmp(host[at].name, "")) {
int addr = GetAddr(at);
sprintf ( n, "%d.%d.%d.%d",
(addr >> 24) & 0xff,
(addr >> 16) & 0xff,
(addr >> 8) & 0xff,
addr & 0xff
);
if(addr==0)
strcpy(n,"");
} else {
strcpy(n, host[at].name);
}
ReleaseMutex(ghMutex);
return 0;
}
int WinMTRNet::GetBest(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
int ret = host[at].best;
ReleaseMutex(ghMutex);
return ret;
}
int WinMTRNet::GetWorst(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
int ret = host[at].worst;
ReleaseMutex(ghMutex);
return ret;
}
int WinMTRNet::GetAvg(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
int ret = host[at].returned == 0 ? 0 : host[at].total / host[at].returned;
ReleaseMutex(ghMutex);
return ret;
}
int WinMTRNet::GetPercent(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
int ret = (host[at].xmit == 0) ? 0 : (100 - (100 * host[at].returned / host[at].xmit));
ReleaseMutex(ghMutex);
return ret;
}
int WinMTRNet::GetLast(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
int ret = host[at].last;
ReleaseMutex(ghMutex);
return ret;
}
int WinMTRNet::GetReturned(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
int ret = host[at].returned;
ReleaseMutex(ghMutex);
return ret;
}
int WinMTRNet::GetXmit(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
int ret = host[at].xmit;
ReleaseMutex(ghMutex);
return ret;
}
int WinMTRNet::GetMax()
{
WaitForSingleObject(ghMutex, INFINITE);
int max = MAX_HOPS;
// first match: traced address responds on ping requests, and the address is in the hosts list
for(int i = 0; i < MAX_HOPS; i++) {
if(host[i].addr == last_remote_addr) {
max = i + 1;
break;
}
}
// second match: traced address doesn't responds on ping requests
if(max == MAX_HOPS) {
while((max > 1) && (host[max - 1].addr == host[max - 2].addr) && (host[max - 1].addr != 0) ) max--;
}
ReleaseMutex(ghMutex);
return max;
}
void WinMTRNet::SetAddr(int at, __int32 addr)
{
WaitForSingleObject(ghMutex, INFINITE);
if(host[at].addr == 0 && addr != 0) {
TRACE_MSG("Start DnsResolverThread for new address " << addr << ". Old addr value was " << host[at].addr);
host[at].addr = addr;
dns_resolver_thread *dnt = new dns_resolver_thread;
dnt->index = at;
dnt->winmtr = this;
if(wmtrdlg->useDNS) _beginthread(DnsResolverThread, 0, dnt);
}
ReleaseMutex(ghMutex);
}
void WinMTRNet::SetName(int at, char *n)
{
WaitForSingleObject(ghMutex, INFINITE);
strcpy(host[at].name, n);
ReleaseMutex(ghMutex);
}
void WinMTRNet::SetBest(int at, int current)
{
WaitForSingleObject(ghMutex, INFINITE);
if(host[at].best > current || host[at].xmit == 1) {
host[at].best = current;
};
if(host[at].worst < current) {
host[at].worst = current;
}
ReleaseMutex(ghMutex);
}
void WinMTRNet::SetWorst(int at, int current)
{
WaitForSingleObject(ghMutex, INFINITE);
ReleaseMutex(ghMutex);
}
void WinMTRNet::SetLast(int at, int last)
{
WaitForSingleObject(ghMutex, INFINITE);
host[at].last = last;
host[at].total += last;
ReleaseMutex(ghMutex);
}
void WinMTRNet::AddReturned(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
host[at].returned++;
ReleaseMutex(ghMutex);
}
void WinMTRNet::AddXmit(int at)
{
WaitForSingleObject(ghMutex, INFINITE);
host[at].xmit++;
ReleaseMutex(ghMutex);
}
void DnsResolverThread(void *p)
{
TRACE_MSG("DNS resolver thread started.");
dns_resolver_thread *dnt = (dns_resolver_thread*)p;
WinMTRNet* wn = dnt->winmtr;
struct hostent *phent ;
char buf[100];
int addr = wn->GetAddr(dnt->index);
sprintf (buf, "%d.%d.%d.%d", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
int haddr = htonl(addr);
phent = gethostbyaddr( (const char*)&haddr, sizeof(int), AF_INET);
if(phent) {
wn->SetName(dnt->index, phent->h_name);
} else {
wn->SetName(dnt->index, buf);
}
delete p;
TRACE_MSG("DNS resolver thread stopped.");
_endthread();
}