Documentation ¶
Overview ¶
Package iprange provides functions to work with different IP ranges. Supports IPv4 and IPv6.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Iterator ¶
type Iterator interface { // Next returns true if there is at least one IP-address left in the iterator // and saves this address into the given pointer. If no addresses left returns false. Next(*net.IP) bool // Reset resets the iterator so it can be used again. Reset() // Count returns total number of IP-addresses in iterator. Count() *big.Int // Contains checks if the given IP is in one of the ranges of the iterator. Contains(net.IP) bool }
Iterator is an interface to iterate IP addresses from one of multiple ranges.
Example ¶
package main import ( "fmt" "github.com/russtone/iprange" "net" ) func main() { r := iprange.Parse("192.168.1.0/29") if r == nil { return } it := r.Iterator() var ip net.IP for it.Next(&ip) { fmt.Println(ip.String()) } }
Output: 192.168.1.0 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5 192.168.1.6 192.168.1.7
type Range ¶
type Range interface { // Contains checks if the given IP-address is in the range. Contains(net.IP) bool // Count returns number of addresses in the range. Count() *big.Int // Iterator returns iterator for the range. Iterator() Iterator }
Range is an interface to work IP-addresses range.
Example (Beginend) ¶
package main import ( "fmt" "github.com/russtone/iprange" "net" ) func main() { r := iprange.Parse("192.168.1.10_192.168.2.9") fmt.Println(r.Count()) fmt.Println(r.Contains(net.ParseIP("192.168.2.1"))) fmt.Println(r.Contains(net.ParseIP("192.168.10.1"))) }
Output: 256 true false
Example (Cidr) ¶
package main import ( "fmt" "github.com/russtone/iprange" "net" ) func main() { r := iprange.Parse("192.168.1.10/24") fmt.Println(r.Count()) fmt.Println(r.Contains(net.ParseIP("192.168.1.50"))) fmt.Println(r.Contains(net.ParseIP("192.168.10.1"))) }
Output: 256 true false
Example (Octets) ¶
package main import ( "fmt" "github.com/russtone/iprange" "net" ) func main() { r := iprange.Parse("192.168.1-2,4.1-10") fmt.Println(r.Count()) fmt.Println(r.Contains(net.ParseIP("192.168.2.5"))) fmt.Println(r.Contains(net.ParseIP("192.168.1.15"))) }
Output: 30 true false
Example (Single) ¶
package main import ( "fmt" "github.com/russtone/iprange" "net" ) func main() { r := iprange.Parse("192.168.1.1") fmt.Println(r.Count()) fmt.Println(r.Contains(net.ParseIP("192.168.1.1"))) fmt.Println(r.Contains(net.ParseIP("192.168.10.1"))) }
Output: 1 true false
func Parse ¶
Parse parses s as an IP addresses range (IPv4 or IPv6), returning the result. The string s can be in the following formats: single IP ("192.0.2.1", "2001:db8::68"), CIDR range ("192.168.1.0/24", "2001:db8::68/120"), begin_end range ("192.168.1.1_192.168.1.10", "2001:db8::68_2001:db8::80") or octets range ("192.168.1,3,5.1-10", "2001:db8::0,1:68-80"). If s is not a valid textual representation of an IP addresses range, ParseIP returns nil.
type Ranges ¶
type Ranges []Range
Ranges allows to combine multiple ranges and use them as one.
Example ¶
package main import ( "fmt" "github.com/russtone/iprange" "net" ) func main() { rr := make(iprange.Ranges, 0) rr = append(rr, iprange.Parse("192.168.1.0/24")) rr = append(rr, iprange.Parse("192.168.2.0/24")) fmt.Println(rr.Count()) fmt.Println(rr.Contains(net.ParseIP("192.168.1.10"))) fmt.Println(rr.Contains(net.ParseIP("192.168.2.10"))) }
Output: 512 true true