ipbinaryset

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 8 Imported by: 0

README

ipbinaryset

IPBinarySet is a fast and memory-efficient data structure for storing and querying IP address ranges. It maintains a sorted, merged list of IPv4 and IPv6 CIDR prefixes, making it ideal for IP blocklists, allowlists, and other IP filtering applications.

The set uses binary search for O(log n) lookup performance and automatically merges overlapping or contained ranges to minimize memory usage and maintain optimal query speed. IPv4 and IPv6 addresses are stored separately in their respective (mostly optimized) formats.

Installation

go get git.vixen.computer/lua/ipbinaryset

Functions

  • New() - Create a new empty set
  • AddAddr(addr) - Add a single IP address
  • AddPrefix(prefix) - Add a CIDR prefix
  • ContainsAddr(addr) - Check if an IP is in the set
  • ContainsPrefix(prefix) - Check if a prefix is fully contained
  • Remove(prefix) - Remove a prefix from the set
  • All() - Iterate over all prefixes
  • Len() - Get the number of prefixes
  • Copy() - Get a slice of all prefixes
  • String() - Get string representation of all prefixes
  • UnmarshalText(data) - Parse a newline-delimited list of prefixes
  • MarshalJSON() - Serialize the set as a JSON string array
  • UnmarshalJSON(data) - Deserialize the set from a JSON string array

Example

package main

import (
    "fmt"
    "net/netip"

    "git.vixen.computer/lua/ipbinaryset"
)

func main() {
    set := ipbinaryset.New()

    // Add IP ranges
    set.AddPrefix(netip.MustParsePrefix("192.168.0.0/16"))
    set.AddPrefix(netip.MustParsePrefix("10.0.0.0/8"))
    set.AddPrefix(netip.MustParsePrefix("2001:db8::/32"))

    // Add individual addresses
    set.AddAddr(netip.MustParseAddr("203.0.113.42"))

    // Check if an IP is in the set
    if set.ContainsAddr(netip.MustParseAddr("192.168.1.100")) {
        fmt.Println("IP is in blocklist")
    }

    // Iterate over all ranges
    for prefix := range set.All() {
        fmt.Println(prefix)
    }

    fmt.Printf("Total ranges: %d\n", set.Len())
}

License

Licensed under the MIT License.

Made with ❤ by Lua (foxgirl.dev).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IPBinarySet

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

IPBinarySet is a fast and memory-efficient data structure for storing and querying IP address ranges. It maintains a sorted, merged list of IPv4 and IPv6 CIDR prefixes, making it ideal for IP blocklists, allowlists, and other IP filtering applications.

The set uses binary search for O(log n) lookup performance and automatically merges overlapping or contained ranges to minimize memory usage and maintain optimal query speed. IPv4 and IPv6 addresses are stored separately in their respective optimized formats.

func New

func New() *IPBinarySet

New creates a new empty IPBinarySet.

func (*IPBinarySet) AddAddr

func (s *IPBinarySet) AddAddr(addr netip.Addr)

AddAddr adds a single IP address to the set.

func (*IPBinarySet) AddPrefix

func (s *IPBinarySet) AddPrefix(prefix netip.Prefix)

AddPrefix adds a CIDR prefix to the set. Ranges that are contained by the new prefix are automatically removed. If the prefix is already contained by an existing range, it is not added.

func (*IPBinarySet) All

func (s *IPBinarySet) All() iter.Seq[netip.Prefix]

All returns an iterator over all prefixes in the set.

func (*IPBinarySet) ContainsAddr

func (s *IPBinarySet) ContainsAddr(addr netip.Addr) bool

ContainsAddr reports whether the set contains the given IP address.

func (*IPBinarySet) ContainsPrefix

func (s *IPBinarySet) ContainsPrefix(prefix netip.Prefix) bool

ContainsPrefix reports whether the set fully contains the given prefix.

func (*IPBinarySet) Copy

func (s *IPBinarySet) Copy() []netip.Prefix

Copy returns a copy of all prefixes in the set as a slice.

func (*IPBinarySet) Len

func (s *IPBinarySet) Len() int

Len returns the number of prefixes in the set.

func (*IPBinarySet) MarshalJSON

func (s *IPBinarySet) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*IPBinarySet) Remove

func (s *IPBinarySet) Remove(prefix netip.Prefix) bool

Remove removes a prefix from the set if it exists. Returns true if the prefix was found and removed.

func (*IPBinarySet) String

func (s *IPBinarySet) String() string

String returns a string representation of all prefixes in the set. Output is newline-delimited with a trailing newline.

func (*IPBinarySet) UnmarshalJSON

func (s *IPBinarySet) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*IPBinarySet) UnmarshalText

func (s *IPBinarySet) UnmarshalText(text []byte) error

UnmarshalText implements text.Unmarshaler.

It parses newline-delimited text as a list of IP prefixes and adds them to the set. It trims spaces, supports comments via `# comment`, ignores empty lines, and allows both single IP addresses and CIDR notation.

type ParseError

type ParseError struct {
	Line    int
	Content string
}

ParseError represents an error that occurred while parsing an IP address or CIDR prefix.

func (*ParseError) Error

func (e *ParseError) Error() string

Error returns a string representation of the error.

Jump to

Keyboard shortcuts

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