Skip to content

Commit 0fc582e

Browse files
committed
net: fix inconsistent error values on Lookup
This change fixes inconsistent error values on Lookup{Addr,CNAME,Host,IP.MX,NS,Port,SRV,TXT}. Updates #4856. Change-Id: I059bc8ffb96ee74dff8a8c4e8e6ae3e4a462a7ef Reviewed-on: https://go-review.googlesource.com/9108 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 456cf0f commit 0fc582e

9 files changed

+177
-192
lines changed

src/net/cgo_unix.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func cgoLookupPort(network, service string) (port int, err error, completed bool
5454
hints.ai_socktype = C.SOCK_DGRAM
5555
hints.ai_protocol = C.IPPROTO_UDP
5656
default:
57-
return 0, UnknownNetworkError(network), true
57+
return 0, &DNSError{Err: "unknown network", Name: network + "/" + service}, true
5858
}
5959
if len(network) >= 4 {
6060
switch network[3] {
@@ -78,7 +78,7 @@ func cgoLookupPort(network, service string) (port int, err error, completed bool
7878
default:
7979
err = addrinfoErrno(gerrno)
8080
}
81-
return 0, err, true
81+
return 0, &DNSError{Err: err.Error(), Name: network + "/" + service}, true
8282
}
8383
defer C.freeaddrinfo(res)
8484

@@ -94,7 +94,7 @@ func cgoLookupPort(network, service string) (port int, err error, completed bool
9494
return int(p[0])<<8 | int(p[1]), nil, true
9595
}
9696
}
97-
return 0, &AddrError{"unknown port", network + "/" + service}, true
97+
return 0, &DNSError{Err: "unknown port", Name: network + "/" + service}, true
9898
}
9999

100100
func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, completed bool) {
@@ -110,7 +110,6 @@ func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, com
110110
var res *C.struct_addrinfo
111111
gerrno, err := C.getaddrinfo(h, nil, &hints, &res)
112112
if gerrno != 0 {
113-
var str string
114113
switch gerrno {
115114
case C.EAI_SYSTEM:
116115
if err == nil {
@@ -123,13 +122,12 @@ func cgoLookupIPCNAME(name string) (addrs []IPAddr, cname string, err error, com
123122
// comes up again. golang.org/issue/6232.
124123
err = syscall.EMFILE
125124
}
126-
str = err.Error()
127125
case C.EAI_NONAME:
128-
str = noSuchHost
126+
err = errNoSuchHost
129127
default:
130-
str = addrinfoErrno(gerrno).Error()
128+
err = addrinfoErrno(gerrno)
131129
}
132-
return nil, "", &DNSError{Err: str, Name: name}, true
130+
return nil, "", &DNSError{Err: err.Error(), Name: name}, true
133131
}
134132
defer C.freeaddrinfo(res)
135133

src/net/dnsclient.go

+7-45
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,6 @@ import (
99
"sort"
1010
)
1111

12-
// DNSError represents a DNS lookup error.
13-
type DNSError struct {
14-
Err string // description of the error
15-
Name string // name looked for
16-
Server string // server used
17-
IsTimeout bool // if true, timed out; not all timeouts set this
18-
}
19-
20-
func (e *DNSError) Error() string {
21-
if e == nil {
22-
return "<nil>"
23-
}
24-
s := "lookup " + e.Name
25-
if e.Server != "" {
26-
s += " on " + e.Server
27-
}
28-
s += ": " + e.Err
29-
return s
30-
}
31-
32-
// Timeout reports whether the DNS lookup is known to have timed out.
33-
// This is not always known; a DNS lookup may fail due to a timeout
34-
// and return a DNSError for which Timeout returns false.
35-
func (e *DNSError) Timeout() bool { return e.IsTimeout }
36-
37-
// Temporary reports whether the DNS error is known to be temporary.
38-
// This is not always known; a DNS lookup may fail due to a temporary
39-
// error and return a DNSError for which Temporary returns false.
40-
func (e *DNSError) Temporary() bool { return e.IsTimeout }
41-
42-
const noSuchHost = "no such host"
43-
4412
// reverseaddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
4513
// address addr suitable for rDNS (PTR) record lookup or an error if it fails
4614
// to parse the IP address.
@@ -50,8 +18,7 @@ func reverseaddr(addr string) (arpa string, err error) {
5018
return "", &DNSError{Err: "unrecognized address", Name: addr}
5119
}
5220
if ip.To4() != nil {
53-
return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." +
54-
uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
21+
return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." + uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
5522
}
5623
// Must be IPv6
5724
buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
@@ -74,7 +41,7 @@ func answer(name, server string, dns *dnsMsg, qtype uint16) (cname string, addrs
7441
addrs = make([]dnsRR, 0, len(dns.answer))
7542

7643
if dns.rcode == dnsRcodeNameError && dns.recursion_available {
77-
return "", nil, &DNSError{Err: noSuchHost, Name: name}
44+
return "", nil, &DNSError{Err: errNoSuchHost.Error(), Name: name}
7845
}
7946
if dns.rcode != dnsRcodeSuccess {
8047
// None of the error codes make sense
@@ -113,7 +80,7 @@ Cname:
11380
}
11481
}
11582
if len(addrs) == 0 {
116-
return "", nil, &DNSError{Err: noSuchHost, Name: name, Server: server}
83+
return "", nil, &DNSError{Err: errNoSuchHost.Error(), Name: name, Server: server}
11784
}
11885
return name, addrs, nil
11986
}
@@ -201,13 +168,10 @@ type SRV struct {
201168
type byPriorityWeight []*SRV
202169

203170
func (s byPriorityWeight) Len() int { return len(s) }
204-
205-
func (s byPriorityWeight) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
206-
207171
func (s byPriorityWeight) Less(i, j int) bool {
208-
return s[i].Priority < s[j].Priority ||
209-
(s[i].Priority == s[j].Priority && s[i].Weight < s[j].Weight)
172+
return s[i].Priority < s[j].Priority || (s[i].Priority == s[j].Priority && s[i].Weight < s[j].Weight)
210173
}
174+
func (s byPriorityWeight) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
211175

212176
// shuffleByWeight shuffles SRV records by weight using the algorithm
213177
// described in RFC 2782.
@@ -255,11 +219,9 @@ type MX struct {
255219
// byPref implements sort.Interface to sort MX records by preference
256220
type byPref []*MX
257221

258-
func (s byPref) Len() int { return len(s) }
259-
222+
func (s byPref) Len() int { return len(s) }
260223
func (s byPref) Less(i, j int) bool { return s[i].Pref < s[j].Pref }
261-
262-
func (s byPref) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
224+
func (s byPref) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
263225

264226
// sort reorders MX records as specified in RFC 5321.
265227
func (s byPref) sort() {

src/net/dnsclient_unix.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func tryOneName(cfg *dnsConfig, name string, qtype uint16) (string, []dnsRR, err
185185
continue
186186
}
187187
cname, addrs, err := answer(name, server, msg, qtype)
188-
if err == nil || err.(*DNSError).Err == noSuchHost {
188+
if err == nil || err.(*DNSError).Err == errNoSuchHost.Error() {
189189
return cname, addrs, err
190190
}
191191
lastErr = err
@@ -269,7 +269,7 @@ func loadConfig(resolvConfPath string, reloadTime time.Duration, quit <-chan cha
269269
}()
270270
}
271271

272-
func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err error) {
272+
func lookup(name string, qtype uint16) (cname string, rrs []dnsRR, err error) {
273273
if !isDomainName(name) {
274274
return name, nil, &DNSError{Err: "invalid domain name", Name: name}
275275
}
@@ -296,7 +296,7 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err error)
296296
rname += "."
297297
}
298298
// Can try as ordinary name.
299-
cname, addrs, err = tryOneName(cfg.dnsConfig, rname, qtype)
299+
cname, rrs, err = tryOneName(cfg.dnsConfig, rname, qtype)
300300
if rooted || err == nil {
301301
return
302302
}
@@ -308,7 +308,7 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err error)
308308
if rname[len(rname)-1] != '.' {
309309
rname += "."
310310
}
311-
cname, addrs, err = tryOneName(cfg.dnsConfig, rname, qtype)
311+
cname, rrs, err = tryOneName(cfg.dnsConfig, rname, qtype)
312312
if err == nil {
313313
return
314314
}
@@ -317,7 +317,7 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err error)
317317
// Last ditch effort: try unsuffixed only if we haven't already,
318318
// that is, name is not rooted and has less than ndots dots.
319319
if count(name, '.') < cfg.dnsConfig.ndots {
320-
cname, addrs, err = tryOneName(cfg.dnsConfig, name+".", qtype)
320+
cname, rrs, err = tryOneName(cfg.dnsConfig, name+".", qtype)
321321
if err == nil {
322322
return
323323
}

src/net/lookup.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,22 @@ func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err
132132
}
133133

134134
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
135-
func LookupMX(name string) (mx []*MX, err error) {
135+
func LookupMX(name string) (mxs []*MX, err error) {
136136
return lookupMX(name)
137137
}
138138

139139
// LookupNS returns the DNS NS records for the given domain name.
140-
func LookupNS(name string) (ns []*NS, err error) {
140+
func LookupNS(name string) (nss []*NS, err error) {
141141
return lookupNS(name)
142142
}
143143

144144
// LookupTXT returns the DNS TXT records for the given domain name.
145-
func LookupTXT(name string) (txt []string, err error) {
145+
func LookupTXT(name string) (txts []string, err error) {
146146
return lookupTXT(name)
147147
}
148148

149149
// LookupAddr performs a reverse lookup for the given address, returning a list
150150
// of names mapping to that address.
151-
func LookupAddr(addr string) (name []string, err error) {
151+
func LookupAddr(addr string) (names []string, err error) {
152152
return lookupAddr(addr)
153153
}

0 commit comments

Comments
 (0)