net/packet: remove NewIP, offer only a netaddr constructor.

Signed-off-by: David Anderson <danderson@tailscale.com>
pull/914/head
David Anderson 4 years ago
parent d7ee3096dd
commit c2cc3acbaf

@ -7,7 +7,6 @@ package packet
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"net"
"inet.af/netaddr" "inet.af/netaddr"
) )
@ -15,17 +14,7 @@ import (
// IP4 is an IPv4 address. // IP4 is an IPv4 address.
type IP4 uint32 type IP4 uint32
// NewIP converts a standard library IP address into an IP. // IPFromNetaddr converts a netaddr.IP to an IP. Panics if !ip.Is4.
// It panics if b is not an IPv4 address.
func NewIP4(b net.IP) IP4 {
b4 := b.To4()
if b4 == nil {
panic(fmt.Sprintf("To4(%v) failed", b))
}
return IP4(binary.BigEndian.Uint32(b4))
}
// IPFromNetaddr converts a netaddr.IP to an IP.
func IP4FromNetaddr(ip netaddr.IP) IP4 { func IP4FromNetaddr(ip netaddr.IP) IP4 {
ipbytes := ip.As4() ipbytes := ip.As4()
return IP4(binary.BigEndian.Uint32(ipbytes[:])) return IP4(binary.BigEndian.Uint32(ipbytes[:]))

@ -6,7 +6,6 @@ package packet
import ( import (
"bytes" "bytes"
"net"
"reflect" "reflect"
"testing" "testing"
@ -31,7 +30,7 @@ func mustIP6(s string) IP6 {
func TestIP4String(t *testing.T) { func TestIP4String(t *testing.T) {
const str = "1.2.3.4" const str = "1.2.3.4"
ip := NewIP4(net.ParseIP(str)) ip := mustIP4(str)
var got string var got string
allocs := testing.AllocsPerRun(1000, func() { allocs := testing.AllocsPerRun(1000, func() {

@ -9,7 +9,6 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -25,6 +24,14 @@ var TCP = packet.TCP
var UDP = packet.UDP var UDP = packet.UDP
var Fragment = packet.Fragment var Fragment = packet.Fragment
func mustIP4(s string) packet.IP4 {
ip, err := netaddr.ParseIP(s)
if err != nil {
panic(err)
}
return packet.IP4FromNetaddr(ip)
}
func pfx(s string) netaddr.IPPrefix { func pfx(s string) netaddr.IPPrefix {
pfx, err := netaddr.ParseIPPrefix(s) pfx, err := netaddr.ParseIPPrefix(s)
if err != nil { if err != nil {
@ -435,19 +442,19 @@ func TestOmitDropLogging(t *testing.T) {
}, },
{ {
name: "v4_multicast_out_low", name: "v4_multicast_out_low",
pkt: &packet.Parsed{IPVersion: 4, DstIP4: packet.NewIP4(net.ParseIP("224.0.0.0"))}, pkt: &packet.Parsed{IPVersion: 4, DstIP4: mustIP4("224.0.0.0")},
dir: out, dir: out,
want: true, want: true,
}, },
{ {
name: "v4_multicast_out_high", name: "v4_multicast_out_high",
pkt: &packet.Parsed{IPVersion: 4, DstIP4: packet.NewIP4(net.ParseIP("239.255.255.255"))}, pkt: &packet.Parsed{IPVersion: 4, DstIP4: mustIP4("239.255.255.255")},
dir: out, dir: out,
want: true, want: true,
}, },
{ {
name: "v4_link_local_unicast", name: "v4_link_local_unicast",
pkt: &packet.Parsed{IPVersion: 4, DstIP4: packet.NewIP4(net.ParseIP("169.254.1.2"))}, pkt: &packet.Parsed{IPVersion: 4, DstIP4: mustIP4("169.254.1.2")},
dir: out, dir: out,
want: true, want: true,
}, },

@ -515,7 +515,7 @@ func (p *pinger) run(ctx context.Context, peerKey wgcfg.Key, ips []wgcfg.IP, src
start := time.Now() start := time.Now()
var dstIPs []packet.IP4 var dstIPs []packet.IP4
for _, ip := range ips { for _, ip := range ips {
dstIPs = append(dstIPs, packet.NewIP4(ip.IP())) dstIPs = append(dstIPs, packet.IP4FromNetaddr(netaddr.IPFrom16(ip.Addr)))
} }
payload := []byte("magicsock_spray") // no meaning payload := []byte("magicsock_spray") // no meaning
@ -555,7 +555,7 @@ func (e *userspaceEngine) pinger(peerKey wgcfg.Key, ips []wgcfg.IP) {
e.wgLock.Lock() e.wgLock.Lock()
if len(e.lastCfgFull.Addresses) > 0 { if len(e.lastCfgFull.Addresses) > 0 {
srcIP = packet.NewIP4(e.lastCfgFull.Addresses[0].IP.IP()) srcIP = packet.IP4FromNetaddr(netaddr.IPFrom16(e.lastCfgFull.Addresses[0].IP.Addr))
} }
e.wgLock.Unlock() e.wgLock.Unlock()

Loading…
Cancel
Save