473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

query regarding ping program

Hi,
i am writing a program for ping operation which supports both Ipv4 and
IpV6 ..first i wrote for both of them individually , but i am unable to
mix them such that they support both the versions ... i would be very
thankful if anyone could come out with a complete program ..

Ipv6 prog
---------
#include<stdio.h>
#include<winsock2.h>
#include<IPExport.h>
#include<IcmpAPI.h>
#include <ws2tcpip.h>

#include <windows.h>
#include <winuser.h>

#define MAX_BUF_SIZE 1024
#define m_uPacketSize 0
#define m_msTimeout 5
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
HANDLE hIcmpFile;
HINSTANCE hndlIcmp6;
IP_OPTION_INFORMATION ipInfo = {255, 0, 0, 0, NULL};
CHAR EchoRequest[MAX_BUF_SIZE] = {0}, EchoReply[MAX_BUF_SIZE +
sizeof(ICMP_ECHO_REPLY) + 8];

struct addrinfo *saDest = NULL;
struct sockaddr_in6 saSource;

CHAR *strHost="ff01::01"; //destination
DWORD dwPingReplies;
DWORD dw ;
int retVal;
int err;
HANDLE (WINAPI *Icmp6CreateFile)(VOID);
BOOL (WINAPI *IcmpCloseHandle)(HANDLE);
DWORD (WINAPI *Icmp6SendEcho2)
(HANDLE, HANDLE, FARPROC, PVOID, struct sockaddr_in6*,
struct sockaddr_in6*, LPVOID, WORD, PIP_OPTION_INFORMATION, LPVOID,
DWORD, DWORD);

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable WinSock DLL.*/
return;
}

if (m_uPacketSize > MAX_BUF_SIZE)
{
printf("ERROR - Data size(%d) too big", m_uPacketSize);
return -2;
}
//Load the IP-Helper API
hndlIcmp6 = LoadLibrary("IPHLPAPI.DLL");
if (!hndlIcmp6)
{
printf("ERROR - Could not load IPHLPAPI.DLL");
return -3;
}

// Retrieve IP-Helper ICMPv6 function pointers
Icmp6CreateFile = (HANDLE (WINAPI
*)(void))GetProcAddress(hndlIcmp6,"Icmp6CreateFile ");

IcmpCloseHandle = (BOOL (WINAPI
*)(HANDLE))GetProcAddress(hndlIcmp6,"IcmpCloseHand le");

Icmp6SendEcho2 = (DWORD (WINAPI *)(HANDLE, HANDLE, FARPROC, PVOID,
struct sockaddr_in6*, struct sockaddr_in6*, LPVOID, WORD,
PIP_OPTION_INFORMATION,
LPVOID, DWORD, DWORD)) GetProcAddress(hndlIcmp6,"Icmp6SendEcho2");

// define local source information
saSource.sin6_addr = in6addr_any;
saSource.sin6_family = AF_INET6;
saSource.sin6_flowinfo = 0;
saSource.sin6_port = 0;
// Get address information for the ping destination
/* if ((retVal=getaddrinfo(strHost1, 0 , NULL, &saSource)))
{
printf("Source socket invalid");
FreeLibrary(hndlIcmp6);
return -4;
}*/
//printf("%d",retVal);
if ((retVal= getaddrinfo(strHost, 0 , NULL, &saDest)))
{
printf("Destination socket invalid1111");
FreeLibrary(hndlIcmp6);
return -4;
}
//printf("%d",retVal);
// Create handle to IPv6 ICMP
if ((hIcmpFile = Icmp6CreateFile()) == INVALID_HANDLE_VALUE)
{
printf("ERROR - IcmpCreateFile() failed");
freeaddrinfo(saDest);
FreeLibrary(hndlIcmp6);
return -5;
}

dwPingReplies = Icmp6SendEcho2(hIcmpFile, //IPv6 Ping Handle
NULL, //Event
NULL, //APC Routine
NULL, //APC Context
&saSource,
(struct sockaddr_in6 *)saDest->ai_addr, //Dest address
EchoRequest, //Request data
m_uPacketSize, //Request size
&ipInfo, //Request Options
EchoReply, //Reply buffer
sizeof(EchoReply), //Reply size
m_msTimeout); //Timeout (ms)

if(dwPingReplies == 0)
{
printf("error : %d \n", GetLastError());
}
// Cleanup
printf("hai");

IcmpCloseHandle(hIcmpFile);
freeaddrinfo(saDest);
FreeLibrary(hndlIcmp6);

printf(" Reply from %s: bytes=%d time=%ldms TTL=%d",
strHost,
((ICMP_ECHO_REPLY *)EchoReply)->DataSize,
((ICMP_ECHO_REPLY *)EchoReply)->RoundTripTime > 0 ?
((ICMP_ECHO_REPLY

*)EchoReply)->RoundTripTime : 0,
((ICMP_ECHO_REPLY *)EchoReply)->Options.Ttl);
printf("\n%d",dwPingReplies);

return dwPingReplies;
}

Nov 17 '05 #1
0 1331

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bernd wegener | last post by:
Hello netties, I coded the following in perl: open(LOGFILE,">&STDOUT") ; if ( $ARGV ) { close(LOGFILE) ; open(LOGFILE,">> $ARGV") or die "Cannot open file $ARGV\n" ;
2
by: mabuHB | last post by:
I am trying to write a Wake-On-LAN utility for a local subnet. I can do the Wake-On-LAN, but before this I need to find the MAC addresses on the local LAN. I had planned to query DHCP on the WAN...
17
by: wana | last post by:
I was reading through original source code of ping for some insight and was confused by unusual code. Entire listing available at: http://www.ping127001.com/pingpage/ping.html #include...
0
by: Ed | last post by:
I've attached some VB.NET code I've hacked together (some taken from MS examples & newsgroup postings) that will perform a ping or IcmpSendEcho using the icmp.dll (see this for more info:...
8
by: genojoe | last post by:
I am looking for a VB.NET code that I can use to monitor an independent Web Site, I would like to do an equivalent of a PING of the site every 5 minutes or so and make sure that it returns the...
21
by: Neel | last post by:
I am trying to "ping" a remote host in my C++/Redhat Linux code to check whether that host is connected or not. if (0 == system("ping -w 2 192.168.0.2)) But, in both cases...
7
by: Linus Cohen | last post by:
Hi all, I'm a newbie to python and programming in general, so I wanted a simple project to start off. What I'm trying to do here is write a python command-line ping program, much like the Unix and...
0
by: Nick Stinemates | last post by:
On Tue, Apr 15, 2008 at 07:24:05AM -0700, shawn s wrote: I love that you call the users of your app retards :) That rocks! ping runs forever. tracert doesnt. try: -- Nick Stinemates...
1
by: Karl Kobata | last post by:
Hi Fredrik, This is exactly what I need. Thank you. I would like to do one additional function. I am not using the tokenizer to parse python code. It happens to work very well for my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.