add bindAddr for darwin

This commit is contained in:
hossinasaadi 2023-10-03 15:53:02 +04:00 committed by yuhan6665
parent 3ffdf93fc2
commit 6177ec7faf

View File

@ -191,14 +191,42 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
}
func bindAddr(fd uintptr, address []byte, port uint32) error {
return nil
setReuseAddr(fd)
setReusePort(fd)
var sockaddr unix.Sockaddr
switch len(address) {
case net.IPv4len:
a4 := &unix.SockaddrInet4{
Port: int(port),
}
copy(a4.Addr[:], address)
sockaddr = a4
case net.IPv6len:
a6 := &unix.SockaddrInet6{
Port: int(port),
}
copy(a6.Addr[:], address)
sockaddr = a6
default:
return newError("unexpected length of ip")
}
return unix.Bind(int(fd), sockaddr)
}
func setReuseAddr(fd uintptr) error {
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1); err != nil {
return newError("failed to set SO_REUSEADDR").Base(err).AtWarning()
}
return nil
}
func setReusePort(fd uintptr) error {
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
return newError("failed to set SO_REUSEPORT").Base(err).AtWarning()
}
return nil
}
func getInterfaceIndexByName(name string) int {