github.com/vishvananda/netlink@v1.3.0/addr.go (about)

     1  package netlink
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"strings"
     7  )
     8  
     9  // Addr represents an IP address from netlink. Netlink ip addresses
    10  // include a mask, so it stores the address as a net.IPNet.
    11  type Addr struct {
    12  	*net.IPNet
    13  	Label       string
    14  	Flags       int
    15  	Scope       int
    16  	Peer        *net.IPNet
    17  	Broadcast   net.IP
    18  	PreferedLft int
    19  	ValidLft    int
    20  	LinkIndex   int
    21  }
    22  
    23  // String returns $ip/$netmask $label
    24  func (a Addr) String() string {
    25  	return strings.TrimSpace(fmt.Sprintf("%s %s", a.IPNet, a.Label))
    26  }
    27  
    28  // ParseAddr parses the string representation of an address in the
    29  // form $ip/$netmask $label. The label portion is optional
    30  func ParseAddr(s string) (*Addr, error) {
    31  	label := ""
    32  	parts := strings.Split(s, " ")
    33  	if len(parts) > 1 {
    34  		s = parts[0]
    35  		label = parts[1]
    36  	}
    37  	m, err := ParseIPNet(s)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return &Addr{IPNet: m, Label: label}, nil
    42  }
    43  
    44  // Equal returns true if both Addrs have the same net.IPNet value.
    45  func (a Addr) Equal(x Addr) bool {
    46  	sizea, _ := a.Mask.Size()
    47  	sizeb, _ := x.Mask.Size()
    48  	// ignore label for comparison
    49  	return a.IP.Equal(x.IP) && sizea == sizeb
    50  }
    51  
    52  func (a Addr) PeerEqual(x Addr) bool {
    53  	sizea, _ := a.Peer.Mask.Size()
    54  	sizeb, _ := x.Peer.Mask.Size()
    55  	// ignore label for comparison
    56  	return a.Peer.IP.Equal(x.Peer.IP) && sizea == sizeb
    57  }