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 ¶
- Constants
- func Compare(a, b *Prefix) int
- type Cursor
- type Position
- type Prefix
- func (p *Prefix) Contains(q *Prefix) bool
- func (p *Prefix) Equal(q *Prefix) bool
- func (p *Prefix) Exclude(q *Prefix) []Prefix
- func (p *Prefix) Hostmask() net.IPMask
- func (p *Prefix) Last() net.IP
- func (p *Prefix) Len() int
- func (p *Prefix) MarshalBinary() ([]byte, error)
- func (p *Prefix) MarshalText() ([]byte, error)
- func (p *Prefix) NumNodes() *big.Int
- func (p *Prefix) Overlaps(q *Prefix) bool
- func (p Prefix) String() string
- func (p *Prefix) Subnets(n int) []Prefix
- func (p *Prefix) UnmarshalBinary(b []byte) error
- func (p *Prefix) UnmarshalText(txt []byte) error
Examples ¶
Constants ¶
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 ¶
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 Parse ¶
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")
type Position ¶
A Position represents a position on IP address space.
func (*Position) IsBroadcast ¶
IsBroadcast reports whether p is an IPv4 directed or limited broadcast address.
func (*Position) IsSubnetRouterAnycast ¶
IsSubnetRouterAnycast reports whether p is an IPv6 subnet router anycast address.
type Prefix ¶
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 Summarize ¶
Summarize summarizes the address range from first to last and returns a list of prefixes.
func Supernet ¶
Supernet finds out a shortest common prefix for ps. It returns nil when no suitable prefix is found.
func (*Prefix) Last ¶
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) MarshalBinary ¶
MarshalBinary returns a BGP NLRI binary form of p.
func (*Prefix) MarshalText ¶
MarshalText returns a UTF-8-encoded text form of p.
func (*Prefix) Subnets ¶
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 ¶
UnmarshalBinary replaces p with the BGP NLRI binary form b.
func (*Prefix) UnmarshalText ¶
UnmarshalText replaces p with txt.