go-nsupdate/addr.go

139 lines
2.6 KiB
Go
Raw Normal View History

package main
import (
2016-06-19 11:46:23 -05:00
"fmt"
2016-06-19 14:50:21 -05:00
"github.com/vishvananda/netlink"
2016-06-19 13:30:00 -05:00
"io"
2016-06-19 14:50:21 -05:00
"log"
"net"
)
2016-06-19 11:46:23 -05:00
type AddrSet struct {
2016-06-19 14:50:21 -05:00
linkAttrs netlink.LinkAttrs
linkChan chan netlink.LinkUpdate
addrChan chan netlink.AddrUpdate
2016-06-19 13:30:00 -05:00
2016-06-19 14:50:21 -05:00
addrs map[string]net.IP
}
2016-06-19 13:30:00 -05:00
func (addrs *AddrSet) String() string {
return fmt.Sprintf("AddrSet iface=%v", addrs.linkAttrs.Name)
}
func (addrs *AddrSet) testFlag(flag net.Flags) bool {
2016-06-19 14:50:21 -05:00
return addrs.linkAttrs.Flags&flag != 0
2016-06-19 13:30:00 -05:00
}
func (addrs *AddrSet) Up() bool {
return addrs.testFlag(net.FlagUp)
}
func InterfaceAddrs(iface string, family Family) (*AddrSet, error) {
var addrs AddrSet
2016-06-19 11:46:23 -05:00
link, err := netlink.LinkByName(iface)
if err != nil {
2016-06-19 13:30:00 -05:00
return nil, fmt.Errorf("netlink.LinkByName %v: %v", iface, err)
} else {
addrs.linkAttrs = *link.Attrs()
}
2016-06-19 13:30:00 -05:00
// list
if addrList, err := netlink.AddrList(link, int(family)); err != nil {
return nil, fmt.Errorf("netlink.AddrList %v: %v", link, err)
} else {
addrs.addrs = make(map[string]net.IP)
for _, addr := range addrList {
addrs.updateAddr(addr, true)
}
2016-06-19 11:46:23 -05:00
}
2016-06-19 13:30:00 -05:00
// update
addrs.linkChan = make(chan netlink.LinkUpdate)
addrs.addrChan = make(chan netlink.AddrUpdate)
if err := netlink.LinkSubscribe(addrs.linkChan, nil); err != nil {
return nil, fmt.Errorf("netlink.LinkSubscribe: %v", err)
}
2016-06-19 11:46:23 -05:00
2016-06-19 13:30:00 -05:00
if err := netlink.AddrSubscribe(addrs.addrChan, nil); err != nil {
return nil, fmt.Errorf("netlink.AddrSubscribe: %v", err)
}
2016-06-19 13:30:00 -05:00
return &addrs, nil
}
2016-06-19 13:30:00 -05:00
func (addrs *AddrSet) Read() error {
for {
select {
case linkUpdate, ok := <-addrs.linkChan:
if !ok {
return io.EOF
}
linkAttrs := linkUpdate.Attrs()
if linkAttrs.Index != addrs.linkAttrs.Index {
continue
}
// update state
addrs.updateLink(*linkAttrs)
case addrUpdate, ok := <-addrs.addrChan:
if !ok {
return io.EOF
}
2016-06-19 11:46:23 -05:00
2016-06-19 13:30:00 -05:00
if addrUpdate.LinkIndex != addrs.linkAttrs.Index {
continue
}
// XXX: scope and other filters?
addrs.updateAddr(addrUpdate.Addr, addrUpdate.NewAddr)
return nil
}
}
}
// Update state for address
func (addrs *AddrSet) updateAddr(addr netlink.Addr, up bool) {
2016-06-19 11:46:23 -05:00
if addr.Scope >= int(netlink.SCOPE_LINK) {
return
}
2016-06-19 13:30:00 -05:00
ip := addr.IP
2016-06-19 11:46:23 -05:00
if up {
2016-06-19 13:30:00 -05:00
log.Printf("%v: up %v", addrs, ip)
2016-06-19 11:46:23 -05:00
2016-06-19 13:30:00 -05:00
addrs.addrs[ip.String()] = ip
2016-06-19 11:46:23 -05:00
} else {
2016-06-19 13:30:00 -05:00
log.Printf("%v: down %v", addrs, ip)
delete(addrs.addrs, ip.String())
}
}
func (addrs *AddrSet) updateLink(linkAttrs netlink.LinkAttrs) {
addrs.linkAttrs = linkAttrs
if !addrs.Up() {
log.Printf("%v: down", addrs)
}
}
func (addrs *AddrSet) Each(visitFunc func(net.IP)) {
if !addrs.Up() {
// link down has no up addrs
return
}
2016-06-19 11:46:23 -05:00
2016-06-19 13:30:00 -05:00
for _, ip := range addrs.addrs {
visitFunc(ip)
2016-06-19 11:46:23 -05:00
}
}