-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
219 lines (177 loc) · 4.01 KB
/
client.go
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
package dns
import (
"context"
"net"
"sync/atomic"
)
// Client is a DNS client.
type Client struct {
// Transport manages connections to DNS servers.
Transport AddrDialer
// Resolver is a handler that may answer all or portions of a query.
// Any questions answered by the handler are not sent to the upstream
// server.
Resolver Handler
id uint32
}
// Dial dials a DNS server and returns a net Conn that reads and writes DNS
// messages.
func (c *Client) Dial(ctx context.Context, network, address string) (net.Conn, error) {
switch network {
case "tcp", "tcp4", "tcp6":
addr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, err
}
conn, err := c.dial(ctx, addr)
if err != nil {
return nil, err
}
return &streamSession{
session: session{
Conn: conn,
addr: addr,
client: c,
msgerrc: make(chan msgerr),
},
}, nil
case "udp", "udp4", "udp6":
addr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, err
}
conn, err := c.dial(ctx, addr)
if err != nil {
return nil, err
}
return &packetSession{
session: session{
Conn: conn,
addr: addr,
client: c,
msgerrc: make(chan msgerr),
},
}, nil
default:
return nil, ErrUnsupportedNetwork
}
}
// Do sends a DNS query to a server and returns the response message.
func (c *Client) Do(ctx context.Context, query *Query) (*Message, error) {
conn, err := c.dial(ctx, query.RemoteAddr)
if err != nil {
return nil, err
}
if t, ok := ctx.Deadline(); ok {
if err := conn.SetDeadline(t); err != nil {
return nil, err
}
}
return c.do(ctx, conn, query)
}
func (c *Client) dial(ctx context.Context, addr net.Addr) (Conn, error) {
tport := c.Transport
if tport == nil {
tport = new(Transport)
}
return tport.DialAddr(ctx, addr)
}
func (c *Client) do(ctx context.Context, conn Conn, query *Query) (*Message, error) {
if c.Resolver == nil {
return c.roundtrip(conn, query)
}
w := &clientWriter{
messageWriter: &messageWriter{
msg: response(query.Message),
},
req: request(query.Message),
addr: query.RemoteAddr,
conn: conn,
roundtrip: c.roundtrip,
}
c.Resolver.ServeDNS(ctx, w, query)
if w.err != nil {
return nil, w.err
}
return response(w.msg), nil
}
func (c *Client) roundtrip(conn Conn, query *Query) (*Message, error) {
id := query.ID
msg := *query.Message
msg.ID = c.nextID()
if err := conn.Send(&msg); err != nil {
return nil, err
}
if err := conn.Recv(&msg); err != nil {
return nil, err
}
msg.ID = id
return &msg, nil
}
const idMask = (1 << 16) - 1
func (c *Client) nextID() int {
return int(atomic.AddUint32(&c.id, 1) & idMask)
}
type clientWriter struct {
*messageWriter
req *Message
err error
addr net.Addr
conn Conn
roundtrip func(Conn, *Query) (*Message, error)
}
func (w *clientWriter) Recur(context.Context) (*Message, error) {
qs := make([]Question, 0, len(w.req.Questions))
for _, q := range w.req.Questions {
if !questionMatched(q, w.msg) {
qs = append(qs, q)
}
}
w.req.Questions = qs
req := &Query{
Message: w.req,
RemoteAddr: w.addr,
}
msg, err := w.roundtrip(w.conn, req)
if err != nil {
w.err = err
}
return msg, err
}
func (w *clientWriter) Reply(context.Context) error {
return ErrUnsupportedOp
}
func request(msg *Message) *Message {
req := new(Message)
*req = *msg // shallow copy
return req
}
func questionMatched(q Question, msg *Message) bool {
mrs := [3][]Resource{
msg.Answers,
msg.Authorities,
msg.Additionals,
}
for _, rs := range mrs {
for _, res := range rs {
if res.Name == q.Name {
return true
}
}
}
return false
}
func writeMessage(w MessageWriter, msg *Message) {
w.Status(msg.RCode)
w.Authoritative(msg.Authoritative)
w.Recursion(msg.RecursionAvailable)
for _, res := range msg.Answers {
w.Answer(res.Name, res.TTL, res.Record)
}
for _, res := range msg.Authorities {
w.Authority(res.Name, res.TTL, res.Record)
}
for _, res := range msg.Additionals {
w.Additional(res.Name, res.TTL, res.Record)
}
}