Skip to content

fast IP to CIDR lookup, longest prefix match in golang

License

Notifications You must be signed in to change notification settings

gaissmai/cidrtree

Repository files navigation

package cidrtree

Go Reference GitHub release (latest SemVer) CI Coverage Status Stand With Ukraine License: MIT

!!! ATTENTION

API currently not stable!

Overview

package cidrtree is a datastructure for IP routing tables (IPv4/IPv6) with fast lookup (longest prefix match).

The implementation is based on treaps, which have been augmented here for CIDRs. Treaps are randomized, self-balancing binary search trees. Due to the nature of treaps the lookups (readers) and the update (writer) can be easily decoupled. This is the perfect fit for a software router or firewall.

This package is a specialization of the more generic interval package of the same author, but explicit for CIDRs. It has a narrow focus with a specialized API for IP routing tables.

API

  import "github.com/gaissmai/cidrtree"

  type Table struct { // Has unexported fields.  }
    Table is an IPv4 and IPv6 routing table. The zero value is ready to use.

  func (t Table) Lookup(ip netip.Addr) (lpm netip.Prefix, value any, ok bool)
  func (t Table) LookupPrefix(pfx netip.Prefix) (lpm netip.Prefix, value any, ok bool)

  func (t *Table) Insert(pfx netip.Prefix, val any)
  func (t *Table) Delete(pfx netip.Prefix) bool
  func (t *Table) Union(other Table)

  func (t Table) InsertImmutable(pfx netip.Prefix, val any) *Table
  func (t Table) DeleteImmutable(pfx netip.Prefix) (*Table, bool)
  func (t Table) UnionImmutable(other Table) *Table
  func (t Table) Clone() *Table

  func (t Table) String() string
  func (t Table) Fprint(w io.Writer) error

  func (t Table) Walk(cb func(pfx netip.Prefix, val any) bool)