]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
global: use Go 1.18's netip helpers jd/netip-go118
authorJason A. Donenfeld <Jason@zx2c4.com>
Fri, 5 Nov 2021 00:52:54 +0000 (01:52 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Sat, 6 Nov 2021 13:32:05 +0000 (14:32 +0100)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
21 files changed:
conn/bind_linux.go
conn/bind_std.go
conn/bind_windows.go
conn/bindtest/bindtest.go
conn/conn.go
device/allowedips.go
device/allowedips_rand_test.go
device/allowedips_test.go
device/device_test.go
device/endpoint_test.go
device/receive.go
device/send.go
device/uapi.go
ratelimiter/ratelimiter.go
ratelimiter/ratelimiter_test.go
tun/netstack/examples/http_client.go
tun/netstack/examples/http_server.go
tun/netstack/go.mod
tun/netstack/go.sum
tun/netstack/tun.go
tun/tuntest/tuntest.go

index 975a6ab60029e08e1530ff1ea1d4238d075d8183..8d418fc6e3faf98aa189bd0711126c38f09b5de5 100644 (file)
@@ -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 {
index 219c7194f690f1f5ac304bf5f2967b7e4ac81666..0f36081c6e1ca786e012b3e66b6cb1d62c052e67 100644 (file)
@@ -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
 }
index 26a3af85120842c9cff607345f48b334d0860427..e3154c9a6a65e78c4d9bdc14e0c32c210c4c70e5 100644 (file)
@@ -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"
 )
index 6a4589613f8f50fa34f748ed67b74bf6922c19aa..0c830794ca24b7d453da553c1a331505abd15b14 100644 (file)
@@ -9,9 +9,9 @@ import (
        "fmt"
        "math/rand"
        "net"
+       "net/netip"
        "os"
 
-       "golang.zx2c4.com/go118/netip"
        "golang.zx2c4.com/wireguard/conn"
 )
 
index 35fb6b1b0020d650bbc4144a9565a98c3bbbe2ef..5a93b2b85d3e3a2352779ea6dfbe22ed58e84215 100644 (file)
@@ -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.
index a6534b1942561dc7dc3ffdd78c366b2a468be73e..95dde63ca9dc26b10ab286c769a7c77b8247cca7 100644 (file)
@@ -10,10 +10,9 @@ import (
        "errors"
        "math/bits"
        "net"
+       "net/netip"
        "sync"
        "unsafe"
-
-       "golang.zx2c4.com/go118/netip"
 )
 
 type parentIndirection struct {
index ff56fe6a9050aa43b220aba8f6a7d93ba4cbc38b..0d3eecb067c188f85661cf62558557f386008c79 100644 (file)
@@ -8,10 +8,9 @@ package device
 import (
        "math/rand"
        "net"
+       "net/netip"
        "sort"
        "testing"
-
-       "golang.zx2c4.com/go118/netip"
 )
 
 const (
index a27499782a0012abd9c49a5fba03aa6eab5be964..b0fc8178d84de8701736ce4eeb0350dc7855433d 100644 (file)
@@ -8,9 +8,8 @@ package device
 import (
        "math/rand"
        "net"
+       "net/netip"
        "testing"
-
-       "golang.zx2c4.com/go118/netip"
 )
 
 type testPairCommonBits struct {
index 84221bed15e298dc14f4d2a670d9f423dbee949a..5439924eed1d10ad19cb8645b57de86e09024df1 100644 (file)
@@ -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"
index f1ae47e34dd549b0b9c3a5dd85a3c2f982479e81..b265be684950a08e29c5a2e2c22b9454a00a4a4f 100644 (file)
@@ -7,8 +7,7 @@ package device
 
 import (
        "math/rand"
-
-       "golang.zx2c4.com/go118/netip"
+       "net/netip"
 )
 
 type DummyEndpoint struct {
index cc3449801ca1dc6478db2f7fb35829ff075fc1f5..58574810f812eeea7b03646a22ad2201152ecc75 100644 (file)
@@ -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"
 )
 
index b05c69e94cc6a8d8dbe68b9f3a239b7d05178d89..7fd90eae77427c304a1de88b50e1868386c0e638 100644 (file)
@@ -10,6 +10,7 @@ import (
        "encoding/binary"
        "errors"
        "net"
+       "net/netip"
        "os"
        "sync"
        "sync/atomic"
index 9572fb8081c415012dd7fadf81d26a2e74ebee3b..beff5aa02c9e3dee7166df1c09095c2e2bcf4879 100644 (file)
@@ -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"
 )
 
index 8e78d5e46ad5d18f3ac8d234a54449d48e9ab2dd..1e3c252e55363445cb8932a41654dd5aa202155b 100644 (file)
@@ -6,10 +6,9 @@
 package ratelimiter
 
 import (
+       "net/netip"
        "sync"
        "time"
-
-       "golang.zx2c4.com/go118/netip"
 )
 
 const (
index 3e06ff77848843ddf21defd0270d766d0955c1c8..ca7db72b1e8200cdc0266fde3e04768261089d79 100644 (file)
@@ -6,10 +6,9 @@
 package ratelimiter
 
 import (
+       "net/netip"
        "testing"
        "time"
-
-       "golang.zx2c4.com/go118/netip"
 )
 
 type result struct {
index b39b453164a7ff6692884beef6919370e0ac2be8..352c1e46104b287aae87fca2c2d7aafce55a1b5a 100644 (file)
@@ -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"
index 40f780447e85543da3745b487763c5f07937311c..0fdf4cd8f8d899d4b535010e0bfaea7bc345cfd8 100644 (file)
@@ -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"
index 46b57bab10f8114589fbc9897942c61c776c993b..8db9f4bcdb51963bb730bdd5b37b2b9d23d4085e 100644 (file)
@@ -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
 )
index 01bfbc70cb0e1e94a6695497ca91664423af28d9..5c25b21767d22aa947caf98d0cecb15a92b8c6e8 100644 (file)
@@ -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=
index f1c03f48cceeebb1382ea7a7de93ecff36dafa5a..503fcdb0b2b1a8ce87386271ef31c087820cc5ec 100644 (file)
@@ -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"
index bdf0467b4f159cff07a140d66ea159f79ab441b6..8196c349dc857c17320ec36f10c43d9ea546a644 100644 (file)
@@ -8,9 +8,9 @@ package tuntest
 import (
        "encoding/binary"
        "io"
+       "net/netip"
        "os"
 
-       "golang.zx2c4.com/go118/netip"
        "golang.zx2c4.com/wireguard/tun"
 )