The following code works fine on Linux:
addr, _ := net.ResolveUDPAddr("udp", ":0")
conn, _ := net.ListenUDP("udp", addr)
rawConn, _ := conn.SyscallConn()
rawConn.Control(func(fd uintptr) {
unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_MTU_DISCOVER, 1)
})
On OSX, the sockopt doesn't have a named constant, the correct value to use here is 28. The code above only works if I'm listening on an address that's unambiguously IPv4, e.g. 127.0.0.1:0. If I listen on an address that allows me to send and receive both IPv4 and IPv6 (like :0), it doesn't work anymore: SetsockoptInt now returns error 22 (invalid argument). That's unfortunate as users of the library often listen on :0.
Is there any way to make this work analogously to how it works on Linux?