From aa353b8d0f5358bfc93fa735c57015237ab37e95 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 15 Dec 2020 02:28:14 -0800 Subject: [PATCH] net/packet: add an IP6 constructor from a raw byte array. Signed-off-by: David Anderson --- net/packet/ip6.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/net/packet/ip6.go b/net/packet/ip6.go index 407a93216..8fd964c21 100644 --- a/net/packet/ip6.go +++ b/net/packet/ip6.go @@ -16,13 +16,17 @@ type IP6 struct { 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. func IP6FromNetaddr(ip netaddr.IP) IP6 { if !ip.Is6() { panic(fmt.Sprintf("IP6FromNetaddr called with non-v6 addr %q", ip)) } - b := ip.As16() - return IP6{binary.BigEndian.Uint64(b[:8]), binary.BigEndian.Uint64(b[8:])} + return IP6FromRaw16(ip.As16()) } // Netaddr converts ip to a netaddr.IP.