Skip to content

Commit

Permalink
🗽 darwin: use upstreamed connectx(2) libc wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Aug 30, 2024
1 parent e499cb0 commit ba6f1bd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 168 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/database64128/tfo-go/v2

go 1.21.0

require golang.org/x/sys v0.24.0
require golang.org/x/sys v0.24.1-0.20240828075529-ed67b1566aaf
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.1-0.20240828075529-ed67b1566aaf h1:q2Cx0keWwW5HecyZeIyA3DCuupo8A/zjDqsOQK0+Z80=
golang.org/x/sys v0.24.1-0.20240828075529-ed67b1566aaf/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
157 changes: 0 additions & 157 deletions syscall_darwin.go

This file was deleted.

29 changes: 27 additions & 2 deletions tfo_bsd+linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,18 @@ func (d *Dialer) dialSingle(ctx context.Context, network string, laddr, raddr *n
}
}

rusa, err := unixSockaddrFromSyscallSockaddr(rsa)
if err != nil {
return nil, err
}

var (
n int
canFallback bool
)

if err = connWriteFunc(ctx, f, func(f *os.File) (err error) {
n, canFallback, err = connect(rawConn, rsa, b)
n, canFallback, err = connect(rawConn, rusa, b)
return err
}); err != nil {
if d.Fallback && canFallback {
Expand All @@ -127,7 +132,27 @@ func (d *Dialer) dialSingle(ctx context.Context, network string, laddr, raddr *n
return c.(*net.TCPConn), err
}

func connect(rawConn syscall.RawConn, rsa syscall.Sockaddr, b []byte) (n int, canFallback bool, err error) {
func unixSockaddrFromSyscallSockaddr(sa syscall.Sockaddr) (unix.Sockaddr, error) {
if sa == nil {
return nil, nil
}
switch sa := sa.(type) {
case *syscall.SockaddrInet4:
return &unix.SockaddrInet4{
Port: sa.Port,
Addr: sa.Addr,
}, nil
case *syscall.SockaddrInet6:
return &unix.SockaddrInet6{
Port: sa.Port,
ZoneId: sa.ZoneId,
Addr: sa.Addr,
}, nil
}
return nil, errors.New("unsupported sockaddr type")
}

func connect(rawConn syscall.RawConn, rsa unix.Sockaddr, b []byte) (n int, canFallback bool, err error) {
var done bool

if perr := rawConn.Write(func(fd uintptr) bool {
Expand Down
17 changes: 15 additions & 2 deletions tfo_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,20 @@ const setTFODialerFromSocketSockoptName = "TCP_FASTOPEN_FORCE_ENABLE"

const connectSyscallName = "connectx"

func doConnect(fd uintptr, rsa syscall.Sockaddr, b []byte) (int, error) {
n, err := Connectx(int(fd), 0, nil, rsa, b)
func doConnect(fd uintptr, rsa unix.Sockaddr, b []byte) (int, error) {
var (
flags uint32
iov []unix.Iovec
)
if len(b) > 0 {
flags = unix.CONNECT_DATA_IDEMPOTENT
iov = []unix.Iovec{
{
Base: &b[0],
Len: uint64(len(b)),
},
}
}
n, err := unix.Connectx(int(fd), 0, nil, rsa, unix.SAE_ASSOCID_ANY, flags, iov, nil)
return int(n), err
}
6 changes: 2 additions & 4 deletions tfo_freebsd+linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package tfo

import (
"syscall"

"golang.org/x/sys/unix"
)

Expand All @@ -18,6 +16,6 @@ func (*Dialer) setIPv6Only(fd int, family int, ipv6only bool) error {

const connectSyscallName = "sendmsg"

func doConnect(fd uintptr, rsa syscall.Sockaddr, b []byte) (int, error) {
return syscall.SendmsgN(int(fd), b, nil, rsa, sendtoImplicitConnectFlag|unix.MSG_NOSIGNAL)
func doConnect(fd uintptr, rsa unix.Sockaddr, b []byte) (int, error) {
return unix.SendmsgN(int(fd), b, nil, rsa, sendtoImplicitConnectFlag|unix.MSG_NOSIGNAL)
}

0 comments on commit ba6f1bd

Please sign in to comment.