473,325 Members | 2,671 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,325 software developers and data experts.

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::PingNew(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(saSource);
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(saDest);
freeaddrinfo(saSource);
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(saDest);
freeaddrinfo(saSource);
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(EchoReply) + sizeof(ICMP_ECHO_REPLY),
m_msTimeout ); //Timeout (ms)

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

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

freeaddrinfo(saDest);
freeaddrinfo(saSource);
IcmpCloseHandle(hIcmpFile);
return 1;
}
Nov 17 '05 #1
2 6321
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::Ping6(CString strHost, CString &retData)
{
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];
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("IPHLPAPI.DLL");
if (!hndlIcmp6)
{
retData = _T("ERROR - Could not load IPHLPAPI.DLL");
return -3;
}

// Retrieve IP-Helper ICMPv6 function pointers
pIcmp6CreateFile = (HANDLE (WINAPI
*)(void))GetProcAddress(hndlIcmp6,"Icmp6CreateFile ");
pIcmpCloseHandle = (BOOL (WINAPI
*)(HANDLE))GetProcAddress(hndlIcmp6,"IcmpCloseHand le");
pIcmp6SendEcho2 = (DWORD (WINAPI *)(HANDLE, HANDLE, FARPROC, PVOID, struct
sockaddr_in6*, struct sockaddr_in6*, LPVOID, WORD, PIP_OPTION_INFORMATION,
LPVOID, DWORD, DWORD)) GetProcAddress(hndlIcmp6,"Icmp6SendEcho2");
pIcmp6ParseReplies = (DWORD (WINAPI *)(LPVOID, DWORD
))GetProcAddress(hndlIcmp6,"Icmp6ParseReplies");

//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(strHost, 0, NULL, &saDest))
{
retData.Format("Destination socket invalid");
FreeLibrary(hndlIcmp6);
return -4;
}

// Create handle to IPv6 ICMP
if ((hIcmpFile = pIcmp6CreateFile()) == INVALID_HANDLE_VALUE)
{
retData = _T("ERROR - IcmpCreateFile() failed");
freeaddrinfo(saDest);
FreeLibrary(hndlIcmp6);
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(EchoReply), //Reply size
m_msTimeout); //Timeout (ms)
// Cleanup
pIcmpCloseHandle(hIcmpFile);
freeaddrinfo(saDest);
FreeLibrary(hndlIcmp6);

// Translate the replies
pIcmp6ParseReplies(EchoReply, sizeof(EchoReply));
if (!dwPingReplies)
{
if (((ICMP_ECHO_REPLY *)EchoReply)->Status == IP_REQ_TIMED_OUT)
{
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_REPLY *)EchoReply)->DataSize,
((ICMP_ECHO_REPLY *)EchoReply)->RoundTripTime > 0 ? ((ICMP_ECHO_REPLY
*)EchoReply)->RoundTripTime : 0,
((ICMP_ECHO_REPLY *)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::PingNew(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(saSource);
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(saDest);
freeaddrinfo(saSource);
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(saDest);
freeaddrinfo(saSource);
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(EchoReply) + sizeof(ICMP_ECHO_REPLY),
m_msTimeout ); //Timeout (ms)

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

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

freeaddrinfo(saDest);
freeaddrinfo(saSource);
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
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...
7
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...
8
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...
2
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...
8
by: prabhuram.k | last post by:
Can anybody know how to validate IPV4 and IPV6 address in C++
3
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
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...
8
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...
14
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.