1// +build go1.11
2// +build aix darwin dragonfly freebsd linux netbsd openbsd
3
4package dns
5
6import (
7	"context"
8	"net"
9	"syscall"
10
11	"golang.org/x/sys/unix"
12)
13
14const supportsReusePort = true
15
16func reuseportControl(network, address string, c syscall.RawConn) error {
17	var opErr error
18	err := c.Control(func(fd uintptr) {
19		opErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
20	})
21	if err != nil {
22		return err
23	}
24
25	return opErr
26}
27
28func listenTCP(network, addr string, reuseport bool) (net.Listener, error) {
29	var lc net.ListenConfig
30	if reuseport {
31		lc.Control = reuseportControl
32	}
33
34	return lc.Listen(context.Background(), network, addr)
35}
36
37func listenUDP(network, addr string, reuseport bool) (net.PacketConn, error) {
38	var lc net.ListenConfig
39	if reuseport {
40		lc.Control = reuseportControl
41	}
42
43	return lc.ListenPacket(context.Background(), network, addr)
44}
45