1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 01:05:15 +01:00

global: use Go 1.18's netip helpers

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2021-11-05 01:52:54 +01:00
parent 23d4e52ac9
commit b51df10251
21 changed files with 41 additions and 58 deletions

View File

@ -8,13 +8,13 @@ package conn
import ( import (
"errors" "errors"
"net" "net"
"net/netip"
"strconv" "strconv"
"sync" "sync"
"syscall" "syscall"
"unsafe" "unsafe"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"golang.zx2c4.com/go118/netip"
) )
type ipv4Source struct { type ipv4Source struct {

View File

@ -8,10 +8,9 @@ package conn
import ( import (
"errors" "errors"
"net" "net"
"net/netip"
"sync" "sync"
"syscall" "syscall"
"golang.zx2c4.com/go118/netip"
) )
// StdNetBind is meant to be a temporary solution on platforms for which // StdNetBind is meant to be a temporary solution on platforms for which
@ -28,24 +27,20 @@ type StdNetBind struct {
func NewStdNetBind() Bind { return &StdNetBind{} } func NewStdNetBind() Bind { return &StdNetBind{} }
type StdNetEndpoint net.UDPAddr type StdNetEndpoint netip.AddrPort
var _ Bind = (*StdNetBind)(nil) var _ Bind = (*StdNetBind)(nil)
var _ Endpoint = (*StdNetEndpoint)(nil) var _ Endpoint = (*StdNetEndpoint)(nil)
func (*StdNetBind) ParseEndpoint(s string) (Endpoint, error) { func (*StdNetBind) ParseEndpoint(s string) (Endpoint, error) {
e, err := netip.ParseAddrPort(s) e, err := netip.ParseAddrPort(s)
return (*StdNetEndpoint)(&net.UDPAddr{ return (*StdNetEndpoint)(&e), err
IP: e.Addr().AsSlice(),
Port: int(e.Port()),
Zone: e.Addr().Zone(),
}), err
} }
func (*StdNetEndpoint) ClearSrc() {} func (*StdNetEndpoint) ClearSrc() {}
func (e *StdNetEndpoint) DstIP() netip.Addr { func (e *StdNetEndpoint) DstIP() netip.Addr {
return netip.AddrFromSlice((*net.UDPAddr)(e).IP) return (*netip.AddrPort)(e).Addr()
} }
func (e *StdNetEndpoint) SrcIP() netip.Addr { func (e *StdNetEndpoint) SrcIP() netip.Addr {
@ -53,18 +48,15 @@ func (e *StdNetEndpoint) SrcIP() netip.Addr {
} }
func (e *StdNetEndpoint) DstToBytes() []byte { func (e *StdNetEndpoint) DstToBytes() []byte {
addr := (*net.UDPAddr)(e) addr := (*netip.AddrPort)(e)
out := addr.IP.To4() out := addr.Addr().AsSlice()
if out == nil { out = append(out, byte(addr.Port()&0xff))
out = addr.IP out = append(out, byte((addr.Port()>>8)&0xff))
}
out = append(out, byte(addr.Port&0xff))
out = append(out, byte((addr.Port>>8)&0xff))
return out return out
} }
func (e *StdNetEndpoint) DstToString() string { func (e *StdNetEndpoint) DstToString() string {
return (*net.UDPAddr)(e).String() return (*netip.AddrPort)(e).String()
} }
func (e *StdNetEndpoint) SrcToString() string { func (e *StdNetEndpoint) SrcToString() string {
@ -160,18 +152,15 @@ func (bind *StdNetBind) Close() error {
func (*StdNetBind) makeReceiveIPv4(conn *net.UDPConn) ReceiveFunc { func (*StdNetBind) makeReceiveIPv4(conn *net.UDPConn) ReceiveFunc {
return func(buff []byte) (int, Endpoint, error) { return func(buff []byte) (int, Endpoint, error) {
n, endpoint, err := conn.ReadFromUDP(buff) n, endpoint, err := conn.ReadFromUDPAddrPort(buff)
if endpoint != nil { return n, (*StdNetEndpoint)(&endpoint), err
endpoint.IP = endpoint.IP.To4()
}
return n, (*StdNetEndpoint)(endpoint), err
} }
} }
func (*StdNetBind) makeReceiveIPv6(conn *net.UDPConn) ReceiveFunc { func (*StdNetBind) makeReceiveIPv6(conn *net.UDPConn) ReceiveFunc {
return func(buff []byte) (int, Endpoint, error) { return func(buff []byte) (int, Endpoint, error) {
n, endpoint, err := conn.ReadFromUDP(buff) n, endpoint, err := conn.ReadFromUDPAddrPort(buff)
return n, (*StdNetEndpoint)(endpoint), err return n, (*StdNetEndpoint)(&endpoint), err
} }
} }
@ -181,11 +170,16 @@ func (bind *StdNetBind) Send(buff []byte, endpoint Endpoint) error {
if !ok { if !ok {
return ErrWrongEndpointType return ErrWrongEndpointType
} }
addr := (*netip.AddrPort)(nend)
bind.mu.Lock() bind.mu.Lock()
blackhole := bind.blackhole4 var (
conn := bind.ipv4 blackhole bool
if nend.IP.To4() == nil { conn *net.UDPConn
)
if addr.Addr().Is4() {
blackhole = bind.blackhole4
conn = bind.ipv4
} else if addr.Addr().Is6() {
blackhole = bind.blackhole6 blackhole = bind.blackhole6
conn = bind.ipv6 conn = bind.ipv6
} }
@ -197,6 +191,6 @@ func (bind *StdNetBind) Send(buff []byte, endpoint Endpoint) error {
if conn == nil { if conn == nil {
return syscall.EAFNOSUPPORT return syscall.EAFNOSUPPORT
} }
_, err = conn.WriteToUDP(buff, (*net.UDPAddr)(nend)) _, err = conn.WriteToUDPAddrPort(buff, *addr)
return err return err
} }

View File

@ -9,13 +9,13 @@ import (
"encoding/binary" "encoding/binary"
"io" "io"
"net" "net"
"net/netip"
"strconv" "strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
"unsafe" "unsafe"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"golang.zx2c4.com/go118/netip"
"golang.zx2c4.com/wireguard/conn/winrio" "golang.zx2c4.com/wireguard/conn/winrio"
) )

View File

@ -9,9 +9,9 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
"net/netip"
"os" "os"
"golang.zx2c4.com/go118/netip"
"golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/conn"
) )

View File

@ -9,11 +9,10 @@ package conn
import ( import (
"errors" "errors"
"fmt" "fmt"
"net/netip"
"reflect" "reflect"
"runtime" "runtime"
"strings" "strings"
"golang.zx2c4.com/go118/netip"
) )
// A ReceiveFunc receives a single inbound packet from the network. // A ReceiveFunc receives a single inbound packet from the network.

View File

@ -10,10 +10,9 @@ import (
"errors" "errors"
"math/bits" "math/bits"
"net" "net"
"net/netip"
"sync" "sync"
"unsafe" "unsafe"
"golang.zx2c4.com/go118/netip"
) )
type parentIndirection struct { type parentIndirection struct {

View File

@ -8,10 +8,9 @@ package device
import ( import (
"math/rand" "math/rand"
"net" "net"
"net/netip"
"sort" "sort"
"testing" "testing"
"golang.zx2c4.com/go118/netip"
) )
const ( const (

View File

@ -8,9 +8,8 @@ package device
import ( import (
"math/rand" "math/rand"
"net" "net"
"net/netip"
"testing" "testing"
"golang.zx2c4.com/go118/netip"
) )
type testPairCommonBits struct { type testPairCommonBits struct {

View File

@ -11,6 +11,7 @@ import (
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand"
"net/netip"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"sync" "sync"
@ -18,7 +19,6 @@ import (
"testing" "testing"
"time" "time"
"golang.zx2c4.com/go118/netip"
"golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/conn"
"golang.zx2c4.com/wireguard/conn/bindtest" "golang.zx2c4.com/wireguard/conn/bindtest"
"golang.zx2c4.com/wireguard/tun/tuntest" "golang.zx2c4.com/wireguard/tun/tuntest"

View File

@ -7,8 +7,7 @@ package device
import ( import (
"math/rand" "math/rand"
"net/netip"
"golang.zx2c4.com/go118/netip"
) )
type DummyEndpoint struct { type DummyEndpoint struct {

View File

@ -17,6 +17,7 @@ import (
"golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/chacha20poly1305"
"golang.org/x/net/ipv4" "golang.org/x/net/ipv4"
"golang.org/x/net/ipv6" "golang.org/x/net/ipv6"
"golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/conn"
) )

View File

@ -10,6 +10,7 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"net" "net"
"net/netip"
"os" "os"
"sync" "sync"
"sync/atomic" "sync/atomic"

View File

@ -12,13 +12,13 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"net/netip"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"golang.zx2c4.com/go118/netip"
"golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/ipc"
) )

View File

@ -6,10 +6,9 @@
package ratelimiter package ratelimiter
import ( import (
"net/netip"
"sync" "sync"
"time" "time"
"golang.zx2c4.com/go118/netip"
) )
const ( const (

View File

@ -6,10 +6,9 @@
package ratelimiter package ratelimiter
import ( import (
"net/netip"
"testing" "testing"
"time" "time"
"golang.zx2c4.com/go118/netip"
) )
type result struct { type result struct {

View File

@ -12,8 +12,8 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"net/netip"
"golang.zx2c4.com/go118/netip"
"golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/conn"
"golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/tun/netstack" "golang.zx2c4.com/wireguard/tun/netstack"

View File

@ -13,8 +13,8 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/netip"
"golang.zx2c4.com/go118/netip"
"golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/conn"
"golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/tun/netstack" "golang.zx2c4.com/wireguard/tun/netstack"

View File

@ -6,7 +6,6 @@ require (
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6 golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 // indirect golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // 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 golang.zx2c4.com/wireguard v0.0.0-20210424170727-c9db4b7aaa22
gvisor.dev/gvisor v0.0.0-20211020211948-f76a604701b6 gvisor.dev/gvisor v0.0.0-20211020211948-f76a604701b6
) )

View 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-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-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-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/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-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/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-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-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/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 h1:ytS28bw9HtZVDRMDxviC6ryCJuccw+zXhh04u2IRWJw=
golang.zx2c4.com/wireguard v0.0.0-20210424170727-c9db4b7aaa22/go.mod h1:a057zjmoc00UN7gVkaJt2sXVK523kMJcogDTEvPIasg= 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= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=

View File

@ -13,12 +13,12 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"net/netip"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"golang.zx2c4.com/go118/netip"
"golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/tun"
"golang.org/x/net/dns/dnsmessage" "golang.org/x/net/dns/dnsmessage"

View File

@ -8,9 +8,9 @@ package tuntest
import ( import (
"encoding/binary" "encoding/binary"
"io" "io"
"net/netip"
"os" "os"
"golang.zx2c4.com/go118/netip"
"golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/tun"
) )