[TASK] improve autoselect ip_address (#137)

This commit is contained in:
Martin/Geno 2018-05-08 17:15:49 +02:00 committed by Geno
parent 10f58a72ea
commit 7e97b691eb
2 changed files with 5 additions and 6 deletions

View File

@ -120,6 +120,7 @@ ifname = "br-ffhb"
{% method %} {% method %}
ip address is the own address which is used for sending. 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 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" %} {% sample lang="toml" %}
```toml ```toml
ip_address = "fe80::..." ip_address = "fe80::..."

View File

@ -65,7 +65,7 @@ func (coll *Collector) listenUDP(iface InterfaceConfig) {
if iface.IPAddress != "" { if iface.IPAddress != "" {
addr = net.ParseIP(iface.IPAddress) addr = net.ParseIP(iface.IPAddress)
} else { } else {
addr, err = getUnicastAddr(iface.InterfaceName) addr, err = getUnicastAddr(iface.InterfaceName, iface.MulticastAddress == "")
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
@ -97,8 +97,8 @@ func (coll *Collector) listenUDP(iface InterfaceConfig) {
go coll.receiver(conn) go coll.receiver(conn)
} }
// Returns a unicast address of given interface (prefer global unicast address over link local address) // Returns a unicast address of given interface (linklocal or global unicast address)
func getUnicastAddr(ifname string) (net.IP, error) { func getUnicastAddr(ifname string, linklocal bool) (net.IP, error) {
iface, err := net.InterfaceByName(ifname) iface, err := net.InterfaceByName(ifname)
if err != nil { if err != nil {
return nil, err return nil, err
@ -115,9 +115,7 @@ func getUnicastAddr(ifname string) (net.IP, error) {
if !ok { if !ok {
continue continue
} }
if ipnet.IP.IsGlobalUnicast() { if (!linklocal && ipnet.IP.IsGlobalUnicast()) || (linklocal && ipnet.IP.IsLinkLocalUnicast()) {
ip = ipnet.IP
} else if ipnet.IP.IsLinkLocalUnicast() && ip == nil {
ip = ipnet.IP ip = ipnet.IP
} }
} }