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