Solved

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

Posted on 2010-11-10
1
1,480 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now