Add helper method for creating an IPNet from an IP

This commit is contained in:
Vishvananda Ishaya 2014-09-07 11:19:42 -07:00
parent 8dab8b7462
commit ec452b3aa1
1 changed files with 8 additions and 3 deletions

View File

@ -35,10 +35,15 @@ func GetIPFamily(ip net.IP) int {
// This is valuable because addresses in netlink are often IPNets and // This is valuable because addresses in netlink are often IPNets and
// ParseCIDR returns an IPNet with the IP part set to the base IP of the // ParseCIDR returns an IPNet with the IP part set to the base IP of the
// range. // range.
func ParseIPNet(s string) (net.IPNet, error) { func ParseIPNet(s string) (*net.IPNet, error) {
ip, ipNet, err := net.ParseCIDR(s) ip, ipNet, err := net.ParseCIDR(s)
if err != nil { if err != nil {
return net.IPNet{}, err return nil, err
} }
return net.IPNet{ip, ipNet.Mask}, nil return &net.IPNet{ip, ipNet.Mask}, nil
}
// NewIPNet generates an IPNet from an ip address using a netmask of 32.
func NewIPNet(ip net.IP) (*net.IPNet) {
return &net.IPNet{ip, net.CIDRMask(32, 32)}
} }