Skip to content

Commit

Permalink
Improve linux bind interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Sep 30, 2023
1 parent e727641 commit 818cd2d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions common/control/bind_linux.go
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)
})
}

0 comments on commit 818cd2d

Please sign in to comment.