iprange

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2023 License: GPL-3.0 Imports: 7 Imported by: 0

README

iprange

This package helps you to work with IP ranges,Both IPv4 and IPv6 are supported.

IP range doesn't contain network and broadcast IP addresses if the format is IPv4 CIDR, IPv4 subnet mask or IPv6 CIDR.

Installation

Install iprange with go mod

  go get github.com/NSObjects/iprange  

Supported formats

  • IPv4 address (192.0.2.1)
  • IPv4 range (192.0.2.0-192.0.2.10)
  • IPv4 CIDR (192.0.2.0/24)
  • IPv4 subnet mask (192.0.2.0/255.255.255.0)
  • IPv6 address (2001:db8::1)
  • IPv6 range (2001:db8::-2001:db8::10)
  • IPv6 CIDR (2001:db8::/64)

Usage/Examples

Range
parseRange, err := iprange.ParseRange("192.0.2.1-192.0.2.10")
if err != nil {
	panic(err)
}
fmt.Println(parseRange.Ips())
}
CIDR
parseRange, err := iprange.ParseRange("192.0.2.1/24")
if err != nil {
	panic(err)
}
fmt.Println(parseRange.Ips())
}
subnet mask
parseRange, err := iprange.ParseRange("192.0.2.1/255.255.255.255")
if err != nil {
	panic(err)
}
fmt.Println(parseRange.Ips())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Family

type Family uint8

Family represents IP Range address-family.

const (
	// V4Family is IPv4 address-family.
	V4Family Family = iota
	// V6Family is IPv6 address-family.
	V6Family
)

type Range

type Range interface {
	Family() Family
	Contains(ip netip.Addr) bool
	Size() *big.Int
	fmt.Stringer
	Ips() []string
}

Range represents an IP range.

func New

func New(start, end netip.Addr) Range

New returns new IP Range. If it is not a valid range (start and end IPs have different address-families, or start > end), New returns nil.

func ParseRange

func ParseRange(s string) (Range, error)

ParseRange parses s as an IP Range, returning the result and an error if any. The string s can be in IPv4 address ("192.0.2.1"), IPv4 range ("192.0.2.0-192.0.2.10") IPv4 CIDR ("192.0.2.0/24"), IPv4 subnet mask ("192.0.2.0/255.255.255.0"), IPv6 address ("2001:db8::1"), IPv6 range ("2001:db8::-2001:db8::10"), or IPv6 CIDR ("2001:db8::/64") form. IPv4 CIDR, IPv4 subnet mask and IPv6 CIDR ranges don't include network and broadcast addresses.

func ParseRanges

func ParseRanges(s string) ([]Range, error)

ParseRanges parses s as a space separated list of IP Ranges, returning the result and an error if any. IP Range can be in IPv4 address ("192.0.2.1"), IPv4 range ("192.0.2.0-192.0.2.10") IPv4 CIDR ("192.0.2.0/24"), IPv4 subnet mask ("192.0.2.0/255.255.255.0"), IPv6 address ("2001:db8::1"), IPv6 range ("2001:db8::-2001:db8::10"), or IPv6 CIDR ("2001:db8::/64") form. IPv4 CIDR, IPv4 subnet mask and IPv6 CIDR ranges don't include network and broadcast addresses.

Jump to

Keyboard shortcuts

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