Improve linux bind interface

This commit is contained in:
世界 2023-09-30 21:36:00 +08:00
parent 49f5dfd767
commit c298c7b1bf
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 65 additions and 38 deletions

View file

@ -1,16 +1,24 @@
package control
import (
"os"
"syscall"
"golang.org/x/sys/unix"
)
func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error {
if interfaceIndex == -1 {
return nil
}
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 interfaceIndex == -1 {
if finder == nil {
return os.ErrInvalid
}
interfaceIndex, err = finder.InterfaceIndexByName(interfaceName)
if err != nil {
return err
}
}
switch network {
case "tcp6", "udp6":
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, interfaceIndex)