Click to See Complete Forum and Search --> : getaddrinfo error


rohitnegi
March 22nd, 2010, 05:25 AM
Below line of code i had found somewhere and tried to find a internet connection is enabled or not but it is giving getaddrinfo error. This was working fine some days before but now giving problem. what could be reason?

char szAddrASCII[MAX_LOCAL_NAME_LEN];
ADDRINFO AddrHints, *pAI, *pAddrInfo;


memset(&AddrHints, 0, sizeof(AddrHints));
AddrHints.ai_family = PF_UNSPEC;//caller will accept any protocol family
AddrHints.ai_flags = AI_PASSIVE;

if(getaddrinfo(szAddrASCII, "10", &AddrHints, &pAddrInfo))
{
AfxMessageBox(L"getaddrinfo error %d ", WSAGetLastError() );
return FALSE;
}

hoxsiew
March 22nd, 2010, 07:36 AM
What kind of problems?

rohitnegi
March 23rd, 2010, 12:34 AM
getaddrinfo() is solved but now I am getting scalar deleting destructor(void )error when my below function returns . I have enabled my internet connection but why it executes
if(memcmp(&(((SOCKADDR_IN6 *)(pAI->ai_addr))->sin6_addr), &BIN_IPV6_ADDR_LOOPBACK, sizeof(BIN_IPV6_ADDR_LOOPBACK)) == 0) line of code ?


BOOL CNetwork::IsPresent()
{
WSADATA WSAData ;
WSAStartup(MAKEWORD(2,2),&WSAData);


BOOL bFoundLocalAddr = FALSE;
char szAddrASCII[MAX_LOCAL_NAME_LEN];
ADDRINFO AddrHints, *pAI, *pAddrInfo;

//
// Get the local host's name in ASCII text.
//
if(gethostname(szAddrASCII, MAX_LOCAL_NAME_LEN - 1))
{
// Print(TEXT("Error getting local host name, error = %d"), WSAGetLastError());
AfxMessageBox(L"Error getting local host name, error = %d" , WSAGetLastError());

return FALSE;
}

//
// To obtain a list of all the local
// addresses, resolve the local name with getaddrinfo for all

memset(&AddrHints, 0, sizeof(AddrHints));
AddrHints.ai_family = PF_UNSPEC;//caller will accept any protocol family
AddrHints.ai_flags = AI_PASSIVE; //the caller intends to use the returned socket address struc
int err ;
if((err=getaddrinfo(szAddrASCII, "10", &AddrHints, &pAddrInfo))!=0)
{
// Print(TEXT("getaddrinfo(%hs) error %d"), szAddrASCII, WSAGetLastError());
//AfxMessageBox(L"getaddrinfo error %d ", WSAGetLastError() );
printf("errore is %d = %s", err ,gai_strerror(err));
CString s ;

s.Format(L"error is %d", err);

return FALSE;
}
//
// Search the addresses returned.
// If any of them match the loopback address, then
// are not connected to an outside network.
//
// Note: This will not tell you how many networks you
// are connected to. If one or more networks are present,
// then the loopback addresses will not be included in the
// list returned from getaddrinfo.
bFoundLocalAddr = TRUE;
for(pAI = pAddrInfo; pAI != NULL && bFoundLocalAddr; pAI = pAI->ai_next)
{
if(pAI->ai_family == PF_INET)
{
if(memcmp(&(((SOCKADDR_IN *)(pAI->ai_addr))->sin_addr), &BIN_IPV4_ADDR_LOOPBACK, sizeof(BIN_IPV4_ADDR_LOOPBACK)) == 0)

bFoundLocalAddr = FALSE;
}
else if(pAI->ai_family == PF_INET6)
{
if(memcmp(&(((SOCKADDR_IN6 *)(pAI->ai_addr))->sin6_addr), &BIN_IPV6_ADDR_LOOPBACK, sizeof(BIN_IPV6_ADDR_LOOPBACK)) == 0)
bFoundLocalAddr = FALSE;
}
}

freeaddrinfo(pAddrInfo);

return bFoundLocalAddr;
}


thanks
Rohit