~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/net/lookup_unix.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2011 The Go Authors.  All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package net
 
6
 
 
7
import (
 
8
        "os"
 
9
        "rand"
 
10
        "sort"
 
11
)
 
12
 
 
13
// LookupHost looks up the given host using the local resolver.
 
14
// It returns an array of that host's addresses.
 
15
func LookupHost(host string) (addrs []string, err os.Error) {
 
16
        addrs, err, ok := cgoLookupHost(host)
 
17
        if !ok {
 
18
                addrs, err = goLookupHost(host)
 
19
        }
 
20
        return
 
21
}
 
22
 
 
23
// LookupIP looks up host using the local resolver.
 
24
// It returns an array of that host's IPv4 and IPv6 addresses.
 
25
func LookupIP(host string) (addrs []IP, err os.Error) {
 
26
        addrs, err, ok := cgoLookupIP(host)
 
27
        if !ok {
 
28
                addrs, err = goLookupIP(host)
 
29
        }
 
30
        return
 
31
}
 
32
 
 
33
// LookupPort looks up the port for the given network and service.
 
34
func LookupPort(network, service string) (port int, err os.Error) {
 
35
        port, err, ok := cgoLookupPort(network, service)
 
36
        if !ok {
 
37
                port, err = goLookupPort(network, service)
 
38
        }
 
39
        return
 
40
}
 
41
 
 
42
// LookupCNAME returns the canonical DNS host for the given name.
 
43
// Callers that do not care about the canonical name can call
 
44
// LookupHost or LookupIP directly; both take care of resolving
 
45
// the canonical name as part of the lookup.
 
46
func LookupCNAME(name string) (cname string, err os.Error) {
 
47
        cname, err, ok := cgoLookupCNAME(name)
 
48
        if !ok {
 
49
                cname, err = goLookupCNAME(name)
 
50
        }
 
51
        return
 
52
}
 
53
 
 
54
// LookupSRV tries to resolve an SRV query of the given service,
 
55
// protocol, and domain name, as specified in RFC 2782. In most cases
 
56
// the proto argument can be the same as the corresponding
 
57
// Addr.Network(). The returned records are sorted by priority 
 
58
// and randomized by weight within a priority.
 
59
func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err os.Error) {
 
60
        target := "_" + service + "._" + proto + "." + name
 
61
        var records []dnsRR
 
62
        cname, records, err = lookup(target, dnsTypeSRV)
 
63
        if err != nil {
 
64
                return
 
65
        }
 
66
        addrs = make([]*SRV, len(records))
 
67
        for i, rr := range records {
 
68
                r := rr.(*dnsRR_SRV)
 
69
                addrs[i] = &SRV{r.Target, r.Port, r.Priority, r.Weight}
 
70
        }
 
71
        sort.Sort(byPriorityWeight(addrs))
 
72
        i := 0
 
73
        for j := 1; j < len(addrs); j++ {
 
74
                if addrs[i].Priority != addrs[j].Priority {
 
75
                        shuffleSRVByWeight(addrs[i:j])
 
76
                        i = j
 
77
                }
 
78
        }
 
79
        shuffleSRVByWeight(addrs[i:len(addrs)])
 
80
        return
 
81
}
 
82
 
 
83
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
 
84
func LookupMX(name string) (mx []*MX, err os.Error) {
 
85
        _, rr, err := lookup(name, dnsTypeMX)
 
86
        if err != nil {
 
87
                return
 
88
        }
 
89
        mx = make([]*MX, len(rr))
 
90
        for i := range rr {
 
91
                r := rr[i].(*dnsRR_MX)
 
92
                mx[i] = &MX{r.Mx, r.Pref}
 
93
        }
 
94
        // Shuffle the records to match RFC 5321 when sorted
 
95
        for i := range mx {
 
96
                j := rand.Intn(i + 1)
 
97
                mx[i], mx[j] = mx[j], mx[i]
 
98
        }
 
99
        sort.Sort(byPref(mx))
 
100
        return
 
101
}
 
102
 
 
103
// LookupAddr performs a reverse lookup for the given address, returning a list
 
104
// of names mapping to that address.
 
105
func LookupAddr(addr string) (name []string, err os.Error) {
 
106
        name = lookupStaticAddr(addr)
 
107
        if len(name) > 0 {
 
108
                return
 
109
        }
 
110
        var arpa string
 
111
        arpa, err = reverseaddr(addr)
 
112
        if err != nil {
 
113
                return
 
114
        }
 
115
        var records []dnsRR
 
116
        _, records, err = lookup(arpa, dnsTypePTR)
 
117
        if err != nil {
 
118
                return
 
119
        }
 
120
        name = make([]string, len(records))
 
121
        for i := range records {
 
122
                r := records[i].(*dnsRR_PTR)
 
123
                name[i] = r.Ptr
 
124
        }
 
125
        return
 
126
}