dns/client.go

320 lines
8.6 KiB
Go
Raw Normal View History

2011-04-12 19:44:56 +00:00
package dns
2013-01-28 19:41:17 +00:00
// A client implementation.
2011-04-12 19:44:56 +00:00
import (
"bytes"
2011-04-15 21:55:27 +00:00
"io"
"net"
2012-01-20 11:13:47 +00:00
"time"
2011-04-12 19:44:56 +00:00
)
const dnsTimeout time.Duration = 2 * 1e9
const tcpIdleTimeout time.Duration = 8 * time.Second
2013-01-28 19:41:17 +00:00
2013-10-11 21:34:04 +00:00
// A Conn represents a connection to a DNS server.
2013-09-28 19:31:29 +00:00
type Conn struct {
2013-10-11 16:18:37 +00:00
net.Conn // a net.Conn holding the connection
2014-07-31 08:48:26 +00:00
UDPSize uint16 // minimum receive buffer for UDP messages
TsigSecret map[string]string // secret(s) for Tsig map[<zonename>]<base64 secret>, zonename must be fully qualified
2012-05-04 21:18:29 +00:00
rtt time.Duration
t time.Time
2013-10-11 16:18:37 +00:00
tsigRequestMAC string
}
// A Client defines parameters for a DNS client.
2011-04-12 19:44:56 +00:00
type Client struct {
2013-09-06 09:49:07 +00:00
Net string // if "tcp" a TCP query will be initiated, otherwise an UDP one (default is "" for UDP)
2014-07-31 08:48:26 +00:00
UDPSize uint16 // minimum receive buffer for UDP messages
DialTimeout time.Duration // net.DialTimeout (ns), defaults to 2 * 1e9
2013-10-02 19:35:13 +00:00
ReadTimeout time.Duration // net.Conn.SetReadTimeout value for connections (ns), defaults to 2 * 1e9
WriteTimeout time.Duration // net.Conn.SetWriteTimeout value for connections (ns), defaults to 2 * 1e9
2013-09-06 09:49:07 +00:00
TsigSecret map[string]string // secret(s) for Tsig map[<zonename>]<base64 secret>, zonename must be fully qualified
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 an reply. Exchange does not retry a failed query, nor
2013-12-30 16:46:53 +00:00
// will it fall back to TCP in case of truncation.
2014-07-31 08:48:26 +00:00
// If you need to send a DNS message on an already existing connection, you can use the
// following:
//
// co := &dns.Conn{Conn: c} // c is your net.Conn
// co.WriteMsg(m)
// in, err := co.ReadMsg()
// co.Close()
//
func Exchange(m *Msg, a string) (r *Msg, err error) {
var co *Conn
co, err = DialTimeout("udp", a, dnsTimeout)
if err != nil {
return nil, err
}
2013-09-29 10:46:39 +00:00
defer co.Close()
2013-09-29 10:46:39 +00:00
co.SetReadDeadline(time.Now().Add(dnsTimeout))
co.SetWriteDeadline(time.Now().Add(dnsTimeout))
if err = co.WriteMsg(m); err != nil {
return nil, err
}
r, err = co.ReadMsg()
return r, err
2013-09-28 19:31:29 +00:00
}
// ExchangeConn performs a synchronous query. It sends the message m via the connection
// c and waits for a reply. The connection c is not closed by ExchangeConn.
// This function is going away, but can easily be mimicked:
//
2013-10-18 09:14:31 +00:00
// co := &dns.Conn{Conn: c} // c is your net.Conn
// co.WriteMsg(m)
// in, _ := co.ReadMsg()
2013-10-18 09:14:31 +00:00
// co.Close()
//
2013-10-12 11:35:09 +00:00
func ExchangeConn(c net.Conn, m *Msg) (r *Msg, err error) {
println("dns: this function is deprecated")
co := new(Conn)
co.Conn = c
2013-10-12 11:35:09 +00:00
if err = co.WriteMsg(m); err != nil {
return nil, err
}
r, err = co.ReadMsg()
return r, err
}
// Exchange performs an synchronous query. It sends the message m to the address
// contained in a and waits for an reply. Basic use pattern with a *dns.Client:
//
// c := new(dns.Client)
// in, rtt, err := c.Exchange(message, "127.0.0.1:53")
//
// Exchange does not retry a failed query, nor will it fall back to TCP in
// case of truncation.
func (c *Client) Exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err error) {
2013-09-06 09:49:07 +00:00
if !c.SingleInflight {
return c.exchange(m, a)
}
// This adds a bunch of garbage, TODO(miek).
t := "nop"
if t1, ok := TypeToString[m.Question[0].Qtype]; ok {
t = t1
}
cl := "nop"
if cl1, ok := ClassToString[m.Question[0].Qclass]; ok {
cl = cl1
}
r, rtt, err, shared := c.group.Do(m.Question[0].Name+t+cl, func() (*Msg, time.Duration, error) {
return c.exchange(m, a)
})
if err != nil {
return r, rtt, err
}
if shared {
return r.Copy(), rtt, nil
}
return r, rtt, nil
}
func (c *Client) exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err error) {
timeout := dnsTimeout
var co *Conn
if c.DialTimeout != 0 {
timeout = c.DialTimeout
2011-08-04 09:27:56 +00:00
}
if c.Net == "" {
co, err = DialTimeout("udp", a, timeout)
} else {
co, err = DialTimeout(c.Net, a, timeout)
2011-08-08 11:10:35 +00:00
}
if err != nil {
2013-01-28 19:30:13 +00:00
return nil, 0, err
}
2013-09-29 10:46:39 +00:00
timeout = dnsTimeout
if c.ReadTimeout != 0 {
timeout = c.ReadTimeout
2012-05-26 10:02:37 +00:00
}
2013-10-11 16:36:37 +00:00
co.SetReadDeadline(time.Now().Add(timeout))
2013-09-29 10:46:39 +00:00
timeout = dnsTimeout
if c.WriteTimeout != 0 {
2013-10-02 19:35:13 +00:00
timeout = c.WriteTimeout
2011-08-08 11:10:35 +00:00
}
2013-10-11 16:36:37 +00:00
co.SetWriteDeadline(time.Now().Add(timeout))
defer co.Close()
opt := m.IsEdns0()
2014-07-31 08:48:26 +00:00
// If EDNS0 is used use that for size.
if opt != nil && opt.UDPSize() >= MinMsgSize {
co.UDPSize = opt.UDPSize()
}
2014-07-31 08:48:26 +00:00
// Otherwise use the client's configured UDP size.
if opt == nil && c.UDPSize >= MinMsgSize {
co.UDPSize = c.UDPSize
}
co.TsigSecret = c.TsigSecret
if err = co.WriteMsg(m); err != nil {
2013-01-28 19:30:13 +00:00
return nil, 0, err
}
r, err = co.ReadMsg()
return r, co.rtt, err
2011-08-08 11:10:35 +00:00
}
// ReadMsg reads a message from the connection co.
2013-10-11 21:34:04 +00:00
// If the received message contains a TSIG record the transaction
// signature is verified.
func (co *Conn) ReadMsg() (*Msg, error) {
var p []byte
m := new(Msg)
if _, ok := co.Conn.(*net.TCPConn); ok {
2011-04-18 16:29:46 +00:00
p = make([]byte, MaxMsgSize)
} else {
if co.UDPSize >= 512 {
p = make([]byte, co.UDPSize)
} else {
p = make([]byte, MinMsgSize)
}
2011-04-18 20:08:12 +00:00
}
n, err := co.Read(p)
if err != nil && n == 0 {
2011-04-18 20:08:12 +00:00
return nil, err
}
p = p[:n]
if err := m.Unpack(p); err != nil {
return nil, err
2011-04-18 20:08:12 +00:00
}
co.rtt = time.Since(co.t)
if t := m.IsTsig(); t != nil {
if _, ok := co.TsigSecret[t.Hdr.Name]; !ok {
2012-10-16 07:42:38 +00:00
return m, ErrSecret
2011-04-22 14:37:26 +00:00
}
2012-02-25 20:42:08 +00:00
// Need to work on the original message p, as that was used to calculate the tsig.
2013-10-11 16:18:37 +00:00
err = TsigVerify(p, co.TsigSecret[t.Hdr.Name], co.tsigRequestMAC, false)
2011-04-22 14:37:26 +00:00
}
return m, err
}
2011-04-15 21:55:27 +00:00
// Read implements the net.Conn read method.
func (co *Conn) Read(p []byte) (n int, err error) {
if co.Conn == nil {
2011-11-02 22:06:54 +00:00
return 0, ErrConnEmpty
}
if len(p) < 2 {
2012-05-26 08:24:47 +00:00
return 0, io.ErrShortBuffer
}
2013-10-12 17:32:53 +00:00
if t, ok := co.Conn.(*net.TCPConn); ok {
n, err = t.Read(p[0:2])
if err != nil || n != 2 {
return n, err
}
l, _ := unpackUint16(p[0:2], 0)
if l == 0 {
return 0, ErrShortRead
}
if int(l) > len(p) {
return int(l), io.ErrShortBuffer
}
2013-10-12 17:32:53 +00:00
n, err = t.Read(p[:l])
if err != nil {
return n, err
}
i := n
for i < int(l) {
2013-10-12 17:32:53 +00:00
j, err := t.Read(p[i:int(l)])
2011-04-18 20:08:12 +00:00
if err != nil {
return i, err
}
i += j
2011-04-18 20:08:12 +00:00
}
n = i
2013-10-12 17:32:53 +00:00
return n, err
}
2014-02-14 22:47:53 +00:00
// UDP connection
2013-10-12 17:47:11 +00:00
n, err = co.Conn.Read(p)
2013-10-12 17:32:53 +00:00
if err != nil {
return n, err
2011-04-17 08:54:34 +00:00
}
return n, err
2011-04-15 21:55:27 +00:00
}
2013-10-11 21:34:04 +00:00
// WriteMsg sends a message throught the connection co.
2011-04-18 20:08:12 +00:00
// If the message m contains a TSIG record the transaction
// signature is calculated.
func (co *Conn) WriteMsg(m *Msg) (err error) {
2012-03-02 20:19:37 +00:00
var out []byte
if t := m.IsTsig(); t != nil {
2012-03-02 20:19:37 +00:00
mac := ""
if _, ok := co.TsigSecret[t.Hdr.Name]; !ok {
return ErrSecret
2011-04-22 14:37:26 +00:00
}
2013-10-11 16:18:37 +00:00
out, mac, err = TsigGenerate(m, co.TsigSecret[t.Hdr.Name], co.tsigRequestMAC, false)
// Set for the next read, allthough only used in zone transfers
2013-10-11 16:18:37 +00:00
co.tsigRequestMAC = mac
2012-03-02 20:19:37 +00:00
} else {
out, err = m.Pack()
}
if err != nil {
return err
2011-04-15 21:55:27 +00:00
}
co.t = time.Now()
if _, err = co.Write(out); err != nil {
2012-03-02 20:19:37 +00:00
return err
}
2011-04-15 21:55:27 +00:00
return nil
}
// Write implements the net.Conn Write method.
func (co *Conn) Write(p []byte) (n int, err error) {
if t, ok := co.Conn.(*net.TCPConn); ok {
lp := len(p)
if lp < 2 {
2011-04-15 21:55:27 +00:00
return 0, io.ErrShortBuffer
}
if lp > MaxMsgSize {
return 0, &Error{err: "message too large"}
2011-04-15 21:55:27 +00:00
}
l := make([]byte, 2, lp+2)
l[0], l[1] = packUint16(uint16(lp))
p = append(l, p...)
n, err := io.Copy(t, bytes.NewReader(p))
return int(n), err
2011-04-15 21:55:27 +00:00
}
n, err = co.Conn.(*net.UDPConn).Write(p)
return n, err
2011-04-15 21:55:27 +00:00
}
2012-05-04 21:18:29 +00:00
// Dial connects to the address on the named network.
func Dial(network, address string) (conn *Conn, err error) {
2013-11-01 09:50:55 +00:00
conn = new(Conn)
conn.Conn, err = net.Dial(network, address)
if err != nil {
return nil, err
}
return conn, nil
}
// Dialtimeout acts like Dial but takes a timeout.
func DialTimeout(network, address string, timeout time.Duration) (conn *Conn, err error) {
2013-11-01 09:50:55 +00:00
conn = new(Conn)
conn.Conn, err = net.DialTimeout(network, address, timeout)
if err != nil {
return nil, err
}
return conn, nil
}
// Close implements the net.Conn Close method.
func (co *Conn) Close() error { return co.Conn.Close() }
2012-05-26 08:24:47 +00:00
// LocalAddr implements the net.Conn LocalAddr method.
func (co *Conn) LocalAddr() net.Addr { return co.Conn.LocalAddr() }
// RemoteAddr implements the net.Conn RemoteAddr method.
func (co *Conn) RemoteAddr() net.Addr { return co.Conn.RemoteAddr() }
// SetDeadline implements the net.Conn SetDeadline method.
func (co *Conn) SetDeadline(t time.Time) error { return co.Conn.SetDeadline(t) }
// SetReadDeadline implements the net.Conn SetReadDeadline method.
func (co *Conn) SetReadDeadline(t time.Time) error { return co.Conn.SetReadDeadline(t) }
// SetWriteDeadline implements the net.Conn SetWriteDeadline method.
func (co *Conn) SetWriteDeadline(t time.Time) error { return co.Conn.SetWriteDeadline(t) }