Fix a few msvc warnings
[reactos.git] / reactos / apps / utils / net / tracert / tracert.c
1 /*
2 * ReactOS Win32 Applications
3 * Copyright (C) 2005 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS traceroute utility
22 * FILE: apps/utils/net/tracert/tracert.c
23 * PURPOSE: trace a packets route through a network
24 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
25 * REVISIONS:
26 * GM 03/05/05 Created
27 *
28 */
29
30
31 #include <winsock2.h>
32 #include <tchar.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ws2tcpip.h>
36 #include <string.h>
37 #include <time.h>
38 #include "tracert.h"
39
40 #define WIN32_LEAN_AND_MEAN
41
42 #ifdef DBG
43 #undef DBG
44 #endif
45
46 /*
47 * globals
48 */
49 SOCKET icmpSock; // socket descriptor
50 SOCKADDR_IN source, dest; // source and destination address info
51 ECHO_REPLY_HEADER sendpacket; // ICMP echo packet
52 IPv4_HEADER recvpacket; // return reveive packet
53
54 BOOL bUsePerformanceCounter; // whether to use the high res performance counter
55 LARGE_INTEGER TicksPerMs; // number of millisecs in relation to proc freq
56 LARGE_INTEGER TicksPerUs; // number of microsecs in relation to proc freq
57 LONGLONG lTimeStart; // send packet, timer start
58 LONGLONG lTimeEnd; // receive packet, timer end
59
60 CHAR cHostname[256]; // target hostname
61 CHAR cDestIP[18]; // target IP
62
63
64 /*
65 * command line options
66 */
67 BOOL bResolveAddresses = TRUE; // -d MS ping defaults to true.
68 INT iMaxHops = 30; // -h Max number of hops before trace ends
69 INT iHostList; // -j @UNIMPLEMENTED@
70 INT iTimeOut = 2000; // -w time before packet times out
71
72
73 /*
74 *
75 * Parse command line parameters and set any options
76 *
77 */
78 static BOOL ParseCmdline(int argc, char* argv[])
79 {
80 int i;
81
82 if (argc < 2)
83 {
84 Usage();
85 return FALSE;
86 }
87
88 for (i = 1; i < argc; i++)
89 {
90 if (argv[i][0] == '-')
91 {
92 switch (argv[i][1])
93 {
94 case 'd': bResolveAddresses = FALSE;
95 break;
96 case 'h': sscanf(argv[i+1], "%d", &iMaxHops);
97 break;
98 case 'j': break; /* @unimplemented@ */
99 case 'w': sscanf(argv[i+1], "%d", &iTimeOut);
100 break;
101 default:
102 _tprintf(_T("%s is not a valid option.\n"), argv[i]);
103 Usage();
104 return FALSE;
105 }
106 }
107 else
108 /* copy target address */
109 strncpy(cHostname, argv[i], 255);
110 }
111
112 return TRUE;
113 }
114
115
116
117 /*
118 *
119 * Driver function, controls the traceroute program
120 *
121 */
122 static INT Driver(VOID)
123 {
124
125 INT iHopCount = 1; // hop counter. default max is 30
126 USHORT iSeqNum = 0; // initialise packet sequence number
127 INT iTTL = 1; // set initial packet TTL to 1
128 BOOL bFoundTarget = FALSE; // Have we reached our destination yet
129 BOOL bAwaitPacket; // indicates whether we have recieved a good packet
130 INT iDecRes; // DecodeResponse return value
131 INT iRecieveReturn; // RecieveReturn return value
132 INT iNameInfoRet; // getnameinfo return value
133 INT iPacketSize = PACKET_SIZE; // packet size
134 WORD wHeaderLen; // header length
135 PECHO_REPLY_HEADER icmphdr;
136
137
138 //temps for getting host name
139 CHAR cHost[256];
140 CHAR cServ[256];
141 CHAR *ip;
142
143 /* setup winsock */
144 WSADATA wsaData;
145
146 /* check for winsock 2 */
147 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
148 {
149 #ifdef DBG
150 _tprintf(_T("WSAStartup failed.\n"));
151 #endif /* DBG */
152 exit(1);
153 }
154
155 /* establish what timing method we can use */
156 SetupTimingMethod();
157
158 /* setup target info */
159 ResolveHostname();
160
161 /* print standard tracing info to screen */
162 _tprintf(_T("\nTracing route to %s [%s]\n"), cHostname, cDestIP);
163 _tprintf(_T("over a maximum of %d hop"), iMaxHops);
164 iMaxHops > 1 ? _tprintf(_T("s:\n\n")) : _tprintf(_T(":\n\n"));
165
166 /* run until we hit either max hops, or we recieve 3 echo replys */
167 while ((iHopCount <= iMaxHops) && (bFoundTarget != TRUE))
168 {
169 INT i;
170
171 _tprintf(_T("%3d "), iHopCount);
172 /* run 3 pings for each hop */
173 for (i=0; i<3; i++)
174 {
175 if (Setup(iTTL) != TRUE)
176 {
177 #ifdef DBG
178 _tprintf(_T("error in Setup()\n"));
179 #endif /* DBG */
180 WSACleanup();
181 exit(1);
182 }
183 PreparePacket(iPacketSize, iSeqNum);
184 if (SendPacket(iPacketSize) != SOCKET_ERROR)
185 {
186 /* loop until we get a good packet */
187 bAwaitPacket = TRUE;
188 while (bAwaitPacket)
189 {
190 /* Receive replies until we either get a successful
191 * read, or a fatal error occurs. */
192 if ((iRecieveReturn = ReceivePacket(iPacketSize)) < 0)
193 {
194 /* check the sequence number in the packet
195 * if it's bad, complain and wait for another packet
196 * , otherwise break */
197 wHeaderLen = recvpacket.h_len * 4;
198 icmphdr = (ECHO_REPLY_HEADER *)((char*)&recvpacket + wHeaderLen);
199 if (icmphdr->icmpheader.seq != iSeqNum)
200 {
201 _tprintf(_T("bad sequence number!\n"));
202 continue;
203 }
204 else
205 break;
206 }
207
208 /* if RecievePacket timed out we don't bother decoding */
209 if (iRecieveReturn != 1)
210 {
211 iDecRes = DecodeResponse(iPacketSize, iSeqNum);
212
213 switch (iDecRes)
214 {
215 case 0 : bAwaitPacket = FALSE; /* time exceeded */
216 break;
217 case 1 : bAwaitPacket = FALSE; /* echo reply */
218 break;
219 case 2 : bAwaitPacket = FALSE; /* destination unreachable */
220 break;
221 #ifdef DBG
222 case -1 :
223 _tprintf(_T("recieved foreign packet\n"));
224 break;
225 case -2 :
226 _tprintf(_T("error in DecodeResponse\n"));
227 break;
228 case -3 :
229 _tprintf(_T("unknown ICMP packet\n"));
230 break;
231 #endif /* DBG */
232 default : break;
233 }
234 }
235 else
236 /* packet timed out. Don't wait for it again */
237 bAwaitPacket = FALSE;
238 }
239 }
240
241 iSeqNum++;
242 _tprintf(_T(" "));
243 }
244
245 if(bResolveAddresses)
246 {
247 /* gethostbyaddr() and getnameinfo() are
248 * unimplemented in ROS at present.
249 * Alex has advised he will be implementing getnameinfo.
250 * I've used that for the time being for testing in Windows*/
251
252 //ip = inet_addr(inet_ntoa(source.sin_addr));
253 //host = gethostbyaddr((char *)&ip, 4, 0);
254
255 ip = inet_ntoa(source.sin_addr);
256
257 iNameInfoRet = getnameinfo((SOCKADDR *)&source,
258 sizeof(SOCKADDR),
259 cHost,
260 256,
261 cServ,
262 256,
263 NI_NUMERICSERV);
264 if (iNameInfoRet == 0)
265 {
266 /* if IP address resolved to a hostname,
267 * print the IP address after it */
268 if (lstrcmpA(cHost, ip) != 0)
269 _tprintf(_T("%s [%s]"), cHost, ip);
270 else
271 _tprintf(_T("%s"), cHost);
272 }
273 else
274 {
275 _tprintf(_T("error: %d"), WSAGetLastError());
276 #ifdef DBG
277 _tprintf(_T(" getnameinfo failed: %d"), iNameInfoRet);
278 #endif /* DBG */
279 }
280
281 }
282 else
283 _tprintf(_T("%s"), inet_ntoa(source.sin_addr));
284
285 _tprintf(_T("\n"));
286
287 /* check if we've arrived at the target */
288 if (strcmp(cDestIP, inet_ntoa(source.sin_addr)) == 0)
289 bFoundTarget = TRUE;
290 else
291 {
292 iTTL++;
293 iHopCount++;
294 Sleep(500);
295 }
296 }
297 _tprintf(_T("\nTrace complete.\n"));
298 WSACleanup();
299
300 return 0;
301 }
302
303
304 /*
305 * Establish if performance counters are available and
306 * set up timing figures in relation to processor frequency.
307 * If performance counters are not available, we'll be using
308 * gettickcount, so set the figures to 1
309 *
310 */
311 static VOID SetupTimingMethod(VOID)
312 {
313 LARGE_INTEGER PerformanceCounterFrequency;
314
315 /* check if performance counters are available */
316 bUsePerformanceCounter = QueryPerformanceFrequency(&PerformanceCounterFrequency);
317 if (bUsePerformanceCounter)
318 {
319 /* restrict execution to first processor on SMP systems */
320 if (SetThreadAffinityMask(GetCurrentThread(), 1) == 0)
321 bUsePerformanceCounter = FALSE;
322
323 TicksPerMs.QuadPart = PerformanceCounterFrequency.QuadPart / 1000;
324 TicksPerUs.QuadPart = PerformanceCounterFrequency.QuadPart / 1000000;
325 }
326
327 if (!bUsePerformanceCounter)
328 {
329 TicksPerMs.QuadPart = 1;
330 TicksPerUs.QuadPart = 1;
331 }
332 }
333
334
335 /*
336 *
337 * Check for a hostname or dotted deciamal for our target.
338 * If we have a hostname, resolve to an IP and store it, else
339 * just store the target IP address. Also set up other key
340 * SOCKADDR_IN members needed for the connection.
341 *
342 */
343 static VOID ResolveHostname(VOID)
344 {
345 HOSTENT *hp;
346 ULONG addr;
347
348 memset(&dest, 0, sizeof(dest));
349
350 addr = inet_addr(cHostname);
351 /* if address is not a dotted decimal */
352 if (addr == INADDR_NONE)
353 {
354 hp = gethostbyname(cHostname);
355 if (hp != 0)
356 {
357 memcpy(&dest.sin_addr, hp->h_addr, hp->h_length);
358 //dest.sin_addr = *((struct in_addr *)hp->h_addr);
359 dest.sin_family = hp->h_addrtype;
360 }
361 else
362 {
363 _tprintf(_T("Unable to resolve target system name %s.\n"), cHostname);
364 WSACleanup();
365 exit(1);
366 }
367 }
368 else
369 {
370 dest.sin_addr.s_addr = addr;
371 dest.sin_family = AF_INET;
372 }
373 /* copy destination IP address into a string */
374 strcpy(cDestIP, inet_ntoa(dest.sin_addr));
375 }
376
377
378
379 /*
380 *
381 * Create our socket which will be used for sending and recieving,
382 * Socket Type is raw, Protocol is ICMP. Also set the TTL value which will be
383 * set in the outgoing IP packet.
384 *
385 */
386 static INT Setup(INT iTTL)
387 {
388 INT iSockRet;
389
390 /* create raw socket */
391 icmpSock = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, 0, 0);
392 if (icmpSock == INVALID_SOCKET)
393 {
394 _tprintf(_T("Could not create socket : %d.\n"), WSAGetLastError());
395 if (WSAGetLastError() == WSAEACCES)
396 {
397 _tprintf(_T("\n\nYou must be an administrator to run this program!\n\n"));
398 WSACleanup();
399 exit(1);
400 }
401 return FALSE;
402 }
403
404 /* setup for TTL */
405 iSockRet = setsockopt(icmpSock, IPPROTO_IP, IP_TTL, (const char *)&iTTL, sizeof(iTTL));
406 if (iSockRet == SOCKET_ERROR)
407 {
408 _tprintf(_T("TTL setsockopt failed : %d. \n"), WSAGetLastError());
409 return FALSE;
410 }
411
412 return TRUE;
413 }
414
415
416
417 /*
418 * Prepare the ICMP echo request packet for sending.
419 * Calculate the packet checksum
420 *
421 */
422 static VOID PreparePacket(INT iPacketSize, USHORT iSeqNum)
423 {
424 /* assemble ICMP echo request packet */
425 sendpacket.icmpheader.type = ECHO_REQUEST;
426 sendpacket.icmpheader.code = 0;
427 sendpacket.icmpheader.checksum = 0;
428 sendpacket.icmpheader.id = (USHORT)GetCurrentProcessId();
429 sendpacket.icmpheader.seq = iSeqNum;
430
431 /* calculate checksum of packet */
432 sendpacket.icmpheader.checksum = CheckSum((PUSHORT)&sendpacket, sizeof(ICMP_HEADER) + iPacketSize);
433 }
434
435
436
437 /*
438 *
439 * Get the system time and send the ICMP packet to the destination
440 * address.
441 *
442 */
443 static INT SendPacket(INT datasize)
444 {
445 INT iSockRet;
446 INT iPacketSize;
447
448 iPacketSize = sizeof(ECHO_REPLY_HEADER) + datasize;
449
450 #ifdef DBG
451 _tprintf(_T("\nsending packet of %d bytes\n"), iPacketSize);
452 #endif /* DBG */
453
454 /* get time packet was sent */
455 lTimeStart = GetTime();
456
457 iSockRet = sendto(icmpSock, //socket
458 (char *)&sendpacket, //buffer
459 iPacketSize, //size of buffer
460 0, //flags
461 (SOCKADDR *)&dest, //destination
462 sizeof(dest)); //address length
463
464 if (iSockRet == SOCKET_ERROR)
465 {
466 if (WSAGetLastError() == WSAEACCES)
467 {
468 _tprintf(_T("\n\nYou must be an administrator to run this program!\n\n"));
469 WSACleanup();
470 exit(1);
471 }
472 else
473 {
474 #ifdef DBG
475 _tprintf(_T("sendto failed %d\n"), WSAGetLastError());
476 #endif /* DBG */
477 return FALSE;
478 }
479 }
480 #ifdef DBG
481 _tprintf(_T("sent %d bytes\n"), iSockRet);
482 #endif /* DBG */
483
484 /* return number of bytes sent */
485 return iSockRet;
486 }
487
488
489
490 /*
491 *
492 * Set up a timeout value and put the socket in a select poll.
493 * Wait until we recieve an IPv4 reply packet in reply to the ICMP
494 * echo request packet and get the time the packet was recieved.
495 * If we don't recieve a packet, do some checking to establish why.
496 *
497 */
498 static INT ReceivePacket(INT datasize)
499 {
500 TIMEVAL timeVal;
501 FD_SET readFDS;
502 int iSockRet = 0, iSelRet;
503 int iFromLen;
504 int iPacketSize;
505
506 /* allow for a larger recv buffer to store ICMP TTL
507 * exceed, IP header and orginal ICMP request */
508 iPacketSize = MAX_REC_SIZE + datasize;
509
510 iFromLen = sizeof(source);
511
512 #ifdef DBG
513 _tprintf(_T("receiving packet. Available buffer, %d bytes\n"), iPacketSize);
514 #endif /* DBG */
515
516 /* monitor icmpSock for incomming connections */
517 FD_ZERO(&readFDS);
518 FD_SET(icmpSock, &readFDS);
519
520 /* set timeout values */
521 timeVal.tv_sec = iTimeOut / 1000;
522 timeVal.tv_usec = iTimeOut % 1000;
523
524 iSelRet = select(0, &readFDS, NULL, NULL, &timeVal);
525
526 if ((iSelRet != SOCKET_ERROR) && (iSelRet != 0))
527 {
528 iSockRet = recvfrom(icmpSock, //socket
529 (char *)&recvpacket, //buffer
530 iPacketSize, //size of buffer
531 0, //flags
532 (SOCKADDR *)&source, //source address
533 &iFromLen); //pointer to address length
534 /* get time packet was recieved */
535 lTimeEnd = GetTime();
536 /* if socket timed out */
537 }
538 else if (iSelRet == 0)
539 {
540 _tprintf(_T(" * "));
541 return 1;
542 }
543 else if (iSelRet == SOCKET_ERROR)
544 {
545 _tprintf(_T("select() failed in sendPacket() %d\n"), WSAGetLastError());
546 return -1;
547 }
548
549
550 if (iSockRet == SOCKET_ERROR)
551 {
552 _tprintf(_T("recvfrom failed: %d\n"), WSAGetLastError());
553 return -2;
554 }
555 #ifdef DBG
556 else
557 _tprintf(_T("reveived %d bytes\n"), iSockRet);
558 #endif /* DBG */
559
560 return 0;
561 }
562
563
564
565 /*
566 *
567 * Cast the IPv4 packet to an echo reply and to a TTL exceed.
568 * Check the 'type' field to establish what was recieved, and
569 * ensure the packet is related to the originating process.
570 * It all is well, print the time taken for the round trip.
571 *
572 */
573 static INT DecodeResponse(INT iPacketSize, USHORT iSeqNum)
574 {
575 unsigned short header_len = recvpacket.h_len * 4;
576 /* cast the recieved packet into an ECHO reply and a TTL Exceed so we can check the ID*/
577 ECHO_REPLY_HEADER *IcmpHdr = (ECHO_REPLY_HEADER *)((char*)&recvpacket + header_len);
578 TTL_EXCEED_HEADER *TTLExceedHdr = (TTL_EXCEED_HEADER *)((char *)&recvpacket + header_len);
579
580 /* Make sure the reply is ok */
581 if (iPacketSize < header_len + ICMP_MIN_SIZE)
582 {
583 _tprintf(_T("too few bytes from %s\n"), inet_ntoa(dest.sin_addr));
584 return -2;
585 }
586
587 switch (IcmpHdr->icmpheader.type)
588 {
589 case TTL_EXCEEDED :
590 if (TTLExceedHdr->OrigIcmpHeader.id != (USHORT)GetCurrentProcessId())
591 {
592 /* FIXME */
593 /* we've picked up a packet not related to this process
594 * probably from another local program. We ignore it */
595 #ifdef DGB
596 _tprintf(_T("header id, process id %d"), TTLExceedHdr->OrigIcmpHeader.id, GetCurrentProcessId());
597 #endif /* DBG */
598 //_tprintf(_T("oops ");
599 return -1;
600 }
601 _tprintf(_T("%3Ld ms"), (lTimeEnd - lTimeStart) / TicksPerMs.QuadPart);
602 return 0;
603 case ECHO_REPLY :
604 if (IcmpHdr->icmpheader.id != (USHORT)GetCurrentProcessId())
605 {
606 /* FIXME */
607 /* we've picked up a packet not related to this process
608 * probably from another local program. We ignore it */
609 #ifdef DGB
610 _tprintf(_T("\nPicked up wrong packet. icmpheader.id = %d and process id = %d"), IcmpHdr->icmpheader.id, GetCurrentProcessId());
611 #endif /* DBG */
612 //_tprintf(_T("oops ");
613 return -1;
614 }
615 _tprintf(_T("%3Ld ms"), (lTimeEnd - lTimeStart) / TicksPerMs.QuadPart);
616 return 1;
617 case DEST_UNREACHABLE :
618 _tprintf(_T(" * "));
619 return 2;
620 default :
621 /* unknown ICMP packet */
622 return -3;
623 }
624 }
625
626
627 /*
628 *
629 * Get the system time using preformance counters if available,
630 * otherwise fall back to GetTickCount()
631 *
632 */
633
634 static LONGLONG GetTime(VOID)
635 {
636 LARGE_INTEGER Time;
637
638 if (bUsePerformanceCounter)
639 {
640 if (QueryPerformanceCounter(&Time) == 0)
641 {
642 Time.u.LowPart = (DWORD)GetTickCount();
643 Time.u.HighPart = 0;
644 return (LONGLONG)Time.u.LowPart;
645 }
646 }
647 else
648 {
649 Time.u.LowPart = (DWORD)GetTickCount();
650 Time.u.HighPart = 0;
651 return (LONGLONG)Time.u.LowPart;
652 }
653 return Time.QuadPart;
654 }
655
656
657 /*
658 *
659 * Calculate packet checksum.
660 *
661 */
662 static WORD CheckSum(PUSHORT data, UINT size)
663 {
664 DWORD dwSum = 0;
665
666 while (size > 1)
667 {
668 dwSum += *data++;
669 size -= sizeof(USHORT);
670 }
671
672 if (size)
673 dwSum += *(UCHAR*)data;
674
675 dwSum = (dwSum >> 16) + (dwSum & 0xFFFF);
676 dwSum += (dwSum >> 16);
677
678 return (USHORT)(~dwSum);
679 }
680
681
682 /*
683 *
684 * print program usage to screen
685 *
686 */
687 static VOID Usage(VOID)
688 {
689 _tprintf(_T("\nUsage: tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] target_name\n\n"
690 "Options:\n"
691 " -d Do not resolve addresses to hostnames.\n"
692 " -h maximum_hops Maximum number of hops to search for target.\n"
693 " -j host-list Loose source route along host-list.\n"
694 " -w timeout Wait timeout milliseconds for each reply.\n\n"));
695
696 /* temp notes to stop user questions until getnameinfo/gethostbyaddr and getsockopt are implemented */
697 _tprintf(_T("NOTES\n-----\n"
698 "- Setting TTL values is not currently supported in ReactOS, so the trace will\n"
699 " jump straight to the destination. This feature will be implemented soon.\n"
700 "- Host info is not currently available in ReactOS and will fail with strange\n"
701 " results. Use -d to force it not to resolve IP's.\n"
702 "- For testing purposes, all should work as normal in a Windows environment\n\n"));
703 }
704
705
706
707 /*
708 *
709 * Program entry point
710 *
711 */
712 int main(int argc, char* argv[])
713 {
714 if (!ParseCmdline(argc, argv)) return -1;
715
716 Driver();
717
718 return 0;
719 }