Skip to content

Commit 40d4be5

Browse files
committed
net: make all Resolver methods respect Resolver.PreferGo
Fixes #17532 Change-Id: Id62671d505c77ea924b3570a504cdc3b157e5a0d Reviewed-on: https://go-review.googlesource.com/31734 Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 23173fc commit 40d4be5

File tree

5 files changed

+64
-76
lines changed

5 files changed

+64
-76
lines changed

src/net/lookup.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ type Resolver struct {
101101
// TODO(bradfitz): Timeout time.Duration?
102102
}
103103

104-
func (r *Resolver) lookupIPFunc() func(context.Context, string) ([]IPAddr, error) {
105-
if r != nil && r.PreferGo {
106-
return goLookupIP
107-
}
108-
return lookupIP
109-
}
110-
111104
// LookupHost looks up the given host using the local resolver.
112105
// It returns a slice of that host's addresses.
113106
func LookupHost(host string) (addrs []string, err error) {
@@ -125,7 +118,7 @@ func (r *Resolver) LookupHost(ctx context.Context, host string) (addrs []string,
125118
if ip := ParseIP(host); ip != nil {
126119
return []string{host}, nil
127120
}
128-
return lookupHost(ctx, host)
121+
return r.lookupHost(ctx, host)
129122
}
130123

131124
// LookupIP looks up host using the local resolver.
@@ -160,7 +153,7 @@ func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]IPAddr, err
160153
// The underlying resolver func is lookupIP by default but it
161154
// can be overridden by tests. This is needed by net/http, so it
162155
// uses a context key instead of unexported variables.
163-
resolverFunc := r.lookupIPFunc()
156+
resolverFunc := r.lookupIP
164157
if alt, _ := ctx.Value(nettrace.LookupIPAltResolverKey{}).(func(context.Context, string) ([]IPAddr, error)); alt != nil {
165158
resolverFunc = alt
166159
}
@@ -229,7 +222,7 @@ func LookupPort(network, service string) (port int, err error) {
229222
func (r *Resolver) LookupPort(ctx context.Context, network, service string) (port int, err error) {
230223
port, needsLookup := parsePort(service)
231224
if needsLookup {
232-
port, err = lookupPort(ctx, network, service)
225+
port, err = r.lookupPort(ctx, network, service)
233226
if err != nil {
234227
return 0, err
235228
}
@@ -245,15 +238,15 @@ func (r *Resolver) LookupPort(ctx context.Context, network, service string) (por
245238
// LookupHost or LookupIP directly; both take care of resolving
246239
// the canonical name as part of the lookup.
247240
func LookupCNAME(name string) (cname string, err error) {
248-
return lookupCNAME(context.Background(), name)
241+
return DefaultResolver.lookupCNAME(context.Background(), name)
249242
}
250243

251244
// LookupCNAME returns the canonical DNS host for the given name.
252245
// Callers that do not care about the canonical name can call
253246
// LookupHost or LookupIP directly; both take care of resolving
254247
// the canonical name as part of the lookup.
255248
func (r *Resolver) LookupCNAME(ctx context.Context, name string) (cname string, err error) {
256-
return lookupCNAME(ctx, name)
249+
return r.lookupCNAME(ctx, name)
257250
}
258251

259252
// LookupSRV tries to resolve an SRV query of the given service,
@@ -266,7 +259,7 @@ func (r *Resolver) LookupCNAME(ctx context.Context, name string) (cname string,
266259
// publishing SRV records under non-standard names, if both service
267260
// and proto are empty strings, LookupSRV looks up name directly.
268261
func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) {
269-
return lookupSRV(context.Background(), service, proto, name)
262+
return DefaultResolver.lookupSRV(context.Background(), service, proto, name)
270263
}
271264

272265
// LookupSRV tries to resolve an SRV query of the given service,
@@ -279,47 +272,47 @@ func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err
279272
// publishing SRV records under non-standard names, if both service
280273
// and proto are empty strings, LookupSRV looks up name directly.
281274
func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*SRV, err error) {
282-
return lookupSRV(ctx, service, proto, name)
275+
return r.lookupSRV(ctx, service, proto, name)
283276
}
284277

285278
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
286279
func LookupMX(name string) ([]*MX, error) {
287-
return lookupMX(context.Background(), name)
280+
return DefaultResolver.lookupMX(context.Background(), name)
288281
}
289282

290283
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
291284
func (r *Resolver) LookupMX(ctx context.Context, name string) ([]*MX, error) {
292-
return lookupMX(ctx, name)
285+
return r.lookupMX(ctx, name)
293286
}
294287

295288
// LookupNS returns the DNS NS records for the given domain name.
296289
func LookupNS(name string) ([]*NS, error) {
297-
return lookupNS(context.Background(), name)
290+
return DefaultResolver.lookupNS(context.Background(), name)
298291
}
299292

300293
// LookupNS returns the DNS NS records for the given domain name.
301294
func (r *Resolver) LookupNS(ctx context.Context, name string) ([]*NS, error) {
302-
return lookupNS(ctx, name)
295+
return r.lookupNS(ctx, name)
303296
}
304297

305298
// LookupTXT returns the DNS TXT records for the given domain name.
306299
func LookupTXT(name string) ([]string, error) {
307-
return lookupTXT(context.Background(), name)
300+
return DefaultResolver.lookupTXT(context.Background(), name)
308301
}
309302

310303
// LookupTXT returns the DNS TXT records for the given domain name.
311304
func (r *Resolver) LookupTXT(ctx context.Context, name string) ([]string, error) {
312-
return lookupTXT(ctx, name)
305+
return r.lookupTXT(ctx, name)
313306
}
314307

315308
// LookupAddr performs a reverse lookup for the given address, returning a list
316309
// of names mapping to that address.
317310
func LookupAddr(addr string) (names []string, err error) {
318-
return lookupAddr(context.Background(), addr)
311+
return DefaultResolver.lookupAddr(context.Background(), addr)
319312
}
320313

321314
// LookupAddr performs a reverse lookup for the given address, returning a list
322315
// of names mapping to that address.
323316
func (r *Resolver) LookupAddr(ctx context.Context, addr string) (names []string, err error) {
324-
return lookupAddr(ctx, addr)
317+
return r.lookupAddr(ctx, addr)
325318
}

src/net/lookup_nacl.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,38 @@ func lookupProtocol(ctx context.Context, name string) (proto int, err error) {
1515
return lookupProtocolMap(name)
1616
}
1717

18-
func lookupHost(ctx context.Context, host string) (addrs []string, err error) {
18+
func (*Resolver) lookupHost(ctx context.Context, host string) (addrs []string, err error) {
1919
return nil, syscall.ENOPROTOOPT
2020
}
2121

22-
func goLookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
22+
func (*Resolver) lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
2323
return nil, syscall.ENOPROTOOPT
2424
}
2525

26-
func lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
27-
return nil, syscall.ENOPROTOOPT
28-
}
29-
30-
func lookupPort(ctx context.Context, network, service string) (port int, err error) {
26+
func (*Resolver) lookupPort(ctx context.Context, network, service string) (port int, err error) {
3127
return goLookupPort(network, service)
3228
}
3329

34-
func lookupCNAME(ctx context.Context, name string) (cname string, err error) {
30+
func (*Resolver) lookupCNAME(ctx context.Context, name string) (cname string, err error) {
3531
return "", syscall.ENOPROTOOPT
3632
}
3733

38-
func lookupSRV(ctx context.Context, service, proto, name string) (cname string, srvs []*SRV, err error) {
34+
func (*Resolver) lookupSRV(ctx context.Context, service, proto, name string) (cname string, srvs []*SRV, err error) {
3935
return "", nil, syscall.ENOPROTOOPT
4036
}
4137

42-
func lookupMX(ctx context.Context, name string) (mxs []*MX, err error) {
38+
func (*Resolver) lookupMX(ctx context.Context, name string) (mxs []*MX, err error) {
4339
return nil, syscall.ENOPROTOOPT
4440
}
4541

46-
func lookupNS(ctx context.Context, name string) (nss []*NS, err error) {
42+
func (*Resolver) lookupNS(ctx context.Context, name string) (nss []*NS, err error) {
4743
return nil, syscall.ENOPROTOOPT
4844
}
4945

50-
func lookupTXT(ctx context.Context, name string) (txts []string, err error) {
46+
func (*Resolver) lookupTXT(ctx context.Context, name string) (txts []string, err error) {
5147
return nil, syscall.ENOPROTOOPT
5248
}
5349

54-
func lookupAddr(ctx context.Context, addr string) (ptrs []string, err error) {
50+
func (*Resolver) lookupAddr(ctx context.Context, addr string) (ptrs []string, err error) {
5551
return nil, syscall.ENOPROTOOPT
5652
}

src/net/lookup_plan9.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func lookupProtocol(ctx context.Context, name string) (proto int, err error) {
117117
return 0, UnknownNetworkError(name)
118118
}
119119

120-
func lookupHost(ctx context.Context, host string) (addrs []string, err error) {
120+
func (*Resolver) lookupHost(ctx context.Context, host string) (addrs []string, err error) {
121121
// Use netdir/cs instead of netdir/dns because cs knows about
122122
// host names in local network (e.g. from /lib/ndb/local)
123123
lines, err := queryCS(ctx, "net", host, "1")
@@ -148,10 +148,8 @@ loop:
148148
return
149149
}
150150

151-
var goLookupIP = lookupIP
152-
153-
func lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
154-
lits, err := lookupHost(ctx, host)
151+
func (r *Resolver) lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
152+
lits, err := r.lookupHost(ctx, host)
155153
if err != nil {
156154
return
157155
}
@@ -165,7 +163,7 @@ func lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
165163
return
166164
}
167165

168-
func lookupPort(ctx context.Context, network, service string) (port int, err error) {
166+
func (*Resolver) lookupPort(ctx context.Context, network, service string) (port int, err error) {
169167
switch network {
170168
case "tcp4", "tcp6":
171169
network = "tcp"
@@ -194,7 +192,7 @@ func lookupPort(ctx context.Context, network, service string) (port int, err err
194192
return 0, unknownPortError
195193
}
196194

197-
func lookupCNAME(ctx context.Context, name string) (cname string, err error) {
195+
func (*Resolver) lookupCNAME(ctx context.Context, name string) (cname string, err error) {
198196
lines, err := queryDNS(ctx, name, "cname")
199197
if err != nil {
200198
return
@@ -207,7 +205,7 @@ func lookupCNAME(ctx context.Context, name string) (cname string, err error) {
207205
return "", errors.New("bad response from ndb/dns")
208206
}
209207

210-
func lookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*SRV, err error) {
208+
func (*Resolver) lookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*SRV, err error) {
211209
var target string
212210
if service == "" && proto == "" {
213211
target = name
@@ -236,7 +234,7 @@ func lookupSRV(ctx context.Context, service, proto, name string) (cname string,
236234
return
237235
}
238236

239-
func lookupMX(ctx context.Context, name string) (mx []*MX, err error) {
237+
func (*Resolver) lookupMX(ctx context.Context, name string) (mx []*MX, err error) {
240238
lines, err := queryDNS(ctx, name, "mx")
241239
if err != nil {
242240
return
@@ -254,7 +252,7 @@ func lookupMX(ctx context.Context, name string) (mx []*MX, err error) {
254252
return
255253
}
256254

257-
func lookupNS(ctx context.Context, name string) (ns []*NS, err error) {
255+
func (*Resolver) lookupNS(ctx context.Context, name string) (ns []*NS, err error) {
258256
lines, err := queryDNS(ctx, name, "ns")
259257
if err != nil {
260258
return
@@ -269,7 +267,7 @@ func lookupNS(ctx context.Context, name string) (ns []*NS, err error) {
269267
return
270268
}
271269

272-
func lookupTXT(ctx context.Context, name string) (txt []string, err error) {
270+
func (*Resolver) lookupTXT(ctx context.Context, name string) (txt []string, err error) {
273271
lines, err := queryDNS(ctx, name, "txt")
274272
if err != nil {
275273
return
@@ -282,7 +280,7 @@ func lookupTXT(ctx context.Context, name string) (txt []string, err error) {
282280
return
283281
}
284282

285-
func lookupAddr(ctx context.Context, addr string) (name []string, err error) {
283+
func (*Resolver) lookupAddr(ctx context.Context, addr string) (name []string, err error) {
286284
arpa, err := reverseaddr(addr)
287285
if err != nil {
288286
return

src/net/lookup_unix.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ func lookupProtocol(_ context.Context, name string) (int, error) {
4848
return lookupProtocolMap(name)
4949
}
5050

51-
func lookupHost(ctx context.Context, host string) (addrs []string, err error) {
51+
func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string, err error) {
5252
order := systemConf().hostLookupOrder(host)
53-
if order == hostLookupCgo {
53+
if !r.PreferGo && order == hostLookupCgo {
5454
if addrs, err, ok := cgoLookupHost(ctx, host); ok {
5555
return addrs, err
5656
}
@@ -60,7 +60,10 @@ func lookupHost(ctx context.Context, host string) (addrs []string, err error) {
6060
return goLookupHostOrder(ctx, host, order)
6161
}
6262

63-
func lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
63+
func (r *Resolver) lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
64+
if r.PreferGo {
65+
return goLookupIP(ctx, host)
66+
}
6467
order := systemConf().hostLookupOrder(host)
6568
if order == hostLookupCgo {
6669
if addrs, err, ok := cgoLookupIP(ctx, host); ok {
@@ -72,30 +75,30 @@ func lookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
7275
return goLookupIPOrder(ctx, host, order)
7376
}
7477

75-
func lookupPort(ctx context.Context, network, service string) (int, error) {
78+
func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
7679
// TODO: use the context if there ever becomes a need. Related
7780
// is issue 15321. But port lookup generally just involves
7881
// local files, and the os package has no context support. The
7982
// files might be on a remote filesystem, though. This should
8083
// probably race goroutines if ctx != context.Background().
81-
if systemConf().canUseCgo() {
84+
if !r.PreferGo && systemConf().canUseCgo() {
8285
if port, err, ok := cgoLookupPort(ctx, network, service); ok {
8386
return port, err
8487
}
8588
}
8689
return goLookupPort(network, service)
8790
}
8891

89-
func lookupCNAME(ctx context.Context, name string) (string, error) {
90-
if systemConf().canUseCgo() {
92+
func (r *Resolver) lookupCNAME(ctx context.Context, name string) (string, error) {
93+
if !r.PreferGo && systemConf().canUseCgo() {
9194
if cname, err, ok := cgoLookupCNAME(ctx, name); ok {
9295
return cname, err
9396
}
9497
}
9598
return goLookupCNAME(ctx, name)
9699
}
97100

98-
func lookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV, error) {
101+
func (*Resolver) lookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV, error) {
99102
var target string
100103
if service == "" && proto == "" {
101104
target = name
@@ -115,7 +118,7 @@ func lookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV
115118
return cname, srvs, nil
116119
}
117120

118-
func lookupMX(ctx context.Context, name string) ([]*MX, error) {
121+
func (*Resolver) lookupMX(ctx context.Context, name string) ([]*MX, error) {
119122
_, rrs, err := lookup(ctx, name, dnsTypeMX)
120123
if err != nil {
121124
return nil, err
@@ -129,7 +132,7 @@ func lookupMX(ctx context.Context, name string) ([]*MX, error) {
129132
return mxs, nil
130133
}
131134

132-
func lookupNS(ctx context.Context, name string) ([]*NS, error) {
135+
func (*Resolver) lookupNS(ctx context.Context, name string) ([]*NS, error) {
133136
_, rrs, err := lookup(ctx, name, dnsTypeNS)
134137
if err != nil {
135138
return nil, err
@@ -141,7 +144,7 @@ func lookupNS(ctx context.Context, name string) ([]*NS, error) {
141144
return nss, nil
142145
}
143146

144-
func lookupTXT(ctx context.Context, name string) ([]string, error) {
147+
func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) {
145148
_, rrs, err := lookup(ctx, name, dnsTypeTXT)
146149
if err != nil {
147150
return nil, err
@@ -153,8 +156,8 @@ func lookupTXT(ctx context.Context, name string) ([]string, error) {
153156
return txts, nil
154157
}
155158

156-
func lookupAddr(ctx context.Context, addr string) ([]string, error) {
157-
if systemConf().canUseCgo() {
159+
func (r *Resolver) lookupAddr(ctx context.Context, addr string) ([]string, error) {
160+
if !r.PreferGo && systemConf().canUseCgo() {
158161
if ptrs, err, ok := cgoLookupPTR(ctx, addr); ok {
159162
return ptrs, err
160163
}

0 commit comments

Comments
 (0)