mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-05 04:47:40 +03:00
Fix SO_BINDTOIFINDEX
usage
This commit is contained in:
parent
8e89f9b4dc
commit
6e3921083b
1 changed files with 14 additions and 12 deletions
|
@ -14,19 +14,18 @@ var ifIndexDisabled atomic.Bool
|
||||||
|
|
||||||
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int, preferInterfaceName bool) 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 {
|
return Raw(conn, func(fd uintptr) error {
|
||||||
if interfaceIndex != -1 {
|
if !preferInterfaceName && !ifIndexDisabled.Load() {
|
||||||
return unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex)
|
if interfaceIndex == -1 {
|
||||||
}
|
if interfaceName == "" {
|
||||||
if interfaceName == "" {
|
return os.ErrInvalid
|
||||||
return os.ErrInvalid
|
}
|
||||||
}
|
var err error
|
||||||
if !preferInterfaceName && finder != nil && !ifIndexDisabled.Load() {
|
interfaceIndex, err = finder.InterfaceIndexByName(interfaceName)
|
||||||
var err error
|
if err != nil {
|
||||||
interfaceIndex, err = finder.InterfaceIndexByName(interfaceName)
|
return err
|
||||||
if err != nil {
|
}
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex)
|
err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
} else if E.IsMulti(err, unix.ENOPROTOOPT, unix.EINVAL) {
|
} else if E.IsMulti(err, unix.ENOPROTOOPT, unix.EINVAL) {
|
||||||
|
@ -35,6 +34,9 @@ func bindToInterface(conn syscall.RawConn, network string, address string, finde
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if interfaceName == "" {
|
||||||
|
return os.ErrInvalid
|
||||||
|
}
|
||||||
return unix.BindToDevice(int(fd), interfaceName)
|
return unix.BindToDevice(int(fd), interfaceName)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue