github.com/cilium/cilium@v1.16.2/pkg/types/ipv6.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package types 5 6 import ( 7 "net" 8 "net/netip" 9 ) 10 11 // IPv6 is the binary representation for encoding in binary structs. 12 type IPv6 [16]byte 13 14 func (v6 IPv6) IP() net.IP { 15 return v6[:] 16 } 17 18 func (v6 IPv6) Addr() netip.Addr { 19 return netip.AddrFrom16(v6) 20 } 21 22 func (v6 IPv6) String() string { 23 return v6.IP().String() 24 } 25 26 // DeepCopyInto is a deepcopy function, copying the receiver, writing into out. in must be non-nil. 27 func (v6 *IPv6) DeepCopyInto(out *IPv6) { 28 copy(out[:], v6[:]) 29 } 30 31 // FromAddr will populate the receiver with the specified address if and only 32 // if the provided address is a valid IPv6 address. Any other address, 33 // including the "invalid ip" value netip.Addr{} will zero the receiver. 34 func (v6 *IPv6) FromAddr(addr netip.Addr) { 35 if addr.Is6() { 36 a := IPv6(addr.As16()) 37 copy(v6[:], a[:]) 38 } else { 39 clear(v6[:]) 40 } 41 }