CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2004
    Posts
    133

    Question getaddrinfo does not resolve IPV6 address

    Hi all

    I am in deeeeeeeeep trouble....i have to resolve this one, so please help

    the getaddrinfo, retuns the IP address for given hostname , my application works fine and fetches the IPV4 and IPV6 addresses, But getaddrinfo fails , if the hostname is remote.
    it does not return IPV6 addresses, but returns IPV4 addreses.

    i need the IPV6 addresses of remote host

    the Below is the code, if anyone can suggest what could be the problem , it would be of great help.

    {code}
    void PrintIPv4(char *sHostName,char *type)
    {
    DWORD dwRetval;
    struct addrinfo *result = NULL;
    struct addrinfo *ptr = NULL;
    struct sockaddr_in *sockaddr_ipv4;
    struct addrinfo sHints;

    ZeroMemory( &sHints, sizeof(sHints) );
    sHints.ai_family = AF_INET;
    sHints.ai_socktype = SOCK_STREAM;

    dwRetval = getaddrinfo(sHostName, NULL, &sHints, &result);
    if ( dwRetval != 0 ) {
    printf("getaddrinfo failed for IPV4 with error: %d\n", dwRetval);
    WSACleanup();
    return;
    }

    // Retrieve each address and print out the hex bytes
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next)
    {
    if(ptr->ai_family == AF_INET)
    {
    printf("AF_INET (IPv4)\n");
    sockaddr_ipv4 = (struct sockaddr_in *) ptr->ai_addr;
    printf("For HostName \"%s\" IPv4 address %s\n Type is %s",sHostName,inet_ntoa(sockaddr_ipv4->sin_addr),type );
    }
    }
    freeaddrinfo(result);

    }


    void PrintIPv6(char *sHostName,char *type)
    {
    DWORD dwRetval;
    struct addrinfo *result = NULL;
    struct addrinfo *ptr = NULL;
    struct addrinfo sHints;

    INT iRetval;
    LPSOCKADDR sockaddr_ip;

    char ipstringbuffer[46];
    DWORD ipbufferlength = 46;

    ZeroMemory( &sHints, sizeof(sHints) );
    sHints.ai_family = AF_INET6;
    sHints.ai_socktype = SOCK_STREAM;


    dwRetval = getaddrinfo(sHostName, NULL, &sHints, &result);
    if ( dwRetval != 0 ) {
    printf("getaddrinfo(IPV6) Failed for HostName =\"%s\" Type = \"%s\" Return vlaue = %d\n\n",sHostName,type,dwRetval);
    WSACleanup();
    return;
    }

    printf("getaddrinfo IPV6 returned success for \"%s\" which is a \"%s\" \n\n",sHostName,type);

    // Retrieve each address and print out the hex bytes
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next)
    {
    if(ptr->ai_family == AF_INET6)
    {
    printf("AF_INET6 (IPv6)\n");
    sockaddr_ip = (LPSOCKADDR) ptr->ai_addr;
    ipbufferlength = 46;
    iRetval = WSAAddressToString(sockaddr_ip, (DWORD) ptr->ai_addrlen, NULL,
    ipstringbuffer, &ipbufferlength );
    if (iRetval)
    printf("WSAAddressToString failed with %u\n", WSAGetLastError() );
    else
    printf("For the Hostname \"%s\" IPv6 address %s\n",sHostName,ipstringbuffer);

    }
    }
    freeaddrinfo(result);

    }



    int __cdecl main(int argc, char **argv)
    {

    //-----------------------------------------
    // Declare and initialize variables
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
    printf("WSAStartup failed: %d\n", iResult);
    return 1;
    }

    PrintIPv4(argv[1],argv[2]);
    PrintIPv6(argv[1],argv[2]);

    WSACleanup();
    //--------------------------------
    // Setup the hints address info structure
    // which is passed to the getaddrinfo() function


    return 0;
    }


    {code}

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: getaddrinfo does not resolve IPV6 address

    It may be a stupid question, but since you say it works internally, are you sure your gateway supports IPV6? Does WSAGetLastError return anything useful?

    (Also, please use code tags.)

  3. #3
    Join Date
    Nov 2004
    Posts
    133

    Re: getaddrinfo does not resolve IPV6 address

    Hi

    i get this error for IPV6

    WSAHOST_NOT_FOUND
    11001

    Host not found.
    No such host is known. The name is not an official host name or alias, or it cannot be found in the database(s) being queried. This error may also be returned for protocol and service queries, and means that the specified name could not be found in the relevant database.

    But i think you are right, may be i dont have the IPV6 envorinment, can you tell me what other things i should take care off for the the IPV6 to work correctly.


    regards
    pradish

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured