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