What would be the disadvantages/risks of using AF_UNSPEC?

13,463

Solution 1

You have to differentiate between client and server applications.

On the client, it is easy: just call getaddrinfo() and try each of the answers in sequence until you get a connection.

On the server, things are a little bit harder:

  • There are systems whose IPv4 and v6 stacks are interconnected, there it is enough to just listen on IPv6. Maybe the socket has to be enabled to listen to both.
  • Other systems, like Windows XP, have separated stacks where this connection is not possible. There you would have to work with several sockets at once. Let me concentrate on those in the following.

Even on servers, getaddrinfo() can be used. There you use the flag AI_PASSIVE in the hints. Then you get results. On these all you'll have to listen, perhaps enabling the IPV6_V6ONLY flag.

accept() should either be done non-blocking or with select() or poll() (not sure if the latter is possible).

Solution 2

The way things should be:

Applications should be layer-3 agnostic. Connecting to another system should be done by name. The name should be resolved to one or more addresses, and the application should connect to them without looking at the actual protocol being used. That way the networking configuration is the responsibility of the network- and system-admins. If IPv6 is introduced in a network then the application continues to work without even noticing the difference.

Some real-world issues:

Sometimes IPv6 is badly configured, a firewall doesn't know how to deal with IPv6, IPv6 is only used in the local network without a connection to the internet, etc. This should not be a problem, but sometimes you encounter a bad implementation or configuration. To deal with that the IETF is working on a draft called happy-eyeballs. It makes sure that the user doesn't notice such problems. Take a look at that draft. Using the techniques specified in that draft will make sure that your application works good for all users.

Share:
13,463
Kiril Kirov
Author by

Kiril Kirov

SOreadytohelp

Updated on July 11, 2022

Comments

  • Kiril Kirov
    Kiril Kirov almost 2 years

    From Beej's Guide to Network programming

    You can force it to use IPv4 or IPv6 in the ai_family field, or leave it as AF_UNSPEC to use whatever. This is cool because your code can be IP version-agnostic.

    As the title says - what would be the disadvantages (or risks, if any) of always using AF_UNSPEC, instead of specifying IPv4 or IPv6?

    Or it's only for one reason - if the version is specified, this will guarantee that this and only this version is supported?


    A little background - I think about adding support for IPv6 in client-server (C++) applications and both versions should be supported. So I wondered if it's fine to use AF_UNSPEC or it's better to "recognize" the address from the string and use AF_INET6 or AF_INET, depending on the address.

  • Kiril Kirov
    Kiril Kirov over 12 years
    Thanks. I changed one of the tags to unix. So, my question is about linux. I tried with AF_UNSPEC and it seems to work perfect for both versions. I just wondered if I can just use it like this, or it's better to deal with several sockets.
  • glglgl
    glglgl over 12 years
    Look at stackoverflow.com/questions/8113805/…. I wrote a conclusion about it...
  • Kiril Kirov
    Kiril Kirov over 12 years
    Ah, I see now. It took me so much time to understand what exactly you mean :D I'm just kinda new to network programming. Thanks, +1 and accepted (even though this does not actually answers the question in its title, but helped me clear my mind :) )