0

I'm trying to build a simple webserver which is IP version agnostic.

This is my sample code snippet

hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_flags = AI_PASSIVE;     // fill in my IP for me
hints.ai_socktype = SOCK_STREAM;




if ((status = getaddrinfo(NULL, "8000", &hints, &res)) != 0){
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
    exit(0);
}   

After populating status, I go through each element of the linked list "res" till I find a valid entry, and then I initialize a socket with that structure.

My question is, in the linked list "res" returned by getaddrinfo(), do I get separate structures for the IPv4 and IPv6 loopback addresses? And in that case, do I need to create two sockets to listen and serve the IPv4 and IPv6 loopback addresses seperately? Or is there a single structure in the "res" linked list which can be used to create a socket which "magically" listens on both the IPv4/v6 loopback addresses?

Thank you

Gaurav Suman
  • 515
  • 1
  • 3
  • 17

1 Answers1

2

My question is, in the linked list "res" returned by getaddrinfo(), do I get separate structures for the IPv4 and IPv6 loopback addresses?

You get separate results for IPv4 (family AF_INET) and IPv6 (family AF_INET6) addresses. With your particular arguments, however, you should not get results bearing a loopback address, because you've given specified no node and given the AI_PASSIVE hint. Every struct returned should represent a wildcard address.

And in that case, do I need to create two sockets to listen and serve the IPv4 and IPv6 loopback addresses seperately?

No, you should not need multiple sockets. You will get both IPv4 and IPv6 addresses only on a machine running a dual stack. If you want to serve both IPv4 and IPv6 clients from such a machine (and you should), then just be sure to select an IPv6 address. That should work for clients using either protocol.

Or is there a single structure in the "res" linked list which can be used to create a socket which "magically" listens on both the IPv4/v6 loopback addresses?

There's a bit of magic involved, but it's at the network and protocol levels. IPv6 was designed with the expectation of a significant transition period, and therefore it has good provisions for interoperability with IPv4.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thanks for your detailed reply. When I go through the returned linked list, I do get a structure for the ipv4 loopback address (I stop at the first valid structure in the linked list) and I can bind to it and deliver webpages to my browser using the ipv4 address 127.0.0.1. What I want to do is to create a webserver which I can access using both the ipv4 127.0.0.1 or the ipv6 [::1] loopback address (this is what I believe is meant by being ip agnostic). I've tried running a standalone ipv6 server, and it works. So I believe that my network inerface supports both the IP stacks – Gaurav Suman May 11 '15 at 20:50
  • It seems like my understanding was lacking. I now understand that if I use the ipv6 in6addr_any, my server will work both with v4 and v6 loopback addresses. – Gaurav Suman May 11 '15 at 21:17
  • Yes, that's what I was trying to say. If your network and interface support both protocols then have the server listen on an IPv6 address. IPv4 traffic arriving at that interface will be served, too. – John Bollinger May 11 '15 at 21:35