github.com/vishvananda/netlink@v1.3.0/netlink.go (about) 1 // Package netlink provides a simple library for netlink. Netlink is 2 // the interface a user-space program in linux uses to communicate with 3 // the kernel. It can be used to add and remove interfaces, set up ip 4 // addresses and routes, and confiugre ipsec. Netlink communication 5 // requires elevated privileges, so in most cases this code needs to 6 // be run as root. The low level primitives for netlink are contained 7 // in the nl subpackage. This package attempts to provide a high-level 8 // interface that is loosly modeled on the iproute2 cli. 9 package netlink 10 11 import ( 12 "errors" 13 "net" 14 ) 15 16 var ( 17 // ErrNotImplemented is returned when a requested feature is not implemented. 18 ErrNotImplemented = errors.New("not implemented") 19 ) 20 21 // ParseIPNet parses a string in ip/net format and returns a net.IPNet. 22 // This is valuable because addresses in netlink are often IPNets and 23 // ParseCIDR returns an IPNet with the IP part set to the base IP of the 24 // range. 25 func ParseIPNet(s string) (*net.IPNet, error) { 26 ip, ipNet, err := net.ParseCIDR(s) 27 if err != nil { 28 return nil, err 29 } 30 ipNet.IP = ip 31 return ipNet, nil 32 } 33 34 // NewIPNet generates an IPNet from an ip address using a netmask of 32 or 128. 35 func NewIPNet(ip net.IP) *net.IPNet { 36 if ip.To4() != nil { 37 return &net.IPNet{IP: ip, Mask: net.CIDRMask(32, 32)} 38 } 39 return &net.IPNet{IP: ip, Mask: net.CIDRMask(128, 128)} 40 }