Debian Codegraph @

client.go

raw
1 package dns
2
3 // A DNS client implementation, modelled after http.Client
4
5 import (
6 "context"
7 "fmt"
8 "io"
9 "net"
10 "time"
11 )
12
13 // A Client is a DNS client. It is safe to use a client from multiple goroutines.
14 type Client struct {
15 *Transport // If Transport is nil it gets a [NewTransport].
16
17 *Transfer // If a transfer is attempted, this holds the optional signing settings.
18 }
19
20 // NewClient returns a client with the transport set to [NewTransport].
21 func NewClient() *Client {
22 return &Client{Transport: NewTransport()}
23 }
24
25 // Exchange performs a synchronous query over "network". It sends the message m to the address
26 // and waits for a reply. Exchange does not retry a failed query, nor
27 // will it fall back to TCP in case of truncation. If the Data buffer in m is empty, Exchange calls m.Pack().
28 //
29 // See [Client.Exchange] for more information on setting larger buffer sizes.
30 func Exchange(ctx context.Context, m *Msg, network, address string) (r *Msg, err error) {
31 client := &Client{}
32 r, _, err = client.Exchange(ctx, m, network, address)
33 return r, err
34 }
35
36 // Exchange performs a synchronous query. It sends the message m to the address contained in a and waits for
37 // a reply. Basic use pattern with a *dns.Client:
38 //
39 // c := new(dns.Client)
40 // resp, rtt, err := c.Exchange(ctx, m, "udp", "127.0.0.1:53")
41 //
42 // If client does not have a transport set [NewTransport] is set and used. Exchange does not retry a failed query,
43 // nor will it fall back to TCP in case of truncation when UDP is used.
44 //
45 // If the TLS config is set in the transport a (TCP) connection with TLS is attempted.
46 //
47 // It is up to the caller to create a message that allows for larger responses to be returned. Specifically
48 // this means setting [Msg.UDPSize] that will advertise a larger buffer. Messages without a UDPSize will
49 // fall back to the historic limit of 512 octets (bytes).
50 //
51 // The full binary data is included in the (decoded) message as r.Data. If the Data buffer in m is empty
52 // client.Exchange calls m.Pack(), the buffer (in m) will be reset.
53 //
54 // An error is returned if:
55 // - if the message returned does not have the same ID as the message sent.
56 // - the response bit is not set on the reply.
57 //
58 // See [CompareName] for checking the question name the point to another possible check. See
59 // [codeberg.org/miekg/dns/dnsutil.Randomize] to randomize the question name.
60 func (c *Client) Exchange(ctx context.Context, m *Msg, network, address string) (r *Msg, rtt time.Duration, err error) {
61 if c.Transport == nil {
62 c.Transport = NewTransport()
63 }
64
65 conn, err := c.dial(ctx, network, address)
66 if err != nil {
67 return nil, 0, err
68 }
69 defer conn.Close()
70 return c.ExchangeWithConn(ctx, m, conn)
71 }
72
73 // ExchangeWithConn behaves like [client.Exchange], but with a supplied connection.
74 func (c *Client) ExchangeWithConn(ctx context.Context, m *Msg, conn net.Conn) (r *Msg, rtt time.Duration, err error) {
75 if len(m.Data) == 0 {
76 if err := m.Pack(); err != nil {
77 return nil, 0, err
78 }
79 }
80
81 t := time.Now()
82 remote := &response{conn: conn} // for Session() call in msg.go#L750
83 if _, err := io.Copy(remote, m); err != nil {
84 return nil, time.Since(t), err
85 }
86
87 if err := ctx.Err(); err != nil {
88 return nil, time.Since(t), err
89 }
90
91 r = &Msg{Data: m.Data}
92 m.Data = m.Data[:0]
93 if len(r.Data) < int(m.UDPSize) {
94 r.Data = append(r.Data, make([]byte, (int(m.UDPSize)-len(r.Data)))...)
95 }
96 if len(r.Data) < MinMsgSize {
97 r.Data = append(r.Data, make([]byte, MinMsgSize-len(r.Data))...)
98 }
99
100 conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
101 if _, err := io.Copy(r, conn); err != nil {
102 return nil, time.Since(t), err
103 }
104
105 if err := ctx.Err(); err != nil {
106 return nil, time.Since(t), err
107 }
108
109 if err = r.Unpack(); err != nil {
110 return r, time.Since(t), err
111 }
112 if !r.Response {
113 return r, time.Since(t), &Error{err: "response bit is not set"}
114 }
115 if r.ID != m.ID {
116 return r, time.Since(t), fmt.Errorf("%w: %d != %d", ErrID, r.ID, m.ID)
117 }
118
119 return r, time.Since(t), nil
120 }