Skip to content

Commit

Permalink
Merge pull request #2927 from lucas-clemente/avoid-syscall
Browse files Browse the repository at this point in the history
use golang.org/x/sys/unix instead of syscall
  • Loading branch information
marten-seemann committed Dec 6, 2020
2 parents 2525abb + f59cd92 commit 325bc16
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
14 changes: 8 additions & 6 deletions conn_ecn.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"syscall"
"time"

"golang.org/x/sys/unix"

"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
)
Expand All @@ -29,7 +31,7 @@ func inspectReadBuffer(c net.PacketConn) (int, error) {
var size int
var serr error
if err := rawConn.Control(func(fd uintptr) {
size, serr = syscall.GetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF)
size, serr = unix.GetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUF)
}); err != nil {
return 0, err
}
Expand All @@ -53,12 +55,12 @@ func newConn(c ECNCapablePacketConn) (*ecnConn, error) {
// We expect at least one of those syscalls to succeed.
var errIPv4, errIPv6 error
if err := rawConn.Control(func(fd uintptr) {
errIPv4 = setRECVTOS(fd)
errIPv4 = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_RECVTOS, 1)
}); err != nil {
return nil, err
}
if err := rawConn.Control(func(fd uintptr) {
errIPv6 = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_RECVTCLASS, 1)
errIPv6 = unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_RECVTCLASS, 1)
}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -88,17 +90,17 @@ func (c *ecnConn) ReadPacket() (*receivedPacket, error) {
if err != nil {
return nil, err
}
ctrlMsgs, err := syscall.ParseSocketControlMessage(c.oobBuffer[:oobn])
ctrlMsgs, err := unix.ParseSocketControlMessage(c.oobBuffer[:oobn])
if err != nil {
return nil, err
}
var ecn protocol.ECN
for _, ctrlMsg := range ctrlMsgs {
if ctrlMsg.Header.Level == syscall.IPPROTO_IP && ctrlMsg.Header.Type == msgTypeIPTOS {
if ctrlMsg.Header.Level == unix.IPPROTO_IP && ctrlMsg.Header.Type == msgTypeIPTOS {
ecn = protocol.ECN(ctrlMsg.Data[0] & ecnMask)
break
}
if ctrlMsg.Header.Level == syscall.IPPROTO_IPV6 && ctrlMsg.Header.Type == syscall.IPV6_TCLASS {
if ctrlMsg.Header.Level == unix.IPPROTO_IPV6 && ctrlMsg.Header.Type == unix.IPV6_TCLASS {
ecn = protocol.ECN(ctrlMsg.Data[0] & ecnMask)
break
}
Expand Down
11 changes: 6 additions & 5 deletions conn_ecn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package quic

import (
"net"
"syscall"
"time"

"golang.org/x/sys/unix"

"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"

Expand Down Expand Up @@ -60,7 +61,7 @@ var _ = Describe("Basic Conn Test", func() {
"udp4",
conn.LocalAddr().(*net.UDPAddr),
func(fd uintptr) {
Expect(syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 2)).To(Succeed())
Expect(unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_TOS, 2)).To(Succeed())
},
)

Expand All @@ -80,7 +81,7 @@ var _ = Describe("Basic Conn Test", func() {
"udp6",
conn.LocalAddr().(*net.UDPAddr),
func(fd uintptr) {
Expect(syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_TCLASS, 3)).To(Succeed())
Expect(unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_TCLASS, 3)).To(Succeed())
},
)

Expand All @@ -102,7 +103,7 @@ var _ = Describe("Basic Conn Test", func() {
"udp4",
&net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: port},
func(fd uintptr) {
Expect(syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TOS, 3)).To(Succeed())
Expect(unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_TOS, 3)).To(Succeed())
},
)

Expand All @@ -116,7 +117,7 @@ var _ = Describe("Basic Conn Test", func() {
"udp6",
&net.UDPAddr{IP: net.IPv6loopback, Port: port},
func(fd uintptr) {
Expect(syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_TCLASS, 1)).To(Succeed())
Expect(unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_TCLASS, 1)).To(Succeed())
},
)

Expand Down
12 changes: 2 additions & 10 deletions conn_helper_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

package quic

import "syscall"
import "golang.org/x/sys/unix"

const (
//nolint:stylecheck
ip_recvtos = 27
msgTypeIPTOS = ip_recvtos
)

func setRECVTOS(fd uintptr) error {
return syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, ip_recvtos, 1)
}
const msgTypeIPTOS = unix.IP_RECVTOS
8 changes: 2 additions & 6 deletions conn_helper_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

package quic

import "syscall"
import "golang.org/x/sys/unix"

const msgTypeIPTOS = syscall.IP_TOS

func setRECVTOS(fd uintptr) error {
return syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_RECVTOS, 1)
}
const msgTypeIPTOS = unix.IP_TOS
2 changes: 1 addition & 1 deletion conn_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func inspectReadBuffer(c net.PacketConn) (int, error) {
var size int
var serr error
if err := rawConn.Control(func(fd uintptr) {
size, serr = windows.GetsockoptInt(windows.Handle(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF)
size, serr = windows.GetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_RCVBUF)
}); err != nil {
return 0, err
}
Expand Down

0 comments on commit 325bc16

Please sign in to comment.