38 lines
577 B
Go
38 lines
577 B
Go
|
package cidr
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
)
|
||
|
|
||
|
func NewCIDRRanger(cidr string) (CIDRRanger, error) {
|
||
|
nIP, nIPNet, err := net.ParseCIDR(cidr)
|
||
|
if nil != err {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
switch len(nIP) {
|
||
|
case net.IPv4len:
|
||
|
cr := &cidrRangeIPv4{
|
||
|
cidrNet: nIPNet,
|
||
|
}
|
||
|
return cr, nil
|
||
|
case net.IPv6len:
|
||
|
cr := &cidrRangeIPv6{
|
||
|
cidrNet: nIPNet,
|
||
|
}
|
||
|
return cr, nil
|
||
|
default:
|
||
|
return nil, fmt.Errorf("Net: not supported IP length")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type CIDRRanger interface {
|
||
|
Contains(ip net.IP) bool
|
||
|
First() net.IP
|
||
|
Last() net.IP
|
||
|
Range() []net.IP
|
||
|
Broadcast() net.IP
|
||
|
Network() net.IP
|
||
|
}
|