summaryrefslogtreecommitdiff
path: root/src/pkg/net/lookup.go
blob: bec93ec08cd0415219ddcf8d8477ee5acf515b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2012 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package net

import (
	"time"
)

// LookupHost looks up the given host using the local resolver.
// It returns an array of that host's addresses.
func LookupHost(host string) (addrs []string, err error) {
	return lookupHost(host)
}

func lookupHostDeadline(host string, deadline time.Time) (addrs []string, err error) {
	if deadline.IsZero() {
		return lookupHost(host)
	}

	// TODO(bradfitz): consider pushing the deadline down into the
	// name resolution functions. But that involves fixing it for
	// the native Go resolver, cgo, Windows, etc.
	//
	// In the meantime, just use a goroutine. Most users affected
	// by http://golang.org/issue/2631 are due to TCP connections
	// to unresponsive hosts, not DNS.
	timeout := deadline.Sub(time.Now())
	if timeout <= 0 {
		err = errTimeout
		return
	}
	t := time.NewTimer(timeout)
	defer t.Stop()
	type res struct {
		addrs []string
		err   error
	}
	resc := make(chan res, 1)
	go func() {
		a, err := lookupHost(host)
		resc <- res{a, err}
	}()
	select {
	case <-t.C:
		err = errTimeout
	case r := <-resc:
		addrs, err = r.addrs, r.err
	}
	return
}

// LookupIP looks up host using the local resolver.
// It returns an array of that host's IPv4 and IPv6 addresses.
func LookupIP(host string) (addrs []IP, err error) {
	return lookupIP(host)
}

// LookupPort looks up the port for the given network and service.
func LookupPort(network, service string) (port int, err error) {
	return lookupPort(network, service)
}

// LookupCNAME returns the canonical DNS host for the given name.
// Callers that do not care about the canonical name can call
// LookupHost or LookupIP directly; both take care of resolving
// the canonical name as part of the lookup.
func LookupCNAME(name string) (cname string, err error) {
	return lookupCNAME(name)
}

// LookupSRV tries to resolve an SRV query of the given service,
// protocol, and domain name.  The proto is "tcp" or "udp".
// The returned records are sorted by priority and randomized
// by weight within a priority.
//
// LookupSRV constructs the DNS name to look up following RFC 2782.
// That is, it looks up _service._proto.name.  To accommodate services
// publishing SRV records under non-standard names, if both service
// and proto are empty strings, LookupSRV looks up name directly.
func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) {
	return lookupSRV(service, proto, name)
}

// LookupMX returns the DNS MX records for the given domain name sorted by preference.
func LookupMX(name string) (mx []*MX, err error) {
	return lookupMX(name)
}

// LookupNS returns the DNS NS records for the given domain name.
func LookupNS(name string) (ns []*NS, err error) {
	return lookupNS(name)
}

// LookupTXT returns the DNS TXT records for the given domain name.
func LookupTXT(name string) (txt []string, err error) {
	return lookupTXT(name)
}

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