-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0ec961
commit 2032ec8
Showing
4 changed files
with
64 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,48 @@ | ||
package control | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
|
||
"github.com/sagernet/sing/common/atomic" | ||
E "github.com/sagernet/sing/common/exceptions" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error { | ||
var ifIndexDisabled atomic.Bool | ||
|
||
func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) 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 | ||
} | ||
} | ||
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex) | ||
if err == nil { | ||
return nil | ||
} else if E.IsMulti(err, unix.ENOPROTOOPT, unix.EINVAL) { | ||
ifIndexDisabled.Store(true) | ||
} else { | ||
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters