🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#165
From: Bob Gonder
Date: Sun, 29 Jun 2008 20:21
71 lines
2835 bytes
Asger Joergensen wrote:

>Do You know of anything for windows ?

I just worked this up while playing with the ICMP_RR thread.

#include <winsock2.h>
#include <icmpapi.h>
#include <iphlpapi.h>

#pragma pack(1)
typedef struct {
    BYTE   cbType;
    BYTE   cbSize;
    BYTE   pointer;
    struct in_addr address;
} RecordRouteOption, *PRecordRouteOption;


void _stdcall TraceRoute( struct in_addr addr )
{
	char*	SendData = "Hello";
	UCHAR	TTL = 0;
	HANDLE	icmp = IcmpCreateFile();
	DWORD	count;
	DWORD	ReplySize = sizeof(ICMP_ECHO_REPLY) +
			sizeof(RecordRouteOption) +
			sizeof("Hello") + 8;
	ICMP_ECHO_REPLY* reply = (PICMP_ECHO_REPLY) malloc(ReplySize);
	IP_OPTION_INFORMATION info;
	struct in_addr	LastAddr = {0};

	if( (icmp != INVALID_HANDLE_VALUE) && (reply != NULL) )
	{
	    printf("Start trace\r\n");
	    printf( "\tHops\tReply From\tRoundTrip\n" );
	    while( LastAddr.s_addr != addr.s_addr )
	    {
		memset( &info, 0, sizeof(info) );
		info.Ttl = TTL++;
		count = IcmpSendEcho2( icmp, NULL,NULL,NULL,
				addr.s_addr, SendData, sizeof("Hello"),
				&info, reply, ReplySize, 1000 );
		if( count )
		{
			LastAddr.s_addr = reply->Address;
			printf("\t%3d:\t%s:\t%3dms %s\r\n",
				info.Ttl,
				inet_ntoa(LastAddr),
				reply->RoundTripTime,
				((reply->DataSize && reply->Data)? reply->Data:" ")
				);
		}else{
			// DWORD error = GetLastError();
			// Add error handling if desired
		};
		if( TTL == 0 )
		{
			//ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData) + 8;
			//Causes destination to be unable to reply (reply too small).
			printf( "Unreachable within 255 hops, or Program Error\n");
			break;
		};
	    };// while addr
	    printf("End trace\r\n");
	}else{
		// report lack of memory or icmp resources
	};// if icmp
	free( reply );
	IcmpCloseHandle(icmp);
}
Technical Details
Message-ID: <0bqf641akpt80elfgll6oaihucmhjp2gp7@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.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>

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
From: Bob Gonder <none@nowhere.invalid>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Re: How can I Trace the Route to an IP Addres from within my program
Date: Sun, 29 Jun 2008 13:21:16 -0700
Message-ID: <0bqf641akpt80elfgll6oaihucmhjp2gp7@4ax.com>
References: <MPG.22c8301ab312798c989a97@newsgroups.borland.com> <485faf40$1@newsgroups.borland.com> <MPG.22cb3a3e698457ac989a9a@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 67.77.147.203
X-Trace: newsgroups.borland.com 1214770874 67.77.147.203 (29 Jun 2008 13:21:14 -0700)
Lines: 73
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder.news-service.com!news.isoc.lu!newsgroups.borland.com!not-for-mail