Today I write a sample code for my customer for demonstrating how to implement a IPv6 ping command. I would like to share it here with you.

#include "StdAfx.h"
#include "GRunner.h"
#include "ipexport.h"
#include "Icmpapi.h"
#include "Iphlpapi.h"
#include "Winsock2.h"
#include "ws2tcpip.h"
#include <conio.h>
#include <ctype.h>


void testIPv6Ping()

...{
HANDLE hIcmp6File = NULL;
struct sockaddr_in6* source=NULL;
struct addrinfo* dest=NULL;
char* requestData = NULL;
char* replyData = NULL;
char* strHost = NULL;

int nret = 0;
const int REQUEST_DATA_SIZE = 6;

IP_OPTION_INFORMATION ipInfo=...{255, 0, 0, 0, NULL};
source = new sockaddr_in6;
strHost = new char[128];
requestData = new char[EQUEST_DATA_SIZE];
strcpy(requestData,"HELLO");
int reply_data_size = sizeof(ICMPV6_ECHO_REPLY)+REQUEST_DATA_SIZE;
replyData = new char[reply_data_size];
int try_timeout = 1000;
char end;

WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );

if((hIcmp6File = Icmp6CreateFile())==INVALID_HANDLE_VALUE)

...{
printf(" Unable to open handle! ");
printf("Icmp6CreateFile returned error: %ld ",GetLastError());
}
else

...{
source->sin6_family = AF_INET6;
source->sin6_addr = in6addr_any;
source->sin6_flowinfo = 0;
source->sin6_port=0;

printf("input the destination address: ");
scanf("%s",strHost);
if((nret = getaddrinfo(strHost,NULL,NULL,&dest))!=0)

...{
printf("Invalid dest! Type any character to exit!");
_getche();
}
else

...{

do...{
DWORD numreplies = Icmp6SendEcho2( hIcmp6File, 0, 0, 0,
source, (sockaddr_in6*)dest,
requestData, 5, &ipInfo,
replyData, reply_data_size,
try_timeout ) ;
printf("output:%s ",replyData);
}while ( (end=_getch())==' ' );
}
IcmpCloseHandle(hIcmp6File);
}
delete source;
freeaddrinfo(dest);
delete[] requestData;
delete[] replyData;

WSACleanup();

}

























































































