net/packet: add an IP6 constructor from a raw byte array.

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson 2020-12-15 02:28:14 -08:00 committed by Dave Anderson
parent f0b0a62873
commit aa353b8d0f

View File

@ -16,13 +16,17 @@ type IP6 struct {
Hi, Lo uint64 Hi, Lo uint64
} }
// IP6FromRaw16 converts a raw 16-byte IPv6 address to an IP6.
func IP6FromRaw16(ip [16]byte) IP6 {
return IP6{binary.BigEndian.Uint64(ip[:8]), binary.BigEndian.Uint64(ip[8:])}
}
// IP6FromNetaddr converts a netaddr.IP to an IP6. Panics if !ip.Is6. // IP6FromNetaddr converts a netaddr.IP to an IP6. Panics if !ip.Is6.
func IP6FromNetaddr(ip netaddr.IP) IP6 { func IP6FromNetaddr(ip netaddr.IP) IP6 {
if !ip.Is6() { if !ip.Is6() {
panic(fmt.Sprintf("IP6FromNetaddr called with non-v6 addr %q", ip)) panic(fmt.Sprintf("IP6FromNetaddr called with non-v6 addr %q", ip))
} }
b := ip.As16() return IP6FromRaw16(ip.As16())
return IP6{binary.BigEndian.Uint64(b[:8]), binary.BigEndian.Uint64(b[8:])}
} }
// Netaddr converts ip to a netaddr.IP. // Netaddr converts ip to a netaddr.IP.