mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 04:55:33 +03:00
05262d7b6b
Squashed commit of the following: commit 7a24cf8f9c5515f642cbfc7e730b95005eeab11d Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Aug 15 14:54:16 2023 +0300 all: upd code, deps, tools
38 lines
1 KiB
Go
38 lines
1 KiB
Go
//go:build darwin || freebsd || openbsd
|
|
|
|
package aghnet
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"os"
|
|
"syscall"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// reuseAddrCtrl is the function to be set to net.ListenConfig.Control. It
|
|
// configures the socket to have a reusable port binding.
|
|
func reuseAddrCtrl(_, _ string, c syscall.RawConn) (err error) {
|
|
cerr := c.Control(func(fd uintptr) {
|
|
// TODO(e.burkov): Consider using SO_REUSEPORT.
|
|
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
|
|
if err != nil {
|
|
err = os.NewSyscallError("setsockopt", err)
|
|
}
|
|
})
|
|
|
|
err = errors.Join(err, cerr)
|
|
|
|
return errors.Annotate(err, "setting control options: %w")
|
|
}
|
|
|
|
// listenPacketReusable announces on the local network address additionally
|
|
// configuring the socket to have a reusable binding.
|
|
func listenPacketReusable(_, network, address string) (c net.PacketConn, err error) {
|
|
var lc net.ListenConfig
|
|
lc.Control = reuseAddrCtrl
|
|
|
|
return lc.ListenPacket(context.Background(), network, address)
|
|
}
|