Skip to content
Snippets Groups Projects
Select Git revision
  • 624963eadecd21515b04c0e9e6dc5ff5c274c676
  • master default protected
  • lintian-fixes
  • renovate/github.com-miekg-dns-1.x
  • renovate/golang.org-x-crypto-digest
5 results

client.go

Blame
  • Forked from ai3 / tools / acmeserver
    Source project has a limited visibility.
    client.go 13.31 KiB
    package dns
    
    // A client implementation.
    
    import (
    	"context"
    	"crypto/tls"
    	"encoding/binary"
    	"fmt"
    	"io"
    	"net"
    	"strings"
    	"time"
    )
    
    const (
    	dnsTimeout     time.Duration = 2 * time.Second
    	tcpIdleTimeout time.Duration = 8 * time.Second
    )
    
    // A Conn represents a connection to a DNS server.
    type Conn struct {
    	net.Conn                         // a net.Conn holding the connection
    	UDPSize        uint16            // minimum receive buffer for UDP messages
    	TsigSecret     map[string]string // secret(s) for Tsig map[<zonename>]<base64 secret>, zonename must be in canonical form (lowercase, fqdn, see RFC 4034 Section 6.2)
    	tsigRequestMAC string
    }
    
    // A Client defines parameters for a DNS client.
    type Client struct {
    	Net       string      // if "tcp" or "tcp-tls" (DNS over TLS) a TCP query will be initiated, otherwise an UDP one (default is "" for UDP)
    	UDPSize   uint16      // minimum receive buffer for UDP messages
    	TLSConfig *tls.Config // TLS connection configuration
    	Dialer    *net.Dialer // a net.Dialer used to set local address, timeouts and more
    	// Timeout is a cumulative timeout for dial, write and read, defaults to 0 (disabled) - overrides DialTimeout, ReadTimeout,
    	// WriteTimeout when non-zero. Can be overridden with net.Dialer.Timeout (see Client.ExchangeWithDialer and
    	// Client.Dialer) or context.Context.Deadline (see ExchangeContext)
    	Timeout        time.Duration
    	DialTimeout    time.Duration     // net.DialTimeout, defaults to 2 seconds, or net.Dialer.Timeout if expiring earlier - overridden by Timeout when that value is non-zero
    	ReadTimeout    time.Duration     // net.Conn.SetReadTimeout value for connections, defaults to 2 seconds - overridden by Timeout when that value is non-zero
    	WriteTimeout   time.Duration     // net.Conn.SetWriteTimeout value for connections, defaults to 2 seconds - overridden by Timeout when that value is non-zero
    	TsigSecret     map[string]string // secret(s) for Tsig map[<zonename>]<base64 secret>, zonename must be in canonical form (lowercase, fqdn, see RFC 4034 Section 6.2)
    	SingleInflight bool              // if true suppress multiple outstanding queries for the same Qname, Qtype and Qclass
    	group          singleflight
    }
    
    // Exchange performs a synchronous UDP query. It sends the message m to the address
    // contained in a and waits for a reply. Exchange does not retry a failed query, nor
    // will it fall back to TCP in case of truncation.
    // See client.Exchange for more information on setting larger buffer sizes.
    func Exchange(m *Msg, a string) (r *Msg, err error) {
    	client := Client{Net: "udp"}
    	r, _, err = client.Exchange(m, a)
    	return r, err
    }
    
    func (c *Client) dialTimeout() time.Duration {
    	if c.Timeout != 0 {
    		return c.Timeout
    	}
    	if c.DialTimeout != 0 {
    		return c.DialTimeout
    	}
    	return dnsTimeout
    }
    
    func (c *Client) readTimeout() time.Duration {
    	if c.ReadTimeout != 0 {
    		return c.ReadTimeout
    	}