'How to use the Socket module *and* set ipv6_v6only?

I create an UDP socket with the Socket module and version: 6. I want it to listen only to IPv6 (by default, version: 6 listens on both v4 and v6). Erlang's inet module has a ipv6_v6only option but I don't know how to use it when using Elixir's Socket module. Calling :inet.setopts/2 after the call to Socket.UDP.open/2 always produce a {:error, :einval}. Any code example?

(I know I can do it by tuning the net.ipv6.bindv6only sysctl on Linux but I would prefer a solution that does not require to be root.)



Solution 1:[1]

Socket.UDP.open/2 is effectively routed to :gen_udp.open/2 which accepts a keyword of type open_option() as a second argument, which is in turn an extended type option().

Somewhat alongside the following would do

Socket.UDP.socket(port, version: 6)

This could have been easily seen from the arguments/1 implementation I’ve linked above.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1