2018-01-02 09:43:14 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS trace route utility
|
2022-06-28 20:36:30 +00:00
|
|
|
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
|
2018-01-02 09:43:14 +00:00
|
|
|
* PURPOSE: Trace network paths through networks
|
|
|
|
* COPYRIGHT: Copyright 2018 Ged Murphy <gedmurphy@reactos.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __REACTOS__
|
2018-04-02 16:10:31 +00:00
|
|
|
#define USE_CONUTILS
|
2018-01-02 09:43:14 +00:00
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#include <winuser.h>
|
|
|
|
#define _INC_WINDOWS
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <winsock2.h>
|
2018-04-02 16:10:31 +00:00
|
|
|
#include <conutils.h>
|
2018-01-02 09:43:14 +00:00
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <iphlpapi.h>
|
|
|
|
#include <icmpapi.h>
|
|
|
|
#include <strsafe.h>
|
|
|
|
#include "resource.h"
|
|
|
|
|
|
|
|
#define SIZEOF_ICMP_ERROR 8
|
|
|
|
#define SIZEOF_IO_STATUS_BLOCK 8
|
|
|
|
#define PACKET_SIZE 32
|
|
|
|
#define MAX_IPADDRESS 32
|
|
|
|
#define NUM_OF_PINGS 3
|
|
|
|
|
|
|
|
struct TraceInfo
|
|
|
|
{
|
|
|
|
bool ResolveAddresses;
|
|
|
|
ULONG MaxHops;
|
|
|
|
ULONG Timeout;
|
|
|
|
WCHAR HostName[NI_MAXHOST];
|
|
|
|
WCHAR TargetIP[MAX_IPADDRESS];
|
|
|
|
int Family;
|
|
|
|
|
|
|
|
HANDLE hIcmpFile;
|
|
|
|
PADDRINFOW Target;
|
|
|
|
|
|
|
|
} Info = { 0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef USE_CONUTILS
|
|
|
|
static
|
|
|
|
INT
|
|
|
|
LengthOfStrResource(
|
|
|
|
_In_ HINSTANCE hInst,
|
|
|
|
_In_ UINT uID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
HRSRC hrSrc;
|
|
|
|
HGLOBAL hRes;
|
|
|
|
LPWSTR lpName, lpStr;
|
|
|
|
|
|
|
|
if (hInst == NULL) return -1;
|
|
|
|
|
|
|
|
lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
|
|
|
|
|
|
|
|
if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
|
|
|
|
(hRes = LoadResource(hInst, hrSrc)) &&
|
|
|
|
(lpStr = (WCHAR*)LockResource(hRes)))
|
|
|
|
{
|
|
|
|
UINT x;
|
|
|
|
uID &= 0xF;
|
|
|
|
for (x = 0; x < uID; x++)
|
|
|
|
{
|
|
|
|
lpStr += (*lpStr) + 1;
|
|
|
|
}
|
|
|
|
return (int)(*lpStr);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
INT
|
|
|
|
AllocAndLoadString(
|
|
|
|
_In_ UINT uID,
|
|
|
|
_Out_ LPWSTR *lpTarget
|
|
|
|
)
|
|
|
|
{
|
|
|
|
HMODULE hInst;
|
|
|
|
INT Length;
|
|
|
|
|
|
|
|
hInst = GetModuleHandleW(NULL);
|
|
|
|
Length = LengthOfStrResource(hInst, uID);
|
|
|
|
if (Length++ > 0)
|
|
|
|
{
|
|
|
|
(*lpTarget) = (LPWSTR)LocalAlloc(LMEM_FIXED,
|
|
|
|
Length * sizeof(WCHAR));
|
|
|
|
if ((*lpTarget) != NULL)
|
|
|
|
{
|
|
|
|
INT Ret;
|
|
|
|
if (!(Ret = LoadStringW(hInst, uID, *lpTarget, Length)))
|
|
|
|
{
|
|
|
|
LocalFree((HLOCAL)(*lpTarget));
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
INT
|
|
|
|
OutputText(
|
|
|
|
_In_ UINT uID,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
LPWSTR Format;
|
|
|
|
DWORD Ret = 0;
|
|
|
|
va_list lArgs;
|
|
|
|
|
|
|
|
if (AllocAndLoadString(uID, &Format) > 0)
|
|
|
|
{
|
|
|
|
va_start(lArgs, uID);
|
|
|
|
|
|
|
|
LPWSTR Buffer;
|
|
|
|
Ret = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
|
|
|
|
Format,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(LPWSTR)&Buffer,
|
|
|
|
0,
|
|
|
|
&lArgs);
|
|
|
|
va_end(lArgs);
|
|
|
|
|
|
|
|
if (Ret)
|
|
|
|
{
|
|
|
|
wprintf(Buffer);
|
|
|
|
LocalFree(Buffer);
|
|
|
|
}
|
|
|
|
LocalFree((HLOCAL)Format);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
#else
|
2018-04-02 16:10:31 +00:00
|
|
|
#define OutputText(Id, ...) ConResMsgPrintfEx(StdOut, NULL, 0, Id, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), ##__VA_ARGS__)
|
2018-01-02 09:43:14 +00:00
|
|
|
#endif //USE_CONUTILS
|
|
|
|
|
|
|
|
static
|
|
|
|
VOID
|
|
|
|
Usage()
|
|
|
|
{
|
|
|
|
OutputText(IDS_USAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG
|
|
|
|
GetULONG(
|
|
|
|
_In_z_ LPWSTR String
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ULONG Length;
|
|
|
|
Length = wcslen(String);
|
|
|
|
|
|
|
|
ULONG i = 0;
|
|
|
|
while ((i < Length) && ((String[i] < L'0') || (String[i] > L'9'))) i++;
|
|
|
|
if ((i >= Length) || ((String[i] < L'0') || (String[i] > L'9')))
|
|
|
|
{
|
|
|
|
return (ULONG)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPWSTR StopString;
|
|
|
|
return wcstoul(&String[i], &StopString, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ResolveTarget()
|
|
|
|
{
|
|
|
|
ADDRINFOW Hints;
|
|
|
|
ZeroMemory(&Hints, sizeof(Hints));
|
|
|
|
Hints.ai_family = Info.Family;
|
|
|
|
Hints.ai_flags = AI_CANONNAME;
|
|
|
|
|
|
|
|
int Status;
|
|
|
|
Status = GetAddrInfoW(Info.HostName,
|
|
|
|
NULL,
|
|
|
|
&Hints,
|
|
|
|
&Info.Target);
|
|
|
|
if (Status != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = GetNameInfoW(Info.Target->ai_addr,
|
|
|
|
Info.Target->ai_addrlen,
|
|
|
|
Info.TargetIP,
|
|
|
|
MAX_IPADDRESS,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NI_NUMERICHOST);
|
|
|
|
if (Status != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
PrintHopInfo(_In_ PVOID Buffer)
|
|
|
|
{
|
|
|
|
SOCKADDR_IN6 SockAddrIn6 = { 0 };
|
|
|
|
SOCKADDR_IN SockAddrIn = { 0 };
|
|
|
|
PSOCKADDR SockAddr;
|
|
|
|
socklen_t Size;
|
|
|
|
|
|
|
|
if (Info.Family == AF_INET6)
|
|
|
|
{
|
|
|
|
PIPV6_ADDRESS_EX Ipv6Addr = (PIPV6_ADDRESS_EX)Buffer;
|
|
|
|
SockAddrIn6.sin6_family = AF_INET6;
|
|
|
|
CopyMemory(SockAddrIn6.sin6_addr.u.Word, Ipv6Addr->sin6_addr, sizeof(SockAddrIn6.sin6_addr));
|
|
|
|
//SockAddrIn6.sin6_addr = Ipv6Addr->sin6_addr;
|
|
|
|
SockAddr = (PSOCKADDR)&SockAddrIn6;
|
|
|
|
Size = sizeof(SOCKADDR_IN6);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IPAddr *Address = (IPAddr *)Buffer;
|
|
|
|
SockAddrIn.sin_family = AF_INET;
|
|
|
|
SockAddrIn.sin_addr.S_un.S_addr = *Address;
|
|
|
|
SockAddr = (PSOCKADDR)&SockAddrIn;
|
|
|
|
Size = sizeof(SOCKADDR_IN);
|
|
|
|
}
|
|
|
|
|
|
|
|
INT Status;
|
|
|
|
bool Resolved = false;
|
|
|
|
WCHAR HostName[NI_MAXHOST];
|
|
|
|
if (Info.ResolveAddresses)
|
|
|
|
{
|
|
|
|
Status = GetNameInfoW(SockAddr,
|
|
|
|
Size,
|
|
|
|
HostName,
|
|
|
|
NI_MAXHOST,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NI_NAMEREQD);
|
|
|
|
if (Status == 0)
|
|
|
|
{
|
|
|
|
Resolved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WCHAR IpAddress[MAX_IPADDRESS];
|
|
|
|
Status = GetNameInfoW(SockAddr,
|
|
|
|
Size,
|
|
|
|
IpAddress,
|
2018-11-15 13:15:23 +00:00
|
|
|
MAX_IPADDRESS,
|
2018-01-02 09:43:14 +00:00
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NI_NUMERICHOST);
|
|
|
|
if (Status == 0)
|
|
|
|
{
|
|
|
|
if (Resolved)
|
|
|
|
{
|
|
|
|
OutputText(IDS_HOP_RES_INFO, HostName, IpAddress);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OutputText(IDS_HOP_IP_INFO, IpAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Status == 0);
|
|
|
|
}
|
|
|
|
|
2022-06-28 20:36:30 +00:00
|
|
|
static ULONG
|
|
|
|
GetResponseStats(
|
2018-01-02 09:43:14 +00:00
|
|
|
_In_ PVOID ReplyBuffer,
|
2022-06-28 20:36:30 +00:00
|
|
|
_Out_ ULONG& RoundTripTime,
|
|
|
|
_Out_ PVOID& AddressInfo
|
2018-01-02 09:43:14 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
ULONG Status;
|
|
|
|
|
|
|
|
if (Info.Family == AF_INET6)
|
|
|
|
{
|
|
|
|
PICMPV6_ECHO_REPLY EchoReplyV6;
|
|
|
|
EchoReplyV6 = (PICMPV6_ECHO_REPLY)ReplyBuffer;
|
|
|
|
Status = EchoReplyV6->Status;
|
|
|
|
RoundTripTime = EchoReplyV6->RoundTripTime;
|
|
|
|
AddressInfo = &EchoReplyV6->Address;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-09 18:41:39 +00:00
|
|
|
#ifdef _WIN64
|
|
|
|
PICMP_ECHO_REPLY32 EchoReplyV4;
|
|
|
|
EchoReplyV4 = (PICMP_ECHO_REPLY32)ReplyBuffer;
|
|
|
|
#else
|
2018-01-02 09:43:14 +00:00
|
|
|
PICMP_ECHO_REPLY EchoReplyV4;
|
|
|
|
EchoReplyV4 = (PICMP_ECHO_REPLY)ReplyBuffer;
|
2020-03-09 18:41:39 +00:00
|
|
|
#endif
|
2018-01-02 09:43:14 +00:00
|
|
|
Status = EchoReplyV4->Status;
|
|
|
|
RoundTripTime = EchoReplyV4->RoundTripTime;
|
|
|
|
AddressInfo = &EchoReplyV4->Address;
|
|
|
|
}
|
|
|
|
|
2022-06-28 20:36:30 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
DecodeResponse(
|
|
|
|
_In_ PVOID ReplyBuffer,
|
|
|
|
_In_ PVOID LastGoodResponse,
|
|
|
|
_In_ bool OutputHopAddress,
|
|
|
|
_Out_ bool& GoodResponse,
|
|
|
|
_Out_ bool& FoundTarget
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ULONG RoundTripTime;
|
|
|
|
PVOID AddressInfo;
|
|
|
|
ULONG Status = GetResponseStats(ReplyBuffer, RoundTripTime, AddressInfo);
|
|
|
|
|
2018-01-02 09:43:14 +00:00
|
|
|
switch (Status)
|
|
|
|
{
|
|
|
|
case IP_SUCCESS:
|
|
|
|
case IP_TTL_EXPIRED_TRANSIT:
|
|
|
|
if (RoundTripTime)
|
|
|
|
{
|
|
|
|
OutputText(IDS_HOP_TIME, RoundTripTime);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OutputText(IDS_HOP_ZERO);
|
|
|
|
}
|
2022-06-28 20:36:30 +00:00
|
|
|
GoodResponse = true;
|
2018-01-02 09:43:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IP_DEST_HOST_UNREACHABLE:
|
2018-01-26 18:37:43 +00:00
|
|
|
case IP_DEST_NET_UNREACHABLE:
|
|
|
|
FoundTarget = true;
|
|
|
|
PrintHopInfo(AddressInfo);
|
|
|
|
OutputText(IDS_HOP_RESPONSE);
|
|
|
|
if (Status == IP_DEST_HOST_UNREACHABLE)
|
|
|
|
{
|
|
|
|
OutputText(IDS_DEST_HOST_UNREACHABLE);
|
|
|
|
}
|
|
|
|
else if (Status == IP_DEST_NET_UNREACHABLE)
|
|
|
|
{
|
|
|
|
OutputText(IDS_DEST_NET_UNREACHABLE);
|
|
|
|
}
|
|
|
|
return true;
|
2018-01-02 09:43:14 +00:00
|
|
|
|
|
|
|
case IP_REQ_TIMED_OUT:
|
|
|
|
OutputText(IDS_TIMEOUT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IP_GENERAL_FAILURE:
|
|
|
|
OutputText(IDS_GEN_FAILURE);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
2018-01-26 18:37:43 +00:00
|
|
|
OutputText(IDS_TRANSMIT_FAILED, Status);
|
2018-01-02 09:43:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OutputHopAddress)
|
|
|
|
{
|
2022-06-28 20:36:30 +00:00
|
|
|
if (Status == IP_REQ_TIMED_OUT && LastGoodResponse)
|
|
|
|
{
|
|
|
|
Status = GetResponseStats(LastGoodResponse, RoundTripTime, AddressInfo);
|
|
|
|
}
|
2018-01-02 09:43:14 +00:00
|
|
|
if (Status == IP_SUCCESS)
|
|
|
|
{
|
|
|
|
FoundTarget = true;
|
|
|
|
}
|
|
|
|
if (Status == IP_TTL_EXPIRED_TRANSIT || Status == IP_SUCCESS)
|
|
|
|
{
|
|
|
|
PrintHopInfo(AddressInfo);
|
2018-01-26 18:37:43 +00:00
|
|
|
OutputText(IDS_LINEBREAK);
|
2018-01-02 09:43:14 +00:00
|
|
|
}
|
|
|
|
else if (Status == IP_REQ_TIMED_OUT)
|
|
|
|
{
|
|
|
|
OutputText(IDS_REQ_TIMED_OUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
RunTraceRoute()
|
|
|
|
{
|
|
|
|
bool Success = false;
|
2022-06-28 20:36:30 +00:00
|
|
|
PVOID ReplyBuffer = NULL, LastGoodResponse = NULL;
|
|
|
|
DWORD ReplySize;
|
|
|
|
|
|
|
|
HANDLE heap = GetProcessHeap();
|
|
|
|
bool Quit = false;
|
|
|
|
ULONG HopCount = 1;
|
|
|
|
bool FoundTarget = false;
|
|
|
|
|
2018-01-02 09:43:14 +00:00
|
|
|
Success = ResolveTarget();
|
|
|
|
if (!Success)
|
|
|
|
{
|
|
|
|
OutputText(IDS_UNABLE_RESOLVE, Info.HostName);
|
2022-06-28 20:36:30 +00:00
|
|
|
goto Cleanup;
|
2018-01-02 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 20:36:30 +00:00
|
|
|
ReplySize = PACKET_SIZE + SIZEOF_ICMP_ERROR + SIZEOF_IO_STATUS_BLOCK;
|
2018-01-02 09:43:14 +00:00
|
|
|
if (Info.Family == AF_INET6)
|
|
|
|
{
|
|
|
|
ReplySize += sizeof(ICMPV6_ECHO_REPLY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef _WIN64
|
|
|
|
ReplySize += sizeof(ICMP_ECHO_REPLY32);
|
|
|
|
#else
|
|
|
|
ReplySize += sizeof(ICMP_ECHO_REPLY);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:36:07 +00:00
|
|
|
ReplyBuffer = HeapAlloc(heap, HEAP_ZERO_MEMORY, ReplySize);
|
|
|
|
if (ReplyBuffer == NULL)
|
|
|
|
{
|
2022-06-28 20:36:30 +00:00
|
|
|
Success = false;
|
|
|
|
goto Cleanup;
|
2020-03-08 18:36:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 09:43:14 +00:00
|
|
|
if (Info.Family == AF_INET6)
|
|
|
|
{
|
|
|
|
Info.hIcmpFile = Icmp6CreateFile();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Info.hIcmpFile = IcmpCreateFile();
|
|
|
|
}
|
|
|
|
if (Info.hIcmpFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2022-06-28 20:36:30 +00:00
|
|
|
Success = false;
|
|
|
|
goto Cleanup;
|
2018-01-02 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OutputText(IDS_TRACE_INFO, Info.HostName, Info.TargetIP, Info.MaxHops);
|
|
|
|
|
|
|
|
IP_OPTION_INFORMATION IpOptionInfo;
|
|
|
|
ZeroMemory(&IpOptionInfo, sizeof(IpOptionInfo));
|
|
|
|
|
|
|
|
while ((HopCount <= Info.MaxHops) && (FoundTarget == false) && (Quit == false))
|
|
|
|
{
|
|
|
|
OutputText(IDS_HOP_COUNT, HopCount);
|
|
|
|
|
2022-06-28 20:36:30 +00:00
|
|
|
if (LastGoodResponse)
|
|
|
|
{
|
|
|
|
HeapFree(heap, 0, LastGoodResponse);
|
|
|
|
LastGoodResponse = NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-02 09:43:14 +00:00
|
|
|
for (int Ping = 1; Ping <= NUM_OF_PINGS; Ping++)
|
|
|
|
{
|
2022-06-28 20:36:30 +00:00
|
|
|
BYTE SendBuffer[PACKET_SIZE];
|
|
|
|
bool GoodResponse = false;
|
|
|
|
|
2018-01-02 09:43:14 +00:00
|
|
|
IpOptionInfo.Ttl = static_cast<UCHAR>(HopCount);
|
|
|
|
|
|
|
|
if (Info.Family == AF_INET6)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 Source;
|
|
|
|
|
|
|
|
ZeroMemory(&Source, sizeof(Source));
|
|
|
|
Source.sin6_family = AF_INET6;
|
|
|
|
|
|
|
|
(void)Icmp6SendEcho2(Info.hIcmpFile,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&Source,
|
|
|
|
(struct sockaddr_in6 *)Info.Target->ai_addr,
|
|
|
|
SendBuffer,
|
|
|
|
(USHORT)PACKET_SIZE,
|
|
|
|
&IpOptionInfo,
|
|
|
|
ReplyBuffer,
|
|
|
|
ReplySize,
|
|
|
|
Info.Timeout);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(void)IcmpSendEcho2(Info.hIcmpFile,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
((PSOCKADDR_IN)Info.Target->ai_addr)->sin_addr.s_addr,
|
|
|
|
SendBuffer,
|
|
|
|
(USHORT)PACKET_SIZE,
|
|
|
|
&IpOptionInfo,
|
|
|
|
ReplyBuffer,
|
|
|
|
ReplySize,
|
|
|
|
Info.Timeout);
|
|
|
|
}
|
|
|
|
|
2022-06-28 20:36:30 +00:00
|
|
|
if (DecodeResponse(ReplyBuffer,
|
|
|
|
LastGoodResponse,
|
|
|
|
(Ping == NUM_OF_PINGS),
|
|
|
|
GoodResponse,
|
|
|
|
FoundTarget) == false)
|
2018-01-02 09:43:14 +00:00
|
|
|
{
|
|
|
|
Quit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FoundTarget)
|
|
|
|
{
|
|
|
|
Success = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-28 20:36:30 +00:00
|
|
|
|
|
|
|
if (GoodResponse)
|
|
|
|
{
|
|
|
|
if (LastGoodResponse)
|
|
|
|
{
|
|
|
|
HeapFree(heap, 0, LastGoodResponse);
|
|
|
|
}
|
|
|
|
LastGoodResponse = HeapAlloc(heap, HEAP_ZERO_MEMORY, ReplySize);
|
|
|
|
if (LastGoodResponse == NULL)
|
|
|
|
{
|
|
|
|
Success = false;
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
CopyMemory(LastGoodResponse, ReplyBuffer, ReplySize);
|
|
|
|
}
|
2018-01-02 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HopCount++;
|
|
|
|
Sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputText(IDS_TRACE_COMPLETE);
|
|
|
|
|
2022-06-28 20:36:30 +00:00
|
|
|
Cleanup:
|
|
|
|
if (ReplyBuffer)
|
|
|
|
{
|
|
|
|
HeapFree(heap, 0, ReplyBuffer);
|
|
|
|
}
|
|
|
|
if (LastGoodResponse)
|
|
|
|
{
|
|
|
|
HeapFree(heap, 0, LastGoodResponse);
|
|
|
|
}
|
|
|
|
if (Info.Target)
|
|
|
|
{
|
|
|
|
FreeAddrInfoW(Info.Target);
|
|
|
|
}
|
2018-01-02 09:43:14 +00:00
|
|
|
if (Info.hIcmpFile)
|
|
|
|
{
|
|
|
|
IcmpCloseHandle(Info.hIcmpFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ParseCmdline(int argc, wchar_t *argv[])
|
|
|
|
{
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
Usage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (argv[i][0] == '-')
|
|
|
|
{
|
|
|
|
switch (argv[i][1])
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
Info.ResolveAddresses = FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
Info.MaxHops = GetULONG(argv[++i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'j':
|
|
|
|
printf("-j is not yet implemented.\n");
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
Info.Timeout = GetULONG(argv[++i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '4':
|
|
|
|
Info.Family = AF_INET;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '6':
|
|
|
|
Info.Family = AF_INET6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
OutputText(IDS_INVALID_OPTION, argv[i]);
|
|
|
|
Usage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StringCchCopyW(Info.HostName, NI_MAXHOST, argv[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXTERN_C
|
|
|
|
int wmain(int argc, wchar_t *argv[])
|
|
|
|
{
|
2018-04-02 16:10:31 +00:00
|
|
|
#ifdef USE_CONUTILS
|
|
|
|
/* Initialize the Console Standard Streams */
|
|
|
|
ConInitStdStreams();
|
|
|
|
#endif
|
|
|
|
|
2018-01-02 09:43:14 +00:00
|
|
|
Info.ResolveAddresses = true;
|
|
|
|
Info.MaxHops = 30;
|
|
|
|
Info.Timeout = 4000;
|
|
|
|
Info.Family = AF_UNSPEC;
|
|
|
|
|
|
|
|
if (!ParseCmdline(argc, argv))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
WSADATA WsaData;
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &WsaData))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Success;
|
|
|
|
Success = RunTraceRoute();
|
|
|
|
|
|
|
|
WSACleanup();
|
|
|
|
|
|
|
|
return Success ? 0 : 1;
|
|
|
|
}
|