Documentation ¶
Overview ¶
Package ipv4 provides functionality for conveniently working with IPv4 and CIDR ranges.
Index ¶
- Variables
- func CIDR2Range(c string) (string, string, error)
- func FromDots(ipstr string) (uint32, error)
- func FromNetIP(ip net.IP) (uint32, error)
- func Interval2CIDRs(a1, a2 uint32, out func(left uint32, mask byte))
- func IsIPv4(s string) bool
- func IsPrivate(ipdots string) bool
- func Range2CIDRs(dots1, dots2 string) (r []string)
- func SortUniqueUint32(in []uint32)
- func ToDots(p4 uint32) string
- func ToNetIP(val uint32) net.IP
- type Interval
- type IntervalList
- type IntervalMap
- func (ipset *IntervalMap) Add(dots string, value interface{}) error
- func (ipset *IntervalMap) AddRange(dotsleft, dotsright string, value interface{}) error
- func (ipset IntervalMap) Contains(dots string) interface{}
- func (ipset IntervalMap) Go() string
- func (ipset IntervalMap) Len() int
- func (ipset IntervalMap) String() string
- func (ipset IntervalMap) Valid() error
- type Set
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrBadIP = errors.New("Bad IP address")
ErrBadIP is a generic error that an IP address could not be parsed
Functions ¶
func CIDR2Range ¶
CIDR2Range converts a CIDR to a dotted IP address pair, or empty strings and error. Generic: does not care if IPv4 or IPv6.
Example ¶
left, right, err := CIDR2Range("199.27.72.0/21") if err != nil { log.Fatal(err) } fmt.Println(left, right)
Output: 199.27.72.0 199.27.79.255
func Interval2CIDRs ¶ added in v1.2.0
Interval2CIDRs is the binary version of Range2CIDRs
A function is passed in to emit the networks.
func IsIPv4 ¶
IsIPv4 returns true if the input is either a dotted IPv4 address or if it's IPv4 dotted/cidr notation
Example ¶
fmt.Println(IsIPv4("10.0.0.0")) fmt.Println(IsIPv4("10.0.0.0/8")) fmt.Println(IsIPv4("2001:4860:0:2001::68")) fmt.Println(IsIPv4("2001:DB8::/48"))
Output: true true false false
func IsPrivate ¶
IsPrivate determines if an IP address is Not Public.
Note in this case Private means "localhost, loopback, link local and private subnets".
func Range2CIDRs ¶
Range2CIDRs take a pair of IPv4 addresses in dotted form, and return a list of IPv4 CIDR ranges. (or nil if invalid input)
Example ¶
fmt.Println(Range2CIDRs("127.0.0.0", "127.0.0.255"))
Output: [127.0.0.0/24]
func SortUniqueUint32 ¶ added in v1.2.0
func SortUniqueUint32(in []uint32)
SortUniqueUint32 sorts, and dedups a slice of uint32 (maybe representing binary representation of IPv4 address
sorting and uniqueness is done in place
func ToDots ¶
ToDots converts a uint32 to a IPv4 Dotted notation
About 10x faster than doing something with fmt.Sprintf one allocation per call.
Based on golang's net/IP.String() https://golang.org/src/net/ip.go?s=7645:7673#L281
Types ¶
type IntervalList ¶
type IntervalList []Interval
IntervalList is listing of Interval types, most for use in sorting
func (IntervalList) Len ¶
func (ipset IntervalList) Len() int
func (IntervalList) Less ¶
func (ipset IntervalList) Less(i, j int) bool
func (IntervalList) String ¶
func (ipset IntervalList) String() string
func (IntervalList) Swap ¶
func (ipset IntervalList) Swap(i, j int)
type IntervalMap ¶
type IntervalMap struct {
Intervals IntervalList
}
IntervalMap is set of disjoint intervals
func NewIntervalMap ¶
func NewIntervalMap(capacity int) *IntervalMap
NewIntervalMap creates a new set
func (*IntervalMap) Add ¶
func (ipset *IntervalMap) Add(dots string, value interface{}) error
Add inserts a single IP or a range based with CIDR notation
func (*IntervalMap) AddRange ¶
func (ipset *IntervalMap) AddRange(dotsleft, dotsright string, value interface{}) error
AddRange adds a range of IP addresses
func (IntervalMap) Contains ¶
func (ipset IntervalMap) Contains(dots string) interface{}
Contains returns true if the ip is in the set, error if set or input isn't valid
func (IntervalMap) Go ¶
func (ipset IntervalMap) Go() string
Go emits a source code representation of the data
func (IntervalMap) Len ¶
func (ipset IntervalMap) Len() int
Len returns the number of intervals in the set
func (IntervalMap) String ¶
func (ipset IntervalMap) String() string
func (IntervalMap) Valid ¶
func (ipset IntervalMap) Valid() error
Valid return error if internally invalid or nil if correct
type Set ¶
type Set []uint32
Set defines a unique set of IPv4 addresses using a simple []uint32
Since Set is a alias of []unit32, one can use `make(Set, length, capacity)` or use the NewSet constructor