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

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2020-11-10 21:58:09 -08:00
parent d7ee3096dd
commit c2cc3acbaf
4 changed files with 15 additions and 20 deletions

View File

@ -7,7 +7,6 @@ package packet
import (
"encoding/binary"
"fmt"
"net"
"inet.af/netaddr"
)
@ -15,17 +14,7 @@ import (
// IP4 is an IPv4 address.
type IP4 uint32
// NewIP converts a standard library IP address into an IP.
// 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.
// IPFromNetaddr converts a netaddr.IP to an IP. Panics if !ip.Is4.
func IP4FromNetaddr(ip netaddr.IP) IP4 {
ipbytes := ip.As4()
return IP4(binary.BigEndian.Uint32(ipbytes[:]))

View File

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