2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Addr represents an IP address from netlink. Netlink ip addresses
|
|
|
|
// include a mask, so it stores the address as a net.IPNet.
|
|
|
|
type Addr struct {
|
2014-09-07 18:27:29 +00:00
|
|
|
*net.IPNet
|
2017-04-12 16:54:00 +00:00
|
|
|
Label string
|
|
|
|
Flags int
|
|
|
|
Scope int
|
|
|
|
Peer *net.IPNet
|
|
|
|
Broadcast net.IP
|
|
|
|
PreferedLft int
|
|
|
|
ValidLft int
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns $ip/$netmask $label
|
2015-06-19 00:41:46 +00:00
|
|
|
func (a Addr) String() string {
|
2015-10-15 13:26:22 +00:00
|
|
|
return strings.TrimSpace(fmt.Sprintf("%s %s", a.IPNet, a.Label))
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAddr parses the string representation of an address in the
|
|
|
|
// form $ip/$netmask $label. The label portion is optional
|
|
|
|
func ParseAddr(s string) (*Addr, error) {
|
|
|
|
label := ""
|
|
|
|
parts := strings.Split(s, " ")
|
|
|
|
if len(parts) > 1 {
|
|
|
|
s = parts[0]
|
|
|
|
label = parts[1]
|
|
|
|
}
|
|
|
|
m, err := ParseIPNet(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Addr{IPNet: m, Label: label}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal returns true if both Addrs have the same net.IPNet value.
|
|
|
|
func (a Addr) Equal(x Addr) bool {
|
|
|
|
sizea, _ := a.Mask.Size()
|
|
|
|
sizeb, _ := x.Mask.Size()
|
|
|
|
// ignore label for comparison
|
|
|
|
return a.IP.Equal(x.IP) && sizea == sizeb
|
|
|
|
}
|
2016-10-30 05:46:24 +00:00
|
|
|
|
|
|
|
func (a Addr) PeerEqual(x Addr) bool {
|
|
|
|
sizea, _ := a.Peer.Mask.Size()
|
|
|
|
sizeb, _ := x.Peer.Mask.Size()
|
|
|
|
// ignore label for comparison
|
|
|
|
return a.Peer.IP.Equal(x.Peer.IP) && sizea == sizeb
|
|
|
|
}
|