Documentation ¶
Index ¶
- func Broadcast(a *net.IPNet) net.IP
- func Collapse(toMerge []*net.IPNet) []*net.IPNet
- func Exclude(a, b *net.IPNet) []*net.IPNet
- func IncrIP(ip net.IP, incr int)
- func IncrNet(ipNet *net.IPNet, incr int)
- func IsSubnet(a, b *net.IPNet) bool
- func IsSupernet(a, b *net.IPNet) bool
- func SummarizeRange(first, last net.IP) []*net.IPNet
- func Supernet(ipN *net.IPNet, newPrefix int) *net.IPNet
- type IPIter
- type NetIter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Broadcast ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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]
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 ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
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