Documentation ¶
Overview ¶
Package ip is a library for performing manipulations on IPv4 and IPv6 addresses and CIDR prefixes.
Index ¶
- func CoalesceCIDRs(cidrs []*net.IPNet) ([]*net.IPNet, []*net.IPNet)
- func CountIPsInCIDR(ipnet *net.IPNet) int
- func GetCIDRPrefixesFromIPs(ips []net.IP) []*net.IPNet
- func GetNextIP(ip net.IP) net.IP
- func IPToPrefix(ip net.IP) *net.IPNet
- func IsPublicAddr(ip net.IP) bool
- func KeepUniqueIPs(ips []net.IP) []net.IP
- func ParseCIDRs(cidrs []string) (valid []*net.IPNet, invalid []string)
- func RemoveCIDRs(allowCIDRs, removeCIDRs []*net.IPNet) ([]*net.IPNet, error)
- type NetsByMask
- type NetsByRange
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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. Returns 0 if the input CIDR cannot be parsed.
func GetCIDRPrefixesFromIPs ¶ added in v1.6.0
GetCIDRPrefixesFromIPs returns all of the ips as a slice of *net.IPNet.
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 IPToPrefix ¶ added in v1.6.0
IPToPrefix returns the corresponding IPNet for the given IP.
func IsPublicAddr ¶
IsPublicAddr returns whether a given global IP is from a public range.
func KeepUniqueIPs ¶
KeepUniqueIPs transforms the provided multiset of IPs into a single set, lexicographically sorted via a byte-wise comparison of the IP slices (i.e. IPv4 addresses show up before IPv6). The slice is manipulated in-place destructively.
1- Sort the slice by comparing the IPs as bytes 2- For every unseen unique IP in the sorted slice, move it to the end of the return slice. Note that the slice is always large enough and, because it is sorted, we will not overwrite a valid element with another. To overwrite an element i with j, i must have come before j AND we decided it was a duplicate of the element at i-1.
func ParseCIDRs ¶
ParseCIDRs fetches all CIDRs referred to by the specified slice and returns them as regular golang CIDR objects.
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.
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)