Merge pull request #6 from neingeist/fix/restrict-family

🐛 Fix restricting address family when watching for changes
This commit is contained in:
Tero Marttila 2022-06-24 15:51:21 +03:00 committed by GitHub
commit d65f28d109
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,