Documentation ¶
Overview ¶
The ip package contains yet another IP address (and CIDR) type :-). The types differ from the ones in the net package in that they are backed by fixed-sized arrays of the appropriate size. The key advantage of using a fixed-size array is that it makes the types hashable so they can be used as map keys. In addition, they can be converted to net.IP by slicing.
Index ¶
- Constants
- Variables
- func IPNetsEqual(net1, net2 *net.IPNet) bool
- func Int2NetIP(addr uint32) net.IP
- func ParseIPAs16Byte(ip string) (ipb [16]byte, ok bool)
- type Addr
- type CIDR
- func CIDRFromAddrAndPrefix(addr Addr, prefixLen int) CIDR
- func CIDRFromCalicoNet(ipNet calinet.IPNet) CIDR
- func CIDRFromIPNet(ipNet *net.IPNet) CIDR
- func CIDRFromNetIP(netIP net.IP) CIDR
- func CIDRFromString(cidrStr string) (CIDR, error)
- func CIDRsFromCalicoNets(ipNets []calinet.IPNet) []CIDR
- func CommonPrefix(a, b CIDR) CIDR
- func MustParseCIDROrIP(s string) CIDR
- func ParseCIDROrIP(s string) (CIDR, error)
- type CIDRNode
- type CIDRTrie
- func (t *CIDRTrie) ClosestDescendants(buf []CIDR, parent CIDR) []CIDR
- func (t *CIDRTrie) CoveredBy(cidr CIDR) bool
- func (t *CIDRTrie) Covers(cidr CIDR) bool
- func (t *CIDRTrie) Delete(cidr CIDR)
- func (t *CIDRTrie) Get(cidr CIDR) interface{}
- func (t *CIDRTrie) Intersects(cidr CIDR) bool
- func (t *CIDRTrie) LPM(cidr CIDR) (CIDR, interface{})
- func (t *CIDRTrie) LookupPath(buffer []CIDRTrieEntry, cidr CIDR) []CIDRTrieEntry
- func (t *CIDRTrie) ToSlice() []CIDRTrieEntry
- func (t *CIDRTrie) Update(cidr CIDR, value interface{})
- func (t *CIDRTrie) Visit(f func(cidr CIDR, data interface{}) bool)
- type CIDRTrieEntry
- type V4Addr
- func (a V4Addr) Add(n int) Addr
- func (a V4Addr) AsBinary() string
- func (a V4Addr) AsCIDR() CIDR
- func (a V4Addr) AsCalicoNetIP() calinet.IP
- func (a V4Addr) AsNetIP() net.IP
- func (a V4Addr) AsUint32() uint32
- func (a V4Addr) NthBit(n uint) int
- func (a V4Addr) String() string
- func (a V4Addr) Version() uint8
- type V4CIDR
- func (c V4CIDR) Addr() Addr
- func (c V4CIDR) AsBinary() string
- func (c V4CIDR) Contains(addr Addr) bool
- func (c V4CIDR) ContainsV4(addr V4Addr) bool
- func (c V4CIDR) IsSingleAddress() bool
- func (c V4CIDR) Prefix() uint8
- func (c V4CIDR) String() string
- func (c V4CIDR) ToIPNet() net.IPNet
- func (c V4CIDR) Version() uint8
- type V6Addr
- func (a V6Addr) Add(n int) Addr
- func (a V6Addr) AsBinary() string
- func (a V6Addr) AsCIDR() CIDR
- func (a V6Addr) AsCalicoNetIP() calinet.IP
- func (a V6Addr) AsNetIP() net.IP
- func (a V6Addr) AsUint64Pair() (uint64, uint64)
- func (a V6Addr) NthBit(n uint) int
- func (a V6Addr) String() string
- func (a V6Addr) Version() uint8
- type V6CIDR
- func (c V6CIDR) Addr() Addr
- func (c V6CIDR) AsBinary() string
- func (c V6CIDR) Contains(addr Addr) bool
- func (c V6CIDR) ContainsV6(addr V6Addr) bool
- func (c V6CIDR) IsSingleAddress() bool
- func (c V6CIDR) Prefix() uint8
- func (c V6CIDR) String() string
- func (c V6CIDR) ToIPNet() net.IPNet
- func (c V6CIDR) Version() uint8
Constants ¶
const ( IPv4SizeDword = 1 IPv6SizeDword = 4 )
Variables ¶
var ErrInvalidIP = errors.New("Failed to parse IP address")
Functions ¶
func IPNetsEqual ¶
func ParseIPAs16Byte ¶
Types ¶
type Addr ¶
type Addr interface { // Version returns the IP version; 4 or 6. Version() uint8 // AsNetIP returns a net.IP, which is backed by/shares storage with // this object. AsNetIP() net.IP AsCalicoNetIP() calinet.IP AsCIDR() CIDR String() string AsBinary() string Add(int) Addr NthBit(uint) int }
Addr represents either an IPv4 or IPv6 IP address.
func FromCalicoIP ¶
func FromIPOrCIDRString ¶
Parses an IP or CIDR string and returns the IP.
func FromString ¶
type CIDR ¶
type CIDR interface { Version() uint8 Addr() Addr Prefix() uint8 String() string ToIPNet() net.IPNet AsBinary() string Contains(addr Addr) bool // IsSingleAddress returns true if the CIDR represents a single address. // I.e. a /32 for IPv4 or a /128 for IPv6. IsSingleAddress() bool }
func CIDRFromAddrAndPrefix ¶
CIDRFromAddrAndPrefix.
func CIDRFromCalicoNet ¶
func CIDRFromIPNet ¶
CIDRFromIPNet converts a *net.IPNet to a CIDR; if passed nil, returns nil.
func CIDRFromNetIP ¶
CIDRFromNetIP converts the given IP into our CIDR representation as a /32 or /128.
func CIDRFromString ¶
func CIDRsFromCalicoNets ¶
func CommonPrefix ¶
func MustParseCIDROrIP ¶
MustParseCIDROrIP parses the given IP address or CIDR, treating IP addresses as "full length" CIDRs. For example, "10.0.0.1" is treated as "10.0.0.1/32". It panics on failure.
func ParseCIDROrIP ¶
ParseCIDROrIP parses the given IP address or CIDR, treating IP addresses as "full length" CIDRs. For example, "10.0.0.1" is treated as "10.0.0.1/32".
type CIDRTrie ¶
type CIDRTrie struct {
// contains filtered or unexported fields
}
func NewCIDRTrie ¶
func NewCIDRTrie() *CIDRTrie
func (*CIDRTrie) ClosestDescendants ¶
ClosestDescendants returns a list of CIDRs representing the closest descendants of the given CIDR in the trie that have data associated with them. It does not return a full list of all descendants - only any descendants that are tied for being the closest to the given CIDR.
For example, given the following trie, where * indicates an intermediate node with no data associated with it:
10.0.0.0/16 | +----------------+ | | 10.0.1.0/24 10.0.2.0/24* | | 10.0.1.1/32 10.0.2.1/32
ClosestDescendants(10.0.0.0/16) => [10.0.1.0/24, 10.0.2.1/32] ClosestDescendants(10.0.1.0/24) => 10.0.1.1/32 ClosestDescendants(10.0.2.0/24) => 10.0.2.1/32
The caller may pass in a buffer to store the results, which will be appended to and returned. If the buffer is nil, a new slice will be created and returned.
func (*CIDRTrie) Intersects ¶
func (*CIDRTrie) LookupPath ¶
func (t *CIDRTrie) LookupPath(buffer []CIDRTrieEntry, cidr CIDR) []CIDRTrieEntry
LookupPath looks up the given CIDR in the trie. It returns a slice containing a CIDRTrieEntry for each CIDR in the trie that encloses the given CIDR. If buffer is non-nil, then it is used to store the entries; if it is too short append() is used to extend it and the updated slice is returned.
If the CIDR is not in the trie then an empty slice is returned.
func (*CIDRTrie) ToSlice ¶
func (t *CIDRTrie) ToSlice() []CIDRTrieEntry
type CIDRTrieEntry ¶
type CIDRTrieEntry struct { CIDR CIDR Data interface{} }
type V4CIDR ¶
type V4CIDR struct {
// contains filtered or unexported fields
}
func V4CommonPrefix ¶
func (V4CIDR) ContainsV4 ¶
func (V4CIDR) IsSingleAddress ¶
type V6Addr ¶
type V6Addr [16]byte
func (V6Addr) AsCalicoNetIP ¶
func (V6Addr) AsUint64Pair ¶
AsUint64Pair returns a pair of uint64 representing a V6Addr as there is no native 128 bit uint type in go.
type V6CIDR ¶
type V6CIDR struct {
// contains filtered or unexported fields
}