Solved

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

Posted on 2010-11-10
1
1,513 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
[X]
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

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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