use golang.org/x/sys/unix instead of syscall

This commit is contained in:
Marten Seemann 2020-12-04 10:08:51 +07:00
parent 5a0ce24265
commit f59cd928f8
5 changed files with 19 additions and 28 deletions

View file

@ -9,6 +9,8 @@ import (
"syscall" "syscall"
"time" "time"
"golang.org/x/sys/unix"
"github.com/lucas-clemente/quic-go/internal/protocol" "github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils" "github.com/lucas-clemente/quic-go/internal/utils"
) )
@ -29,7 +31,7 @@ func inspectReadBuffer(c net.PacketConn) (int, error) {
var size int var size int
var serr error var serr error
if err := rawConn.Control(func(fd uintptr) { 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 { }); err != nil {
return 0, err return 0, err
} }
@ -53,12 +55,12 @@ func newConn(c ECNCapablePacketConn) (*ecnConn, error) {
// We expect at least one of those syscalls to succeed. // We expect at least one of those syscalls to succeed.
var errIPv4, errIPv6 error var errIPv4, errIPv6 error
if err := rawConn.Control(func(fd uintptr) { if err := rawConn.Control(func(fd uintptr) {
errIPv4 = setRECVTOS(fd) errIPv4 = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_RECVTOS, 1)
}); err != nil { }); err != nil {
return nil, err return nil, err
} }
if err := rawConn.Control(func(fd uintptr) { 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 { }); err != nil {
return nil, err return nil, err
} }
@ -88,17 +90,17 @@ func (c *ecnConn) ReadPacket() (*receivedPacket, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
ctrlMsgs, err := syscall.ParseSocketControlMessage(c.oobBuffer[:oobn]) ctrlMsgs, err := unix.ParseSocketControlMessage(c.oobBuffer[:oobn])
if err != nil { if err != nil {
return nil, err return nil, err
} }
var ecn protocol.ECN var ecn protocol.ECN
for _, ctrlMsg := range ctrlMsgs { 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) ecn = protocol.ECN(ctrlMsg.Data[0] & ecnMask)
break 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) ecn = protocol.ECN(ctrlMsg.Data[0] & ecnMask)
break break
} }

View file

@ -4,9 +4,10 @@ package quic
import ( import (
"net" "net"
"syscall"
"time" "time"
"golang.org/x/sys/unix"
"github.com/lucas-clemente/quic-go/internal/protocol" "github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils" "github.com/lucas-clemente/quic-go/internal/utils"
@ -60,7 +61,7 @@ var _ = Describe("Basic Conn Test", func() {
"udp4", "udp4",
conn.LocalAddr().(*net.UDPAddr), conn.LocalAddr().(*net.UDPAddr),
func(fd uintptr) { 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())
}, },
) )
@ -80,7 +81,7 @@ var _ = Describe("Basic Conn Test", func() {
"udp6", "udp6",
conn.LocalAddr().(*net.UDPAddr), conn.LocalAddr().(*net.UDPAddr),
func(fd uintptr) { 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())
}, },
) )
@ -102,7 +103,7 @@ var _ = Describe("Basic Conn Test", func() {
"udp4", "udp4",
&net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: port}, &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: port},
func(fd uintptr) { 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())
}, },
) )
@ -116,7 +117,7 @@ var _ = Describe("Basic Conn Test", func() {
"udp6", "udp6",
&net.UDPAddr{IP: net.IPv6loopback, Port: port}, &net.UDPAddr{IP: net.IPv6loopback, Port: port},
func(fd uintptr) { 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())
}, },
) )

View file

@ -2,14 +2,6 @@
package quic package quic
import "syscall" import "golang.org/x/sys/unix"
const ( const msgTypeIPTOS = unix.IP_RECVTOS
//nolint:stylecheck
ip_recvtos = 27
msgTypeIPTOS = ip_recvtos
)
func setRECVTOS(fd uintptr) error {
return syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, ip_recvtos, 1)
}

View file

@ -2,10 +2,6 @@
package quic package quic
import "syscall" import "golang.org/x/sys/unix"
const msgTypeIPTOS = syscall.IP_TOS const msgTypeIPTOS = unix.IP_TOS
func setRECVTOS(fd uintptr) error {
return syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_RECVTOS, 1)
}

View file

@ -29,7 +29,7 @@ func inspectReadBuffer(c net.PacketConn) (int, error) {
var size int var size int
var serr error var serr error
if err := rawConn.Control(func(fd uintptr) { 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 { }); err != nil {
return 0, err return 0, err
} }