Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sockopt.interface work with UDP on Darwin #4530

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 35 additions & 38 deletions transport/internet/sockopt_darwin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package internet

import (
network "net"
gonet "net"
"os"
"syscall"
"unsafe"
Expand Down Expand Up @@ -108,14 +108,6 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
return err
}
}
if config.Interface != "" {
InterfaceIndex := getInterfaceIndexByName(config.Interface)
if InterfaceIndex != 0 {
if err := unix.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, InterfaceIndex); err != nil {
return errors.New("failed to set Interface").Base(err)
}
}
}

if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
if config.TcpKeepAliveIdle > 0 {
Expand All @@ -138,6 +130,23 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
}
}

if config.Interface != "" {
iface, err := gonet.InterfaceByName(config.Interface)

if err != nil {
return errors.New("failed to get interface ", config.Interface).Base(err)
}
if network == "tcp6" || network == "udp6" {
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index); err != nil {
return errors.New("failed to set IPV6_BOUND_IF").Base(err)
}
} else {
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index); err != nil {
return errors.New("failed to set IP_BOUND_IF").Base(err)
}
}
}

return nil
}

Expand All @@ -152,14 +161,6 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
return err
}
}
if config.Interface != "" {
InterfaceIndex := getInterfaceIndexByName(config.Interface)
if InterfaceIndex != 0 {
if err := unix.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, InterfaceIndex); err != nil {
return errors.New("failed to set Interface").Base(err)
}
}
}

if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
if config.TcpKeepAliveIdle > 0 {
Expand All @@ -182,6 +183,23 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
}
}

if config.Interface != "" {
iface, err := gonet.InterfaceByName(config.Interface)

if err != nil {
return errors.New("failed to get interface ", config.Interface).Base(err)
}
if network == "tcp6" || network == "udp6" {
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index); err != nil {
return errors.New("failed to set IPV6_BOUND_IF").Base(err)
}
} else {
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index); err != nil {
return errors.New("failed to set IP_BOUND_IF").Base(err)
}
}
}

return nil
}

Expand Down Expand Up @@ -224,24 +242,3 @@ func setReusePort(fd uintptr) error {
}
return nil
}
func getInterfaceIndexByName(name string) int {
ifaces, err := network.Interfaces()
if err == nil {
for _, iface := range ifaces {
if (iface.Flags&network.FlagUp == network.FlagUp) && (iface.Flags&network.FlagLoopback != network.FlagLoopback) {
addrs, _ := iface.Addrs()
for _, addr := range addrs {
if ipnet, ok := addr.(*network.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
if iface.Name == name {
return iface.Index
}
}
}
}
}

}
}
return 0
}
2 changes: 1 addition & 1 deletion transport/internet/system_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func hasBindAddr(sockopt *SocketConfig) bool {
func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
errors.LogDebug(ctx, "dialing to "+dest.String())

if dest.Network == net.Network_UDP && !hasBindAddr(sockopt) {
if dest.Network == net.Network_UDP && !hasBindAddr(sockopt) && sockopt.Interface == "" {
srcAddr := resolveSrcAddr(net.Network_UDP, src)
if srcAddr == nil {
srcAddr = &net.UDPAddr{
Expand Down
Loading