locked
trouble in getting local ipv6 address RRS feed

  • Question

  • hello all,

    i am trying to get the ipv6  local address of the system. i use gethostname. and with the hostname  i am trying to get the address. i want the address in dotted format.

        struct addrinfo *hostptr;
        struct in6_addr *ptr;
        getaddrinfo ((PCSTR)szLocalAddress, NULL, NULL, &hostptr);
        ptr = (struct in6_addr *) hostptr->ai_addr;
        WSAStringToAddress(&(ptr), AF_INET6, NULL, szLocalAddress, sizeof(szLocalAddress));

    i am able to get the host name. but not able to get the ipv6 address.

    please help. where am i going wrong ?

    regards,

    kumaresh


    Regards, Kumaresh

    Friday, December 28, 2012 7:36 AM

Answers

  • Let's see. First I'll conclude that this is C code and uses ANSI characters, otherwise it would not compile. In this case the PCSTR cast you had earlier was "correct" but useless, I see that you have removed it.

    The main problem with your code is the incorrect use of WSAAddressToString, you're passing wrong parameters to it. This code also leak memory because it doesn't free the hostptr. The last few lines of your function should look like this:

        } else {
            int result = 0;
    
            if (GetAddrInfo(szLocalAddress, NULL, NULL, &hostptr) == 0) {
                result = WSAAddressToString(hostptr->ai_addr, hostptr->ai_addrlen, NULL, szLocalAddress, &len) == 0;
                FreeAddrInfo(hostptr);
            }
    
            return result;
        }
    

    In particular the second parameter needs to contain the address length, not address family and the last parameter is supposed to be a pointer to a DWORD containing the length of the buffer, not sizeof(szLocalAddress) which is completly meaningless in this context.

    It should be noted that getaddrinfo returns a list of addresses, not a single address. Obviously your code only returns the first address in that list.

    • Marked as answer by Damon Zheng Friday, January 4, 2013 6:05 AM
    Friday, December 28, 2012 1:40 PM

All replies

  • What exactly do you mean by dotted format? Normally that's the string representation of a IPv4 address but it seems that you are lookingf for an IPv6 address. You also use WSAStringToAddress when you probably should use WSAAddressToString, after all getaddrinfo gives you an address, not a string.
    Friday, December 28, 2012 8:17 AM
  • yes i meant the string representation. getaddrinfo should give me the address in ai_addr field, which is null! and it is not able to evaluate ai_cannonname also.

    Regards, Kumaresh

    Friday, December 28, 2012 10:15 AM
  • "should give me the address in ai_addr field, which is null!"

    That probably means that getaddrinfo failed, you should always check for errors when calling such functions. What's up with the (PCSTR) cast for szLocalAddress? Such casts usually indicate a problem with Unicode vs. ANSI and in this case a cast is the wrong way to solve it.

    "and it is not able to evaluate ai_cannonname also."

    Your need to pass AI_CANONNAME as a hint to getaddrinfo for that. Even when you the function may still return null for ai_canonname.

    Friday, December 28, 2012 10:35 AM

  • i checked for errors. there were none. the function is returning 0. well that cast was very unnecessary. removed it. still not able to resolve local ipv6 address.

    Regards, Kumaresh

    Friday, December 28, 2012 11:47 AM
  • this is the full function

    GetLocalAddress (char *szLocalAddress, int len)
    {

      char szBuffer[40];
      struct addrinfo *hostptr;
      struct in6_addr *ptr;
      int i;
                                                                
      /* Let us try getting local host name and the IP address corresponding to the name. */

      if (gethostname (szLocalAddress, len) == SOCKET_ERROR) {

        MessageBox (NULL, "Host name not given", "HOST ENTRY ??", MB_OK);

        /* First get the "Interfaces" string from MSTCP section of system.ini.
           This code is highly specific to Microsoft's TCP/IP implementation.
         */

        GetPrivateProfileString ("MSTCP", "Interfaces", "Installation of MSTCP required", szBuffer, sizeof szBuffer, SessionInformation.IniFile);
        if (!strcmp (szBuffer, "Installation of MSTCP required")) {
          MessageBox (NULL, szBuffer, "Initialisation Error ", MB_OK);
          return 0;
        }
        GetPrivateProfileString (szBuffer, "IPAddress", "IP address not found: Configure MSTCP", szLocalAddress, len, SessionInformation.IniFile);
        if (!strcmp (szLocalAddress, "IP address not found in system.ini: Configure MSTCP")) {
          MessageBox (NULL, szLocalAddress, "Initialisation Error ", MB_OK);
          return 0;
        }
      } else {
        
        GetAddrInfo (szLocalAddress, NULL, NULL, &hostptr);
        ptr = (struct in6_addr *) hostptr->ai_addr;
        WSAAddressToString(&(ptr), AF_INET6, NULL, szLocalAddress, sizeof(szLocalAddress));
        return 1;
      }

      return 0;
    }


    Regards, Kumaresh

    Friday, December 28, 2012 11:48 AM
  • Let's see. First I'll conclude that this is C code and uses ANSI characters, otherwise it would not compile. In this case the PCSTR cast you had earlier was "correct" but useless, I see that you have removed it.

    The main problem with your code is the incorrect use of WSAAddressToString, you're passing wrong parameters to it. This code also leak memory because it doesn't free the hostptr. The last few lines of your function should look like this:

        } else {
            int result = 0;
    
            if (GetAddrInfo(szLocalAddress, NULL, NULL, &hostptr) == 0) {
                result = WSAAddressToString(hostptr->ai_addr, hostptr->ai_addrlen, NULL, szLocalAddress, &len) == 0;
                FreeAddrInfo(hostptr);
            }
    
            return result;
        }
    

    In particular the second parameter needs to contain the address length, not address family and the last parameter is supposed to be a pointer to a DWORD containing the length of the buffer, not sizeof(szLocalAddress) which is completly meaningless in this context.

    It should be noted that getaddrinfo returns a list of addresses, not a single address. Obviously your code only returns the first address in that list.

    • Marked as answer by Damon Zheng Friday, January 4, 2013 6:05 AM
    Friday, December 28, 2012 1:40 PM