replace xnet.Transport by xnet.Pipe
This commit is contained in:
@@ -135,7 +135,7 @@ func (d *Dialer) dialOnce(ctx context.Context, network, addr, ifceName string, i
|
||||
}
|
||||
err = sc.Control(func(fd uintptr) {
|
||||
if ifceName != "" {
|
||||
if err := bindDevice(network, fd, ifceName); err != nil {
|
||||
if err := bindDevice(network, addr, fd, ifceName); err != nil {
|
||||
log.Warnf("bind device: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -159,13 +159,13 @@ func (d *Dialer) dialOnce(ctx context.Context, network, addr, ifceName string, i
|
||||
Control: func(network, address string, c syscall.RawConn) error {
|
||||
return c.Control(func(fd uintptr) {
|
||||
if ifceName != "" {
|
||||
if err := bindDevice(network, fd, ifceName); err != nil {
|
||||
log.Warnf("bind device: %v", err)
|
||||
if err := bindDevice(network, address, fd, ifceName); err != nil {
|
||||
log.Warnf("%s/%s bind device: %v", address, network, err)
|
||||
}
|
||||
}
|
||||
if d.Mark != 0 {
|
||||
if err := setMark(fd, d.Mark); err != nil {
|
||||
log.Warnf("set mark: %v", err)
|
||||
log.Warnf("%s/%s set mark: %v", address, network, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -7,18 +7,23 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func bindDevice(network string, fd uintptr, ifceName string) error {
|
||||
func bindDevice(network, address string, fd uintptr, ifceName string) error {
|
||||
if ifceName == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
host, _, _ := net.SplitHostPort(address)
|
||||
if ip := net.ParseIP(host); ip != nil && !ip.IsGlobalUnicast() {
|
||||
return nil
|
||||
}
|
||||
|
||||
ifce, err := net.InterfaceByName(ifceName)
|
||||
if err == nil {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4", "udp4":
|
||||
case "tcp", "tcp4", "udp", "udp4":
|
||||
return unix.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, ifce.Index)
|
||||
case "tcp6", "udp6":
|
||||
return unix.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BOUND_IF, ifce.Index)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func bindDevice(network, address string, fd uintptr, ifceName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func setMark(fd uintptr, mark int) error {
|
||||
if mark != 0 {
|
||||
return nil
|
||||
}
|
||||
return unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_USER_COOKIE, mark)
|
||||
}
|
||||
@@ -1,13 +1,21 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func bindDevice(network string, fd uintptr, ifceName string) error {
|
||||
func bindDevice(network, address string, fd uintptr, ifceName string) error {
|
||||
if ifceName == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
host, _, _ := net.SplitHostPort(address)
|
||||
if ip := net.ParseIP(host); ip != nil && !ip.IsGlobalUnicast() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return unix.BindToDevice(int(fd), ifceName)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func bindDevice(network, address string, fd uintptr, ifceName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func setMark(fd uintptr, mark int) error {
|
||||
if mark != 0 {
|
||||
return nil
|
||||
}
|
||||
return unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RTABLE, mark)
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
//go:build !linux && !windows && !darwin
|
||||
//go:build !unix && !windows
|
||||
|
||||
package dialer
|
||||
|
||||
func bindDevice(network string, fd uintptr, ifceName string) error {
|
||||
func bindDevice(network, address string, fd uintptr, ifceName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -13,21 +13,33 @@ const (
|
||||
IPV6_UNICAST_IF = 31
|
||||
)
|
||||
|
||||
func bindDevice(network string, fd uintptr, ifceName string) error {
|
||||
func bindDevice(network, address string, fd uintptr, ifceName string) error {
|
||||
if ifceName == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
host, _, _ := net.SplitHostPort(address)
|
||||
ip := net.ParseIP(host)
|
||||
if ip != nil && !ip.IsGlobalUnicast() {
|
||||
return nil
|
||||
}
|
||||
|
||||
ifce, err := net.InterfaceByName(ifceName)
|
||||
if err == nil {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4", "udp4":
|
||||
case "tcp", "tcp4", "udp", "udp4":
|
||||
return bindSocketToInterface4(windows.Handle(fd), uint32(ifce.Index))
|
||||
case "tcp6", "udp6":
|
||||
return bindSocketToInterface6(windows.Handle(fd), uint32(ifce.Index))
|
||||
err = bindSocketToInterface6(windows.Handle(fd), uint32(ifce.Index))
|
||||
if network == "udp6" && ip == nil {
|
||||
// The underlying IP net maybe IPv4 even if the `network` param is `udp6`,
|
||||
// so we should bind socket to interface4 at the same time.
|
||||
err = bindSocketToInterface4(windows.Handle(fd), uint32(ifce.Index))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user