8

I'm trying to turn off the socket option IPV6_V6ONLY.

int no = 0;     
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&no, sizeof(no)); 

Why does the above fail with errno 22 (EINVAL)?

This is on OS X. It also doesn't work when no is 1. Setting other socket options works, for example

int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes)); 

4 Answers 4

9

It looks like *BSD derived OS doesn't allow set nor clear this option. I see the same behavior on FreeBSD 8.X. The socket is 100% AF_INET6.

1
  • 4
    FreeBSD since 5.x has disabled IPv4 mapped on IPv6 addresses and thus unless you turn that feature back on by setting the required configuration flag in rc.conf you won't be able to use it.
    – X-Istence
    Commented Aug 8, 2012 at 7:27
6

What did your call to socket() look like for fd? If the first parameter, the protocol family, wasn't AF_INET6 (or PF_INET6), then this call isn't applicable.

6

Make sure you are calling bind() after setsockopt() for this option.

2

Another thing that can cause this to fail is doing it too late, it seems that on Linux at least it must be done before the socket is bound.

Not the answer you're looking for? Browse other questions tagged or ask your own question.