Commit e7183e75 authored by Russ Cox's avatar Russ Cox

net: return cname in LookupHost

Fixes #1395.

R=adg
CC=golang-dev
https://golang.org/cl/4036044
parent 4871d0d1
...@@ -98,18 +98,18 @@ func exchange(cfg *dnsConfig, c Conn, name string, qtype uint16) (*dnsMsg, os.Er ...@@ -98,18 +98,18 @@ func exchange(cfg *dnsConfig, c Conn, name string, qtype uint16) (*dnsMsg, os.Er
// Find answer for name in dns message. // Find answer for name in dns message.
// On return, if err == nil, addrs != nil. // On return, if err == nil, addrs != nil.
func answer(name, server string, dns *dnsMsg, qtype uint16) (addrs []dnsRR, err os.Error) { func answer(name, server string, dns *dnsMsg, qtype uint16) (cname string, addrs []dnsRR, err os.Error) {
addrs = make([]dnsRR, 0, len(dns.answer)) addrs = make([]dnsRR, 0, len(dns.answer))
if dns.rcode == dnsRcodeNameError && dns.recursion_available { if dns.rcode == dnsRcodeNameError && dns.recursion_available {
return nil, &DNSError{Error: noSuchHost, Name: name} return "", nil, &DNSError{Error: noSuchHost, Name: name}
} }
if dns.rcode != dnsRcodeSuccess { if dns.rcode != dnsRcodeSuccess {
// None of the error codes make sense // None of the error codes make sense
// for the query we sent. If we didn't get // for the query we sent. If we didn't get
// a name error and we didn't get success, // a name error and we didn't get success,
// the server is behaving incorrectly. // the server is behaving incorrectly.
return nil, &DNSError{Error: "server misbehaving", Name: name, Server: server} return "", nil, &DNSError{Error: "server misbehaving", Name: name, Server: server}
} }
// Look for the name. // Look for the name.
...@@ -137,19 +137,19 @@ Cname: ...@@ -137,19 +137,19 @@ Cname:
} }
} }
if len(addrs) == 0 { if len(addrs) == 0 {
return nil, &DNSError{Error: noSuchHost, Name: name, Server: server} return "", nil, &DNSError{Error: noSuchHost, Name: name, Server: server}
} }
return addrs, nil return name, addrs, nil
} }
return nil, &DNSError{Error: "too many redirects", Name: name, Server: server} return "", nil, &DNSError{Error: "too many redirects", Name: name, Server: server}
} }
// Do a lookup for a single name, which must be rooted // Do a lookup for a single name, which must be rooted
// (otherwise answer will not find the answers). // (otherwise answer will not find the answers).
func tryOneName(cfg *dnsConfig, name string, qtype uint16) (addrs []dnsRR, err os.Error) { func tryOneName(cfg *dnsConfig, name string, qtype uint16) (cname string, addrs []dnsRR, err os.Error) {
if len(cfg.servers) == 0 { if len(cfg.servers) == 0 {
return nil, &DNSError{Error: "no DNS servers", Name: name} return "", nil, &DNSError{Error: "no DNS servers", Name: name}
} }
for i := 0; i < len(cfg.servers); i++ { for i := 0; i < len(cfg.servers); i++ {
// Calling Dial here is scary -- we have to be sure // Calling Dial here is scary -- we have to be sure
...@@ -170,7 +170,7 @@ func tryOneName(cfg *dnsConfig, name string, qtype uint16) (addrs []dnsRR, err o ...@@ -170,7 +170,7 @@ func tryOneName(cfg *dnsConfig, name string, qtype uint16) (addrs []dnsRR, err o
err = merr err = merr
continue continue
} }
addrs, err = answer(name, server, msg, qtype) cname, addrs, err = answer(name, server, msg, qtype)
if err == nil || err.(*DNSError).Error == noSuchHost { if err == nil || err.(*DNSError).Error == noSuchHost {
break break
} }
...@@ -261,9 +261,8 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Erro ...@@ -261,9 +261,8 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Erro
rname += "." rname += "."
} }
// Can try as ordinary name. // Can try as ordinary name.
addrs, err = tryOneName(cfg, rname, qtype) cname, addrs, err = tryOneName(cfg, rname, qtype)
if err == nil { if err == nil {
cname = rname
return return
} }
} }
...@@ -277,9 +276,8 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Erro ...@@ -277,9 +276,8 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Erro
if rname[len(rname)-1] != '.' { if rname[len(rname)-1] != '.' {
rname += "." rname += "."
} }
addrs, err = tryOneName(cfg, rname, qtype) cname, addrs, err = tryOneName(cfg, rname, qtype)
if err == nil { if err == nil {
cname = rname
return return
} }
} }
...@@ -289,9 +287,8 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Erro ...@@ -289,9 +287,8 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Erro
if !rooted { if !rooted {
rname += "." rname += "."
} }
addrs, err = tryOneName(cfg, rname, qtype) cname, addrs, err = tryOneName(cfg, rname, qtype)
if err == nil { if err == nil {
cname = rname
return return
} }
return return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment