github.com/cilium/cilium@v1.16.2/pkg/types/ipv4.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 // IPv4 is the binary representation for encoding in binary structs. 12 type IPv4 [4]byte 13 14 func (v4 IPv4) IsZero() bool { 15 return v4[0] == 0 && v4[1] == 0 && v4[2] == 0 && v4[3] == 0 16 } 17 18 func (v4 IPv4) IP() net.IP { 19 return v4[:] 20 } 21 22 func (v4 IPv4) Addr() netip.Addr { 23 return netip.AddrFrom4(v4) 24 } 25 26 func (v4 IPv4) String() string { 27 return v4.IP().String() 28 } 29 30 // DeepCopyInto is a deepcopy function, copying the receiver, writing into out. in must be non-nil. 31 func (v4 *IPv4) DeepCopyInto(out *IPv4) { 32 copy(out[:], v4[:]) 33 } 34 35 // FromAddr will populate the receiver with the specified address if and only 36 // if the provided address is a valid IPv4 address. Any other address, 37 // including the "invalid ip" value netip.Addr{} will zero the receiver. 38 func (v4 *IPv4) FromAddr(addr netip.Addr) { 39 if addr.Is4() { 40 a := IPv4(addr.As4()) 41 copy(v4[:], a[:]) 42 } else { 43 clear(v4[:]) 44 } 45 }