Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't turn off socket option IPV6_V6ONLY

Tags:

c

macos

sockets

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)); 
like image 310
jlstrecker Avatar asked Apr 07 '11 21:04

jlstrecker


4 Answers

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.

like image 118
okor Avatar answered Sep 24 '22 21:09

okor


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.

like image 35
Bill Evans at Mariposa Avatar answered Sep 25 '22 21:09

Bill Evans at Mariposa


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

like image 45
Joe Hildebrand Avatar answered Sep 25 '22 21:09

Joe Hildebrand


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.

like image 29
plugwash Avatar answered Sep 25 '22 21:09

plugwash