OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [dnsclient.go] - Blame information for rev 775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 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
        "bytes"
9
        "fmt"
10
        "math/rand"
11
        "sort"
12
)
13
 
14
// DNSError represents a DNS lookup error.
15
type DNSError struct {
16
        Err       string // description of the error
17
        Name      string // name looked for
18
        Server    string // server used
19
        IsTimeout bool
20
}
21
 
22
func (e *DNSError) Error() string {
23
        if e == nil {
24
                return ""
25
        }
26
        s := "lookup " + e.Name
27
        if e.Server != "" {
28
                s += " on " + e.Server
29
        }
30
        s += ": " + e.Err
31
        return s
32
}
33
 
34
func (e *DNSError) Timeout() bool   { return e.IsTimeout }
35
func (e *DNSError) Temporary() bool { return e.IsTimeout }
36
 
37
const noSuchHost = "no such host"
38
 
39
// reverseaddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
40
// address addr suitable for rDNS (PTR) record lookup or an error if it fails
41
// to parse the IP address.
42
func reverseaddr(addr string) (arpa string, err error) {
43
        ip := ParseIP(addr)
44
        if ip == nil {
45
                return "", &DNSError{Err: "unrecognized address", Name: addr}
46
        }
47
        if ip.To4() != nil {
48
                return fmt.Sprintf("%d.%d.%d.%d.in-addr.arpa.", ip[15], ip[14], ip[13], ip[12]), nil
49
        }
50
        // Must be IPv6
51
        var buf bytes.Buffer
52
        // Add it, in reverse, to the buffer
53
        for i := len(ip) - 1; i >= 0; i-- {
54
                s := fmt.Sprintf("%02x", ip[i])
55
                buf.WriteByte(s[1])
56
                buf.WriteByte('.')
57
                buf.WriteByte(s[0])
58
                buf.WriteByte('.')
59
        }
60
        // Append "ip6.arpa." and return (buf already has the final .)
61
        return buf.String() + "ip6.arpa.", nil
62
}
63
 
64
// Find answer for name in dns message.
65
// On return, if err == nil, addrs != nil.
66
func answer(name, server string, dns *dnsMsg, qtype uint16) (cname string, addrs []dnsRR, err error) {
67
        addrs = make([]dnsRR, 0, len(dns.answer))
68
 
69
        if dns.rcode == dnsRcodeNameError && dns.recursion_available {
70
                return "", nil, &DNSError{Err: noSuchHost, Name: name}
71
        }
72
        if dns.rcode != dnsRcodeSuccess {
73
                // None of the error codes make sense
74
                // for the query we sent.  If we didn't get
75
                // a name error and we didn't get success,
76
                // the server is behaving incorrectly.
77
                return "", nil, &DNSError{Err: "server misbehaving", Name: name, Server: server}
78
        }
79
 
80
        // Look for the name.
81
        // Presotto says it's okay to assume that servers listed in
82
        // /etc/resolv.conf are recursive resolvers.
83
        // We asked for recursion, so it should have included
84
        // all the answers we need in this one packet.
85
Cname:
86
        for cnameloop := 0; cnameloop < 10; cnameloop++ {
87
                addrs = addrs[0:0]
88
                for _, rr := range dns.answer {
89
                        if _, justHeader := rr.(*dnsRR_Header); justHeader {
90
                                // Corrupt record: we only have a
91
                                // header. That header might say it's
92
                                // of type qtype, but we don't
93
                                // actually have it. Skip.
94
                                continue
95
                        }
96
                        h := rr.Header()
97
                        if h.Class == dnsClassINET && h.Name == name {
98
                                switch h.Rrtype {
99
                                case qtype:
100
                                        addrs = append(addrs, rr)
101
                                case dnsTypeCNAME:
102
                                        // redirect to cname
103
                                        name = rr.(*dnsRR_CNAME).Cname
104
                                        continue Cname
105
                                }
106
                        }
107
                }
108
                if len(addrs) == 0 {
109
                        return "", nil, &DNSError{Err: noSuchHost, Name: name, Server: server}
110
                }
111
                return name, addrs, nil
112
        }
113
 
114
        return "", nil, &DNSError{Err: "too many redirects", Name: name, Server: server}
115
}
116
 
117
func isDomainName(s string) bool {
118
        // See RFC 1035, RFC 3696.
119
        if len(s) == 0 {
120
                return false
121
        }
122
        if len(s) > 255 {
123
                return false
124
        }
125
        if s[len(s)-1] != '.' { // simplify checking loop: make name end in dot
126
                s += "."
127
        }
128
 
129
        last := byte('.')
130
        ok := false // ok once we've seen a letter
131
        partlen := 0
132
        for i := 0; i < len(s); i++ {
133
                c := s[i]
134
                switch {
135
                default:
136
                        return false
137
                case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
138
                        ok = true
139
                        partlen++
140
                case '0' <= c && c <= '9':
141
                        // fine
142
                        partlen++
143
                case c == '-':
144
                        // byte before dash cannot be dot
145
                        if last == '.' {
146
                                return false
147
                        }
148
                        partlen++
149
                case c == '.':
150
                        // byte before dot cannot be dot, dash
151
                        if last == '.' || last == '-' {
152
                                return false
153
                        }
154
                        if partlen > 63 || partlen == 0 {
155
                                return false
156
                        }
157
                        partlen = 0
158
                }
159
                last = c
160
        }
161
 
162
        return ok
163
}
164
 
165
// An SRV represents a single DNS SRV record.
166
type SRV struct {
167
        Target   string
168
        Port     uint16
169
        Priority uint16
170
        Weight   uint16
171
}
172
 
173
// byPriorityWeight sorts SRV records by ascending priority and weight.
174
type byPriorityWeight []*SRV
175
 
176
func (s byPriorityWeight) Len() int { return len(s) }
177
 
178
func (s byPriorityWeight) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
179
 
180
func (s byPriorityWeight) Less(i, j int) bool {
181
        return s[i].Priority < s[j].Priority ||
182
                (s[i].Priority == s[j].Priority && s[i].Weight < s[j].Weight)
183
}
184
 
185
// shuffleByWeight shuffles SRV records by weight using the algorithm
186
// described in RFC 2782.
187
func (addrs byPriorityWeight) shuffleByWeight() {
188
        sum := 0
189
        for _, addr := range addrs {
190
                sum += int(addr.Weight)
191
        }
192
        for sum > 0 && len(addrs) > 1 {
193
                s := 0
194
                n := rand.Intn(sum + 1)
195
                for i := range addrs {
196
                        s += int(addrs[i].Weight)
197
                        if s >= n {
198
                                if i > 0 {
199
                                        t := addrs[i]
200
                                        copy(addrs[1:i+1], addrs[0:i])
201
                                        addrs[0] = t
202
                                }
203
                                break
204
                        }
205
                }
206
                sum -= int(addrs[0].Weight)
207
                addrs = addrs[1:]
208
        }
209
}
210
 
211
// sort reorders SRV records as specified in RFC 2782.
212
func (addrs byPriorityWeight) sort() {
213
        sort.Sort(addrs)
214
        i := 0
215
        for j := 1; j < len(addrs); j++ {
216
                if addrs[i].Priority != addrs[j].Priority {
217
                        addrs[i:j].shuffleByWeight()
218
                        i = j
219
                }
220
        }
221
        addrs[i:].shuffleByWeight()
222
}
223
 
224
// An MX represents a single DNS MX record.
225
type MX struct {
226
        Host string
227
        Pref uint16
228
}
229
 
230
// byPref implements sort.Interface to sort MX records by preference
231
type byPref []*MX
232
 
233
func (s byPref) Len() int { return len(s) }
234
 
235
func (s byPref) Less(i, j int) bool { return s[i].Pref < s[j].Pref }
236
 
237
func (s byPref) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
238
 
239
// sort reorders MX records as specified in RFC 5321.
240
func (s byPref) sort() {
241
        for i := range s {
242
                j := rand.Intn(i + 1)
243
                s[i], s[j] = s[j], s[i]
244
        }
245
        sort.Sort(s)
246
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.