3
votes

(Please close this thread if duplicate, I tried hard, but couldn't find any matching question)

It seems some OS/platforms listen for both IPv6 and IPv4 (tcp) connections when bound to IPv6 widcard address, while some listen for only IPv6, as mentioned in:

for the V6Only argument

with the following lines:

If your platform does not support disabling this option but you still want to listen for both AF_INET and AF_INET6 connections you will have to create two listening sockets, one bound to each protocol

And in the section "How IPv6 Works on a Java Platform"

And as per correct answer in this SO question

Now I want to write some Perl code which can determine if the underlying OS/platform listens for both IPv6 and IPv4 (if bound to IPv6), if yes, I'll bind to IPv6 only, if no I'll create 2 sockets (1 for IPv4 and another for IPv6).

I wonder what could be the best way for this?

As mentioned in IO::Socket::IP I could use

if( IO::Socket::IP->CAN_DISABLE_V6ONLY ) {
    ...
}
else {
    ...
}

But, I am not sure if it will tell me exactly

if the underlying OS/platform listens for both IPv6 and IPv4 (if bound to IPv6)

Or it will just tells that the

IPV6_V6ONLY socket option cannot be disabled

1

1 Answers

4
votes

it just tells that the "IPV6_V6ONLY socket option cannot be disabled"

This is correct.

What you can do is attempt to create a PF_INET6 socket, then if successful, check its IPV6_V6ONLY socket option. If that's true, then the socket is listening only on IPv6 and not IPv4 as well, so you'll have to create another. If it is false, then the socket will capture both IPv6 and IPv4, and this one socket will be sufficient.