Skip to content

Commit

Permalink
Add 'preferInterfaceName' parameter to BindToInterface0
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 21, 2023
1 parent 1326ec1 commit c223a30
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
8 changes: 4 additions & 4 deletions common/control/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func BindToInterface(finder InterfaceFinder, interfaceName string, interfaceIndex int) Func {
return func(network, address string, conn syscall.RawConn) error {
return BindToInterface0(finder, conn, network, address, interfaceName, interfaceIndex)
return BindToInterface0(finder, conn, network, address, interfaceName, interfaceIndex, false)
}
}

Expand All @@ -20,16 +20,16 @@ func BindToInterfaceFunc(finder InterfaceFinder, block func(network string, addr
if err != nil {
return err
}
return BindToInterface0(finder, conn, network, address, interfaceName, interfaceIndex)
return BindToInterface0(finder, conn, network, address, interfaceName, interfaceIndex, false)
}
}

func BindToInterface0(finder InterfaceFinder, conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error {
func BindToInterface0(finder InterfaceFinder, conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int, preferInterfaceName bool) error {
if interfaceName == "" && interfaceIndex == -1 {
return E.New("interface not found: ", interfaceName)
}
if addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) {
return nil
}
return bindToInterface(conn, network, address, finder, interfaceName, interfaceIndex)
return bindToInterface(conn, network, address, finder, interfaceName, interfaceIndex, preferInterfaceName)
}
2 changes: 1 addition & 1 deletion common/control/bind_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"golang.org/x/sys/unix"
)

func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) error {
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int, preferInterfaceName bool) error {
return Raw(conn, func(fd uintptr) error {
var err error
if interfaceIndex == -1 {
Expand Down
32 changes: 12 additions & 20 deletions common/control/bind_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ import (

var ifIndexDisabled atomic.Bool

func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) error {
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int, preferInterfaceName bool) error {
return Raw(conn, func(fd uintptr) error {
var err error
if !ifIndexDisabled.Load() {
if interfaceIndex == -1 {
if finder == nil {
return os.ErrInvalid
}
interfaceIndex, err = finder.InterfaceIndexByName(interfaceName)
if err != nil {
return err
}
if interfaceIndex != -1 {
return unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex)
}
if interfaceName == "" {
return os.ErrInvalid
}
if !preferInterfaceName && finder != nil && !ifIndexDisabled.Load() {
var err error
interfaceIndex, err = finder.InterfaceIndexByName(interfaceName)
if err != nil {
return err
}
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex)
if err == nil {
Expand All @@ -34,15 +35,6 @@ func bindToInterface(conn syscall.RawConn, network string, address string, finde
return err
}
}
if interfaceName == "" {
if finder == nil {
return os.ErrInvalid
}
interfaceName, err = finder.InterfaceNameByIndex(interfaceIndex)
if err != nil {
return err
}
}
return unix.BindToDevice(int(fd), interfaceName)
})
}
2 changes: 1 addition & 1 deletion common/control/bind_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ package control

import "syscall"

func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) error {
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int, preferInterfaceName bool) error {
return nil
}
2 changes: 1 addition & 1 deletion common/control/bind_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
M "github.com/sagernet/sing/common/metadata"
)

func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) error {
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int, preferInterfaceName bool) error {
return Raw(conn, func(fd uintptr) error {
var err error
if interfaceIndex == -1 {
Expand Down

0 comments on commit c223a30

Please sign in to comment.