Skip to content

Commit 72899fc

Browse files
committed
Fix darwin writev
1 parent 3af7305 commit 72899fc

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/sagernet/gvisor v0.0.0-20241123041152-536d05261cff
1010
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a
1111
github.com/sagernet/nftables v0.3.0-beta.4
12-
github.com/sagernet/sing v0.6.0-beta.2
12+
github.com/sagernet/sing v0.6.12-0.20250720102517-17fa6611136d
1313
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
1414
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
1515
golang.org/x/net v0.26.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a h1:ObwtHN2VpqE0ZN
2222
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
2323
github.com/sagernet/nftables v0.3.0-beta.4 h1:kbULlAwAC3jvdGAC1P5Fa3GSxVwQJibNenDW2zaXr8I=
2424
github.com/sagernet/nftables v0.3.0-beta.4/go.mod h1:OQXAjvjNGGFxaTgVCSTRIhYB5/llyVDeapVoENYBDS8=
25-
github.com/sagernet/sing v0.6.0-beta.2 h1:Dcutp3kxrsZes9q3oTiHQhYYjQvDn5rwp1OI9fDLYwQ=
26-
github.com/sagernet/sing v0.6.0-beta.2/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
25+
github.com/sagernet/sing v0.6.12-0.20250720102517-17fa6611136d h1:ZR/6vztdiDJ/rB1NHMrSyq0jCPDKMvWLvAU1ckB0pRs=
26+
github.com/sagernet/sing v0.6.12-0.20250720102517-17fa6611136d/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
2727
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
2828
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
2929
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=

tun_darwin.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type NativeTun struct {
4040
inet4Address [4]byte
4141
inet6Address [16]byte
4242
routeSet bool
43-
writeMsgX bool
43+
sendMsgX bool
4444
}
4545

4646
type iovecBuffer struct {
@@ -64,8 +64,7 @@ func (b *iovecBuffer) nextIovecs() []unix.Iovec {
6464
}
6565
if b.buffer == nil {
6666
b.buffer = buf.NewSize(b.mtu)
67-
b.iovecs[1].Base = &b.buffer.FreeBytes()[0]
68-
b.iovecs[1].SetLen(b.mtu)
67+
b.iovecs[1] = b.buffer.Iovec(b.buffer.Cap())
6968
}
7069
return b.iovecs
7170
}
@@ -77,8 +76,7 @@ func (b *iovecBuffer) nextIovecsOutput(buffer *buf.Buffer) []unix.Iovec {
7776
case header.IPv6Version:
7877
b.iovecs[0] = packetHeaderVec6
7978
}
80-
b.iovecs[1].Base = &buffer.Bytes()[0]
81-
b.iovecs[1].SetLen(buffer.Len())
79+
b.iovecs[1] = buffer.Iovec(buffer.Len())
8280
return b.iovecs
8381
}
8482

@@ -132,7 +130,7 @@ func New(options Options) (Tun, error) {
132130
msgHdrs: make([]rawfile.MsgHdrX, batchSize),
133131
msgHdrsOutput: make([]rawfile.MsgHdrX, batchSize),
134132
stopFd: common.Must1(stopfd.New()),
135-
writeMsgX: options.EXP_SendMsgX,
133+
sendMsgX: options.EXP_SendMsgX,
136134
}
137135
for i := 0; i < batchSize; i++ {
138136
nativeTun.iovecs[i] = newIovecBuffer(int(options.MTU))
@@ -364,7 +362,7 @@ func (t *NativeTun) BatchRead() ([]*buf.Buffer, error) {
364362
}
365363

366364
func (t *NativeTun) BatchWrite(buffers []*buf.Buffer) error {
367-
if !t.writeMsgX {
365+
if !t.sendMsgX {
368366
for i, buffer := range buffers {
369367
t.iovecsOutput[i].nextIovecsOutput(buffer)
370368
}
@@ -381,9 +379,13 @@ func (t *NativeTun) BatchWrite(buffers []*buf.Buffer) error {
381379
t.msgHdrsOutput[i].Msg.Iov = &iovecs[0]
382380
t.msgHdrsOutput[i].Msg.Iovlen = 2
383381
}
384-
_, errno := rawfile.NonBlockingSendMMsg(t.tunFd, t.msgHdrsOutput[:len(buffers)])
385-
if errno != 0 {
386-
return errno
382+
var n int
383+
for n != len(buffers) {
384+
sent, errno := rawfile.NonBlockingSendMMsg(t.tunFd, t.msgHdrsOutput[n:len(buffers)])
385+
if errno != 0 {
386+
return errno
387+
}
388+
n += sent
387389
}
388390
}
389391
return nil

0 commit comments

Comments
 (0)