iprange

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: MIT Imports: 6 Imported by: 1

README

iprange

GoDoc

The iprange package parses IPv4/IPv6 addresses from strings in IP range format and handle interval mathematics between multiple IP ranges.

IP range formats

  • 172.18.0.1 / fd00::1
  • 172.18.0.0/24 / fd00::/64
  • 172.18.0.1-10 / fd00::1-a
  • 172.18.0.1-172.18.1.10 / fd00::1-fd00::1:a

Get started

package main

import (
	"fmt"

	"github.com/iiiceoo/iprange"
)

func main() {
	// Parse IP ranges.
	ranges, err := iprange.Parse(
		"172.18.0.1",
		"172.18.0.0/24",
		"172.18.0.1-10",
		"172.18.0.1-172.18.1.10",
	)
	if err != nil {
		fmt.Printf("failed to parse IP ranges: %v\n", err)
		return
	}
	fmt.Printf("IP ranges: %s\n", ranges)

	// Merge IP ranges.
	merged := ranges.Merge()
	fmt.Printf("Merged IP ranges: %s\n", merged)

	// Interval mathematics of IP ranges.
	another, _ := iprange.Parse("172.18.0.0/24")
	diffSet := merged.Diff(another)
	fmt.Printf("Difference set between two IP ranges: %s\n", diffSet)

	// Iterate through IP ranges.
	fmt.Println("Scan the difference set:")
	iter := diffSet.Iterator()
	for {
		ip := iter.Next()
		if ip == nil {
			break
		}
		fmt.Println(ip)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDualStackIPRanges

func IsDualStackIPRanges(err error) bool

func IsInvalidIPRangeFormat

func IsInvalidIPRangeFormat(err error) bool

Types

type IPRanges

type IPRanges []ipRange

func Parse

func Parse(rs ...string) (IPRanges, error)

func (IPRanges) Contains

func (rr IPRanges) Contains(ip net.IP) bool

func (IPRanges) Diff

func (rr IPRanges) Diff(rs IPRanges) IPRanges

func (IPRanges) Equal

func (rr1 IPRanges) Equal(rr2 IPRanges) bool

func (IPRanges) Intersect

func (rr IPRanges) Intersect(rs IPRanges) IPRanges

func (IPRanges) Iterator

func (rr IPRanges) Iterator() *rangesIterator

func (IPRanges) Merge

func (rr IPRanges) Merge() IPRanges

func (IPRanges) MergeEqual

func (rr1 IPRanges) MergeEqual(rr2 IPRanges) bool

func (IPRanges) Size added in v0.0.2

func (rr IPRanges) Size() *big.Int

func (IPRanges) Union

func (rr IPRanges) Union(rs IPRanges) IPRanges

Jump to

Keyboard shortcuts

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