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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
// Copyright 2011 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 (
"errors"
"os"
"syscall"
)
func query(filename, query string, bufSize int) (res []string, err error) {
file, err := os.OpenFile(filename, os.O_RDWR, 0)
if err != nil {
return
}
defer file.Close()
_, err = file.WriteString(query)
if err != nil {
return
}
_, err = file.Seek(0, 0)
if err != nil {
return
}
buf := make([]byte, bufSize)
for {
n, _ := file.Read(buf)
if n <= 0 {
break
}
res = append(res, string(buf[:n]))
}
return
}
func queryCS(net, host, service string) (res []string, err error) {
switch net {
case "tcp4", "tcp6":
net = "tcp"
case "udp4", "udp6":
net = "udp"
}
if host == "" {
host = "*"
}
return query("/net/cs", net+"!"+host+"!"+service, 128)
}
func queryCS1(net string, ip IP, port int) (clone, dest string, err error) {
ips := "*"
if len(ip) != 0 && !ip.IsUnspecified() {
ips = ip.String()
}
lines, err := queryCS(net, ips, itoa(port))
if err != nil {
return
}
f := getFields(lines[0])
if len(f) < 2 {
return "", "", errors.New("net: bad response from ndb/cs")
}
clone, dest = f[0], f[1]
return
}
func queryDNS(addr string, typ string) (res []string, err error) {
return query("/net/dns", addr+" "+typ, 1024)
}
func lookupProtocol(name string) (proto int, err error) {
// TODO: Implement this
return 0, syscall.EPLAN9
}
func lookupHost(host string) (addrs []string, err error) {
// Use /net/cs instead of /net/dns because cs knows about
// host names in local network (e.g. from /lib/ndb/local)
lines, err := queryCS("tcp", host, "1")
if err != nil {
return
}
for _, line := range lines {
f := getFields(line)
if len(f) < 2 {
continue
}
addr := f[1]
if i := byteIndex(addr, '!'); i >= 0 {
addr = addr[:i] // remove port
}
if ParseIP(addr) == nil {
continue
}
addrs = append(addrs, addr)
}
return
}
func lookupIP(host string) (ips []IP, err error) {
addrs, err := LookupHost(host)
if err != nil {
return
}
for _, addr := range addrs {
if ip := ParseIP(addr); ip != nil {
ips = append(ips, ip)
}
}
return
}
func lookupPort(network, service string) (port int, err error) {
switch network {
case "tcp4", "tcp6":
network = "tcp"
case "udp4", "udp6":
network = "udp"
}
lines, err := queryCS(network, "127.0.0.1", service)
if err != nil {
return
}
unknownPortError := &AddrError{"unknown port", network + "/" + service}
if len(lines) == 0 {
return 0, unknownPortError
}
f := getFields(lines[0])
if len(f) < 2 {
return 0, unknownPortError
}
s := f[1]
if i := byteIndex(s, '!'); i >= 0 {
s = s[i+1:] // remove address
}
if n, _, ok := dtoi(s, 0); ok {
return n, nil
}
return 0, unknownPortError
}
func lookupCNAME(name string) (cname string, err error) {
lines, err := queryDNS(name, "cname")
if err != nil {
return
}
if len(lines) > 0 {
if f := getFields(lines[0]); len(f) >= 3 {
return f[2] + ".", nil
}
}
return "", errors.New("net: bad response from ndb/dns")
}
func lookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) {
var target string
if service == "" && proto == "" {
target = name
} else {
target = "_" + service + "._" + proto + "." + name
}
lines, err := queryDNS(target, "srv")
if err != nil {
return
}
for _, line := range lines {
f := getFields(line)
if len(f) < 6 {
continue
}
port, _, portOk := dtoi(f[2], 0)
priority, _, priorityOk := dtoi(f[3], 0)
weight, _, weightOk := dtoi(f[4], 0)
if !(portOk && priorityOk && weightOk) {
continue
}
addrs = append(addrs, &SRV{f[5], uint16(port), uint16(priority), uint16(weight)})
cname = f[0]
}
byPriorityWeight(addrs).sort()
return
}
func lookupMX(name string) (mx []*MX, err error) {
lines, err := queryDNS(name, "mx")
if err != nil {
return
}
for _, line := range lines {
f := getFields(line)
if len(f) < 4 {
continue
}
if pref, _, ok := dtoi(f[2], 0); ok {
mx = append(mx, &MX{f[3], uint16(pref)})
}
}
byPref(mx).sort()
return
}
func lookupNS(name string) (ns []*NS, err error) {
lines, err := queryDNS(name, "ns")
if err != nil {
return
}
for _, line := range lines {
f := getFields(line)
if len(f) < 4 {
continue
}
ns = append(ns, &NS{f[3]})
}
return
}
func lookupTXT(name string) (txt []string, err error) {
lines, err := queryDNS(name, "txt")
if err != nil {
return
}
for _, line := range lines {
if i := byteIndex(line, '\t'); i >= 0 {
txt = append(txt, line[i+1:])
}
}
return
}
func lookupAddr(addr string) (name []string, err error) {
arpa, err := reverseaddr(addr)
if err != nil {
return
}
lines, err := queryDNS(arpa, "ptr")
if err != nil {
return
}
for _, line := range lines {
f := getFields(line)
if len(f) < 3 {
continue
}
name = append(name, f[2])
}
return
}
|