-
Notifications
You must be signed in to change notification settings - Fork 67
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
e727641
commit 818cd2d
Showing
1 changed file
with
15 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
package control | ||
|
||
import ( | ||
"errors" | ||
"syscall" | ||
|
||
"github.com/sagernet/sing/common/atomic" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
var ifIndexDisabled atomic.Bool | ||
|
||
func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error { | ||
return Raw(conn, func(fd uintptr) error { | ||
if !ifIndexDisabled.Load() { | ||
err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex) | ||
if err == nil { | ||
return nil | ||
} else if errors.Is(err, unix.ENOPROTOOPT) { | ||
ifIndexDisabled.Store(true) | ||
} else { | ||
return err | ||
} | ||
} | ||
return unix.BindToDevice(int(fd), interfaceName) | ||
}) | ||
} |