collector: move net.InterfaceByName() call out of getUnicastAddr()

Preparation for setting IPV6_MULTICAST_IF in listenUDP().
This commit is contained in:
Matthias Schiffer 2021-10-02 16:59:20 +02:00 committed by genofire
parent bb0b23bff4
commit 0349e372ac
1 changed files with 12 additions and 13 deletions

View File

@ -59,16 +59,20 @@ func (coll *Collector) listenUDP(iface InterfaceConfig) {
var addr net.IP var addr net.IP
var err error ifaceInfo, err := net.InterfaceByName(iface.InterfaceName)
if iface.IPAddress != "" {
addr = net.ParseIP(iface.IPAddress) if err == nil {
} else { if iface.IPAddress != "" {
addr, err = getUnicastAddr(iface.InterfaceName) addr = net.ParseIP(iface.IPAddress)
if err != nil { } else {
log.WithField("iface", iface.InterfaceName).Panic(err) addr, err = getUnicastAddr(ifaceInfo)
} }
} }
if err != nil {
log.WithField("iface", iface.InterfaceName).Panic(err)
}
multicastAddress := MulticastAddressDefault multicastAddress := MulticastAddressDefault
if iface.MulticastAddress != "" { if iface.MulticastAddress != "" {
multicastAddress = iface.MulticastAddress multicastAddress = iface.MulticastAddress
@ -96,12 +100,7 @@ func (coll *Collector) listenUDP(iface InterfaceConfig) {
} }
// Returns a unicast address of given interface (linklocal or global unicast address) // Returns a unicast address of given interface (linklocal or global unicast address)
func getUnicastAddr(ifname string) (net.IP, error) { func getUnicastAddr(iface *net.Interface) (net.IP, error) {
iface, err := net.InterfaceByName(ifname)
if err != nil {
return nil, err
}
addresses, err := iface.Addrs() addresses, err := iface.Addrs()
if err != nil { if err != nil {
return nil, err return nil, err