Win32 C++ API getaddrinfo() is not returning IPv6 interfaces VC9

Posted on 2010-11-10
Last Modified: 2012-05-10

I am writing sockets code to listen on the local interfaces for a port, and I can't get both IPv4 and IPv6 sockaddr's back from getaddrinfo().

On my understanding (and experience so far) you can bind a listening socket to both the IPv4 and IPv6 local interfaces on a machine.  [Maybe I have this wrong, but I believe about a year ago I had this working, because I was getting a bug where I was getting two simultaneous socket connects on both IPv4 and IPv6 listeners, which was fixed].  The aim is to be IP independent, and to make code work on either Ipv4 or IPv6 or both.

I am using getaddrinfo with AI_PASSIVE, no address string passed in, to try to get SOCKADDR's for both IPv4 and IPv6 interfaces.  Documentation for the function suggests I should get both address families back.  Here's a code extract for the getaddrinfo call...


struct      addrinfo      hints;                  
struct      addrinfo*      ptr = NULL;            
memset(&hints, 0, sizeof(hints));            

if ( strlen(address) == 0 )                  // the passed address = ""
    hints.ai_flags            = AI_PASSIVE;

hints.ai_family            = iAF;      // PASSED AF_UNSPEC
hints.ai_socktype            = SOCK_STREAM;      
hints.ai_protocol            = IPPROTO_TCP;      

int iError = getaddrinfo(
      address,                  // blank string ""
      (FC::String(port)).c_str(),            // private string class


At the moment I am only getting a single IPv4 structure back from the function.  For an example port 8080, using "netstat -an" I can see the listener:

  TCP              LISTENING

... and I have successfully attached a connect socket to this also, so the IPv4 side of things works.

If I pass AF_INET6 as the iAF address family variable, I get two potential SOCKADDR's returned for the fe80 IPv6 link local interfaces, and I can successfully listen on them, so I know that there is potential for this to work...

  TCP    [fe80::3976:1edf:db71:e48f%9]:8080  [::]:0                 LISTENING
  TCP    [fe80::ec5e:1dbe:14a9:1a66%13]:8080  [::]:0                 LISTENING

One thing I know that has changed since I believe I saw getaddrinfo() work, and return both 4 and 6 addresses, is that my BT router has been replaced by a Sky router.  As I am using link-local stuff only, surely this should make no difference?  Plus, I get IPv6 listener sockaddr's if I specifically ask for them, so I am thinking that getaddrinfo should return these interfaces as potential listener sockaddr's.

This is my machine's IP Config report:


Ethernet adapter Local Area Connection 2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::ec5e:1dbe:14a9:1a66%13
   IPv4 Address. . . . . . . . . . . :
   Subnet Mask . . . . . . . . . . . :
   Default Gateway . . . . . . . . . :

Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . : home
   Link-local IPv6 Address . . . . . : fe80::3976:1edf:db71:e48f%9
   IPv4 Address. . . . . . . . . . . :
   Subnet Mask . . . . . . . . . . . :
   Default Gateway . . . . . . . . . :


This is using Visual Studio 2008, Win32 native API.

Thanks in advance for your help.

Question by:stevehibbert
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment

Accepted Solution

stevehibbert earned 0 total points
ID: 34147204
Maybe I have figured it out.  Originally, in the example above, I am passing an empty string, ie a valid c string with zero characters.  This gives me IPv4 addresses only.
To get local interface candidates back, I have to pass NULL to the function...

int iError = getaddrinfo(
      NULL,                  // null pointer
      (FC::String(port)).c_str(),            // private string class

Then I get IPv4 and IPv6 local listeners:

  TCP               LISTENING
  TCP    [::]:8080              [::]:0                 LISTENING


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question