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