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;
}