Handle addresses

This commit is contained in:
Neil Alexander 2020-05-31 20:14:23 +01:00
parent e59756224f
commit 872a6d18e8
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -7,6 +7,7 @@ import (
"net" "net"
"regexp" "regexp"
"syscall" "syscall"
"time"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@ -19,17 +20,30 @@ func (m *Multicast) _multicastStarted() {
linkClose := make(chan struct{}) linkClose := make(chan struct{})
addrClose := make(chan struct{}) addrClose := make(chan struct{})
errorCallback := func(err error) {
fmt.Println("Netlink error:", err)
}
linkSubscribeOptions := netlink.LinkSubscribeOptions{ linkSubscribeOptions := netlink.LinkSubscribeOptions{
ListExisting: true, ListExisting: true,
ErrorCallback: errorCallback,
}
addrSubscribeOptions := netlink.AddrSubscribeOptions{
ListExisting: true,
ErrorCallback: errorCallback,
} }
if err := netlink.LinkSubscribeWithOptions(linkChanges, linkClose, linkSubscribeOptions); err != nil { if err := netlink.LinkSubscribeWithOptions(linkChanges, linkClose, linkSubscribeOptions); err != nil {
panic(err) panic(err)
} }
if err := netlink.AddrSubscribe(addrChanges, addrClose); err != nil { go func() {
time.Sleep(time.Second)
if err := netlink.AddrSubscribeWithOptions(addrChanges, addrClose, addrSubscribeOptions); err != nil {
panic(err) panic(err)
} }
}()
fmt.Println("Listening for netlink changes") fmt.Println("Listening for netlink changes")
@ -91,7 +105,7 @@ func (m *Multicast) _multicastStarted() {
case change := <-addrChanges: case change := <-addrChanges:
name, ok := indexToIntf[change.LinkIndex] name, ok := indexToIntf[change.LinkIndex]
if !ok { if !ok {
return break
} }
add := true add := true
add = add && change.NewAddr add = add && change.NewAddr