Documentation
¶
Overview ¶
Package netipds builds on the net/netip & go4.org/netipx family of packages by adding two immutable, trie-based collection types for netip.Prefix:
- PrefixMap[T] - for associating data with IPs and prefixes and fetching that data with network hierarchy awareness
- PrefixSet - for storing sets of prefixes and combining those sets in useful ways (unions, intersections, etc)
Both are backed by a binary radix tree, which enables a rich set of efficient queries about prefix containment, hierarchy, and overlap.
Index ¶
- type PrefixMap
- func (m *PrefixMap[T]) AncestorsOf(p netip.Prefix) *PrefixMap[T]
- func (m *PrefixMap[T]) Contains(p netip.Prefix) bool
- func (m *PrefixMap[T]) DescendantsOf(p netip.Prefix) *PrefixMap[T]
- func (m *PrefixMap[T]) Encompasses(p netip.Prefix) bool
- func (m *PrefixMap[T]) Filter(s *PrefixSet) *PrefixMap[T]
- func (m *PrefixMap[T]) Get(p netip.Prefix) (T, bool)
- func (m *PrefixMap[T]) OverlapsPrefix(p netip.Prefix) bool
- func (m *PrefixMap[T]) ParentOf(p netip.Prefix) (parent netip.Prefix, val T, ok bool)
- func (m *PrefixMap[T]) RootOf(p netip.Prefix) (root netip.Prefix, val T, ok bool)
- func (m *PrefixMap[T]) Size() int
- func (m *PrefixMap[T]) ToMap() map[netip.Prefix]T
- type PrefixMapBuilder
- type PrefixSet
- func (s *PrefixSet) All() iter.Seq[netip.Prefix]
- func (s *PrefixSet) All4() iter.Seq[netip.Prefix]
- func (s *PrefixSet) All6() iter.Seq[netip.Prefix]
- func (s *PrefixSet) AllCompact() iter.Seq[netip.Prefix]
- func (s *PrefixSet) AllCompact4() iter.Seq[netip.Prefix]
- func (s *PrefixSet) AllCompact6() iter.Seq[netip.Prefix]
- func (s *PrefixSet) AncestorsOf(p netip.Prefix) *PrefixSet
- func (s *PrefixSet) Contains(p netip.Prefix) bool
- func (s *PrefixSet) DescendantsOf(p netip.Prefix) *PrefixSet
- func (s *PrefixSet) Encompasses(p netip.Prefix) bool
- func (s *PrefixSet) OverlapsPrefix(p netip.Prefix) bool
- func (s *PrefixSet) ParentOf(p netip.Prefix) (parent netip.Prefix, ok bool)
- func (s *PrefixSet) Prefixes() []netip.Prefix
- func (s *PrefixSet) PrefixesCompact() []netip.Prefix
- func (s *PrefixSet) RootOf(p netip.Prefix) (root netip.Prefix, ok bool)
- func (s *PrefixSet) Size() int
- type PrefixSetBuilder
- func (s *PrefixSetBuilder) Add(p netip.Prefix) error
- func (s *PrefixSetBuilder) Filter(o *PrefixSet)
- func (s *PrefixSetBuilder) Intersect(o *PrefixSet)
- func (s *PrefixSetBuilder) Merge(o *PrefixSet)
- func (s *PrefixSetBuilder) PrefixSet() *PrefixSet
- func (s *PrefixSetBuilder) Remove(p netip.Prefix) error
- func (s *PrefixSetBuilder) Subtract(o *PrefixSet)
- func (s *PrefixSetBuilder) SubtractPrefix(p netip.Prefix) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PrefixMap ¶
type PrefixMap[T any] struct { // contains filtered or unexported fields }
PrefixMap is a map of netip.Prefix to T. It is implemented as a binary radix tree.
Use PrefixMapBuilder to construct PrefixMaps.
func (*PrefixMap[T]) AncestorsOf ¶
AncestorsOf returns a PrefixMap containing all ancestors of p in m, including p itself if it has an entry.
func (*PrefixMap[T]) Contains ¶
Contains returns true if this map includes the exact Prefix provided.
func (*PrefixMap[T]) DescendantsOf ¶
DescendantsOf returns a PrefixMap containing all descendants of p in m, including p itself if it has an entry.
func (*PrefixMap[T]) Encompasses ¶
Encompasses returns true if this map includes a Prefix which completely encompasses p. The encompassing Prefix may be p itself.
func (*PrefixMap[T]) Filter ¶
Filter returns a new PrefixMap containing the entries of m that are encompassed by s.
For example, if m contains 1.2.3.4/32 and 1.2.0.0/16, then filtering by a PrefixSet that includes 1.2.3.0/24 would retain 1.2.3.4/32 and remove 1.2.0.0/16.
func (*PrefixMap[T]) OverlapsPrefix ¶
OverlapsPrefix returns true if this map includes a Prefix which overlaps p.
func (*PrefixMap[T]) ParentOf ¶
ParentOf returns the longest-prefix ancestor of p in m, if any. If p itself has an entry, then p's entry is returned.
func (*PrefixMap[T]) RootOf ¶
RootOf returns the shortest-prefix ancestor of p in m, if any. If p itself has an entry and has no ancestors, then p's entry is returned.
type PrefixMapBuilder ¶
type PrefixMapBuilder[T any] struct { // contains filtered or unexported fields }
PrefixMapBuilder builds an immutable PrefixMap.
The zero value is a valid PrefixMapBuilder representing a builder with zero Prefixes.
Call PrefixMapBuilder.PrefixMap to obtain an immutable PrefixMap from a PrefixMapBuilder.
func (*PrefixMapBuilder[T]) Filter ¶
func (m *PrefixMapBuilder[T]) Filter(s *PrefixSet)
Filter removes all Prefixes that are not encompassed by s from m.
func (*PrefixMapBuilder[T]) PrefixMap ¶
func (m *PrefixMapBuilder[T]) PrefixMap() *PrefixMap[T]
PrefixMap returns an immutable PrefixMap representing the current state of m.
The builder remains usable after calling PrefixMap.
Values are copied to the PrefixMap using assignment, so use caution if you are storing pointers.
func (*PrefixMapBuilder[T]) Remove ¶
func (m *PrefixMapBuilder[T]) Remove(p netip.Prefix) error
Remove removes p from m. Only the exact Prefix provided is removed; descendants are not.
To remove entire sections of IP space at once, see PrefixMapBuilder.Filter.
type PrefixSet ¶
type PrefixSet struct {
// contains filtered or unexported fields
}
PrefixSet is a set of netip.Prefix values. It is implemented as a binary radix tree.
PrefixSet offers unique functionality beyond what a PrefixMap[bool] can do. In particular, during the building stage (PrefixSetBuilder) you can combine sets in useful ways using methods like PrefixSetBuilder.Merge, PrefixSetBuilder.Intersect, and PrefixSetBuilder.Subtract.
Use PrefixSetBuilder to construct PrefixSets.
func (*PrefixSet) AllCompact ¶ added in v0.1.7
AllCompact returns an iterator over the prefixes in s that are not children of any other prefix in s.
Note: AllCompact does not merge siblings, so the result may contain complete sets of sibling prefixes, e.g. ::0/128 and ::1/128.
func (*PrefixSet) AllCompact4 ¶ added in v0.1.9
AllCompact4 returns an iterator over the prefixes in s that are not children of any other prefix in s.
Note: AllCompact4 does not merge siblings, so the result may contain complete sets of sibling prefixes, e.g. 1.2.3.0/32 and 1.2.3.1/32.
func (*PrefixSet) AllCompact6 ¶ added in v0.1.9
AllCompact6 returns an iterator over the prefixes in s that are not children of any other prefix in s.
Note: AllCompact6 does not merge siblings, so the result may contain complete sets of sibling prefixes, e.g. ::0/128 and ::1/128.
func (*PrefixSet) AncestorsOf ¶ added in v0.1.5
AncestorsOf returns a PrefixSet containing all ancestors of p in s, including p itself if it has an entry.
func (*PrefixSet) DescendantsOf ¶ added in v0.1.5
DescendantsOf returns a PrefixSet containing all descendants of p in s, including p itself if it has an entry.
func (*PrefixSet) Encompasses ¶
Encompasses returns true if this set includes a Prefix which completely encompasses p. The encompassing Prefix may be p itself.
func (*PrefixSet) OverlapsPrefix ¶
OverlapsPrefix returns true if this set includes a Prefix which overlaps p.
func (*PrefixSet) ParentOf ¶ added in v0.1.5
ParentOf returns the longest-prefix ancestor of p in s, if any. If p itself has an entry, then p's entry is returned.
func (*PrefixSet) PrefixesCompact ¶ added in v0.1.1
PrefixesCompact returns a slice of the Prefixes in s that are not children of other Prefixes in s.
Note: PrefixCompact does not merge siblings, so the result may contain complete sets of sibling prefixes, e.g. 1.2.3.0/32 and 1.2.3.1/32.
type PrefixSetBuilder ¶
type PrefixSetBuilder struct {
// contains filtered or unexported fields
}
PrefixSetBuilder builds an immutable PrefixSet.
The zero value is a valid PrefixSetBuilder representing a builder with zero Prefixes.
Call PrefixSet to obtain an immutable PrefixSet from a PrefixSetBuilder.
func (*PrefixSetBuilder) Add ¶
func (s *PrefixSetBuilder) Add(p netip.Prefix) error
Add adds p to s.
func (*PrefixSetBuilder) Filter ¶
func (s *PrefixSetBuilder) Filter(o *PrefixSet)
Filter removes all Prefixes that are not encompassed by o from s.
For example, if s contains 1.2.3.4/32 and 1.2.0.0/16, then filtering by a PrefixSet that includes 1.2.3.0/24 would retain 1.2.3.4/32 and remove 1.2.0.0/16.
To remove sections of Prefixes, see PrefixSetBuilder.Subtract and PrefixSetBuilder.SubtractPrefix.
func (*PrefixSetBuilder) Intersect ¶ added in v0.1.5
func (s *PrefixSetBuilder) Intersect(o *PrefixSet)
Intersect modifies s so that it contains the intersection of the entries in s and o: to be included in the result, a Prefix must either (a) exist in both sets or (b) exist in one set and have an ancestor in the other.
func (*PrefixSetBuilder) Merge ¶ added in v0.1.5
func (s *PrefixSetBuilder) Merge(o *PrefixSet)
Merge modifies s so that it contains the union of the entries in s and o.
func (*PrefixSetBuilder) PrefixSet ¶
func (s *PrefixSetBuilder) PrefixSet() *PrefixSet
PrefixSet returns an immutable PrefixSet representing the current state of s.
The builder remains usable after calling PrefixSet.
func (*PrefixSetBuilder) Remove ¶
func (s *PrefixSetBuilder) Remove(p netip.Prefix) error
Remove removes p from s. Only the exact Prefix provided is removed; descendants are not.
To remove entire sections of IP space at once, see PrefixSetBuilder.Filter, PrefixSetBuilder.Subtract and PrefixSetBuilder.SubtractPrefix.
func (*PrefixSetBuilder) Subtract ¶
func (s *PrefixSetBuilder) Subtract(o *PrefixSet)
Subtract modifies s so that the Prefixes in o, and all of their descendants, are removed from s, leaving behind any remaining portions of affected Prefixes. This may add elements to fill in gaps around the subtracted Prefixes.
For example, if s is {::0/126}, and we subtract ::0/128, then s will become {::1/128, ::2/127}.
func (*PrefixSetBuilder) SubtractPrefix ¶ added in v0.1.5
func (s *PrefixSetBuilder) SubtractPrefix(p netip.Prefix) error
SubtractPrefix modifies s so that p and all of its descendants are removed, leaving behind any remaining portions of affected Prefixes. This may add elements to fill in gaps around the subtracted Prefix.
For example, if s is {::0/126}, and we subtract ::0/128, then s will become {::1/128, ::2/127}.