/iprange

package iprange is an extension to net/netip. Not all IP address ranges in the wild are CIDRs, very often you have to deal with ranges not representable as a prefix. This library handels IP ranges and CIDRs transparently.

Primary LanguageGoMIT LicenseMIT

package iprange

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

package iprange is an extension to net/netip

An additional type IPRange is defined and the most useful methods for it. Not all IP address ranges in the wild are CIDRs, very often you have to deal with ranges not representable as a prefix. This library handels IP ranges and CIDRs transparently.

API

import "github.com/gaissmai/iprange"

type IPRange struct{ ... }

  func FromString(s string) (IPRange, error)
  func FromAddrs(first, last netip.Addr) (IPRange, error)
  func FromPrefix(p netip.Prefix) (IPRange, error)

  func (r IPRange) Addrs() (first, last netip.Addr)
  func (r IPRange) String() string
  func (r IPRange) IsValid() bool

  func Merge(in []IPRange) (out []IPRange)
  func (r IPRange) Remove(in []IPRange) (out []IPRange)

  func (r IPRange) Prefix() (prefix netip.Prefix, ok bool)
  func (r IPRange) Prefixes() []netip.Prefix
  func (r IPRange) PrefixesAppend(dst []netip.Prefix) []netip.Prefix

  func (r IPRange) MarshalBinary() ([]byte, error)
  func (r IPRange) MarshalText() ([]byte, error)

  func (r *IPRange) UnmarshalBinary(data []byte) error
  func (r *IPRange) UnmarshalText(text []byte) error

  func Compare(a, b IPRange) (ll, rr, lr, rl int)

Advanced features

For fast lookups use the Compare function together with the interval package from the same author.