1

I want to open multiple UDP sockets bound to the same port( say 8888). The different sockets will be bound to different vrfs in the system. What I understand is we need to set SO_REUSEPORT sockopts, but I don't see this available in net package of Go.

Could someone help how can I achieve this?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

0

You could set SO_REUSEPORT through unix.SetsockoptInt

Sample codes

    lc := net.ListenConfig{
        Control: func(network, address string, c syscall.RawConn) error {
            var opErr error
            err := c.Control(func(fd uintptr) {
                opErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
            })
            if err != nil {
                return err
            }
            return opErr
        },
    }

    lp, err := lc.ListenPacket(context.Background(), "udp", UDPADDR)

    conn := lp.(*net.UDPConn)
zangw
  • 43,869
  • 19
  • 177
  • 214
  • Thanks, this worked. Any reason why syscall does not expose REUSEPORT settings? – Saurabh Suman Oct 14 '22 at 10:55
  • 1
    @SaurabhSuman, here is one related issue https://github.com/golang/go/issues/26771. `The syscall package is frozen. We generally only add to it if the additions are needed elsewhere in the runtime or standard library.` – zangw Oct 14 '22 at 14:14