ip

package
v1.11.0-cni-plu...-ddfc3b1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2024 License: Apache-2.0, Apache-2.0 Imports: 9 Imported by: 0

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

View Source
const (
	IPv4SizeDword = 1
	IPv6SizeDword = 4
)

Variables

View Source
var ErrInvalidIP = errors.New("Failed to parse IP address")

Functions

func IPNetsEqual

func IPNetsEqual(net1, net2 *net.IPNet) bool

func Int2NetIP

func Int2NetIP(addr uint32) net.IP

func ParseIPAs16Byte

func ParseIPAs16Byte(ip string) (ipb [16]byte, ok bool)

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 FromCalicoIP(ip calinet.IP) Addr

func FromIPOrCIDRString

func FromIPOrCIDRString(s string) Addr

Parses an IP or CIDR string and returns the IP.

func FromNetIP

func FromNetIP(netIP net.IP) Addr

func FromString

func FromString(s string) Addr

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

func CIDRFromAddrAndPrefix(addr Addr, prefixLen int) CIDR

CIDRFromAddrAndPrefix.

func CIDRFromCalicoNet

func CIDRFromCalicoNet(ipNet calinet.IPNet) CIDR

func CIDRFromIPNet

func CIDRFromIPNet(ipNet *net.IPNet) CIDR

CIDRFromIPNet converts a *net.IPNet to a CIDR; if passed nil, returns nil.

func CIDRFromNetIP

func CIDRFromNetIP(netIP net.IP) CIDR

CIDRFromNetIP converts the given IP into our CIDR representation as a /32 or /128.

func CIDRFromString

func CIDRFromString(cidrStr string) (CIDR, error)

func CIDRsFromCalicoNets

func CIDRsFromCalicoNets(ipNets []calinet.IPNet) []CIDR

func CommonPrefix

func CommonPrefix(a, b CIDR) CIDR

func MustParseCIDROrIP

func MustParseCIDROrIP(s string) CIDR

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

func ParseCIDROrIP(s string) (CIDR, error)

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 CIDRNode

type CIDRNode struct {
	// contains filtered or unexported fields
}

type CIDRTrie

type CIDRTrie struct {
	// contains filtered or unexported fields
}

func NewCIDRTrie

func NewCIDRTrie() *CIDRTrie

func (*CIDRTrie) ClosestDescendants

func (t *CIDRTrie) ClosestDescendants(buf []CIDR, parent CIDR) []CIDR

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) CoveredBy

func (t *CIDRTrie) CoveredBy(cidr CIDR) bool

func (*CIDRTrie) Covers

func (t *CIDRTrie) Covers(cidr CIDR) bool

func (*CIDRTrie) Delete

func (t *CIDRTrie) Delete(cidr CIDR)

func (*CIDRTrie) Get

func (t *CIDRTrie) Get(cidr CIDR) interface{}

func (*CIDRTrie) Intersects

func (t *CIDRTrie) Intersects(cidr CIDR) bool

func (*CIDRTrie) LPM

func (t *CIDRTrie) LPM(cidr CIDR) (CIDR, interface{})

LPM does a longest prefix match on the trie

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

func (*CIDRTrie) Update

func (t *CIDRTrie) Update(cidr CIDR, value interface{})

func (*CIDRTrie) Visit

func (t *CIDRTrie) Visit(f func(cidr CIDR, data interface{}) bool)

type CIDRTrieEntry

type CIDRTrieEntry struct {
	CIDR CIDR
	Data interface{}
}

type V4Addr

type V4Addr [4]byte

func (V4Addr) Add

func (a V4Addr) Add(n int) Addr

func (V4Addr) AsBinary

func (a V4Addr) AsBinary() string

func (V4Addr) AsCIDR

func (a V4Addr) AsCIDR() CIDR

func (V4Addr) AsCalicoNetIP

func (a V4Addr) AsCalicoNetIP() calinet.IP

func (V4Addr) AsNetIP

func (a V4Addr) AsNetIP() net.IP

func (V4Addr) AsUint32

func (a V4Addr) AsUint32() uint32

func (V4Addr) NthBit

func (a V4Addr) NthBit(n uint) int

func (V4Addr) String

func (a V4Addr) String() string

func (V4Addr) Version

func (a V4Addr) Version() uint8

type V4CIDR

type V4CIDR struct {
	// contains filtered or unexported fields
}

func V4CommonPrefix

func V4CommonPrefix(a, b V4CIDR) V4CIDR

func (V4CIDR) Addr

func (c V4CIDR) Addr() Addr

func (V4CIDR) AsBinary

func (c V4CIDR) AsBinary() string

func (V4CIDR) Contains

func (c V4CIDR) Contains(addr Addr) bool

func (V4CIDR) ContainsV4

func (c V4CIDR) ContainsV4(addr V4Addr) bool

func (V4CIDR) IsSingleAddress

func (c V4CIDR) IsSingleAddress() bool

func (V4CIDR) Prefix

func (c V4CIDR) Prefix() uint8

func (V4CIDR) String

func (c V4CIDR) String() string

func (V4CIDR) ToIPNet

func (c V4CIDR) ToIPNet() net.IPNet

func (V4CIDR) Version

func (c V4CIDR) Version() uint8

type V6Addr

type V6Addr [16]byte

func (V6Addr) Add

func (a V6Addr) Add(n int) Addr

func (V6Addr) AsBinary

func (a V6Addr) AsBinary() string

func (V6Addr) AsCIDR

func (a V6Addr) AsCIDR() CIDR

func (V6Addr) AsCalicoNetIP

func (a V6Addr) AsCalicoNetIP() calinet.IP

func (V6Addr) AsNetIP

func (a V6Addr) AsNetIP() net.IP

func (V6Addr) AsUint64Pair

func (a V6Addr) AsUint64Pair() (uint64, uint64)

AsUint64Pair returns a pair of uint64 representing a V6Addr as there is no native 128 bit uint type in go.

func (V6Addr) NthBit

func (a V6Addr) NthBit(n uint) int

func (V6Addr) String

func (a V6Addr) String() string

func (V6Addr) Version

func (a V6Addr) Version() uint8

type V6CIDR

type V6CIDR struct {
	// contains filtered or unexported fields
}

func V6CommonPrefix

func V6CommonPrefix(a, b V6CIDR) V6CIDR

func (V6CIDR) Addr

func (c V6CIDR) Addr() Addr

func (V6CIDR) AsBinary

func (c V6CIDR) AsBinary() string

func (V6CIDR) Contains

func (c V6CIDR) Contains(addr Addr) bool

func (V6CIDR) ContainsV6

func (c V6CIDR) ContainsV6(addr V6Addr) bool

func (V6CIDR) IsSingleAddress

func (c V6CIDR) IsSingleAddress() bool

func (V6CIDR) Prefix

func (c V6CIDR) Prefix() uint8

func (V6CIDR) String

func (c V6CIDR) String() string

func (V6CIDR) ToIPNet

func (c V6CIDR) ToIPNet() net.IPNet

func (V6CIDR) Version

func (c V6CIDR) Version() uint8

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL