Skip to content

Commit ea359a0

Browse files
committed
ping: Do not use connected socket on macOS
1 parent a24ab73 commit ea359a0

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

ping/ping.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,27 @@ func (c *Conn) isLinuxUnprivileged() bool {
5959
func (c *Conn) ReadIP(buffer *buf.Buffer) error {
6060
if c.destination.Is6() || c.isLinuxUnprivileged() {
6161
var readMsg func(b, oob []byte) (n, oobn int, addr netip.Addr, err error)
62-
switch conn := c.conn.(type) {
63-
case *net.IPConn:
62+
if ipConn, isIPConn := common.Cast[*net.IPConn](c.conn); isIPConn {
6463
readMsg = func(b, oob []byte) (n, oobn int, addr netip.Addr, err error) {
6564
var ipAddr *net.IPAddr
66-
n, oobn, _, ipAddr, err = conn.ReadMsgIP(b, oob)
65+
n, oobn, _, ipAddr, err = ipConn.ReadMsgIP(b, oob)
6766
if err == nil {
6867
addr = M.AddrFromNet(ipAddr)
6968
}
7069
return
7170
}
72-
case *net.UDPConn:
71+
} else if udpConn, isUDPConn := common.Cast[*net.UDPConn](c.conn); isUDPConn {
7372
readMsg = func(b, oob []byte) (n, oobn int, addr netip.Addr, err error) {
7473
var addrPort netip.AddrPort
75-
n, oobn, _, addrPort, err = conn.ReadMsgUDPAddrPort(b, oob)
74+
n, oobn, _, addrPort, err = udpConn.ReadMsgUDPAddrPort(b, oob)
7675
if err == nil {
7776
addr = addrPort.Addr()
7877
}
7978
return
8079
}
81-
case *UnprivilegedConn:
82-
readMsg = conn.ReadMsg
83-
default:
80+
} else if unprivilegedConn, isUnprivilegedConn := c.conn.(*UnprivilegedConn); isUnprivilegedConn {
81+
readMsg = unprivilegedConn.ReadMsg
82+
} else {
8483
return E.New("unsupported conn type: ", reflect.TypeOf(c.conn))
8584
}
8685
if !c.destination.Is6() {

ping/socket_unix.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"runtime"
1010
"syscall"
1111

12+
"github.com/sagernet/sing/common/bufio"
1213
"github.com/sagernet/sing/common/control"
1314
E "github.com/sagernet/sing/common/exceptions"
1415
M "github.com/sagernet/sing/common/metadata"
@@ -85,14 +86,24 @@ func connect(privileged bool, controlFunc control.Func, destination netip.Addr)
8586
return nil, err
8687
}
8788

88-
err = unix.Connect(fd, M.AddrPortToSockaddr(netip.AddrPortFrom(destination, 0)))
89-
if err != nil {
90-
return nil, E.Cause(err, "connect()")
91-
}
92-
93-
conn, err := net.FileConn(file)
94-
if err != nil {
95-
return nil, err
89+
if runtime.GOOS == "darwin" {
90+
// When running in NetworkExtension on macOS, write to connected socket results in EPIPE.
91+
var packetConn net.PacketConn
92+
packetConn, err = net.FilePacketConn(file)
93+
if err != nil {
94+
return nil, err
95+
}
96+
return bufio.NewBindPacketConn(packetConn, M.SocksaddrFrom(destination, 0).UDPAddr()), nil
97+
} else {
98+
err = unix.Connect(fd, M.AddrPortToSockaddr(netip.AddrPortFrom(destination, 0)))
99+
if err != nil {
100+
return nil, err
101+
}
102+
var conn net.Conn
103+
conn, err = net.FileConn(file)
104+
if err != nil {
105+
return nil, err
106+
}
107+
return conn, nil
96108
}
97-
return conn, nil
98109
}

0 commit comments

Comments
 (0)