prefer IFA_LOCAL address when dumping addresses

IFA_LOCAL and IFA_ADDRESS are the same for bcast interfaces,
however for point-to-point links, IFA_ADDRESS is the remote
address while IFA_LOCAL is the interface address.
This commit is contained in:
Eugene Yakubovich 2015-05-09 18:06:42 -07:00
parent ae3e7dba57
commit efb2ec546f
1 changed files with 15 additions and 1 deletions

View File

@ -95,11 +95,17 @@ func AddrList(link Link, family int) ([]Addr, error) {
return nil, err return nil, err
} }
var local, dst *net.IPNet
var addr Addr var addr Addr
for _, attr := range attrs { for _, attr := range attrs {
switch attr.Attr.Type { switch attr.Attr.Type {
case syscall.IFA_ADDRESS: case syscall.IFA_ADDRESS:
addr.IPNet = &net.IPNet{ dst = &net.IPNet{
IP: attr.Value,
Mask: net.CIDRMask(int(msg.Prefixlen), 8*len(attr.Value)),
}
case syscall.IFA_LOCAL:
local = &net.IPNet{
IP: attr.Value, IP: attr.Value,
Mask: net.CIDRMask(int(msg.Prefixlen), 8*len(attr.Value)), Mask: net.CIDRMask(int(msg.Prefixlen), 8*len(attr.Value)),
} }
@ -107,6 +113,14 @@ func AddrList(link Link, family int) ([]Addr, error) {
addr.Label = string(attr.Value[:len(attr.Value)-1]) addr.Label = string(attr.Value[:len(attr.Value)-1])
} }
} }
// IFA_LOCAL should be there but if not, fall back to IFA_ADDRESS
if local != nil {
addr.IPNet = local
} else {
addr.IPNet = dst
}
res = append(res, addr) res = append(res, addr)
} }