473,569 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IPv6 pings with Icmp6SendEcho2

I am attempting to write a functon that can perform IPv6 compliant pings.
But, Icmp6SendEcho2 causes an access violation whenever it is called:

First-chance exception at 0x76d641e8 in mping2.exe: 0xC0000005: Access
violation reading location 0x00000000.
Unhandled exception at 0x76d641e8 in mping2.exe: 0xC0000005: Access
violation reading location 0x00000000.

I have been unable to pinpoint the cause of the access violation so far, so
I was hoping somebody here would notice the error of my ways and point it out
to me.
Below is the function in question.

Thank you!
int CIcmpEcho::Ping New(CString strHost, CString &retData)
//uses newer IP-Helper to do pings
//IPv6 compliant
{
HANDLE hIcmpFile;
char EchoRequest[MAX_BUF_SIZE] = {0}, EchoReply[MAX_BUF_SIZE +
sizeof(ICMPECHO )];
DWORD dwPingReplies;
IN_ADDR iaDest;
struct addrinfo *saDest = NULL;
struct addrinfo *saSource = NULL;

if ( getaddrinfo("", 0, NULL, &saSource) != 0 )
//get address information for the ping source
{
TRACE("Invalid Socket (Localhost)\n") ;
return -5;
}

if ( getaddrinfo(":: 1", 0, NULL, &saDest) != 0 )
//get address information for the ping destination
{
TRACE("Invalid Socket (Destination)\n ");
freeaddrinfo(sa Source);
return -4;
}

if (m_uPacketSize > MAX_BUF_SIZE)
//check for ping packets that are too large
{
retData.Format( "ERROR - Data size too big %d", m_uPacketSize);
freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
return -3;
}

if ((hIcmpFile = Icmp6CreateFile ()) == INVALID_HANDLE_ VALUE)
//Create handle to IPv6 ICMP
{
TRACE("\tUnable to open file.\n");
retData = _T("ERROR - IcmpCreateFile( ) failed");
freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
return -1;
}

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

if ( !dwPingReplies )
{
if (((ICMPECHO *)EchoReply)->Status == IP_REQ_TIMED_OU T)
{
retData = _T("timed out");
freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
IcmpCloseHandle (hIcmpFile);
return 0;
}
else
{
retData.Format( "ERROR - Status: %ld", GetLastError()) ;
freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
IcmpCloseHandle (hIcmpFile);
return -5;
}
}

iaDest.s_addr = ((ICMPECHO *)EchoReply)->Source;
retData.Format( "Reply from %s: bytes=%d time=%ldms TTL=%d",
inet_ntoa(iaDes t),
((ICMPECHO *)EchoReply)->DataSize,
((ICMPECHO *)EchoReply)->RTTime > 0 ? ((ICMPECHO *)EchoReply)->RTTime :
0,
((ICMPECHO *)EchoReply)->ipInfo.Ttl);

freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
IcmpCloseHandle (hIcmpFile);
return 1;
}
Nov 17 '05 #1
2 6366
I've answered my own question. Since I've seen little literature on the
topic, I'll go ahead and post my answer here. My problem was silly, I'd
forgotton to specify a required parameter (IP option information). Here is
the working code:

int CIcmpEcho::Ping 6(CString strHost, CString &retData)
{
HANDLE hIcmpFile;
HINSTANCE hndlIcmp6;
IP_OPTION_INFOR MATION ipInfo = {255, 0, 0, 0, NULL};
char EchoRequest[MAX_BUF_SIZE] = {0}, EchoReply[MAX_BUF_SIZE +
sizeof(ICMP_ECH O_REPLY) + 8];
DWORD dwPingReplies;
struct addrinfo *saDest = NULL;
struct sockaddr_in6 sa6Source;

if (m_uPacketSize > MAX_BUF_SIZE)
{
retData.Format( "ERROR - Data size(%d) too big", m_uPacketSize);
return -2;
}

//Load the IP-Helper API
hndlIcmp6 = LoadLibrary("IP HLPAPI.DLL");
if (!hndlIcmp6)
{
retData = _T("ERROR - Could not load IPHLPAPI.DLL");
return -3;
}

// Retrieve IP-Helper ICMPv6 function pointers
pIcmp6CreateFil e = (HANDLE (WINAPI
*)(void))GetPro cAddress(hndlIc mp6,"Icmp6Creat eFile");
pIcmpCloseHandl e = (BOOL (WINAPI
*)(HANDLE))GetP rocAddress(hndl Icmp6,"IcmpClos eHandle");
pIcmp6SendEcho2 = (DWORD (WINAPI *)(HANDLE, HANDLE, FARPROC, PVOID, struct
sockaddr_in6*, struct sockaddr_in6*, LPVOID, WORD, PIP_OPTION_INFO RMATION,
LPVOID, DWORD, DWORD)) GetProcAddress( hndlIcmp6,"Icmp 6SendEcho2");
pIcmp6ParseRepl ies = (DWORD (WINAPI *)(LPVOID, DWORD
))GetProcAddres s(hndlIcmp6,"Ic mp6ParseReplies ");

//define local source information
sa6Source.sin6_ addr = in6addr_any;
sa6Source.sin6_ family = AF_INET6;
sa6Source.sin6_ flowinfo = 0;
sa6Source.sin6_ port = 0;

// Get address information for the ping destination
if (getaddrinfo(st rHost, 0, NULL, &saDest))
{
retData.Format( "Destinatio n socket invalid");
FreeLibrary(hnd lIcmp6);
return -4;
}

// Create handle to IPv6 ICMP
if ((hIcmpFile = pIcmp6CreateFil e()) == INVALID_HANDLE_ VALUE)
{
retData = _T("ERROR - IcmpCreateFile( ) failed");
freeaddrinfo(sa Dest);
FreeLibrary(hnd lIcmp6);
return -5;
}

dwPingReplies = pIcmp6SendEcho2 (hIcmpFile, //IPv6 Ping Handle
NULL, //Event
NULL, //APC Routine
NULL, //APC Context
&sa6Source, //local source address
(struct sockaddr_in6 *)saDest->ai_addr, //Dest address
EchoRequest, //Request data
m_uPacketSize, //Request size
&ipInfo, //Request Options
EchoReply, //Reply buffer
sizeof(EchoRepl y), //Reply size
m_msTimeout); //Timeout (ms)
// Cleanup
pIcmpCloseHandl e(hIcmpFile);
freeaddrinfo(sa Dest);
FreeLibrary(hnd lIcmp6);

// Translate the replies
pIcmp6ParseRepl ies(EchoReply, sizeof(EchoRepl y));
if (!dwPingReplies )
{
if (((ICMP_ECHO_RE PLY *)EchoReply)->Status == IP_REQ_TIMED_OU T)
{
retData = _T("timed out");
return 0;
}
else
{
retData.Format( "ERROR - Status: %ld", GetLastError()) ;
return -6;
}
}

retData.Format( "Reply from %s: bytes=%d time=%ldms TTL=%d",
"unknown",
((ICMP_ECHO_REP LY *)EchoReply)->DataSize,
((ICMP_ECHO_REP LY *)EchoReply)->RoundTripTim e > 0 ? ((ICMP_ECHO_REP LY
*)EchoReply)->RoundTripTim e : 0,
((ICMP_ECHO_REP LY *)EchoReply)->Options.Ttl) ;
return dwPingReplies;
}
"PaulH" wrote:
I am attempting to write a functon that can perform IPv6 compliant pings.
But, Icmp6SendEcho2 causes an access violation whenever it is called:

First-chance exception at 0x76d641e8 in mping2.exe: 0xC0000005: Access
violation reading location 0x00000000.
Unhandled exception at 0x76d641e8 in mping2.exe: 0xC0000005: Access
violation reading location 0x00000000.

I have been unable to pinpoint the cause of the access violation so far, so
I was hoping somebody here would notice the error of my ways and point it out
to me.
Below is the function in question.

Thank you!
int CIcmpEcho::Ping New(CString strHost, CString &retData)
//uses newer IP-Helper to do pings
//IPv6 compliant
{
HANDLE hIcmpFile;
char EchoRequest[MAX_BUF_SIZE] = {0}, EchoReply[MAX_BUF_SIZE +
sizeof(ICMPECHO )];
DWORD dwPingReplies;
IN_ADDR iaDest;
struct addrinfo *saDest = NULL;
struct addrinfo *saSource = NULL;

if ( getaddrinfo("", 0, NULL, &saSource) != 0 )
//get address information for the ping source
{
TRACE("Invalid Socket (Localhost)\n") ;
return -5;
}

if ( getaddrinfo(":: 1", 0, NULL, &saDest) != 0 )
//get address information for the ping destination
{
TRACE("Invalid Socket (Destination)\n ");
freeaddrinfo(sa Source);
return -4;
}

if (m_uPacketSize > MAX_BUF_SIZE)
//check for ping packets that are too large
{
retData.Format( "ERROR - Data size too big %d", m_uPacketSize);
freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
return -3;
}

if ((hIcmpFile = Icmp6CreateFile ()) == INVALID_HANDLE_ VALUE)
//Create handle to IPv6 ICMP
{
TRACE("\tUnable to open file.\n");
retData = _T("ERROR - IcmpCreateFile( ) failed");
freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
return -1;
}

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

if ( !dwPingReplies )
{
if (((ICMPECHO *)EchoReply)->Status == IP_REQ_TIMED_OU T)
{
retData = _T("timed out");
freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
IcmpCloseHandle (hIcmpFile);
return 0;
}
else
{
retData.Format( "ERROR - Status: %ld", GetLastError()) ;
freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
IcmpCloseHandle (hIcmpFile);
return -5;
}
}

iaDest.s_addr = ((ICMPECHO *)EchoReply)->Source;
retData.Format( "Reply from %s: bytes=%d time=%ldms TTL=%d",
inet_ntoa(iaDes t),
((ICMPECHO *)EchoReply)->DataSize,
((ICMPECHO *)EchoReply)->RTTime > 0 ? ((ICMPECHO *)EchoReply)->RTTime :
0,
((ICMPECHO *)EchoReply)->ipInfo.Ttl);

freeaddrinfo(sa Dest);
freeaddrinfo(sa Source);
IcmpCloseHandle (hIcmpFile);
return 1;
}

Nov 17 '05 #2

i would be so grateful if someone comes out with a program that supports
both IPv4 and IPv6

ie if we give a host name to ping , how to know whether the given
ipaddress is a IPv4 one or Ipv6 one ... is there any function that
differentiate these both...

Nov 17 '05 #3

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

Similar topics

0
1785
by: Agent | last post by:
I would like to invite you and your associates to attend the US IPv6 (Internet Protocol version 6) Summit, December 8-11, 2003 in Crystal City, VA, near the Pentagon. This major opportunity to participate in transitioning to the new Internet is in response to both DoD and Industry requests for a concentrated, affordable conference in the...
7
5909
by: Torsten Schmidt | last post by:
Hi, I'm trying to connect to a mysql-Server using PHP's mysql-function mysql_connect. The host on which the mysql-server is running is not the same as the host apache and php are running on. The two computers are connected in an IPv6-network. No matter if I give the hostname (defined in /etc/hosts), the IPv6-address ("fec0::4") or the...
8
2529
by: John Burton | last post by:
I'm not sure if this is a question about python socket support or sockets in general ... I have a socket server with which I want to accept incoming connections on ipv4 and ipv6. I've found that if I create the listening socket with sock = socket(AF_INET, SOCK_STREAM) it will accept connections on ipv4 (only) and if I create it with sock...
2
3013
by: gregory_may | last post by:
First the research links: IPv6 spec (look for 'jumbo payload'): http://www.cs-ipv6.lancs.ac.uk/ipv6/documents/rfcs/archive/rfc1883.txt IPv6 Sample C# Client/Server http://www.codeproject.com/csharp/ipv6.asp I am developing an application where I need to broadcast information to clients. I have hit a 64K limit in UDP broadcasting. I...
8
9932
by: prabhuram.k | last post by:
Can anybody know how to validate IPV4 and IPV6 address in C++
3
6233
by: jiri.juranek | last post by:
Hello, is there any common function for validation if string contains valid ip address(both ipv4 and ipv6)? Or does sb wrote some regular expression for this? thanks J
3
1833
by: Pramod TK | last post by:
Hello All, I have some queries related to python support for IPv6. Can you kindly clarify the doubts which I have - 1. Does python support IPv6? 2. Does it support setting of QoS flags? 3. Does it support tunneling of IPv6 on a IPv4 network? 4. If an IPv4 address is given, does it support this on a IPv6 network? If not can you kindly let...
8
8939
by: Giampaolo Rodola' | last post by:
I'm not sure if this is a question about python programming, system administration or sockets in general... I have the FTP server in my signature to which I'd want to add IPv6 support. My hosting company provides me a common IPv4 address. I'd like to set up my workstation (Windows XP or Linux Debian, doesn't really matter) to temporarily use...
14
9248
by: Simon | last post by:
Hi, is there a straight forward way of converting IPv4 to IPv6? I thought that it was just a matter of converting 32 bits to 128 bits, (by adding 96 leading 0s), but that does not seem right in some/most cases. For example, 127.0.0.1, (IPv4 localhost), does not convert ::1, (IPv6 localhost)
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.