Debian Codegraph @

transport.go

raw
1 package dns
2
3 import (
4 "context"
5 "crypto/tls"
6 "errors"
7 "io"
8 "net"
9 "strings"
10 "time"
11 )
12
13 // Transport is the transport used in [Client], it deals with all the networking.
14 type Transport struct {
15 // Dialer is used used to set local address and timeouts.
16 Dialer *net.Dialer
17
18 ReadTimeout time.Duration
19 WriteTimeout time.Duration
20
21 // TLSClientConfig specifies the TLS configuration to use with DialTLS, if TLSConfig is not nil it will
22 // be used to dial.
23 TLSConfig *tls.Config
24 }
25
26 // defaultTransport is the default transport in client, when none is set. Note changing this value has global
27 // effects to future [Client]s and [Transfer]s. The TSIGSigner and TSIGVerifier are both set to [TSIGHMAC].
28 var defaultTransport = Transport{
29 Dialer: &net.Dialer{
30 Timeout: 5 * time.Second,
31 KeepAlive: 3 * time.Second,
32 },
33 ReadTimeout: 2 * time.Second,
34 WriteTimeout: 2 * time.Second,
35 }
36
37 // NewTransport returns the default transport. That transport has Dialer timeout of 5s, keep alive of 3s and
38 // read and write timeout set to 2s.
39 func NewTransport() *Transport {
40 d := defaultTransport
41 return &d
42 }
43
44 // dial dials address via network. If tls config is set, a tls dialer is used. This method can be overridden to
45 // return e.g. a static net.Conn that is previously created.
46 func (t *Transport) dial(ctx context.Context, network, address string) (net.Conn, error) {
47 if t.TLSConfig != nil {
48 dialer := tls.Dialer{NetDialer: t.Dialer, Config: t.TLSConfig}
49 return dialer.DialContext(ctx, network, address)
50 }
51 return t.Dialer.DialContext(ctx, network, address)
52 }
53
54 // isEOFOrClosedNetwork returns true if the error err is an io.EOF or a *net.OpError with the
55 // text 'use of closed network connection' or a time out has been reached.
56 func isEOFOrClosedNetwork(err error) bool {
57 if errors.Is(err, io.EOF) {
58 return true
59 }
60
61 if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
62 return true
63 }
64
65 if _, ok := err.(*net.OpError); ok && strings.Contains(err.Error(), "use of closed network connection") {
66 return true
67 }
68
69 return false
70 }
71
72 // TranferOption defines the signing and other parameters that are used during a zone transfer.
73 type TransferOption struct {
74 // If non zero, TSIG signing and verification is done on messages that have a TSIG record in the pseudo section.
75 TSIGSigner
76 // If non zero SIG0 signing and verification is done on messages that have a SIG0 record in the pseudo section.
77 SIG0Signer
78 }