🐛 Fix restricting address family when watching for changes

This commit is contained in:
neingeist 2022-06-24 00:35:08 +02:00
parent 44eb5ea262
commit d774220311
1 changed files with 8 additions and 0 deletions

View File

@ -7,12 +7,14 @@ import (
"net" "net"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
"github.com/vishvananda/netlink/nl"
) )
type AddrSet struct { type AddrSet struct {
linkAttrs netlink.LinkAttrs linkAttrs netlink.LinkAttrs
linkChan chan netlink.LinkUpdate linkChan chan netlink.LinkUpdate
addrChan chan netlink.AddrUpdate addrChan chan netlink.AddrUpdate
family Family
addrs map[string]net.IP addrs map[string]net.IP
} }
@ -37,6 +39,7 @@ func InterfaceAddrs(iface string, family Family) (*AddrSet, error) {
return nil, fmt.Errorf("netlink.LinkByName %v: %v", iface, err) return nil, fmt.Errorf("netlink.LinkByName %v: %v", iface, err)
} else { } else {
addrs.linkAttrs = *link.Attrs() addrs.linkAttrs = *link.Attrs()
addrs.family = family
} }
// list // list
@ -91,6 +94,11 @@ func (addrs *AddrSet) Read() error {
continue continue
} }
addrUpdateFamily := Family(nl.GetIPFamily(addrUpdate.LinkAddress.IP))
if addrs.family != netlink.FAMILY_ALL && addrUpdateFamily != addrs.family {
continue
}
// XXX: scope and other filters? // XXX: scope and other filters?
addrs.updateAddr(netlink.Addr{ addrs.updateAddr(netlink.Addr{
IPNet: &addrUpdate.LinkAddress, IPNet: &addrUpdate.LinkAddress,