Cleanup server address handling, support IPv6.

This fixes https://github.com/prometheus/prometheus/issues/377, as
IPv6 server addresses are now handled correctly.

Change-Id: Iebde7cfdadb0a52041472517e6fdcff4303a25ab
This commit is contained in:
Julius Volz 2014-03-09 23:17:17 +01:00 committed by Julius Volz
parent c2a2a20f36
commit 84df022025

View File

@ -15,8 +15,8 @@ package retrieval
import (
"fmt"
"net"
"net/url"
"strconv"
"strings"
"time"
@ -106,17 +106,13 @@ func lookupSRV(name string) (*dns.Msg, error) {
return nil, fmt.Errorf("Couldn't load resolv.conf: %s", err)
}
port, err := strconv.Atoi(conf.Port)
if err != nil {
return nil, fmt.Errorf("Invalid dns port in %s", resolvConf)
}
client := &dns.Client{}
response := &dns.Msg{}
for _, server := range conf.Servers {
servAddr := net.JoinHostPort(server, conf.Port)
for _, suffix := range conf.Search {
response, err = lookup(name, dns.TypeSRV, client, server, port, suffix, false)
response, err = lookup(name, dns.TypeSRV, client, servAddr, suffix, false)
if err == nil {
if len(response.Answer) > 0 {
return response, nil
@ -125,7 +121,7 @@ func lookupSRV(name string) (*dns.Msg, error) {
glog.Warningf("Resolving %s.%s failed: %s", name, suffix, err)
}
}
response, err = lookup(name, dns.TypeSRV, client, server, port, "", false)
response, err = lookup(name, dns.TypeSRV, client, servAddr, "", false)
if err == nil {
return response, nil
}
@ -133,7 +129,7 @@ func lookupSRV(name string) (*dns.Msg, error) {
return response, fmt.Errorf("Couldn't resolve %s: No server responded", name)
}
func lookup(name string, queryType uint16, client *dns.Client, server string, port int, suffix string, edns bool) (*dns.Msg, error) {
func lookup(name string, queryType uint16, client *dns.Client, servAddr string, suffix string, edns bool) (*dns.Msg, error) {
msg := &dns.Msg{}
lname := strings.Join([]string{name, suffix}, ".")
msg.SetQuestion(dns.Fqdn(lname), queryType)
@ -149,7 +145,7 @@ func lookup(name string, queryType uint16, client *dns.Client, server string, po
msg.Extra = append(msg.Extra, opt)
}
response, _, err := client.Exchange(msg, fmt.Sprintf("%s:%d", server, port))
response, _, err := client.Exchange(msg, servAddr)
if err != nil {
return nil, err
}
@ -167,7 +163,7 @@ func lookup(name string, queryType uint16, client *dns.Client, server string, po
client.Net = "tcp"
}
return lookup(name, queryType, client, server, port, suffix, !edns)
return lookup(name, queryType, client, servAddr, suffix, !edns)
}
return response, nil