Skip to content

Commit

Permalink
add bindAddr for darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
hossinasaadi authored and yuhan6665 committed Oct 16, 2023
1 parent 3ffdf93 commit 6177ec7
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions transport/internet/sockopt_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
}
}
}

if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
if config.TcpKeepAliveIdle > 0 {
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveInterval)); err != nil {
Expand All @@ -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 {
Expand Down

0 comments on commit 6177ec7

Please sign in to comment.