diff --git a/ping/socket_unix.go b/ping/socket_unix.go index af072ae..3d3c59b 100644 --- a/ping/socket_unix.go +++ b/ping/socket_unix.go @@ -12,7 +12,6 @@ import ( "github.com/sagernet/sing/common/control" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" - "golang.org/x/sys/unix" ) @@ -74,6 +73,17 @@ func connect(privileged bool, controlFunc control.Func, destination netip.Addr) } } + var bindAddress netip.Addr + if !destination.Is6() { + bindAddress = netip.AddrFrom4([4]byte{0, 0, 0, 0}) + } else { + bindAddress = netip.AddrFrom16([16]byte{}) + } + err = unix.Bind(fd, M.AddrPortToSockaddr(netip.AddrPortFrom(bindAddress, 0))) + if err != nil { + return nil, err + } + err = unix.Connect(fd, M.AddrPortToSockaddr(netip.AddrPortFrom(destination, 0))) if err != nil { return nil, E.Cause(err, "connect()")