How to implement an IPv6 ping command in VC++

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.

/*The code is post here for your reference and welcome your criticism. Wacle-2007-May-24*/
   #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
={255000, 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( 22 );     
    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, 000,
                                                     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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值