ipv4

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2020 License: MIT Imports: 6 Imported by: 3

README

ipv4

build

Package for conveniently working with IPv4 and CIDR ranges.

Examples

Get start and end IPs in a CIDR range:
left, right, err := ipv4.CIDR2Range("199.27.72.0/21")
if err != nil {
  log.Fatal(err)
}
fmt.Println(left, right)

Output:

199.27.72.0 199.27.79.255
Check if IP is IPv4 (works for CIDR too):
fmt.Println(ipv4.IsIPv4("10.0.0.0"))
fmt.Println(ipv4.IsIPv4("10.0.0.0/8"))

Output:

true
true
Check if IP is private:
fmt.Println(ipv4.IsPrivate("10.0.0.0"))

Output:

true

See GoDoc for more.

Documentation

Overview

Package ipv4 provides functionality for conveniently working with IPv4 and CIDR ranges.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBadIP = errors.New("Bad IP address")

ErrBadIP is a generic error that an IP address could not be parsed

Functions

func CIDR2Range

func CIDR2Range(c string) (string, string, error)

CIDR2Range converts a CIDR to a dotted IP address pair, or empty strings and error. Generic: does not care if IPv4 or IPv6.

Example
left, right, err := CIDR2Range("199.27.72.0/21")
if err != nil {
	log.Fatal(err)
}
fmt.Println(left, right)
Output:

199.27.72.0 199.27.79.255

func FromDots

func FromDots(ipstr string) (uint32, error)

FromDots converts an IPv4 dotted string into a uint32 (big endian).

func FromNetIP

func FromNetIP(ip net.IP) (uint32, error)

FromNetIP converts a IPv4 net.IP to uint32, error

func Interval2CIDRs added in v1.2.0

func Interval2CIDRs(a1, a2 uint32, out func(left uint32, mask byte))

Interval2CIDRs is the binary version of Range2CIDRs

A function is passed in to emit the networks.

func IsIPv4

func IsIPv4(s string) bool

IsIPv4 returns true if the input is either a dotted IPv4 address or if it's IPv4 dotted/cidr notation

Example
fmt.Println(IsIPv4("10.0.0.0"))
fmt.Println(IsIPv4("10.0.0.0/8"))
fmt.Println(IsIPv4("2001:4860:0:2001::68"))
fmt.Println(IsIPv4("2001:DB8::/48"))
Output:

true
true
false
false

func IsPrivate

func IsPrivate(ipdots string) bool

IsPrivate determines if an IP address is Not Public.

Note in this case Private means "localhost, loopback, link local and private subnets".

func Range2CIDRs

func Range2CIDRs(dots1, dots2 string) (r []string)

Range2CIDRs take a pair of IPv4 addresses in dotted form, and return a list of IPv4 CIDR ranges. (or nil if invalid input)

Example
fmt.Println(Range2CIDRs("127.0.0.0", "127.0.0.255"))
Output:

[127.0.0.0/24]

func SortUniqueUint32 added in v1.2.0

func SortUniqueUint32(in []uint32)

SortUniqueUint32 sorts, and dedups a slice of uint32 (maybe representing binary representation of IPv4 address

sorting and uniqueness is done in place

func ToDots

func ToDots(p4 uint32) string

ToDots converts a uint32 to a IPv4 Dotted notation

About 10x faster than doing something with fmt.Sprintf one allocation per call.

Based on golang's net/IP.String() https://golang.org/src/net/ip.go?s=7645:7673#L281

func ToNetIP

func ToNetIP(val uint32) net.IP

ToNetIP converts a uint32 to a net.IP (net.IPv4 actually)

Types

type Interval

type Interval struct {
	Left  uint32
	Right uint32
	Value interface{}
}

Interval is a closed interval [a,b] (inclusive), with a value

func (Interval) String

func (i Interval) String() string

type IntervalList

type IntervalList []Interval

IntervalList is listing of Interval types, most for use in sorting

func (IntervalList) Len

func (ipset IntervalList) Len() int

func (IntervalList) Less

func (ipset IntervalList) Less(i, j int) bool

func (IntervalList) String

func (ipset IntervalList) String() string

func (IntervalList) Swap

func (ipset IntervalList) Swap(i, j int)

type IntervalMap

type IntervalMap struct {
	Intervals IntervalList
}

IntervalMap is set of disjoint intervals

func NewIntervalMap

func NewIntervalMap(capacity int) *IntervalMap

NewIntervalMap creates a new set

func (*IntervalMap) Add

func (ipset *IntervalMap) Add(dots string, value interface{}) error

Add inserts a single IP or a range based with CIDR notation

func (*IntervalMap) AddRange

func (ipset *IntervalMap) AddRange(dotsleft, dotsright string, value interface{}) error

AddRange adds a range of IP addresses

func (IntervalMap) Contains

func (ipset IntervalMap) Contains(dots string) interface{}

Contains returns true if the ip is in the set, error if set or input isn't valid

func (IntervalMap) Go

func (ipset IntervalMap) Go() string

Go emits a source code representation of the data

func (IntervalMap) Len

func (ipset IntervalMap) Len() int

Len returns the number of intervals in the set

func (IntervalMap) String

func (ipset IntervalMap) String() string

func (IntervalMap) Valid

func (ipset IntervalMap) Valid() error

Valid return error if internally invalid or nil if correct

type Set

type Set []uint32

Set defines a unique set of IPv4 addresses using a simple []uint32

Since Set is a alias of []unit32, one can use `make(Set, length, capacity)` or use the NewSet constructor

func NewSet added in v1.4.0

func NewSet(capacity int) Set

NewSet creates a Set with a given initial capacity.

func (*Set) Add

func (m *Set) Add(ipv4dots string) bool

Add an element to the set.

func (*Set) AddAll added in v1.3.0

func (m *Set) AddAll(ipv4dots []string) bool

AddAll adds many IPv4 at once

func (Set) Contains

func (m Set) Contains(ipv4dots string) bool

Contains matches a dotted IPv4 address with an internal list

func (Set) Len

func (m Set) Len() int

Len returns a length, part of the Sort.Interface

func (Set) Less

func (m Set) Less(i, j int) bool

Less is part of the Sort.Interface

func (Set) Swap

func (m Set) Swap(i, j int)

Swap is part of the Sort.Interface

func (Set) ToDots added in v1.3.0

func (m Set) ToDots() []string

ToDots returns the IP set as a list of dotted-notation strings

func (Set) Valid

func (m Set) Valid() bool

Valid return true if the internal storage is in sorted form and unique

Jump to

Keyboard shortcuts

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