Solved Creating a IPv4/IPv6 socket in C

I figured it out. IPV6_V6ONLY must be set to 0. This smartly only applies when binding to ::. No ill-effect if binding to a specifc address like ::1, it will be a IPv6-only socket in that case.

Code:
int sockopt = 0;
setsockopt(sockin, IPPROTO_IPV6, IPV6_V6ONLY, &sockopt, sizeof (sockopt));

Testing for setsockopt()'s return code would be wise as well.
 
Unsetting the IPV6_V6ONLY socket option should give you what you're looking for, something like
C:
int opt_false = 0;
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt_false, sizeof opt_false);

I personally don't like that too much, it creates issues (it's been a while, IIRC you get V6-notation for all address strings, and it's harder to implement a server where you can freely configure how it should bind, e.g. to hostname, specific address, ...).

In my server code, I do the opposite. Walking the list from getaddrinfo(3) for each value the user wants to bind on, I explicitly check each result for AF_INET6 and then enable this socket option to be sure I get a v6-only socket and not such a hybrid one. (edit: If you don't touch that option, you get whatever is your system's default, which is IMHO pretty unfortunate ...)

Why don't you want to have more than one listening socket btw? Short on file descriptors? :cool:
 
Back
Top