ipset

package module
v0.0.0-...-beef12d Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2023 License: MIT Imports: 6 Imported by: 0

README

GC-friendly radix tree-based IP set

This is a Go implementation of an IP set, i.e. a data structure that holds a set of IP addresses and allows efficient checks whether an IP address is contained within it.

Features

  • GC-friendly. The internal representation is a slice of uint32.
  • Zero-effort serialization/deserialization (on Little-Endian architectures).
  • Support for both IPv4 and IPv6.
  • Zero value is ready to use.

Basic Example

package main

import "net/netip"

var s IPSet
s.Add(netip.MustParsePrefix("127.0.0.0/8"))

if s.Contains(netip.MustParseAddr("127.0.0.1")) {
	// ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidFormat = errors.New("invalid format")

Functions

This section is empty.

Types

type IPSet

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

func (*IPSet) Add

func (s *IPSet) Add(prefix netip.Prefix)

func (*IPSet) Compact

func (s *IPSet) Compact()

func (*IPSet) Contains

func (s *IPSet) Contains(addr netip.Addr) bool

func (*IPSet) Deserialize

func (s *IPSet) Deserialize(r io.Reader) error

func (*IPSet) Iterate

func (s *IPSet) Iterate(step IterStepFunc) bool

Iterate calls the step function for each prefix within the set. If the step function returns false, the iteration stops and the function returns false, otherwise it returns true after all nodes are traversed. The step function must not modify the set.

func (*IPSet) Serialize

func (s *IPSet) Serialize(w io.Writer) error

func (*IPSet) WriteTextTo

func (s *IPSet) WriteTextTo(w io.Writer) (n int64, err error)

WriteTextTo writes a textual representation of the IP set to the provided Writer. The text will contain one prefix per line, separated by '\n'. The order is not guaranteed to match the order in which the prefixes were added. Some contiguous prefixes may be merged. There will be one w.Write() call per prefix, so it is advisable to provide a buffered Writer.

type IPSet4

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

func (*IPSet4) Add

func (s *IPSet4) Add(prefix, length uint32)

func (*IPSet4) Compact

func (s *IPSet4) Compact()

func (*IPSet4) Contains

func (s *IPSet4) Contains(ip uint32) bool

func (*IPSet4) Deserialize

func (s *IPSet4) Deserialize(r io.Reader) error

func (*IPSet4) Iterate

func (s *IPSet4) Iterate(step IterStepFunc) bool

Iterate traverses the tree and calls the step function for each prefix within the set. See IPSet.Iterate for more details.

func (*IPSet4) Serialize

func (s *IPSet4) Serialize(w io.Writer) error

func (*IPSet4) WriteTextTo

func (s *IPSet4) WriteTextTo(w io.Writer) (n int64, err error)

WriteTextTo writes a textual representation of the IP set to the provided Writer. See IPSet.WriteTextTo for more details.

type IPSet6

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

func (*IPSet6) Add

func (s *IPSet6) Add(prefix [16]byte, length uint32)

func (*IPSet6) Compact

func (s *IPSet6) Compact()

func (*IPSet6) Contains

func (s *IPSet6) Contains(addr [16]byte) bool

func (*IPSet6) Deserialize

func (s *IPSet6) Deserialize(r io.Reader) error

func (*IPSet6) Iterate

func (s *IPSet6) Iterate(step IterStepFunc) bool

Iterate traverses the tree and calls the step function for each prefix within the set. See IPSet.Iterate for more details.

func (*IPSet6) Serialize

func (s *IPSet6) Serialize(w io.Writer) error

func (*IPSet6) WriteTextTo

func (s *IPSet6) WriteTextTo(w io.Writer) (n int64, err error)

WriteTextTo writes a textual representation of the IP set to the provided Writer. See IPSet.WriteTextTo for more details.

type IterStepFunc

type IterStepFunc func(prefix netip.Prefix) (continueIteration bool)

Jump to

Keyboard shortcuts

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