G
Guest
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:ingNew(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;
}
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:ingNew(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;
}