🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #158

Re: How can I Trace the Route to an IP Addres from within my program

#158
From: DreamChaser
Date: Fri, 27 Jun 2008 06:12
130 lines
5098 bytes
I went back and recopied/rewrote the pseudo-code and the replies below 
are from the code provided at the end.

Remy Lebeau (TeamB) wrote:
> "DreamChaser" <NO-Spam@No-Span.net> wrote in message 
> news:4861dce2$1@newsgroups.borland.com...
> 
> What EXACTLY is IcmpSendEcho() returning?  What EXACTLY is GetLastError() 
> returning?
IcmpSendEcho() returns 0
GetLastError() returns 19    = "Access Denied."
reply->Status  returns 11017 = "Option too large."
> 
>>    //This is www.google.com
>>    ipaddr.S_un.S_addr = inet_addr("74.125.47.147");
> 
> You can use gethostbyname() for that:
I've had trouble with using this, but I put the minimum code to produce 
the error. Do I need to initialize WinSock?
> 
>     struct hostent *host = gethostbyname("www.google.com");
>     if( host )
>     {
>         ipaddr.S_un.S_addr = ((struc in_addr*)(host->h_addr))->s_addr;
>         ...
>     }
> 
> 
> Gambit 
> 
> 
//---------------------------------------------------------------------------
// An array of error message strings.
String ErrorStrings[] = {
	"Error Base",
	"Buffer too small.",
	"Destination net unreachable.",
	"Destination host unreachable.",
	"Destination protocol unreachable.",
	"Destination port unreachable.",
	"Out of resources.",
	"Bad option.",
	"Hardware error.",
	"Packet too large.",
	"Request timed out.",
	"Bad request.",
	"Bad route.",
	"TTL expired in transit.",
	"TTL expired REASSEM.",
	"Param problem.",
	"Source quench.",
	"Option too large.",
	"Bad destination.",
	"Address deleted.",
	"Spec MNU change.",
	"MTU change.",
	"Unload"
};
#pragma pack(1)
typedef struct {
    BYTE   cbType;
    BYTE   cbSize;
    BYTE   pointer;
    IPAddr addresses[62];
} RecordRouteOption, *PRecordRouteOption;
#pragma pack()
//---------------------------------------------------------------------------
    iPingTimeout = 2000;   //ms
    IPAddr addr;

     addr = inet_addr("74.125.47.147");
    // Setup the option_information structure
     HANDLE icmp = IcmpCreateFile();
     char *SendData = "Hello";

     BYTE options[sizeof(RecordRouteOption)+1] = {0};

     PRecordRouteOption option = (PRecordRouteOption) options;
     option->cbType = IP_OPT_RR;
     option->cbSize = sizeof(RecordRouteOption);
     option->pointer = 4;

     (++option)->cbSize = IP_OPT_EOL;

     IP_OPTION_INFORMATION info = {0};
     info.Ttl = 255;
     info.OptionsSize = sizeof(options);
     info.OptionsData = options;

     DWORD ReplySize =  sizeof(ICMP_ECHO_REPLY) + sizeof(options) +
                        sizeof(SendData) + 8;
     ICMP_ECHO_REPLY *reply = (ICMP_ECHO_REPLY*) malloc(ReplySize);


     DWORD dwRetVal = IcmpSendEcho(icmp, addr, SendData,
                                   strlen(SendData), 

                                   &info, reply, ReplySize,
                                   iPingTimeout);

    if (dwRetVal > 0){
       struct in_addr ReplyAddr;
       ReplyAddr.S_un.S_addr = reply->Address;
       S.sprintf("%s Reply from %s: bytes=%d, time=%3dms TTL=%3d",
                   Now().FormatString(TIME_FORMAT),
                   inet_ntoa(ReplyAddr),
                   reply->DataSize,
                   reply->RoundTripTime,
                   reply->Options.Ttl
                );
       Memo1->Lines->Add(S);
    } else {
       //Error
       LPVOID lpMsgBuf;
       Memo1->Lines->Add(ErrorStrings[reply->Status - IP_STATUS_BASE]);
       FormatMessage(
             FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
             NULL,
             GetLastError(),
             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                 (LPTSTR) &lpMsgBuf,
                 0,
                 NULL
             );
       Memo1->Lines->Add((LPTSTR)lpMsgBuf);
       // Free the buffer.
       LocalFree( lpMsgBuf );
     }
     free(reply);
     IcmpCloseHandle(icmp);
Technical Details
Message-ID: <486484c8$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!news.isoc.lu!newsgroups.borland.com!not-for-mail
References: <MPG.22c8301ab312798c989a97@newsgroups.borland.com> <485faf40$1@newsgroups.borland.com> <MPG.22cb3a3e698457ac989a9a@newsgroups.borland.com> <rrj264107nnff1gqarmhq4p0v1c23k6801@4ax.com> <4861dce2$1@newsgroups.borland.com> <4862a806$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,7aa64e4617379849
X-Google-Attributes: gid11508e,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Fri, 27 Jun 2008 01:12:25 -0500
From: DreamChaser <NO-Spam@No-Span.net>
User-Agent: Thunderbird 2.0.0.14 (Windows/20080421)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Re: How can I Trace the Route to an IP Addres from within my program
References: <MPG.22c8301ab312798c989a97@newsgroups.borland.com> <485faf40$1@newsgroups.borland.com> <MPG.22cb3a3e698457ac989a9a@newsgroups.borland.com> <rrj264107nnff1gqarmhq4p0v1c23k6801@4ax.com> <4861dce2$1@newsgroups.borland.com> <4862a806$1@newsgroups.borland.com>
In-Reply-To: <4862a806$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 65.6.192.60
Message-ID: <486484c8$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1214547144 65.6.192.60 (26 Jun 2008 23:12:24 -0700)
Lines: 130
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!news.isoc.lu!newsgroups.borland.com!not-for-mail