csocketsfreebsdopenbsd

How can I use a IPV6-only socket on BSD systems?


I am trying to listen on a socket. If the user specifies to use IPv6, I use the following, which works on Linux:

setsockopt(listen_fd, SOL_IPV6, IPV6_V6ONLY, &one, sizeof(one));

But SOL_IPV6 does not exist on OpenBSD or FreeBSD. ip(7) on Linux says:

Using  the SOL_IP socket options level isn't portable; BSD-based stacks
use the IPPROTO_IP level.

I looked through some man page on FreeBSD (including ip(4) and setsockopt(2)) but I'm still unsure how to go forward.

(Location in full source)


Solution

  • Replacing SOL_IPV6 with IPPROTO_IPV6 works.

    ip6(4):

    These options and other control requests are accessed with
    getsockopt(2) and setsockopt(2) system calls at level
    IPPROTO_IPV6 and using ancillary data in recvmsg(2) and
    sendmsg(2).