How do I determine if a dual socket is supported?

It seems that multiple stack sockets are not supported by some operating systems. I am looking for a robust approach to determine if this feature is supported or not. My current suggestion (not tested yet) is checking the return value of setsockopt to disable IPV6_V6ONLY when I try to put a socket into double stack mode. Do you think this works or is there a better solution?

thank

+3


source to share


1 answer


One approach that comes to my mind is to try to create an IPv6-only UDP socket and connect it to an IPv4-addressed address. This will not result in actual network traffic, but by looking at the behavior of the code and the resulting local address assigned to the socket, you can figure out if the socket is really IPv6 only.

Here is Python code demonstrating the idea. This can be directly mapped to equivalent C code performing the same system call sequence:

import socket
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, 0)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
s.connect(('::ffff:169.254.1.1', 53))
print s.getsockname()

      



Possible outcomes:

  • setsockopt

    fails because the parameter is IPV6_V6ONLY

    not supported.
  • connect

    fails due to the socket being IPv6 only and connecting, recognizing that you are not allowed to bind such a socket to an IP mapped to IPv4.
  • connect

    fails. If in this case you are left in the dark because you don't know if it is looking for an IPv4 route or an IPv6 route and you need to try to connect to other addresses to figure out exactly what is going on.
  • getsockname

    returns an IP address displaying IPv4, which means IPV6_V6ONLY

    , and the socket is not only IPv6.
  • getsockname

    returns an IPv6 address other than the IP address mapped to IPv4, which means the socket is only IPv6 and the code path used by the connection has no idea that the IP address was any special.

I would find it impossible to predict what quirks you might find on different operating systems. But I'm pretty sure the sequence of socket API calls I propose will expose any such quirks. Thus, you will have to test it on your target OS to see how they behave, and you may find that a simplified version of the tests will suffice.

+1


source







All Articles