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)); 
Community
  • 1
  • 1
jlstrecker
  • 4,953
  • 3
  • 46
  • 60

4 Answers4

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.

okor
  • 91
  • 1
  • 2
  • 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 Aug 08 '12 at 07:27
6

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

Joe Hildebrand
  • 10,354
  • 2
  • 38
  • 48
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.

Bill Evans at Mariposa
  • 3,590
  • 1
  • 18
  • 22
1

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.

plugwash
  • 9,724
  • 2
  • 38
  • 51