Sync with trunk head (part 1 of x)
[reactos.git] / base / applications / network / tracert / tracert.c
1 /*
2 * PROJECT: ReactOS trace route utility
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/network/tracert.c
5 * PURPOSE: Trace network paths through networks
6 * COPYRIGHT: Copyright 2006 - 2007 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include "tracert.h"
11
12 //#define TRACERT_DBG
13
14 CHAR cHostname[256]; // target hostname
15 CHAR cDestIP[18]; // target IP
16
17
18 static VOID
19 DebugPrint(LPTSTR lpString, ...)
20 {
21 #ifdef TRACERT_DBG
22 va_list args;
23 va_start(args, lpString);
24 _vtprintf(lpString, args);
25 va_end(args);
26 #else
27 UNREFERENCED_PARAMETER(lpString);
28 #endif
29 }
30
31
32 static VOID
33 Usage(VOID)
34 {
35 _tprintf(_T("\nUsage: tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] target_name\n\n"
36 "Options:\n"
37 " -d Do not resolve addresses to hostnames.\n"
38 " -h maximum_hops Maximum number of hops to search for target.\n"
39 " -j host-list Loose source route along host-list.\n"
40 " -w timeout Wait timeout milliseconds for each reply.\n\n"));
41
42 _tprintf(_T("NOTES\n-----\n"
43 "- Setting TTL values is not currently supported in ReactOS, so the trace will\n"
44 " jump straight to the destination. This feature will be implemented soon.\n"
45 "- Host info is not currently available in ReactOS and will fail with strange\n"
46 " results. Use -d to force it not to resolve IP's.\n"
47 "- For testing purposes, all should work as normal in a Windows environment\n\n"));
48 }
49
50
51 static BOOL
52 ParseCmdline(int argc,
53 LPCTSTR argv[],
54 PAPPINFO pInfo)
55 {
56 INT i;
57
58 if (argc < 2)
59 {
60 Usage();
61 return FALSE;
62 }
63 else
64 {
65 for (i = 1; i < argc; i++)
66 {
67 if (argv[i][0] == _T('-'))
68 {
69 switch (argv[i][1])
70 {
71 case _T('d'):
72 pInfo->bResolveAddresses = FALSE;
73 break;
74
75 case _T('h'):
76 _stscanf(argv[i+1], _T("%d"), &pInfo->iMaxHops);
77 break;
78
79 case _T('j'):
80 _tprintf(_T("-j is not yet implemented.\n"));
81 break;
82
83 case _T('w'):
84 _stscanf(argv[i+1], _T("%d"), &pInfo->iTimeOut);
85 break;
86
87 default:
88 {
89 _tprintf(_T("%s is not a valid option.\n"), argv[i]);
90 Usage();
91 return FALSE;
92 }
93 }
94 }
95 else
96 /* copy target address */
97 _tcsncpy(cHostname, argv[i], 255);
98 }
99 }
100
101 return TRUE;
102 }
103
104
105 static WORD
106 CheckSum(PUSHORT data,
107 UINT size)
108 {
109 DWORD dwSum = 0;
110
111 while (size > 1)
112 {
113 dwSum += *data++;
114 size -= sizeof(USHORT);
115 }
116
117 if (size)
118 dwSum += *(UCHAR*)data;
119
120 dwSum = (dwSum >> 16) + (dwSum & 0xFFFF);
121 dwSum += (dwSum >> 16);
122
123 return (USHORT)(~dwSum);
124 }
125
126
127 static VOID
128 SetupTimingMethod(PAPPINFO pInfo)
129 {
130 LARGE_INTEGER PerformanceCounterFrequency;
131
132 /* check if performance counters are available */
133 pInfo->bUsePerformanceCounter = QueryPerformanceFrequency(&PerformanceCounterFrequency);
134
135 if (pInfo->bUsePerformanceCounter)
136 {
137 /* restrict execution to first processor on SMP systems */
138 if (SetThreadAffinityMask(GetCurrentThread(), 1) == 0)
139 pInfo->bUsePerformanceCounter = FALSE;
140
141 pInfo->TicksPerMs.QuadPart = PerformanceCounterFrequency.QuadPart / 1000;
142 pInfo->TicksPerUs.QuadPart = PerformanceCounterFrequency.QuadPart / 1000000;
143 }
144 else
145 {
146 pInfo->TicksPerMs.QuadPart = 1;
147 pInfo->TicksPerUs.QuadPart = 1;
148 }
149 }
150
151
152 static BOOL
153 ResolveHostname(PAPPINFO pInfo)
154 {
155 HOSTENT *hp;
156 ULONG addr;
157
158 ZeroMemory(&pInfo->dest, sizeof(pInfo->dest));
159
160 /* if address is not a dotted decimal */
161 if ((addr = inet_addr(cHostname))== INADDR_NONE)
162 {
163 if ((hp = gethostbyname(cHostname)) != 0)
164 {
165 //CopyMemory(&pInfo->dest.sin_addr, hp->h_addr, hp->h_length);
166 pInfo->dest.sin_addr = *((struct in_addr *)hp->h_addr);
167 pInfo->dest.sin_family = hp->h_addrtype;
168 }
169 else
170 {
171 _tprintf(_T("Unable to resolve target system name %s.\n"), cHostname);
172 return FALSE;
173 }
174 }
175 else
176 {
177 pInfo->dest.sin_addr.s_addr = addr;
178 pInfo->dest.sin_family = AF_INET;
179 }
180
181 _tcscpy(cDestIP, inet_ntoa(pInfo->dest.sin_addr));
182
183 return TRUE;
184 }
185
186
187 static LONGLONG
188 GetTime(PAPPINFO pInfo)
189 {
190 LARGE_INTEGER Time;
191
192 /* Get the system time using preformance counters if available */
193 if (pInfo->bUsePerformanceCounter)
194 {
195 if (QueryPerformanceCounter(&Time))
196 {
197 return Time.QuadPart;
198 }
199 }
200
201 /* otherwise fall back to GetTickCount */
202 Time.u.LowPart = (DWORD)GetTickCount();
203 Time.u.HighPart = 0;
204
205 return (LONGLONG)Time.u.LowPart;
206 }
207
208
209 static BOOL
210 SetTTL(SOCKET sock,
211 INT iTTL)
212 {
213 if (setsockopt(sock,
214 IPPROTO_IP,
215 IP_TTL,
216 (const char *)&iTTL,
217 sizeof(iTTL)) == SOCKET_ERROR)
218 {
219 DebugPrint(_T("TTL setsockopt failed : %d. \n"), WSAGetLastError());
220 return FALSE;
221 }
222
223 return TRUE;
224 }
225
226
227 static BOOL
228 CreateSocket(PAPPINFO pInfo)
229 {
230 pInfo->icmpSock = WSASocket(AF_INET,
231 SOCK_RAW,
232 IPPROTO_ICMP,
233 0,
234 0,
235 0);
236
237 if (pInfo->icmpSock == INVALID_SOCKET)
238 {
239 INT err = WSAGetLastError();
240 DebugPrint(_T("Could not create socket : %d.\n"), err);
241
242 if (err == WSAEACCES)
243 {
244 _tprintf(_T("\n\nYou must have access to raw sockets (admin) to run this program!\n\n"));
245 }
246
247 return FALSE;
248 }
249
250 return TRUE;
251 }
252
253
254 static VOID
255 PreparePacket(PAPPINFO pInfo,
256 USHORT iSeqNum)
257 {
258 /* assemble ICMP echo request packet */
259 pInfo->SendPacket->icmpheader.type = ECHO_REQUEST;
260 pInfo->SendPacket->icmpheader.code = 0;
261 pInfo->SendPacket->icmpheader.checksum = 0;
262 pInfo->SendPacket->icmpheader.id = (USHORT)GetCurrentProcessId();
263 pInfo->SendPacket->icmpheader.seq = htons((USHORT)iSeqNum);
264
265 /* calculate checksum of packet */
266 pInfo->SendPacket->icmpheader.checksum = CheckSum((PUSHORT)&pInfo->SendPacket->icmpheader,
267 sizeof(ICMP_HEADER) + PACKET_SIZE);
268 }
269
270
271 static INT
272 SendPacket(PAPPINFO pInfo)
273 {
274 INT iSockRet;
275
276 DebugPrint(_T("\nsending packet of %d bytes... "), PACKET_SIZE);
277
278 /* get time packet was sent */
279 pInfo->lTimeStart = GetTime(pInfo);
280
281 iSockRet = sendto(pInfo->icmpSock, //socket
282 (char *)&pInfo->SendPacket->icmpheader,//buffer
283 sizeof(ICMP_HEADER) + PACKET_SIZE,//size of buffer
284 0, //flags
285 (SOCKADDR *)&pInfo->dest, //destination
286 sizeof(pInfo->dest)); //address length
287
288 if (iSockRet == SOCKET_ERROR)
289 {
290 if (WSAGetLastError() == WSAEACCES)
291 {
292 /* FIXME: Is this correct? */
293 _tprintf(_T("\n\nYou must be an administrator to run this program!\n\n"));
294 WSACleanup();
295 HeapFree(GetProcessHeap(), 0, pInfo);
296 exit(-1);
297 }
298 else
299 {
300 DebugPrint(_T("sendto failed %d\n"), WSAGetLastError());
301 }
302 }
303 else
304 {
305 DebugPrint(_T("sent %d bytes\n"), iSockRet);
306 }
307
308 return iSockRet;
309 }
310
311
312 static BOOL
313 ReceivePacket(PAPPINFO pInfo)
314 {
315 TIMEVAL timeVal;
316 FD_SET readFDS;
317 INT iSockRet = 0, iSelRet;
318 INT iFromLen;
319 BOOL bRet = FALSE;
320
321 iFromLen = sizeof(pInfo->source);
322
323 DebugPrint(_T("Receiving packet. Available buffer, %d bytes... "), MAX_PING_PACKET_SIZE);
324
325 /* monitor icmpSock for incomming connections */
326 FD_ZERO(&readFDS);
327 FD_SET(pInfo->icmpSock, &readFDS);
328
329 /* set timeout values */
330 timeVal.tv_sec = pInfo->iTimeOut / 1000;
331 timeVal.tv_usec = pInfo->iTimeOut % 1000;
332
333 iSelRet = select(0,
334 &readFDS,
335 NULL,
336 NULL,
337 &timeVal);
338
339 if (iSelRet == SOCKET_ERROR)
340 {
341 DebugPrint(_T("select() failed in sendPacket() %d\n"), WSAGetLastError());
342 }
343 else if (iSelRet == 0) /* if socket timed out */
344 {
345 _tprintf(_T(" * "));
346 }
347 else if ((iSelRet != SOCKET_ERROR) && (iSelRet != 0))
348 {
349 iSockRet = recvfrom(pInfo->icmpSock, // socket
350 (char *)pInfo->RecvPacket, // buffer
351 MAX_PING_PACKET_SIZE, // size of buffer
352 0, // flags
353 (SOCKADDR *)&pInfo->source, // source address
354 &iFromLen); // address length
355
356 if (iSockRet != SOCKET_ERROR)
357 {
358 /* get time packet was recieved */
359 pInfo->lTimeEnd = GetTime(pInfo);
360 DebugPrint(_T("reveived %d bytes\n"), iSockRet);
361 bRet = TRUE;
362 }
363 else
364 {
365 DebugPrint(_T("recvfrom failed: %d\n"), WSAGetLastError());
366 }
367 }
368
369 return bRet;
370 }
371
372
373 static INT
374 DecodeResponse(PAPPINFO pInfo)
375 {
376 unsigned short header_len = pInfo->RecvPacket->h_len * 4;
377
378 /* cast the recieved packet into an ECHO reply and a TTL Exceed and check the ID*/
379 ECHO_REPLY_HEADER *IcmpHdr = (ECHO_REPLY_HEADER *)((char*)pInfo->RecvPacket + header_len);
380
381 /* Make sure the reply is ok */
382 if (PACKET_SIZE < header_len + ICMP_MIN_SIZE)
383 {
384 DebugPrint(_T("too few bytes from %s\n"), inet_ntoa(pInfo->dest.sin_addr));
385 return -2;
386 }
387
388 switch (IcmpHdr->icmpheader.type)
389 {
390 case TTL_EXCEEDED :
391 _tprintf(_T("%3ld ms"), (ULONG)((pInfo->lTimeEnd - pInfo->lTimeStart) / pInfo->TicksPerMs.QuadPart));
392 return 0;
393
394 case ECHO_REPLY :
395 if (IcmpHdr->icmpheader.id != (USHORT)GetCurrentProcessId())
396 {
397 /* FIXME: our network stack shouldn't allow this... */
398 /* we've picked up a packet not related to this process probably from another local program. We ignore it */
399 DebugPrint(_T("Rouge packet: header id %d, process id %d"), IcmpHdr->icmpheader.id, GetCurrentProcessId());
400 return -1;
401 }
402 _tprintf(_T("%3ld ms"), (ULONG)((pInfo->lTimeEnd - pInfo->lTimeStart) / pInfo->TicksPerMs.QuadPart));
403 return 1;
404
405 case DEST_UNREACHABLE :
406 _tprintf(_T(" * "));
407 return 2;
408 }
409
410 return -3;
411 }
412
413
414 static BOOL
415 AllocateBuffers(PAPPINFO pInfo)
416 {
417 pInfo->SendPacket = (PECHO_REPLY_HEADER)HeapAlloc(GetProcessHeap(),
418 0,
419 sizeof(ECHO_REPLY_HEADER) + PACKET_SIZE);
420 if (!pInfo->SendPacket)
421 return FALSE;
422
423 pInfo->RecvPacket = (PIPv4_HEADER)HeapAlloc(GetProcessHeap(),
424 0,
425 sizeof(IPv4_HEADER) + PACKET_SIZE);
426 if (!pInfo->RecvPacket)
427 {
428 HeapFree(GetProcessHeap(),
429 0,
430 pInfo->SendPacket);
431
432 return FALSE;
433 }
434
435 return TRUE;
436 }
437
438
439 static INT
440 Driver(PAPPINFO pInfo)
441 {
442 INT iHopCount = 1; // hop counter. default max is 30
443 BOOL bFoundTarget = FALSE; // Have we reached our destination yet
444 INT iRecieveReturn; // RecieveReturn return value
445 PECHO_REPLY_HEADER icmphdr;
446 INT iTTL = 1;
447
448 INT ret = -1;
449
450 //temps for getting host name
451 CHAR cHost[256];
452 CHAR cServ[256];
453 CHAR *ip;
454
455 SetupTimingMethod(pInfo);
456
457 if (AllocateBuffers(pInfo) &&
458 ResolveHostname(pInfo) &&
459 CreateSocket(pInfo))
460 {
461 /* print tracing info to screen */
462 _tprintf(_T("\nTracing route to %s [%s]\n"), cHostname, cDestIP);
463 _tprintf(_T("over a maximum of %d hop"), pInfo->iMaxHops);
464 pInfo->iMaxHops > 1 ? _tprintf(_T("s:\n\n")) : _tprintf(_T(":\n\n"));
465
466 /* run until we hit either max hops, or find the target */
467 while ((iHopCount <= pInfo->iMaxHops) &&
468 (bFoundTarget != TRUE))
469 {
470 USHORT iSeqNum = 0;
471 INT i;
472
473 _tprintf(_T("%3d "), iHopCount);
474
475 /* run 3 pings for each hop */
476 for (i = 0; i < 3; i++)
477 {
478 if (SetTTL(pInfo->icmpSock, iTTL) != TRUE)
479 {
480 DebugPrint(_T("error in Setup()\n"));
481 return ret;
482 }
483
484 PreparePacket(pInfo, iSeqNum);
485
486 if (SendPacket(pInfo) != SOCKET_ERROR)
487 {
488 BOOL bAwaitPacket = FALSE; // indicates whether we have recieved a good packet
489
490 do
491 {
492 /* Receive replies until we get a successful read, or a fatal error */
493 if ((iRecieveReturn = ReceivePacket(pInfo)) < 0)
494 {
495 /* FIXME: consider moving this into RecievePacket */
496 /* check the seq num in the packet, if it's bad wait for another */
497 WORD hdrLen = pInfo->RecvPacket->h_len * 4;
498 icmphdr = (ECHO_REPLY_HEADER *)((char*)&pInfo->RecvPacket + hdrLen);
499 if (icmphdr->icmpheader.seq != iSeqNum)
500 {
501 _tprintf(_T("bad sequence number!\n"));
502 continue;
503 }
504 }
505
506 if (iRecieveReturn)
507 {
508 if (DecodeResponse(pInfo) < 0)
509 bAwaitPacket = TRUE;
510 }
511
512 } while (bAwaitPacket);
513 }
514
515 iSeqNum++;
516 _tprintf(_T(" "));
517 }
518
519 if(pInfo->bResolveAddresses)
520 {
521 INT iNameInfoRet; // getnameinfo return value
522 /* gethostbyaddr() and getnameinfo() are
523 * unimplemented in ROS at present.
524 * Alex has advised he will be implementing getnameinfo.
525 * I've used that for the time being for testing in Windows*/
526
527 //ip = inet_addr(inet_ntoa(source.sin_addr));
528 //host = gethostbyaddr((char *)&ip, 4, 0);
529
530 ip = inet_ntoa(pInfo->source.sin_addr);
531
532 iNameInfoRet = getnameinfo((SOCKADDR *)&pInfo->source,
533 sizeof(SOCKADDR),
534 cHost,
535 256,
536 cServ,
537 256,
538 NI_NUMERICSERV);
539 if (iNameInfoRet == 0)
540 {
541 /* if IP address resolved to a hostname,
542 * print the IP address after it */
543 if (lstrcmpA(cHost, ip) != 0)
544 _tprintf(_T("%s [%s]"), cHost, ip);
545 else
546 _tprintf(_T("%s"), cHost);
547 }
548 else
549 {
550 DebugPrint(_T("error: %d"), WSAGetLastError());
551 DebugPrint(_T(" getnameinfo failed: %d"), iNameInfoRet);
552 _tprintf(_T("%s"), inet_ntoa(pInfo->source.sin_addr));
553 }
554
555 }
556 else
557 _tprintf(_T("%s"), inet_ntoa(pInfo->source.sin_addr));
558
559 _tprintf(_T("\n"));
560
561 /* check if we've arrived at the target */
562 if (strcmp(cDestIP, inet_ntoa(pInfo->source.sin_addr)) == 0)
563 bFoundTarget = TRUE;
564 else
565 {
566 iTTL++;
567 iHopCount++;
568 Sleep(500);
569 }
570 }
571 _tprintf(_T("\nTrace complete.\n"));
572 ret = 0;
573 }
574
575 return ret;
576 }
577
578
579 static VOID
580 Cleanup(PAPPINFO pInfo)
581 {
582 if (pInfo->icmpSock)
583 closesocket(pInfo->icmpSock);
584
585 WSACleanup();
586
587 if (pInfo->SendPacket)
588 HeapFree(GetProcessHeap(),
589 0,
590 pInfo->SendPacket);
591
592 if (pInfo->RecvPacket)
593 HeapFree(GetProcessHeap(),
594 0,
595 pInfo->RecvPacket);
596 }
597
598
599 #if defined(_UNICODE) && defined(__GNUC__)
600 static
601 #endif
602 int _tmain(int argc, LPCTSTR argv[])
603 {
604 PAPPINFO pInfo;
605 WSADATA wsaData;
606 int ret = -1;
607
608 pInfo = (PAPPINFO)HeapAlloc(GetProcessHeap(),
609 HEAP_ZERO_MEMORY,
610 sizeof(APPINFO));
611 if (pInfo)
612 {
613 pInfo->bResolveAddresses = TRUE;
614 pInfo->iMaxHops = 30;
615 pInfo->iTimeOut = 1000;
616
617 if (ParseCmdline(argc, argv, pInfo))
618 {
619 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
620 {
621 DebugPrint(_T("WSAStartup failed.\n"));
622 }
623 else
624 {
625 ret = Driver(pInfo);
626 Cleanup(pInfo);
627 }
628 }
629
630 HeapFree(GetProcessHeap(),
631 0,
632 pInfo);
633 }
634
635 return ret;
636 }
637
638
639 #if defined(_UNICODE) && defined(__GNUC__)
640 /* HACK - MINGW HAS NO OFFICIAL SUPPORT FOR wmain()!!! */
641 int main( int argc, char **argv )
642 {
643 WCHAR **argvW;
644 int i, j, Ret = 1;
645
646 if ((argvW = malloc(argc * sizeof(WCHAR*))))
647 {
648 /* convert the arguments */
649 for (i = 0, j = 0; i < argc; i++)
650 {
651 if (!(argvW[i] = malloc((strlen(argv[i]) + 1) * sizeof(WCHAR))))
652 {
653 j++;
654 }
655 swprintf(argvW[i], L"%hs", argv[i]);
656 }
657
658 if (j == 0)
659 {
660 /* no error converting the parameters, call wmain() */
661 Ret = wmain(argc, (LPCTSTR *)argvW);
662 }
663
664 /* free the arguments */
665 for (i = 0; i < argc; i++)
666 {
667 if (argvW[i])
668 free(argvW[i]);
669 }
670 free(argvW);
671 }
672
673 return Ret;
674 }
675 #endif