ipx

package module
v0.0.0-...-f7e48f1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: MIT Imports: 4 Imported by: 0

README

ipx

Tests Go Report Card GoDoc

Extending IP address support for Go.

func ExampleCollapse() {
	fmt.Println(ipx.Collapse(
		[]*net.IPNet{
			cidr("192.0.2.0/26"),
			cidr("192.0.2.64/26"),
			cidr("192.0.2.128/26"),
			cidr("192.0.2.192/26"),
		},
	))
	// Output:
	// [192.0.2.0/24]
}

func ExampleSplit() {
	c := cidr("10.0.0.0/24")
	splitter := ipx.Split(c, 26)
	for splitter.Next() {
		fmt.Println(splitter.Net())
	}
	// Output:
	// 10.0.0.0/26
	// 10.0.0.64/26
	// 10.0.0.128/26
	// 10.0.0.192/26
}

func ExampleSummarizeRange() {
	fmt.Println(ipx.SummarizeRange(net.ParseIP("192.0.2.0"), net.ParseIP("192.0.2.130")))
	// Output:
	// [192.0.2.0/25 192.0.2.128/31 192.0.2.130/32]
}

func ExampleExclude() {
	fmt.Println(
		ipx.Exclude(
			cidr("10.1.1.0/24"),
			cidr("10.1.1.0/26"),
		),
	)
	// Output:
	// [10.1.1.128/25 10.1.1.64/26]
}

See example tests for more usage.

design thoughts

  • Coordinate on stdlib types
  • Avoid allocations whenever possible.
  • Look to python ipaddress lib for feature list

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Broadcast

func Broadcast(a *net.IPNet) net.IP

Broadcast returns the broadcast address for the provided net.

Example
ipN := cidr("10.0.1.0/24")
fmt.Println(ipx.Broadcast(ipN))
Output:

10.0.1.255

func Collapse

func Collapse(toMerge []*net.IPNet) []*net.IPNet

Collapse combines subnets into their closest available parent.

Example
fmt.Println(ipx.Collapse(
	[]*net.IPNet{
		cidr("192.0.2.0/26"),
		cidr("192.0.2.64/26"),
		cidr("192.0.2.128/26"),
		cidr("192.0.2.192/26"),
	},
))
Output:

[192.0.2.0/24]

func Exclude

func Exclude(a, b *net.IPNet) []*net.IPNet

Exclude returns a list of networks representing the address block when `b` is removed from `a`.

Example
fmt.Println(
	ipx.Exclude(
		cidr("10.1.1.0/24"),
		cidr("10.1.1.0/26"),
	),
)
Output:

[10.1.1.128/25 10.1.1.64/26]

func IncrIP

func IncrIP(ip net.IP, incr int)

IncrIP returns the next IP

Example
package main

import (
	"fmt"
	"github.com/jwilner/ipx"
	"net"
)

func main() {
	ip := net.ParseIP("0.0.0.0")
	ipx.IncrIP(ip, 257)
	fmt.Println(ip)
}
Output:

0.0.1.1

func IncrNet

func IncrNet(ipNet *net.IPNet, incr int)

IncrNet steps to the next net of the same mask

Example
package main

import (
	"fmt"
	"github.com/jwilner/ipx"
	"net"
)

func main() {
	ipN := cidr("10.0.0.0/16")
	ipx.IncrNet(ipN, 2)
	fmt.Println(ipN)
}

func cidr(cidrS string) *net.IPNet {
	_, ipNet, _ := net.ParseCIDR(cidrS)
	return ipNet
}
Output:

10.2.0.0/16

func IsSubnet

func IsSubnet(a, b *net.IPNet) bool

IsSubnet returns whether b is a subnet of a

Example
a, b := cidr("10.0.0.0/16"), cidr("10.0.1.0/24")
fmt.Println(ipx.IsSubnet(a, b))
fmt.Println(ipx.IsSubnet(a, a))
fmt.Println(ipx.IsSubnet(b, a))
Output:

true
true
false

func IsSupernet

func IsSupernet(a, b *net.IPNet) bool

IsSupernet returns whether b is a supernet of a

Example
a, b := cidr("10.0.1.0/24"), cidr("10.0.0.0/16")
fmt.Println(ipx.IsSupernet(a, b))
fmt.Println(ipx.IsSupernet(a, a))
fmt.Println(ipx.IsSupernet(b, a))
Output:

true
true
false

func SummarizeRange

func SummarizeRange(first, last net.IP) []*net.IPNet

SummarizeRange returns a series of networks which combined cover the range between the first and last addresses, inclusive.

Example
package main

import (
	"fmt"
	"github.com/jwilner/ipx"
	"net"
)

func main() {
	fmt.Println(ipx.SummarizeRange(net.ParseIP("192.0.2.0"), net.ParseIP("192.0.2.130")))
}
Output:

[192.0.2.0/25 192.0.2.128/31 192.0.2.130/32]

func Supernet

func Supernet(ipN *net.IPNet, newPrefix int) *net.IPNet

Supernet returns a supernet for the provided network with the specified prefix length

Example
ipN := cidr("192.0.2.0/24")
super := ipx.Supernet(ipN, 20)
fmt.Println(super)
Output:

192.0.0.0/20

Types

type IPIter

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

IPIter permits iteration over a series of ips. It is always start inclusive.

func Addresses

func Addresses(ipNet *net.IPNet) *IPIter

Addresses returns all of the addresses within a network.

Example
c := cidr("10.0.0.0/30")
addrs := ipx.Addresses(c)
for addrs.Next() {
	fmt.Println(addrs.IP())
}
Output:

10.0.0.0
10.0.0.1
10.0.0.2
10.0.0.3

func Hosts

func Hosts(ipNet *net.IPNet) *IPIter

Hosts returns all of the usable addresses within a network except the network itself address and the broadcast address

Example
c := cidr("10.0.0.0/29")
hosts := ipx.Hosts(c)
for hosts.Next() {
	fmt.Println(hosts.IP())
}
Output:

10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5
10.0.0.6

func IterIP

func IterIP(start net.IP, step int, end net.IP) *IPIter

IterIP returns an iter for the given step from [start, end). If end is nil, it is set to the maximum type for the version. If the step is zero, IP versions mismatch or the sign of the increment doesn't match that of end - start, an empty iter is returned.

Example
package main

import (
	"fmt"
	"github.com/jwilner/ipx"
	"net"
)

func main() {
	ip := net.ParseIP("10.0.0.0")
	for i, iter := 0, ipx.IterIP(ip, 100, nil); i < 5 && iter.Next(); i++ {
		ip = iter.IP()
		fmt.Println(ip)
	}
	for i, iter := 0, ipx.IterIP(ip, -100, nil); i < 5 && iter.Next(); i++ {
		fmt.Println(iter.IP())
	}
}
Output:

10.0.0.0
10.0.0.100
10.0.0.200
10.0.1.44
10.0.1.144
10.0.1.144
10.0.1.44
10.0.0.200
10.0.0.100
10.0.0.0

func (*IPIter) IP

func (i *IPIter) IP() net.IP

IP returns the most recent IP; the underlying type may be modified on later calls to `Next`. It does no allocation.

func (*IPIter) Next

func (i *IPIter) Next() bool

Next returns true when the underlying pointer has been successfully updated with the next value.

type NetIter

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

NetIter permits iteration over a series of IP networks. It is always start inclusive.

func IterNet

func IterNet(ipNet *net.IPNet, incr int) *NetIter

IterNet returns an iterator for the given increment starting with the provided network

Example
ipN := cidr("10.0.0.0/16")
for i, iter := 0, ipx.IterNet(ipN, 100); i < 5 && iter.Next(); i++ {
	ipN = iter.Net()
	fmt.Println(ipN)
}
for i, iter := 0, ipx.IterNet(ipN, -100); i < 5 && iter.Next(); i++ {
	fmt.Println(iter.Net())
}
Output:

10.0.0.0/16
10.100.0.0/16
10.200.0.0/16
11.44.0.0/16
11.144.0.0/16
11.144.0.0/16
11.44.0.0/16
10.200.0.0/16
10.100.0.0/16
10.0.0.0/16

func Split

func Split(ipNet *net.IPNet, newPrefix int) *NetIter

Split splits a subnet into smaller subnets according to the new prefix provided.

Example
c := cidr("10.0.0.0/24")
split := ipx.Split(c, 26)
for split.Next() {
	fmt.Println(split.Net())
}
Output:

10.0.0.0/26
10.0.0.64/26
10.0.0.128/26
10.0.0.192/26

func (*NetIter) Net

func (n *NetIter) Net() *net.IPNet

Net returns the most recent IPNet; the underlying type may be modified on later calls to `Next`. It does no allocation.

func (*NetIter) Next

func (n *NetIter) Next() bool

Next returns true when the underlying pointer has been successfully updated with the next value.

Jump to

Keyboard shortcuts

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