Solved

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

Posted on 2010-11-10
1
1,498 Views
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...


<CODE SNIP>

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
      &hints,
      &result);

</CODE SNIP>


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    192.168.1.99:8080      0.0.0.0:0              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:

<IPCONFIG>

Ethernet adapter Local Area Connection 2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::ec5e:1dbe:14a9:1a66%13
   IPv4 Address. . . . . . . . . . . : 192.168.1.99
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   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. . . . . . . . . . . : 192.168.0.2
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1

</IPCONFIG>

This is using Visual Studio 2008, Win32 native API.

Thanks in advance for your help.

0
Comment
Question by:stevehibbert
1 Comment
 

Accepted Solution

by:
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
      &hints,
      &result);

Then I get IPv4 and IPv6 local listeners:

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

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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