From b51df1025175ef4b81dd1ecbb9d7ed389b8d1297 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 5 Nov 2021 01:52:54 +0100 Subject: [PATCH] global: use Go 1.18's netip helpers Signed-off-by: Jason A. Donenfeld --- conn/bind_linux.go | 2 +- conn/bind_std.go | 52 ++++++++++++---------------- conn/bind_windows.go | 2 +- conn/bindtest/bindtest.go | 2 +- conn/conn.go | 3 +- device/allowedips.go | 3 +- device/allowedips_rand_test.go | 3 +- device/allowedips_test.go | 3 +- device/device_test.go | 2 +- device/endpoint_test.go | 3 +- device/receive.go | 1 + device/send.go | 1 + device/uapi.go | 2 +- ratelimiter/ratelimiter.go | 3 +- ratelimiter/ratelimiter_test.go | 3 +- tun/netstack/examples/http_client.go | 2 +- tun/netstack/examples/http_server.go | 2 +- tun/netstack/go.mod | 1 - tun/netstack/go.sum | 5 --- tun/netstack/tun.go | 2 +- tun/tuntest/tuntest.go | 2 +- 21 files changed, 41 insertions(+), 58 deletions(-) diff --git a/conn/bind_linux.go b/conn/bind_linux.go index 975a6ab..8d418fc 100644 --- a/conn/bind_linux.go +++ b/conn/bind_linux.go @@ -8,13 +8,13 @@ package conn import ( "errors" "net" + "net/netip" "strconv" "sync" "syscall" "unsafe" "golang.org/x/sys/unix" - "golang.zx2c4.com/go118/netip" ) type ipv4Source struct { diff --git a/conn/bind_std.go b/conn/bind_std.go index 219c719..0f36081 100644 --- a/conn/bind_std.go +++ b/conn/bind_std.go @@ -8,10 +8,9 @@ package conn import ( "errors" "net" + "net/netip" "sync" "syscall" - - "golang.zx2c4.com/go118/netip" ) // StdNetBind is meant to be a temporary solution on platforms for which @@ -28,24 +27,20 @@ type StdNetBind struct { func NewStdNetBind() Bind { return &StdNetBind{} } -type StdNetEndpoint net.UDPAddr +type StdNetEndpoint netip.AddrPort var _ Bind = (*StdNetBind)(nil) var _ Endpoint = (*StdNetEndpoint)(nil) func (*StdNetBind) ParseEndpoint(s string) (Endpoint, error) { e, err := netip.ParseAddrPort(s) - return (*StdNetEndpoint)(&net.UDPAddr{ - IP: e.Addr().AsSlice(), - Port: int(e.Port()), - Zone: e.Addr().Zone(), - }), err + return (*StdNetEndpoint)(&e), err } func (*StdNetEndpoint) ClearSrc() {} func (e *StdNetEndpoint) DstIP() netip.Addr { - return netip.AddrFromSlice((*net.UDPAddr)(e).IP) + return (*netip.AddrPort)(e).Addr() } func (e *StdNetEndpoint) SrcIP() netip.Addr { @@ -53,18 +48,15 @@ func (e *StdNetEndpoint) SrcIP() netip.Addr { } func (e *StdNetEndpoint) DstToBytes() []byte { - addr := (*net.UDPAddr)(e) - out := addr.IP.To4() - if out == nil { - out = addr.IP - } - out = append(out, byte(addr.Port&0xff)) - out = append(out, byte((addr.Port>>8)&0xff)) + addr := (*netip.AddrPort)(e) + out := addr.Addr().AsSlice() + out = append(out, byte(addr.Port()&0xff)) + out = append(out, byte((addr.Port()>>8)&0xff)) return out } func (e *StdNetEndpoint) DstToString() string { - return (*net.UDPAddr)(e).String() + return (*netip.AddrPort)(e).String() } func (e *StdNetEndpoint) SrcToString() string { @@ -160,18 +152,15 @@ func (bind *StdNetBind) Close() error { func (*StdNetBind) makeReceiveIPv4(conn *net.UDPConn) ReceiveFunc { return func(buff []byte) (int, Endpoint, error) { - n, endpoint, err := conn.ReadFromUDP(buff) - if endpoint != nil { - endpoint.IP = endpoint.IP.To4() - } - return n, (*StdNetEndpoint)(endpoint), err + n, endpoint, err := conn.ReadFromUDPAddrPort(buff) + return n, (*StdNetEndpoint)(&endpoint), err } } func (*StdNetBind) makeReceiveIPv6(conn *net.UDPConn) ReceiveFunc { return func(buff []byte) (int, Endpoint, error) { - n, endpoint, err := conn.ReadFromUDP(buff) - return n, (*StdNetEndpoint)(endpoint), err + n, endpoint, err := conn.ReadFromUDPAddrPort(buff) + return n, (*StdNetEndpoint)(&endpoint), err } } @@ -181,11 +170,16 @@ func (bind *StdNetBind) Send(buff []byte, endpoint Endpoint) error { if !ok { return ErrWrongEndpointType } - + addr := (*netip.AddrPort)(nend) bind.mu.Lock() - blackhole := bind.blackhole4 - conn := bind.ipv4 - if nend.IP.To4() == nil { + var ( + blackhole bool + conn *net.UDPConn + ) + if addr.Addr().Is4() { + blackhole = bind.blackhole4 + conn = bind.ipv4 + } else if addr.Addr().Is6() { blackhole = bind.blackhole6 conn = bind.ipv6 } @@ -197,6 +191,6 @@ func (bind *StdNetBind) Send(buff []byte, endpoint Endpoint) error { if conn == nil { return syscall.EAFNOSUPPORT } - _, err = conn.WriteToUDP(buff, (*net.UDPAddr)(nend)) + _, err = conn.WriteToUDPAddrPort(buff, *addr) return err } diff --git a/conn/bind_windows.go b/conn/bind_windows.go index 26a3af8..e3154c9 100644 --- a/conn/bind_windows.go +++ b/conn/bind_windows.go @@ -9,13 +9,13 @@ import ( "encoding/binary" "io" "net" + "net/netip" "strconv" "sync" "sync/atomic" "unsafe" "golang.org/x/sys/windows" - "golang.zx2c4.com/go118/netip" "golang.zx2c4.com/wireguard/conn/winrio" ) diff --git a/conn/bindtest/bindtest.go b/conn/bindtest/bindtest.go index 6a45896..0c83079 100644 --- a/conn/bindtest/bindtest.go +++ b/conn/bindtest/bindtest.go @@ -9,9 +9,9 @@ import ( "fmt" "math/rand" "net" + "net/netip" "os" - "golang.zx2c4.com/go118/netip" "golang.zx2c4.com/wireguard/conn" ) diff --git a/conn/conn.go b/conn/conn.go index 35fb6b1..5a93b2b 100644 --- a/conn/conn.go +++ b/conn/conn.go @@ -9,11 +9,10 @@ package conn import ( "errors" "fmt" + "net/netip" "reflect" "runtime" "strings" - - "golang.zx2c4.com/go118/netip" ) // A ReceiveFunc receives a single inbound packet from the network. diff --git a/device/allowedips.go b/device/allowedips.go index a6534b1..95dde63 100644 --- a/device/allowedips.go +++ b/device/allowedips.go @@ -10,10 +10,9 @@ import ( "errors" "math/bits" "net" + "net/netip" "sync" "unsafe" - - "golang.zx2c4.com/go118/netip" ) type parentIndirection struct { diff --git a/device/allowedips_rand_test.go b/device/allowedips_rand_test.go index ff56fe6..0d3eecb 100644 --- a/device/allowedips_rand_test.go +++ b/device/allowedips_rand_test.go @@ -8,10 +8,9 @@ package device import ( "math/rand" "net" + "net/netip" "sort" "testing" - - "golang.zx2c4.com/go118/netip" ) const ( diff --git a/device/allowedips_test.go b/device/allowedips_test.go index a274997..b0fc817 100644 --- a/device/allowedips_test.go +++ b/device/allowedips_test.go @@ -8,9 +8,8 @@ package device import ( "math/rand" "net" + "net/netip" "testing" - - "golang.zx2c4.com/go118/netip" ) type testPairCommonBits struct { diff --git a/device/device_test.go b/device/device_test.go index 84221be..5439924 100644 --- a/device/device_test.go +++ b/device/device_test.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "math/rand" + "net/netip" "runtime" "runtime/pprof" "sync" @@ -18,7 +19,6 @@ import ( "testing" "time" - "golang.zx2c4.com/go118/netip" "golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/conn/bindtest" "golang.zx2c4.com/wireguard/tun/tuntest" diff --git a/device/endpoint_test.go b/device/endpoint_test.go index f1ae47e..b265be6 100644 --- a/device/endpoint_test.go +++ b/device/endpoint_test.go @@ -7,8 +7,7 @@ package device import ( "math/rand" - - "golang.zx2c4.com/go118/netip" + "net/netip" ) type DummyEndpoint struct { diff --git a/device/receive.go b/device/receive.go index cc34498..5857481 100644 --- a/device/receive.go +++ b/device/receive.go @@ -17,6 +17,7 @@ import ( "golang.org/x/crypto/chacha20poly1305" "golang.org/x/net/ipv4" "golang.org/x/net/ipv6" + "golang.zx2c4.com/wireguard/conn" ) diff --git a/device/send.go b/device/send.go index b05c69e..7fd90ea 100644 --- a/device/send.go +++ b/device/send.go @@ -10,6 +10,7 @@ import ( "encoding/binary" "errors" "net" + "net/netip" "os" "sync" "sync/atomic" diff --git a/device/uapi.go b/device/uapi.go index 9572fb8..beff5aa 100644 --- a/device/uapi.go +++ b/device/uapi.go @@ -12,13 +12,13 @@ import ( "fmt" "io" "net" + "net/netip" "strconv" "strings" "sync" "sync/atomic" "time" - "golang.zx2c4.com/go118/netip" "golang.zx2c4.com/wireguard/ipc" ) diff --git a/ratelimiter/ratelimiter.go b/ratelimiter/ratelimiter.go index 8e78d5e..1e3c252 100644 --- a/ratelimiter/ratelimiter.go +++ b/ratelimiter/ratelimiter.go @@ -6,10 +6,9 @@ package ratelimiter import ( + "net/netip" "sync" "time" - - "golang.zx2c4.com/go118/netip" ) const ( diff --git a/ratelimiter/ratelimiter_test.go b/ratelimiter/ratelimiter_test.go index 3e06ff7..ca7db72 100644 --- a/ratelimiter/ratelimiter_test.go +++ b/ratelimiter/ratelimiter_test.go @@ -6,10 +6,9 @@ package ratelimiter import ( + "net/netip" "testing" "time" - - "golang.zx2c4.com/go118/netip" ) type result struct { diff --git a/tun/netstack/examples/http_client.go b/tun/netstack/examples/http_client.go index b39b453..352c1e4 100644 --- a/tun/netstack/examples/http_client.go +++ b/tun/netstack/examples/http_client.go @@ -12,8 +12,8 @@ import ( "io" "log" "net/http" + "net/netip" - "golang.zx2c4.com/go118/netip" "golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/tun/netstack" diff --git a/tun/netstack/examples/http_server.go b/tun/netstack/examples/http_server.go index 40f7804..0fdf4cd 100644 --- a/tun/netstack/examples/http_server.go +++ b/tun/netstack/examples/http_server.go @@ -13,8 +13,8 @@ import ( "log" "net" "net/http" + "net/netip" - "golang.zx2c4.com/go118/netip" "golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/tun/netstack" diff --git a/tun/netstack/go.mod b/tun/netstack/go.mod index 46b57ba..8db9f4b 100644 --- a/tun/netstack/go.mod +++ b/tun/netstack/go.mod @@ -6,7 +6,6 @@ require ( golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6 golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 // indirect golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect - golang.zx2c4.com/go118/netip v0.0.0-20211105124833-002a02cb0e53 golang.zx2c4.com/wireguard v0.0.0-20210424170727-c9db4b7aaa22 gvisor.dev/gvisor v0.0.0-20211020211948-f76a604701b6 ) diff --git a/tun/netstack/go.sum b/tun/netstack/go.sum index 01bfbc7..5c25b21 100644 --- a/tun/netstack/go.sum +++ b/tun/netstack/go.sum @@ -559,7 +559,6 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g= golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -805,10 +804,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.zx2c4.com/go118/netip v0.0.0-20211104120624-f0ae7a6e37c5 h1:mV4w4F7AtWXoDNkko9odoTdWpNwyDh8jx+S1fOZKDLg= -golang.zx2c4.com/go118/netip v0.0.0-20211104120624-f0ae7a6e37c5/go.mod h1:5yyfuiqVIJ7t+3MqrpTQ+QqRkMWiESiyDvPNvKYCecg= -golang.zx2c4.com/go118/netip v0.0.0-20211105124833-002a02cb0e53 h1:nFvpdzrHF9IPo9xPgayHWObCATpQYKky8VSSdt9lf9E= -golang.zx2c4.com/go118/netip v0.0.0-20211105124833-002a02cb0e53/go.mod h1:5yyfuiqVIJ7t+3MqrpTQ+QqRkMWiESiyDvPNvKYCecg= golang.zx2c4.com/wireguard v0.0.0-20210424170727-c9db4b7aaa22 h1:ytS28bw9HtZVDRMDxviC6ryCJuccw+zXhh04u2IRWJw= golang.zx2c4.com/wireguard v0.0.0-20210424170727-c9db4b7aaa22/go.mod h1:a057zjmoc00UN7gVkaJt2sXVK523kMJcogDTEvPIasg= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index f1c03f4..503fcdb 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -13,12 +13,12 @@ import ( "fmt" "io" "net" + "net/netip" "os" "strconv" "strings" "time" - "golang.zx2c4.com/go118/netip" "golang.zx2c4.com/wireguard/tun" "golang.org/x/net/dns/dnsmessage" diff --git a/tun/tuntest/tuntest.go b/tun/tuntest/tuntest.go index bdf0467..8196c34 100644 --- a/tun/tuntest/tuntest.go +++ b/tun/tuntest/tuntest.go @@ -8,9 +8,9 @@ package tuntest import ( "encoding/binary" "io" + "net/netip" "os" - "golang.zx2c4.com/go118/netip" "golang.zx2c4.com/wireguard/tun" )