Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-10
1
Medium Priority
?
1,543 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

885 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