ReactOS  0.4.15-dev-1070-ge1a01de
tracert.cpp File Reference
#include <winsock2.h>
#include <Windows.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <strsafe.h>
#include "resource.h"
Include dependency graph for tracert.cpp:

Go to the source code of this file.

Classes

struct  TraceInfo
 

Macros

#define SIZEOF_ICMP_ERROR   8
 
#define SIZEOF_IO_STATUS_BLOCK   8
 
#define PACKET_SIZE   32
 
#define MAX_IPADDRESS   32
 
#define NUM_OF_PINGS   3
 

Functions

static INT LengthOfStrResource (_In_ HINSTANCE hInst, _In_ UINT uID)
 
static INT AllocAndLoadString (_In_ UINT uID, _Out_ LPWSTR *lpTarget)
 
static INT OutputText (_In_ UINT uID,...)
 
static VOID Usage ()
 
static ULONG GetULONG (_In_z_ LPWSTR String)
 
static bool ResolveTarget ()
 
static bool PrintHopInfo (_In_ PVOID Buffer)
 
static bool DecodeResponse (_In_ PVOID ReplyBuffer, _In_ bool OutputHopAddress, _Out_ bool &FoundTarget)
 
static bool RunTraceRoute ()
 
static bool ParseCmdline (int argc, wchar_t *argv[])
 
EXTERN_C int wmain (int argc, wchar_t *argv[])
 

Variables

struct TraceInfo Info = { 0 }
 

Macro Definition Documentation

◆ MAX_IPADDRESS

#define MAX_IPADDRESS   32

Definition at line 34 of file tracert.cpp.

◆ NUM_OF_PINGS

#define NUM_OF_PINGS   3

Definition at line 35 of file tracert.cpp.

◆ PACKET_SIZE

#define PACKET_SIZE   32

Definition at line 33 of file tracert.cpp.

◆ SIZEOF_ICMP_ERROR

#define SIZEOF_ICMP_ERROR   8

Definition at line 31 of file tracert.cpp.

◆ SIZEOF_IO_STATUS_BLOCK

#define SIZEOF_IO_STATUS_BLOCK   8

Definition at line 32 of file tracert.cpp.

Function Documentation

◆ AllocAndLoadString()

static INT AllocAndLoadString ( _In_ UINT  uID,
_Out_ LPWSTR lpTarget 
)
static

Definition at line 86 of file tracert.cpp.

90 {
91  HMODULE hInst;
92  INT Length;
93 
96  if (Length++ > 0)
97  {
98  (*lpTarget) = (LPWSTR)LocalAlloc(LMEM_FIXED,
99  Length * sizeof(WCHAR));
100  if ((*lpTarget) != NULL)
101  {
102  INT Ret;
103  if (!(Ret = LoadStringW(hInst, uID, *lpTarget, Length)))
104  {
105  LocalFree((HLOCAL)(*lpTarget));
106  }
107  return Ret;
108  }
109  }
110  return 0;
111 }
static INT LengthOfStrResource(_In_ HINSTANCE hInst, _In_ UINT uID)
Definition: tracert.cpp:56
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
int32_t INT
Definition: typedefs.h:58
smooth NULL
Definition: ftsmooth.c:416
#define LMEM_FIXED
Definition: winbase.h:349
__wchar_t WCHAR
Definition: xmlstorage.h:180
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
HINSTANCE hInst
Definition: dxdiag.c:13
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
WCHAR * LPWSTR
Definition: xmlstorage.h:184

Referenced by OutputText().

◆ DecodeResponse()

static bool DecodeResponse ( _In_ PVOID  ReplyBuffer,
_In_ bool  OutputHopAddress,
_Out_ bool FoundTarget 
)
static

Definition at line 279 of file tracert.cpp.

284 {
285  ULONG RoundTripTime;
286  PVOID AddressInfo;
287  ULONG Status;
288 
289  if (Info.Family == AF_INET6)
290  {
291  PICMPV6_ECHO_REPLY EchoReplyV6;
292  EchoReplyV6 = (PICMPV6_ECHO_REPLY)ReplyBuffer;
293  Status = EchoReplyV6->Status;
294  RoundTripTime = EchoReplyV6->RoundTripTime;
295  AddressInfo = &EchoReplyV6->Address;
296  }
297  else
298  {
299 #ifdef _WIN64
300  PICMP_ECHO_REPLY32 EchoReplyV4;
301  EchoReplyV4 = (PICMP_ECHO_REPLY32)ReplyBuffer;
302 #else
303  PICMP_ECHO_REPLY EchoReplyV4;
304  EchoReplyV4 = (PICMP_ECHO_REPLY)ReplyBuffer;
305 #endif
306  Status = EchoReplyV4->Status;
307  RoundTripTime = EchoReplyV4->RoundTripTime;
308  AddressInfo = &EchoReplyV4->Address;
309  }
310 
311  switch (Status)
312  {
313  case IP_SUCCESS:
315  if (RoundTripTime)
316  {
317  OutputText(IDS_HOP_TIME, RoundTripTime);
318  }
319  else
320  {
322  }
323  break;
324 
327  FoundTarget = true;
328  PrintHopInfo(AddressInfo);
331  {
333  }
334  else if (Status == IP_DEST_NET_UNREACHABLE)
335  {
337  }
338  return true;
339 
340  case IP_REQ_TIMED_OUT:
342  break;
343 
344  case IP_GENERAL_FAILURE:
346  return false;
347 
348  default:
350  return false;
351  }
352 
353  if (OutputHopAddress)
354  {
355  if (Status == IP_SUCCESS)
356  {
357  FoundTarget = true;
358  }
360  {
361  PrintHopInfo(AddressInfo);
363  }
364  else if (Status == IP_REQ_TIMED_OUT)
365  {
367  }
368  }
369 
370  return true;
371 }
UINT32 RoundTripTime
Definition: ipexport.h:86
struct icmp_echo_reply * PICMP_ECHO_REPLY
#define IDS_TIMEOUT
Definition: resource.h:14
#define IDS_DEST_NET_UNREACHABLE
Definition: resource.h:25
#define IP_TTL_EXPIRED_TRANSIT
Definition: ipexport.h:126
struct TraceInfo Info
IPAddr Address
Definition: ipexport.h:84
#define AF_INET6
Definition: winsock.h:369
#define IDS_LINEBREAK
Definition: resource.h:18
static INT OutputText(_In_ UINT uID,...)
Definition: tracert.cpp:115
#define IDS_HOP_TIME
Definition: resource.h:12
#define IDS_DEST_HOST_UNREACHABLE
Definition: resource.h:24
#define IDS_HOP_RESPONSE
Definition: resource.h:19
#define IP_GENERAL_FAILURE
Definition: ipexport.h:138
#define IP_REQ_TIMED_OUT
Definition: ipexport.h:123
ICMPV6_ECHO_REPLY_LH * PICMPV6_ECHO_REPLY
Definition: ipexport.h:195
#define IP_DEST_NET_UNREACHABLE
Definition: ipexport.h:115
_Must_inspect_result_ _In_ PFLT_PORT _In_ ULONG _Out_writes_bytes_opt_ ReplyLength PVOID ReplyBuffer
Definition: fltkernel.h:1903
Status
Definition: gdiplustypes.h:24
IPV6_ADDRESS_EX Address
Definition: ipexport.h:189
#define IDS_TRANSMIT_FAILED
Definition: resource.h:28
unsigned int RoundTripTime
Definition: ipexport.h:191
#define IDS_HOP_ZERO
Definition: resource.h:13
#define IDS_GEN_FAILURE
Definition: resource.h:8
UINT32 Status
Definition: ipexport.h:85
#define IP_SUCCESS
Definition: ipexport.h:113
unsigned int ULONG
Definition: retypes.h:1
#define IDS_REQ_TIMED_OUT
Definition: resource.h:17
#define IP_DEST_HOST_UNREACHABLE
Definition: ipexport.h:116
static bool PrintHopInfo(_In_ PVOID Buffer)
Definition: tracert.cpp:211

Referenced by RunTraceRoute().

◆ GetULONG()

static ULONG GetULONG ( _In_z_ LPWSTR  String)
static

Definition at line 159 of file tracert.cpp.

162 {
163  ULONG Length;
164  Length = wcslen(String);
165 
166  ULONG i = 0;
167  while ((i < Length) && ((String[i] < L'0') || (String[i] > L'9'))) i++;
168  if ((i >= Length) || ((String[i] < L'0') || (String[i] > L'9')))
169  {
170  return (ULONG)-1;
171  }
172 
173  LPWSTR StopString;
174  return wcstoul(&String[i], &StopString, 10);
175 }
static WCHAR String[]
Definition: stringtable.c:55
_Check_return_ unsigned long __cdecl wcstoul(_In_z_ const wchar_t *_Str, _Out_opt_ _Deref_post_z_ wchar_t **_EndPtr, _In_ int _Radix)
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
static const WCHAR L[]
Definition: oid.c:1250
unsigned int ULONG
Definition: retypes.h:1
WCHAR * LPWSTR
Definition: xmlstorage.h:184
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)

Referenced by ParseCmdline().

◆ LengthOfStrResource()

static INT LengthOfStrResource ( _In_ HINSTANCE  hInst,
_In_ UINT  uID 
)
static

Definition at line 56 of file tracert.cpp.

60 {
61  HRSRC hrSrc;
62  HGLOBAL hRes;
63  LPWSTR lpName, lpStr;
64 
65  if (hInst == NULL) return -1;
66 
67  lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
68 
69  if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
70  (hRes = LoadResource(hInst, hrSrc)) &&
71  (lpStr = (WCHAR*)LockResource(hRes)))
72  {
73  UINT x;
74  uID &= 0xF;
75  for (x = 0; x < uID; x++)
76  {
77  lpStr += (*lpStr) + 1;
78  }
79  return (int)(*lpStr);
80  }
81  return -1;
82 }
#define MAKEINTRESOURCE
Definition: winuser.h:591
_In_ LPCSTR lpName
Definition: winbase.h:2752
LPVOID WINAPI LockResource(HGLOBAL handle)
Definition: res.c:550
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
Definition: res.c:176
HGLOBAL WINAPI LoadResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:532
smooth NULL
Definition: ftsmooth.c:416
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define RT_STRING
Definition: pedump.c:368
HINSTANCE hInst
Definition: dxdiag.c:13
unsigned int UINT
Definition: ndis.h:50
WCHAR * LPWSTR
Definition: xmlstorage.h:184

Referenced by AllocAndLoadString().

◆ OutputText()

static INT OutputText ( _In_ UINT  uID,
  ... 
)
static

Definition at line 115 of file tracert.cpp.

118 {
119  LPWSTR Format;
120  DWORD Ret = 0;
121  va_list lArgs;
122 
123  if (AllocAndLoadString(uID, &Format) > 0)
124  {
125  va_start(lArgs, uID);
126 
127  LPWSTR Buffer;
129  Format,
130  0,
131  0,
132  (LPWSTR)&Buffer,
133  0,
134  &lArgs);
135  va_end(lArgs);
136 
137  if (Ret)
138  {
139  wprintf(Buffer);
140  LocalFree(Buffer);
141  }
143  }
144 
145  return Ret;
146 }
#define FORMAT_MESSAGE_FROM_STRING
Definition: winbase.h:402
#define wprintf(...)
Definition: whoami.c:18
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
#define va_end(ap)
Definition: acmsvcex.h:90
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:400
char * va_list
Definition: acmsvcex.h:78
Definition: bufpool.h:45
static INT AllocAndLoadString(_In_ UINT uID, _Out_ LPWSTR *lpTarget)
Definition: tracert.cpp:86
unsigned long DWORD
Definition: ntddk_ex.h:95
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define va_start(ap, A)
Definition: acmsvcex.h:91
WCHAR * LPWSTR
Definition: xmlstorage.h:184
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char * Format
Definition: acpixf.h:1216

Referenced by DecodeResponse(), ParseCmdline(), PrintHopInfo(), RunTraceRoute(), and Usage().

◆ ParseCmdline()

static bool ParseCmdline ( int  argc,
wchar_t argv[] 
)
static

Definition at line 506 of file tracert.cpp.

507 {
508  if (argc < 2)
509  {
510  Usage();
511  return false;
512  }
513 
514  for (int i = 1; i < argc; i++)
515  {
516  if (argv[i][0] == '-')
517  {
518  switch (argv[i][1])
519  {
520  case 'd':
521  Info.ResolveAddresses = FALSE;
522  break;
523 
524  case 'h':
525  Info.MaxHops = GetULONG(argv[++i]);
526  break;
527 
528  case 'j':
529  printf("-j is not yet implemented.\n");
530  return false;
531 
532  case 'w':
533  Info.Timeout = GetULONG(argv[++i]);
534  break;
535 
536  case '4':
537  Info.Family = AF_INET;
538  break;
539 
540  case '6':
541  Info.Family = AF_INET6;
542  break;
543 
544  default:
545  {
547  Usage();
548  return false;
549  }
550  }
551  }
552  else
553  {
554  StringCchCopyW(Info.HostName, NI_MAXHOST, argv[i]);
555  break;
556  }
557  }
558 
559  return true;
560 }
static int argc
Definition: ServiceArgs.c:12
#define argv
Definition: mplay32.c:18
struct TraceInfo Info
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define FALSE
Definition: types.h:117
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
#define AF_INET6
Definition: winsock.h:369
static INT OutputText(_In_ UINT uID,...)
Definition: tracert.cpp:115
#define NI_MAXHOST
Definition: ws2def.h:359
static VOID Usage()
Definition: tracert.cpp:153
#define AF_INET
Definition: tcpip.h:117
static ULONG GetULONG(_In_z_ LPWSTR String)
Definition: tracert.cpp:159
#define IDS_INVALID_OPTION
Definition: resource.h:4
#define printf
Definition: config.h:203

Referenced by wmain().

◆ PrintHopInfo()

static bool PrintHopInfo ( _In_ PVOID  Buffer)
static

Definition at line 211 of file tracert.cpp.

212 {
213  SOCKADDR_IN6 SockAddrIn6 = { 0 };
214  SOCKADDR_IN SockAddrIn = { 0 };
215  PSOCKADDR SockAddr;
216  socklen_t Size;
217 
218  if (Info.Family == AF_INET6)
219  {
221  SockAddrIn6.sin6_family = AF_INET6;
222  CopyMemory(SockAddrIn6.sin6_addr.u.Word, Ipv6Addr->sin6_addr, sizeof(SockAddrIn6.sin6_addr));
223  //SockAddrIn6.sin6_addr = Ipv6Addr->sin6_addr;
224  SockAddr = (PSOCKADDR)&SockAddrIn6;
225  Size = sizeof(SOCKADDR_IN6);
226 
227  }
228  else
229  {
230  IPAddr *Address = (IPAddr *)Buffer;
231  SockAddrIn.sin_family = AF_INET;
232  SockAddrIn.sin_addr.S_un.S_addr = *Address;
233  SockAddr = (PSOCKADDR)&SockAddrIn;
234  Size = sizeof(SOCKADDR_IN);
235  }
236 
237  INT Status;
238  bool Resolved = false;
239  WCHAR HostName[NI_MAXHOST];
240  if (Info.ResolveAddresses)
241  {
242  Status = GetNameInfoW(SockAddr,
243  Size,
244  HostName,
245  NI_MAXHOST,
246  NULL,
247  0,
248  NI_NAMEREQD);
249  if (Status == 0)
250  {
251  Resolved = true;
252  }
253  }
254 
255  WCHAR IpAddress[MAX_IPADDRESS];
256  Status = GetNameInfoW(SockAddr,
257  Size,
258  IpAddress,
260  NULL,
261  0,
263  if (Status == 0)
264  {
265  if (Resolved)
266  {
267  OutputText(IDS_HOP_RES_INFO, HostName, IpAddress);
268  }
269  else
270  {
271  OutputText(IDS_HOP_IP_INFO, IpAddress);
272  }
273  }
274 
275  return (Status == 0);
276 }
#define NI_NAMEREQD
Definition: ws2def.h:355
struct _IPV6_ADDRESS_EX * PIPV6_ADDRESS_EX
#define NI_NUMERICHOST
Definition: ws2def.h:354
ADDRESS_FAMILY sin6_family
Definition: ws2ipdef.h:179
union in6_addr::@2944 u
#define IDS_HOP_IP_INFO
Definition: resource.h:16
#define MAX_IPADDRESS
Definition: tracert.cpp:34
struct sockaddr * PSOCKADDR
Definition: winsock.h:485
int32_t INT
Definition: typedefs.h:58
USHORT Word[8]
Definition: in6addr.h:6
struct TraceInfo Info
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
#define AF_INET6
Definition: winsock.h:369
Definition: bufpool.h:45
ULONG IPAddr
Definition: pfhook.h:35
static INT OutputText(_In_ UINT uID,...)
Definition: tracert.cpp:115
IN6_ADDR sin6_addr
Definition: ws2ipdef.h:182
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define CopyMemory
Definition: winbase.h:1646
int socklen_t
Definition: tcp.c:35
#define NI_MAXHOST
Definition: ws2def.h:359
INT WSAAPI GetNameInfoW(IN CONST SOCKADDR *pSockaddr, IN socklen_t SockaddrLength, OUT PWCHAR pNodeBuffer, IN DWORD NodeBufferSize, OUT PWCHAR pServiceBuffer, IN DWORD ServiceBufferSize, IN INT Flags)
Definition: addrinfo.c:873
Status
Definition: gdiplustypes.h:24
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
struct in_addr sin_addr
Definition: winsock.h:512
struct sockaddr_in SOCKADDR_IN
Definition: winsock.h:487
SOCKADDR_IN6_LH SOCKADDR_IN6
Definition: ws2ipdef.h:199
#define AF_INET
Definition: tcpip.h:117
#define IDS_HOP_RES_INFO
Definition: resource.h:15
short sin_family
Definition: winsock.h:510
USHORT sin6_addr[8]
Definition: ipexport.h:173

Referenced by DecodeResponse().

◆ ResolveTarget()

static bool ResolveTarget ( )
static

Definition at line 178 of file tracert.cpp.

179 {
180  ADDRINFOW Hints;
181  ZeroMemory(&Hints, sizeof(Hints));
182  Hints.ai_family = Info.Family;
183  Hints.ai_flags = AI_CANONNAME;
184 
185  int Status;
186  Status = GetAddrInfoW(Info.HostName,
187  NULL,
188  &Hints,
189  &Info.Target);
190  if (Status != 0)
191  {
192  return false;
193  }
194 
195  Status = GetNameInfoW(Info.Target->ai_addr,
196  Info.Target->ai_addrlen,
197  Info.TargetIP,
199  NULL,
200  0,
202  if (Status != 0)
203  {
204  return false;
205  }
206 
207  return true;
208 }
#define NI_NUMERICHOST
Definition: ws2def.h:354
#define AI_CANONNAME
Definition: ws2def.h:294
#define ZeroMemory
Definition: winbase.h:1648
#define MAX_IPADDRESS
Definition: tracert.cpp:34
struct TraceInfo Info
INT WSAAPI GetAddrInfoW(IN PCWSTR pszNodeName, IN PCWSTR pszServiceName, IN const ADDRINFOW *ptHints, OUT PADDRINFOW *pptResult)
Definition: addrinfo.c:509
int ai_family
Definition: ws2def.h:677
smooth NULL
Definition: ftsmooth.c:416
INT WSAAPI GetNameInfoW(IN CONST SOCKADDR *pSockaddr, IN socklen_t SockaddrLength, OUT PWCHAR pNodeBuffer, IN DWORD NodeBufferSize, OUT PWCHAR pServiceBuffer, IN DWORD ServiceBufferSize, IN INT Flags)
Definition: addrinfo.c:873
Status
Definition: gdiplustypes.h:24
int ai_flags
Definition: ws2def.h:676

Referenced by RunTraceRoute().

◆ RunTraceRoute()

static bool RunTraceRoute ( )
static

Definition at line 374 of file tracert.cpp.

375 {
376  bool Success = false;
378  if (!Success)
379  {
381  return false;
382  }
383 
384  BYTE SendBuffer[PACKET_SIZE];
385 
387 
389  if (Info.Family == AF_INET6)
390  {
391  ReplySize += sizeof(ICMPV6_ECHO_REPLY);
392  }
393  else
394  {
395 #ifdef _WIN64
396  ReplySize += sizeof(ICMP_ECHO_REPLY32);
397 #else
398  ReplySize += sizeof(ICMP_ECHO_REPLY);
399 #endif
400  }
401 
402  HANDLE heap = GetProcessHeap();
403  ReplyBuffer = HeapAlloc(heap, HEAP_ZERO_MEMORY, ReplySize);
404  if (ReplyBuffer == NULL)
405  {
406  FreeAddrInfoW(Info.Target);
407  return false;
408  }
409 
410  if (Info.Family == AF_INET6)
411  {
412  Info.hIcmpFile = Icmp6CreateFile();
413  }
414  else
415  {
416  Info.hIcmpFile = IcmpCreateFile();
417  }
418  if (Info.hIcmpFile == INVALID_HANDLE_VALUE)
419  {
420  HeapFree(heap, 0, ReplyBuffer);
421  FreeAddrInfoW(Info.Target);
422  return false;
423  }
424 
425  OutputText(IDS_TRACE_INFO, Info.HostName, Info.TargetIP, Info.MaxHops);
426 
427  IP_OPTION_INFORMATION IpOptionInfo;
428  ZeroMemory(&IpOptionInfo, sizeof(IpOptionInfo));
429 
430  bool Quit = false;
431  ULONG HopCount = 1;
432  bool FoundTarget = false;
433  while ((HopCount <= Info.MaxHops) && (FoundTarget == false) && (Quit == false))
434  {
435  OutputText(IDS_HOP_COUNT, HopCount);
436 
437  for (int Ping = 1; Ping <= NUM_OF_PINGS; Ping++)
438  {
439  IpOptionInfo.Ttl = static_cast<UCHAR>(HopCount);
440 
441  if (Info.Family == AF_INET6)
442  {
443  struct sockaddr_in6 Source;
444 
445  ZeroMemory(&Source, sizeof(Source));
446  Source.sin6_family = AF_INET6;
447 
448  (void)Icmp6SendEcho2(Info.hIcmpFile,
449  NULL,
450  NULL,
451  NULL,
452  &Source,
453  (struct sockaddr_in6 *)Info.Target->ai_addr,
454  SendBuffer,
456  &IpOptionInfo,
457  ReplyBuffer,
458  ReplySize,
459  Info.Timeout);
460  }
461  else
462  {
463  (void)IcmpSendEcho2(Info.hIcmpFile,
464  NULL,
465  NULL,
466  NULL,
467  ((PSOCKADDR_IN)Info.Target->ai_addr)->sin_addr.s_addr,
468  SendBuffer,
470  &IpOptionInfo,
471  ReplyBuffer,
472  ReplySize,
473  Info.Timeout);
474  }
475 
476  if (DecodeResponse(ReplyBuffer, (Ping == NUM_OF_PINGS), FoundTarget) == false)
477  {
478  Quit = true;
479  break;
480  }
481 
482  if (FoundTarget)
483  {
484  Success = true;
485  break;
486  }
487  }
488 
489  HopCount++;
490  Sleep(100);
491  }
492 
494 
495  HeapFree(heap, 0, ReplyBuffer);
496  FreeAddrInfoW(Info.Target);
497  if (Info.hIcmpFile)
498  {
499  IcmpCloseHandle(Info.hIcmpFile);
500  }
501 
502  return Success;
503 }
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
BOOL WINAPI IcmpCloseHandle(_In_ HANDLE IcmpHandle)
Definition: icmp.c:202
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
#define NUM_OF_PINGS
Definition: tracert.cpp:35
#define INVALID_HANDLE_VALUE
Definition: compat.h:479
#define ZeroMemory
Definition: winbase.h:1648
struct TraceInfo Info
#define SIZEOF_IO_STATUS_BLOCK
Definition: tracert.cpp:32
#define IDS_HOP_COUNT
Definition: resource.h:11
static void Ping(void)
Definition: ping.c:419
smooth NULL
Definition: ftsmooth.c:416
#define AF_INET6
Definition: winsock.h:369
static bool DecodeResponse(_In_ PVOID ReplyBuffer, _In_ bool OutputHopAddress, _Out_ bool &FoundTarget)
Definition: tracert.cpp:279
static INT OutputText(_In_ UINT uID,...)
Definition: tracert.cpp:115
#define GetProcessHeap()
Definition: compat.h:484
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
DWORD WINAPI Icmp6SendEcho2(_In_ HANDLE IcmpHandle, _In_opt_ HANDLE Event, _In_opt_ PIO_APC_ROUTINE ApcRoutine, _In_opt_ PVOID ApcContext, _In_ struct sockaddr_in6 *SourceAddress, _In_ struct sockaddr_in6 *DestinationAddress, _In_ LPVOID RequestData, _In_ WORD RequestSize, _In_ PIP_OPTION_INFORMATION RequestOptions, _Out_ LPVOID ReplyBuffer, _In_ DWORD ReplySize, _In_ DWORD Timeout)
Definition: icmp.c:77
static bool ResolveTarget()
Definition: tracert.cpp:178
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD WINAPI IcmpSendEcho2(_In_ HANDLE IcmpHandle, _In_opt_ HANDLE Event, _In_opt_ PIO_APC_ROUTINE ApcRoutine, _In_opt_ PVOID ApcContext, _In_ IPAddr DestinationAddress, _In_ LPVOID RequestData, _In_ WORD RequestSize, _In_opt_ PIP_OPTION_INFORMATION RequestOptions, _Out_ LPVOID ReplyBuffer, _In_ DWORD ReplySize, _In_ DWORD Timeout)
Definition: icmp.c:285
unsigned char Ttl
Definition: ipexport.h:33
ICMPV6_ECHO_REPLY_LH ICMPV6_ECHO_REPLY
Definition: ipexport.h:194
_Must_inspect_result_ _In_ PFLT_PORT _In_ ULONG _Out_writes_bytes_opt_ ReplyLength PVOID ReplyBuffer
Definition: fltkernel.h:1903
unsigned char BYTE
Definition: xxhash.c:193
#define SIZEOF_ICMP_ERROR
Definition: tracert.cpp:31
#define IDS_UNABLE_RESOLVE
Definition: resource.h:7
unsigned short USHORT
Definition: pedump.c:61
HANDLE WINAPI Icmp6CreateFile(void)
Definition: icmp.c:15
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
WINSOCK_API_LINKAGE VOID WSAAPI FreeAddrInfoW(_In_opt_ PADDRINFOW pAddrInfo)
#define PACKET_SIZE
Definition: tracert.cpp:33
unsigned int ULONG
Definition: retypes.h:1
#define IDS_TRACE_COMPLETE
Definition: resource.h:6
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3167
HANDLE WINAPI IcmpCreateFile(void)
Definition: icmp.c:219
#define IDS_TRACE_INFO
Definition: resource.h:5
struct icmp_echo_reply ICMP_ECHO_REPLY
#define HeapFree(x, y, z)
Definition: compat.h:483

Referenced by wmain().

◆ Usage()

static VOID Usage ( void  )
static

Definition at line 153 of file tracert.cpp.

154 {
156 }
#define IDS_USAGE
Definition: resource.h:3
static INT OutputText(_In_ UINT uID,...)
Definition: tracert.cpp:115

Referenced by ParseCmdline().

◆ wmain()

EXTERN_C int wmain ( int  argc,
wchar_t argv[] 
)

Definition at line 563 of file tracert.cpp.

564 {
565 #ifdef USE_CONUTILS
566  /* Initialize the Console Standard Streams */
568 #endif
569 
570  Info.ResolveAddresses = true;
571  Info.MaxHops = 30;
572  Info.Timeout = 4000;
573  Info.Family = AF_UNSPEC;
574 
575  if (!ParseCmdline(argc, argv))
576  {
577  return 1;
578  }
579 
580  WSADATA WsaData;
581  if (WSAStartup(MAKEWORD(2, 2), &WsaData))
582  {
583  return 1;
584  }
585 
586  bool Success;
588 
589  WSACleanup();
590 
591  return Success ? 0 : 1;
592 }
static int argc
Definition: ServiceArgs.c:12
#define MAKEWORD(a, b)
Definition: typedefs.h:248
static bool RunTraceRoute()
Definition: tracert.cpp:374
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
#define argv
Definition: mplay32.c:18
struct TraceInfo Info
#define ConInitStdStreams()
Definition: stream.h:128
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
#define AF_UNSPEC
Definition: winsock.h:344
static bool ParseCmdline(int argc, wchar_t *argv[])
Definition: tracert.cpp:506

Variable Documentation

◆ Info

struct TraceInfo Info = { 0 }

Referenced by AcpiDsCallControlMethod(), AcpiDsCreateBankField(), AcpiDsCreateField(), AcpiDsCreateIndexField(), AcpiDsGetFieldNames(), AcpiDsInitAmlWalk(), AcpiDsInitializeObjects(), AcpiDsInitOneObject(), AcpiEvaluateObject(), AcpiEvAsynchExecuteGpeMethod(), AcpiEvExecuteRegMethod(), AcpiEvExecuteRegMethods(), AcpiEvGetGpeDevice(), AcpiEvNotifyDispatch(), AcpiEvQueueNotifyRequest(), AcpiEvRegRun(), AcpiExPrepFieldValue(), AcpiGetDevices(), AcpiGetGpeDevice(), AcpiGetObjectInfo(), AcpiGetSleepTypeData(), AcpiGetVendorResource(), AcpiHwProcessPciList(), AcpiNsBuildInternalName(), AcpiNsCheckArgumentTypes(), AcpiNsCheckObjectType(), AcpiNsCheckPackage(), AcpiNsCheckPackageElements(), AcpiNsCheckPackageList(), AcpiNsCheckReference(), AcpiNsCheckReturnValue(), AcpiNsCheckSortedList(), AcpiNsComplexRepairs(), AcpiNsCustomPackage(), AcpiNsEvaluate(), AcpiNsExecuteTable(), AcpiNsFindIniMethods(), AcpiNsGetDeviceCallback(), AcpiNsGetInternalNameLength(), AcpiNsInitializeDevices(), AcpiNsInitializeObjects(), AcpiNsInitOneDevice(), AcpiNsInitOneObject(), AcpiNsInternalizeName(), AcpiNsRemoveNullElements(), AcpiNsRepair_ALR(), AcpiNsRepair_CID(), AcpiNsRepair_CST(), AcpiNsRepair_FDE(), AcpiNsRepair_HID(), AcpiNsRepair_PRT(), AcpiNsRepair_PSS(), AcpiNsRepair_TSS(), AcpiNsRepairNullElement(), AcpiNsResolveReferences(), AcpiNsSimpleRepair(), AcpiNsWrapWithPackage(), AcpiOsSignal(), AcpiPsExecuteMethod(), AcpiPsExecuteTable(), AcpiPsUpdateParameterList(), AcpiRsConvertAmlToResource(), AcpiRsConvertResourceToAml(), AcpiRsMatchVendorResource(), AcpiRsSetSrsMethodData(), AcpiUtCopyIelementToEelement(), AcpiUtCopyIpackageToEpackage(), AcpiUtEvaluateObject(), AcpiUtGetElementLength(), AcpiUtGetPackageObjectSize(), AddExtension(), AddFile(), AddToolbarButton(), AddToolbarSeparator(), AssocGetPerceivedType(), BasepProbeForDllManifest(), BestFit_FromNLS(), BroadcastDriveLetterChange(), BuildDBCallback(), DriveVolume::BuildFileList(), CallNotificationDlls(), ChangeListViewText(), ChangeServiceConfig2A(), ChangeServiceConfig2W(), ClasspFailurePredict(), ClasspInitializeGesn(), ClasspPrepareMcnIrp(), ClasspSendMediaStateIrp(), CmdFindAppEnum(), CmpQueryKeyData(), CmpQueryKeyValueData(), co_IntSetScrollInfo(), CompareFunc(), ConsoleChildFrmProc(), ConsoleMainFrameWndProc(), CreateEditWindow(), CreateFontWindow(), CreateImageEditWindow(), CreateListView(), CreateMainWindow(), CreateNewMDIChild(), CreateOpenInfo(), CreateStatusBar(), CreateToolbars(), DECLARE_INTERFACE_(), DecodeResponse(), DeleteDialogProc(), DestroyEditWnd(), DestroyMainWnd(), DIB_BitmapMaxBitsSize(), DispTdiSetInformationEx(), DoDeleteService(), DoFileNew(), DoFileOpen(), DoFileSave(), DrawProc(), EditGlyphCommand(), EditGlyphDlgProc(), EditGlyphEditWndProc(), EditGlyphPreviewWndProc(), EditWndProc(), EditWndUpdateScrollInfo(), CInstalledApps::Enum(), CAvailableApps::Enum(), CMainWindow::EnumAvailableAppProc(), EnumDeviceKeys(), CMainWindow::EnumInstalledAppProc(), ExpCopyLookasideInformation(), ExportFile(), CZipExtract::Extract(), CAvailableApps::FindAppsByPkgNameList(), FloatToolbarCreateColorsGui(), FloatToolbarCreateHistoryGui(), FloatToolbarCreateToolsGui(), FolderOptionsFileTypesDlg(), FontBoxesWndProc(), FontFamilyFillInfo(), FontWndProc(), FrameOnCommand(), FrameOnCreate(), CInstalledApps::FreeCachedEntries(), CAvailableApps::FreeCachedEntries(), FreeServiceList(), FsRtlGetFileSize(), FsRtlGetTunnelParameterValue(), ftGdiRealizationInfo(), GdiCreateLocalEnhMetaFile(), GetBitRect(), DriveVolume::GetClusterInfo(), GetFontFamilyInfoForList(), GetFontFamilyInfoForSubstitutes(), GetScrollInfo(), GetSelectedService(), GetServiceList(), GetTextFromListView(), GreGetDIBitsInternal(), ImageEditWndProc(), ImageEditWndRepaint(), InitEditWnd(), InitFont(), Initialize(), InitializeProvider(), InitListViewImage(), InitMainWnd(), InitMenuPopup(), InitResources(), anonymous_namespace{settingsdlg.cpp}::InitSettingsControls(), InitToolbox(), CAppRichEdit::InsertLanguageInfo(), CAppRichEdit::InsertLicenseInfo(), CAppRichEdit::InsertVersionInfo(), CMainWindow::InstallApplication(), IntDrawScrollBar(), IntEnumFontFamilies(), IntGetModuleInformation(), IntLoadRegistryParameters(), IntScrollCreateScrollBar(), IntSetScrollInfo(), IsConsoleProcess(), LanAdapterInfo(), ListViewSelectionChanged(), ListWndNotifyProc(), LoadAllSettings(), LoadBlankCanvas(), LoadDIBImage(), LsarQueryModification(), LsarSetAccountDomain(), LsarSetAuditEvents(), LsarSetAuditFull(), LsarSetAuditLog(), LsarSetDefaultQuota(), LsarSetModification(), LsarSetPrimaryDomain(), LsarSetServerRole(), LsaSpSetExtendedInformation(), MainWndCommand(), MainWndCreateFloatWindows(), MainWndDestroyFloatWindows(), MainWndDropFiles(), MainWndGetCurrentEditor(), MainWndMenuHint(), MainWndMoveFloatingWindows(), MainWndOpenFile(), MainWndProc(), MainWndResetFloatingWindows(), MainWndResize(), MainWndSize(), MainWndSwitchEditorContext(), MainWndTranslateMDISysAccel(), MenuCommand(), MmQuerySectionView(), DriveVolume::MoveFileDumb(), MupGetProviderInformation(), NLS_RecordsCountForDBCSTable(), NotifyShims(), NtfsGetBothDirectoryInformation(), NtfsGetDirectoryInformation(), NtfsGetFullDirectoryInformation(), NtfsGetNamesInformation(), NtGdiGetFontFamilyInfo(), COpenWithList::OpenMRUList(), OpenPropSheet(), OutOfMemory(), ParseCmdline(), pCreateToolbar(), PlayLogonSoundThread(), PnpDecodeFixedMemory(), PnpDecodeMemory16(), PnpDecodeMemory32(), PopReadShutdownPolicy(), PrintHopInfo(), PrintStatistics(), ProcessFiles(), QSI_DEF(), QueryFileInfo(), RChangeServiceConfig2A(), RChangeServiceConfig2W(), RealGetScrollInfo(), RealSetScrollInfo(), RefreshServiceList(), RegpCopyTree(), ResolveTarget(), RtlpPageHeapFree(), RtlSetInformationAcl(), RunTraceRoute(), CMainWindow::s_EnumAvailableAppProc(), CMainWindow::s_EnumInstalledAppProc(), CMainWindow::s_EnumSelectedAppForDownloadProc(), SaveAllSettings(), SaveServicesToFile(), DriveVolume::ScanDirectory(), SetAccountsDomainSid(), SetFileName(), SetMenuAndButtonStates(), SetPasteButtonState(), SetPixelBit(), SetPrimaryDomain(), SetScrollInfo(), SetSelectedCharacter(), SetToolbarButtonState(), SetToolbarFileButtonState(), SHELL_execute(), CAppRichEdit::ShowAvailableAppInfo(), CAppInfoDisplay::ShowAvailableAppInfo(), CAppRichEdit::ShowInstalledAppInfo(), CAppInfoDisplay::ShowInstalledAppInfo(), Defragment::Start(), StoreUnwindCodes(), StoreUnwindInfo(), Test1(), Test2(), Test3(), test_match_ex(), test_QueryBacktrace(), TestCommandLine(), TimerCallback1(), TimerCallback2(), TimerCallback3(), UnInitResources(), UpdateMainStatusBar(), UpdateServiceCount(), USBD_GetDeviceInformationEx(), USBH_IoctlGetNodeConnectionInformation(), VBEGetVideoChildDescriptor(), VBEReadEdidUsingSCI(), VbeService(), VBESortModesCallback(), VfatGetAllInformation(), VfatGetEaInformation(), WaitForService(), wmain(), WSHSetSocketInformation(), and wWinMain().