ipaddr

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

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

Go to latest
Published: Apr 4, 2019 License: BSD-2-Clause Imports: 11 Imported by: 41

README

Package ipaddr provides basic functions for the manipulation of IP address prefixes and subsequent addresses as described in RFC 4632 and RFC 4291.

GoDoc Build Status Build status Go Report Card

Documentation

Overview

Package ipaddr provides basic functions for the manipulation of IP address prefixes and subsequent addresses as described in RFC 4632 and RFC 4291.

Index

Examples

Constants

View Source
const (
	IPv4PrefixLen = 8 * net.IPv4len // maximum number of prefix length in bits
	IPv6PrefixLen = 8 * net.IPv6len // maximum number of prefix length in bits
)

Variables

This section is empty.

Functions

func Compare

func Compare(a, b *Prefix) int

Compare returns an integer comparing two prefixes. The result will be 0 if a == b, -1 if a < b, and +1 if a > b.

Types

type Cursor

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

A Cursor represents a movable indicator on single or multiple prefixes.

Example (Traversal)
package main

import (
	"fmt"
	"log"

	"github.com/mikioh/ipaddr"
)

func main() {
	c, err := ipaddr.Parse("2001:db8::/126,192.0.2.128/30,198.51.100.0/29")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(c.Pos(), c.First(), c.Last(), c.List())
	for pos := c.Next(); pos != nil; pos = c.Next() {
		fmt.Println(pos)
	}
	fmt.Println(c.Pos(), c.First(), c.Last(), c.List())
	for pos := c.Prev(); pos != nil; pos = c.Prev() {
		fmt.Println(pos)
	}
	fmt.Println(c.Pos(), c.First(), c.Last(), c.List())
}
Output:

&{192.0.2.128 192.0.2.128/30} &{192.0.2.128 192.0.2.128/30} &{2001:db8::3 2001:db8::/126} [192.0.2.128/30 198.51.100.0/29 2001:db8::/126]
&{192.0.2.129 192.0.2.128/30}
&{192.0.2.130 192.0.2.128/30}
&{192.0.2.131 192.0.2.128/30}
&{198.51.100.0 198.51.100.0/29}
&{198.51.100.1 198.51.100.0/29}
&{198.51.100.2 198.51.100.0/29}
&{198.51.100.3 198.51.100.0/29}
&{198.51.100.4 198.51.100.0/29}
&{198.51.100.5 198.51.100.0/29}
&{198.51.100.6 198.51.100.0/29}
&{198.51.100.7 198.51.100.0/29}
&{2001:db8:: 2001:db8::/126}
&{2001:db8::1 2001:db8::/126}
&{2001:db8::2 2001:db8::/126}
&{2001:db8::3 2001:db8::/126}
&{2001:db8::3 2001:db8::/126} &{192.0.2.128 192.0.2.128/30} &{2001:db8::3 2001:db8::/126} [192.0.2.128/30 198.51.100.0/29 2001:db8::/126]
&{2001:db8::2 2001:db8::/126}
&{2001:db8::1 2001:db8::/126}
&{2001:db8:: 2001:db8::/126}
&{198.51.100.7 198.51.100.0/29}
&{198.51.100.6 198.51.100.0/29}
&{198.51.100.5 198.51.100.0/29}
&{198.51.100.4 198.51.100.0/29}
&{198.51.100.3 198.51.100.0/29}
&{198.51.100.2 198.51.100.0/29}
&{198.51.100.1 198.51.100.0/29}
&{198.51.100.0 198.51.100.0/29}
&{192.0.2.131 192.0.2.128/30}
&{192.0.2.130 192.0.2.128/30}
&{192.0.2.129 192.0.2.128/30}
&{192.0.2.128 192.0.2.128/30}
&{192.0.2.128 192.0.2.128/30} &{192.0.2.128 192.0.2.128/30} &{2001:db8::3 2001:db8::/126} [192.0.2.128/30 198.51.100.0/29 2001:db8::/126]

func NewCursor

func NewCursor(ps []Prefix) *Cursor

NewCursor returns a new cursor.

func Parse

func Parse(s string) (*Cursor, error)

Parse parses s as a single or combination of multiple IP addresses and IP address prefixes.

Examples:

Parse("192.0.2.1")
Parse("2001:db8::1/128")
Parse("203.0.113.0/24")
Parse("192.0.2.1,2001:db8::1/128,203.0.113.0/24")

func (*Cursor) First

func (c *Cursor) First() *Position

First returns the start position on c.

func (*Cursor) Last

func (c *Cursor) Last() *Position

Last returns the end position on c.

func (*Cursor) List

func (c *Cursor) List() []Prefix

List returns the list of prefixes on c.

func (*Cursor) Next

func (c *Cursor) Next() *Position

Next turns to the next position on c. It returns nil at the end on c.

func (*Cursor) Pos

func (c *Cursor) Pos() *Position

Pos returns the current position on c.

func (*Cursor) Prev

func (c *Cursor) Prev() *Position

Prev turns to the previous position on c. It returns nil at the start on c.

func (*Cursor) Reset

func (c *Cursor) Reset(ps []Prefix)

Reset resets all state and switches to ps. It uses the existing prefixes when ps is nil.

func (*Cursor) Set

func (c *Cursor) Set(pos *Position) error

Set sets the current position on c to pos.

type Position

type Position struct {
	IP     net.IP // IP address
	Prefix Prefix // IP address prefix
}

A Position represents a position on IP address space.

func (*Position) IsBroadcast

func (p *Position) IsBroadcast() bool

IsBroadcast reports whether p is an IPv4 directed or limited broadcast address.

func (*Position) IsSubnetRouterAnycast

func (p *Position) IsSubnetRouterAnycast() bool

IsSubnetRouterAnycast reports whether p is an IPv6 subnet router anycast address.

type Prefix

type Prefix struct {
	net.IPNet
}

A Prefix represents an IP address prefix.

Example (AddressRangeSummarization)
package main

import (
	"fmt"
	"net"

	"github.com/mikioh/ipaddr"
)

func main() {
	ps := ipaddr.Summarize(net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::8000"))
	for _, p := range ps {
		fmt.Println(p)
	}
}
Output:

2001:db8::1/128
2001:db8::2/127
2001:db8::4/126
2001:db8::8/125
2001:db8::10/124
2001:db8::20/123
2001:db8::40/122
2001:db8::80/121
2001:db8::100/120
2001:db8::200/119
2001:db8::400/118
2001:db8::800/117
2001:db8::1000/116
2001:db8::2000/115
2001:db8::4000/114
2001:db8::8000/128
Example (SubnettingAndAggregation)
package main

import (
	"fmt"
	"log"
	"net"

	"github.com/mikioh/ipaddr"
)

func main() {
	_, n, err := net.ParseCIDR("192.0.2.0/24")
	if err != nil {
		log.Fatal(err)
	}
	p := ipaddr.NewPrefix(n)
	fmt.Println(p.IP, p.Last(), p.Len(), p.Mask, p.Hostmask())
	fmt.Println()
	ps := p.Subnets(3)
	for _, p := range ps {
		fmt.Println(p)
	}
	fmt.Println()
	fmt.Println(ipaddr.Aggregate(ps))
	fmt.Println(ipaddr.Aggregate(ps[1:7]))
	fmt.Println(ipaddr.Aggregate(ps[:4]))
	fmt.Println(ipaddr.Aggregate(ps[4:8]))
}
Output:

192.0.2.0 192.0.2.255 24 ffffff00 000000ff

192.0.2.0/27
192.0.2.32/27
192.0.2.64/27
192.0.2.96/27
192.0.2.128/27
192.0.2.160/27
192.0.2.192/27
192.0.2.224/27

[192.0.2.0/24]
[192.0.2.32/27 192.0.2.64/26 192.0.2.128/26 192.0.2.192/27]
[192.0.2.0/25]
[192.0.2.128/25]
Example (SubnettingAndSupernetting)
package main

import (
	"fmt"
	"log"
	"net"

	"github.com/mikioh/ipaddr"
)

func main() {
	_, n, err := net.ParseCIDR("203.0.113.0/24")
	if err != nil {
		log.Fatal(err)
	}
	p := ipaddr.NewPrefix(n)
	fmt.Println(p.IP, p.Last(), p.Len(), p.Mask, p.Hostmask())
	fmt.Println()
	ps := p.Subnets(3)
	for _, p := range ps {
		fmt.Println(p)
	}
	fmt.Println()
	fmt.Println(ipaddr.Supernet(ps))
	fmt.Println(ipaddr.Supernet(ps[1:7]))
	fmt.Println(ipaddr.Supernet(ps[:4]))
	fmt.Println(ipaddr.Supernet(ps[4:8]))
}
Output:

203.0.113.0 203.0.113.255 24 ffffff00 000000ff

203.0.113.0/27
203.0.113.32/27
203.0.113.64/27
203.0.113.96/27
203.0.113.128/27
203.0.113.160/27
203.0.113.192/27
203.0.113.224/27

203.0.113.0/24
203.0.113.0/24
203.0.113.0/25
203.0.113.128/25

func Aggregate

func Aggregate(ps []Prefix) []Prefix

Aggregate aggregates ps and returns a list of aggregated prefixes.

func NewPrefix

func NewPrefix(n *net.IPNet) *Prefix

NewPrefix returns a new prefix.

func Summarize

func Summarize(first, last net.IP) []Prefix

Summarize summarizes the address range from first to last and returns a list of prefixes.

func Supernet

func Supernet(ps []Prefix) *Prefix

Supernet finds out a shortest common prefix for ps. It returns nil when no suitable prefix is found.

func (*Prefix) Contains

func (p *Prefix) Contains(q *Prefix) bool

Contains reports whether q is a subnetwork of p.

func (*Prefix) Equal

func (p *Prefix) Equal(q *Prefix) bool

Equal reports whether p and q are equal.

func (*Prefix) Exclude

func (p *Prefix) Exclude(q *Prefix) []Prefix

Exclude returns a list of prefixes that do not contain q.

func (*Prefix) Hostmask

func (p *Prefix) Hostmask() net.IPMask

Hostmask returns a host mask, the inverse mask of p's network mask.

func (*Prefix) Last

func (p *Prefix) Last() net.IP

Last returns the last IP in the address range of p. It returns the address of p when p contains only one address.

func (*Prefix) Len

func (p *Prefix) Len() int

Len returns the length of p in bits.

func (*Prefix) MarshalBinary

func (p *Prefix) MarshalBinary() ([]byte, error)

MarshalBinary returns a BGP NLRI binary form of p.

func (*Prefix) MarshalText

func (p *Prefix) MarshalText() ([]byte, error)

MarshalText returns a UTF-8-encoded text form of p.

func (*Prefix) NumNodes

func (p *Prefix) NumNodes() *big.Int

NumNodes returns the number of IP node addresses in p.

func (*Prefix) Overlaps

func (p *Prefix) Overlaps(q *Prefix) bool

Overlaps reports whether p overlaps with q.

func (Prefix) String

func (p Prefix) String() string

func (*Prefix) Subnets

func (p *Prefix) Subnets(n int) []Prefix

Subnets returns a list of prefixes that are split from p, into small address blocks by n which represents a number of subnetworks in the power of 2 notation.

func (*Prefix) UnmarshalBinary

func (p *Prefix) UnmarshalBinary(b []byte) error

UnmarshalBinary replaces p with the BGP NLRI binary form b.

func (*Prefix) UnmarshalText

func (p *Prefix) UnmarshalText(txt []byte) error

UnmarshalText replaces p with txt.

Jump to

Keyboard shortcuts

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