From 7e97b691ebb6342ecbf69cfff590aff7fb251ad5 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Tue, 8 May 2018 17:15:49 +0200 Subject: [PATCH] [TASK] improve autoselect ip_address (#137) --- docs/docs_configuration.md | 1 + respond/collector.go | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/docs/docs_configuration.md b/docs/docs_configuration.md index 470a5ae..826267d 100644 --- a/docs/docs_configuration.md +++ b/docs/docs_configuration.md @@ -120,6 +120,7 @@ ifname = "br-ffhb" {% method %} ip address is the own address which is used for sending. If not set or set with empty string it will take an address of ifname. +(If `multicast_address` is not set the link local address otherwise a global unicast address) {% sample lang="toml" %} ```toml ip_address = "fe80::..." diff --git a/respond/collector.go b/respond/collector.go index 6908980..2ca7395 100644 --- a/respond/collector.go +++ b/respond/collector.go @@ -65,7 +65,7 @@ func (coll *Collector) listenUDP(iface InterfaceConfig) { if iface.IPAddress != "" { addr = net.ParseIP(iface.IPAddress) } else { - addr, err = getUnicastAddr(iface.InterfaceName) + addr, err = getUnicastAddr(iface.InterfaceName, iface.MulticastAddress == "") if err != nil { log.Panic(err) } @@ -97,8 +97,8 @@ func (coll *Collector) listenUDP(iface InterfaceConfig) { go coll.receiver(conn) } -// Returns a unicast address of given interface (prefer global unicast address over link local address) -func getUnicastAddr(ifname string) (net.IP, error) { +// Returns a unicast address of given interface (linklocal or global unicast address) +func getUnicastAddr(ifname string, linklocal bool) (net.IP, error) { iface, err := net.InterfaceByName(ifname) if err != nil { return nil, err @@ -115,9 +115,7 @@ func getUnicastAddr(ifname string) (net.IP, error) { if !ok { continue } - if ipnet.IP.IsGlobalUnicast() { - ip = ipnet.IP - } else if ipnet.IP.IsLinkLocalUnicast() && ip == nil { + if (!linklocal && ipnet.IP.IsGlobalUnicast()) || (linklocal && ipnet.IP.IsLinkLocalUnicast()) { ip = ipnet.IP } }