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 err error
if iface.IPAddress != "" {
addr = net.ParseIP(iface.IPAddress)
} else {
addr, err = getUnicastAddr(iface.InterfaceName)
if err != nil {
log.WithField("iface", iface.InterfaceName).Panic(err)
ifaceInfo, err := net.InterfaceByName(iface.InterfaceName)
if err == nil {
if iface.IPAddress != "" {
addr = net.ParseIP(iface.IPAddress)
} else {
addr, err = getUnicastAddr(ifaceInfo)
}
}
if err != nil {
log.WithField("iface", iface.InterfaceName).Panic(err)
}
multicastAddress := MulticastAddressDefault
if 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)
func getUnicastAddr(ifname string) (net.IP, error) {
iface, err := net.InterfaceByName(ifname)
if err != nil {
return nil, err
}
func getUnicastAddr(iface *net.Interface) (net.IP, error) {
addresses, err := iface.Addrs()
if err != nil {
return nil, err