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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "errors"
9
        "os"
10
)
11
 
12
func query(filename, query string, bufSize int) (res []string, err error) {
13
        file, err := os.OpenFile(filename, os.O_RDWR, 0)
14
        if err != nil {
15
                return
16
        }
17
        defer file.Close()
18
 
19
        _, err = file.WriteString(query)
20
        if err != nil {
21
                return
22
        }
23
        _, err = file.Seek(0, 0)
24
        if err != nil {
25
                return
26
        }
27
        buf := make([]byte, bufSize)
28
        for {
29
                n, _ := file.Read(buf)
30
                if n <= 0 {
31
                        break
32
                }
33
                res = append(res, string(buf[:n]))
34
        }
35
        return
36
}
37
 
38
func queryCS(net, host, service string) (res []string, err error) {
39
        switch net {
40
        case "tcp4", "tcp6":
41
                net = "tcp"
42
        case "udp4", "udp6":
43
                net = "udp"
44
        }
45
        if host == "" {
46
                host = "*"
47
        }
48
        return query("/net/cs", net+"!"+host+"!"+service, 128)
49
}
50
 
51
func queryCS1(net string, ip IP, port int) (clone, dest string, err error) {
52
        ips := "*"
53
        if len(ip) != 0 && !ip.IsUnspecified() {
54
                ips = ip.String()
55
        }
56
        lines, err := queryCS(net, ips, itoa(port))
57
        if err != nil {
58
                return
59
        }
60
        f := getFields(lines[0])
61
        if len(f) < 2 {
62
                return "", "", errors.New("net: bad response from ndb/cs")
63
        }
64
        clone, dest = f[0], f[1]
65
        return
66
}
67
 
68
func queryDNS(addr string, typ string) (res []string, err error) {
69
        return query("/net/dns", addr+" "+typ, 1024)
70
}
71
 
72
func lookupProtocol(name string) (proto int, err error) {
73
        // TODO: Implement this
74
        return 0, os.EPLAN9
75
}
76
 
77
func lookupHost(host string) (addrs []string, err error) {
78
        // Use /net/cs insead of /net/dns because cs knows about
79
        // host names in local network (e.g. from /lib/ndb/local)
80
        lines, err := queryCS("tcp", host, "1")
81
        if err != nil {
82
                return
83
        }
84
        for _, line := range lines {
85
                f := getFields(line)
86
                if len(f) < 2 {
87
                        continue
88
                }
89
                addr := f[1]
90
                if i := byteIndex(addr, '!'); i >= 0 {
91
                        addr = addr[:i] // remove port
92
                }
93
                if ParseIP(addr) == nil {
94
                        continue
95
                }
96
                addrs = append(addrs, addr)
97
        }
98
        return
99
}
100
 
101
func lookupIP(host string) (ips []IP, err error) {
102
        addrs, err := LookupHost(host)
103
        if err != nil {
104
                return
105
        }
106
        for _, addr := range addrs {
107
                if ip := ParseIP(addr); ip != nil {
108
                        ips = append(ips, ip)
109
                }
110
        }
111
        return
112
}
113
 
114
func lookupPort(network, service string) (port int, err error) {
115
        switch network {
116
        case "tcp4", "tcp6":
117
                network = "tcp"
118
        case "udp4", "udp6":
119
                network = "udp"
120
        }
121
        lines, err := queryCS(network, "127.0.0.1", service)
122
        if err != nil {
123
                return
124
        }
125
        unknownPortError := &AddrError{"unknown port", network + "/" + service}
126
        if len(lines) == 0 {
127
                return 0, unknownPortError
128
        }
129
        f := getFields(lines[0])
130
        if len(f) < 2 {
131
                return 0, unknownPortError
132
        }
133
        s := f[1]
134
        if i := byteIndex(s, '!'); i >= 0 {
135
                s = s[i+1:] // remove address
136
        }
137
        if n, _, ok := dtoi(s, 0); ok {
138
                return n, nil
139
        }
140
        return 0, unknownPortError
141
}
142
 
143
func lookupCNAME(name string) (cname string, err error) {
144
        lines, err := queryDNS(name, "cname")
145
        if err != nil {
146
                return
147
        }
148
        if len(lines) > 0 {
149
                if f := getFields(lines[0]); len(f) >= 3 {
150
                        return f[2] + ".", nil
151
                }
152
        }
153
        return "", errors.New("net: bad response from ndb/dns")
154
}
155
 
156
func lookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) {
157
        var target string
158
        if service == "" && proto == "" {
159
                target = name
160
        } else {
161
                target = "_" + service + "._" + proto + "." + name
162
        }
163
        lines, err := queryDNS(target, "srv")
164
        if err != nil {
165
                return
166
        }
167
        for _, line := range lines {
168
                f := getFields(line)
169
                if len(f) < 6 {
170
                        continue
171
                }
172
                port, _, portOk := dtoi(f[2], 0)
173
                priority, _, priorityOk := dtoi(f[3], 0)
174
                weight, _, weightOk := dtoi(f[4], 0)
175
                if !(portOk && priorityOk && weightOk) {
176
                        continue
177
                }
178
                addrs = append(addrs, &SRV{f[5], uint16(port), uint16(priority), uint16(weight)})
179
                cname = f[0]
180
        }
181
        byPriorityWeight(addrs).sort()
182
        return
183
}
184
 
185
func lookupMX(name string) (mx []*MX, err error) {
186
        lines, err := queryDNS(name, "mx")
187
        if err != nil {
188
                return
189
        }
190
        for _, line := range lines {
191
                f := getFields(line)
192
                if len(f) < 4 {
193
                        continue
194
                }
195
                if pref, _, ok := dtoi(f[2], 0); ok {
196
                        mx = append(mx, &MX{f[3], uint16(pref)})
197
                }
198
        }
199
        byPref(mx).sort()
200
        return
201
}
202
 
203
func lookupTXT(name string) (txt []string, err error) {
204
        lines, err := queryDNS(name, "txt")
205
        if err != nil {
206
                return
207
        }
208
        for _, line := range lines {
209
                if i := byteIndex(line, '\t'); i >= 0 {
210
                        txt = append(txt, line[i+1:])
211
                }
212
        }
213
        return
214
}
215
 
216
func lookupAddr(addr string) (name []string, err error) {
217
        arpa, err := reverseaddr(addr)
218
        if err != nil {
219
                return
220
        }
221
        lines, err := queryDNS(arpa, "ptr")
222
        if err != nil {
223
                return
224
        }
225
        for _, line := range lines {
226
                f := getFields(line)
227
                if len(f) < 3 {
228
                        continue
229
                }
230
                name = append(name, f[2])
231
        }
232
        return
233
}

powered by: WebSVN 2.1.0

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