Documentation ¶
Overview ¶
Package ip is a library for performing manipulations on IPv4 and IPv6 addresses and CIDR prefixes.
Index ¶
- func AddrFromIP(ip net.IP) (netip.Addr, bool)
- func AddrToIPNet(addr netip.Addr) *net.IPNet
- func CoalesceCIDRs(cidrs []*net.IPNet) ([]*net.IPNet, []*net.IPNet)
- func CountIPsInCIDR(ipnet *net.IPNet) *big.Int
- func GetIPAtIndex(ipNet net.IPNet, index int64) net.IP
- func GetIPFromListByFamily(ipList []net.IP, v4Family bool) net.IP
- func GetNextIP(ip net.IP) net.IP
- func IPToNetPrefix(ip net.IP) netip.Prefix
- func IPToPrefix(ip net.IP) *net.IPNet
- func IPsToNetPrefixes(ips []net.IP) []netip.Prefix
- func IsIPv4(ip net.IP) bool
- func IsIPv6(ip net.IP) bool
- func IsPublicAddr(ip net.IP) bool
- func KeepUniqueAddrs(addrs []netip.Addr) []netip.Addr
- func ListContainsIP(ipList []net.IP, ip net.IP) bool
- func MustAddrFromIP(ip net.IP) netip.Addr
- func MustAddrsFromIPs(ips []net.IP) []netip.Addr
- func NetsContainsAny(a, b []*net.IPNet) bool
- func ParseCIDRs(cidrs []string) (valid []*net.IPNet, invalid []string)
- func ParsePrefixes(cidrs []string) (valid []netip.Prefix, invalid []string, errors []error)
- func ParseScope(scope string) (int, error)
- func PartitionCIDR(targetCIDR net.IPNet, excludeCIDR net.IPNet) ([]*net.IPNet, []*net.IPNet, []*net.IPNet)
- func PrefixCeil(numIPs int, multiple int) int
- func PrefixToIps(prefixCidr string, maxIPs int) ([]string, error)
- func RemoveCIDRs(allowCIDRs, removeCIDRs []*net.IPNet) []*net.IPNet
- func SortAddrList(ipList []netip.Addr)
- func SortIPList(ipList []net.IP)
- func UnsortedIPListsAreEqual(ipList1, ipList2 []net.IP) bool
- type NetsByMask
- type NetsByRange
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddrFromIP ¶
AddrFromIP converts a net.IP to netip.Addr using netip.AddrFromSlice, but preserves the original address family. It assumes given net.IP is not an IPv4 mapped IPv6 address.
The problem behind this is that when we convert the IPv4 net.IP address with netip.AddrFromSlice, the address is interpreted as an IPv4 mapped IPv6 address in some cases.
For example, when we do netip.AddrFromSlice(net.ParseIP("1.1.1.1")), it is interpreted as an IPv6 address "::ffff:1.1.1.1". This is because 1) net.IP created with net.ParseIP(IPv4 string) holds IPv4 address as an IPv4 mapped IPv6 address internally and 2) netip.AddrFromSlice recognizes address family with length of the slice (4-byte = IPv4 and 16-byte = IPv6).
By using AddrFromIP, we can preserve the address family, but since we cannot distinguish IPv4 and IPv4 mapped IPv6 address only from net.IP value (see #37921 on golang/go) we need an assumption that given net.IP is not an IPv4 mapped IPv6 address.
func AddrToIPNet ¶
AddrToIPNet is a convenience helper to convert a netip.Addr to a *net.IPNet with a mask corresponding to the addresses's bit length.
func CoalesceCIDRs ¶
CoalesceCIDRs transforms the provided list of CIDRs into the most-minimal equivalent set of IPv4 and IPv6 CIDRs. It removes CIDRs that are subnets of other CIDRs in the list, and groups together CIDRs that have the same mask size into a CIDR of the same mask size provided that they share the same number of most significant mask-size bits.
Note: this algorithm was ported from the Python library netaddr. https://github.com/drkjam/netaddr .
func CountIPsInCIDR ¶
CountIPsInCIDR takes a RFC4632/RFC4291-formatted IPv4/IPv6 CIDR and determines how many IP addresses reside within that CIDR. The first and the last (base and broadcast) IPs are excluded.
Returns 0 if the input CIDR cannot be parsed.
func GetIPAtIndex ¶
GetIPAtIndex get the IP by index in the range of ipNet. The index is start with 0.
func GetIPFromListByFamily ¶
GetIPFromListByFamily returns a single IP address of the provided family from a list of ip addresses.
func GetNextIP ¶
GetNextIP returns the next IP from the given IP address. If the given IP is the last IP of a v4 or v6 range, the same IP is returned.
func IPToNetPrefix ¶
IPToNetPrefix is a convenience helper for migrating from the older 'net' standard library types to the newer 'netip' types. Use this to plug the new types in newer code into older types in older code during the migration.
Note: This function assumes given ip is not an IPv4 mapped IPv6 address. See the comment of AddrFromIP for more details.
func IPToPrefix ¶
IPToPrefix returns the corresponding IPNet for the given IP.
func IPsToNetPrefixes ¶
IPsToNetPrefixes returns all of the ips as a slice of netip.Prefix.
See IPToNetPrefix() for how net.IP types are handled by this function.
func IsPublicAddr ¶
IsPublicAddr returns whether a given global IP is from a public range.
func KeepUniqueAddrs ¶
KeepUniqueAddrs transforms the provided multiset of IP addresses into a single set, lexicographically sorted via comparison of the addresses using netip.Addr.Compare (i.e. IPv4 addresses show up before IPv6). The slice is manipulated in-place destructively; it does not create a new slice.
func ListContainsIP ¶
ListContainsIP returns whether a list of IPs contains a given IP.
func MustAddrFromIP ¶
MustAddrFromIP is the same as AddrFromIP except that it assumes the input is a valid IP address and always returns a valid netip.Addr.
func MustAddrsFromIPs ¶
MustAddrsFromIPs converts a slice of net.IP to a slice of netip.Addr. It assumes the input slice contains only valid IP addresses and always returns a slice containing valid netip.Addr.
func NetsContainsAny ¶
NetsContainsAny checks that any subnet in the `a` subnet group *fully* contains any of the subnets in the `b` subnet group.
func ParseCIDRs ¶
ParseCIDRs fetches all CIDRs referred to by the specified slice and returns them as regular golang CIDR objects.
Deprecated. Consider using ParsePrefixes() instead.
func ParsePrefixes ¶
ParsePrefixes parses all CIDRs referred to by the specified slice and returns them as regular golang netip.Prefix objects.
func ParseScope ¶
func PartitionCIDR ¶
func PartitionCIDR(targetCIDR net.IPNet, excludeCIDR net.IPNet) ([]*net.IPNet, []*net.IPNet, []*net.IPNet)
PartitionCIDR returns a list of IP Networks partitioned upon excludeCIDR. The first list contains the networks to the left of the excludeCIDR in the partition, the second is a list containing the excludeCIDR itself if it is contained within the targetCIDR (nil otherwise), and the third is a list containing the networks to the right of the excludeCIDR in the partition.
func PrefixCeil ¶
PrefixCeil converts the given number of IPs to the minimum number of prefixes needed to host those IPs. multiple indicates the number of IPs in a single prefix.
func PrefixToIps ¶
PrefixToIps converts the given prefix to an array containing IPs in the provided prefix/CIDR block. When maxIPs is set to 0, the returned array will contain all IPs in the given prefix. Otherwise, the returned array of IPs will be limited to the value of maxIPs starting at the first IP in the provided CIDR. For example, when providing 192.168.1.0/28 as a CIDR with 4 maxIPs, 192.168.1.0, 192.168.1.1, 192.168.1.2, 192.168.1.3 will be returned.
func RemoveCIDRs ¶
RemoveCIDRs removes the specified CIDRs from another set of CIDRs. If a CIDR to remove is not contained within the CIDR, the CIDR to remove is ignored. A slice of CIDRs is returned which contains the set of CIDRs provided minus the set of CIDRs which were removed. Both input slices may be modified by calling this function.
func SortAddrList ¶ added in v1.15.0
func SortIPList ¶
SortIPList sorts the provided net.IP slice in place.
func UnsortedIPListsAreEqual ¶
UnsortedIPListsAreEqual returns true if the list of net.IP provided is same without considering the order of the IPs in the list. The function will first attempt to sort both the IP lists and then validate equality for sorted lists.
Types ¶
type NetsByMask ¶
NetsByMask is used to sort a list of IP networks by the size of their masks. Implements sort.Interface.
func (NetsByMask) Len ¶
func (s NetsByMask) Len() int
func (NetsByMask) Less ¶
func (s NetsByMask) Less(i, j int) bool
func (NetsByMask) Swap ¶
func (s NetsByMask) Swap(i, j int)
type NetsByRange ¶
type NetsByRange []*netWithRange
NetsByRange is used to sort a list of ranges, first by their last IPs, then by their first IPs Implements sort.Interface.
func (NetsByRange) Len ¶
func (s NetsByRange) Len() int
func (NetsByRange) Less ¶
func (s NetsByRange) Less(i, j int) bool
func (NetsByRange) Swap ¶
func (s NetsByRange) Swap(i, j int)