dns/client.go

295 lines
7.8 KiB
Go
Raw Normal View History

2013-05-13 00:09:52 +10:00
// Copyright 2011 Miek Gieben. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2011-04-13 05:44:56 +10:00
package dns
2013-01-29 06:41:17 +11:00
// A client implementation.
2011-04-13 05:44:56 +10:00
import (
2011-04-16 07:55:27 +10:00
"io"
"net"
2012-01-20 22:13:47 +11:00
"time"
2011-04-13 05:44:56 +10:00
)
const dnsTimeout time.Duration = 2 * 1e9
const tcpIdleTimeout time.Duration = 8 * time.Second
2013-01-29 06:41:17 +11:00
2013-10-12 08:34:04 +11:00
// A Conn represents a connection to a DNS server.
2013-09-29 05:31:29 +10:00
type Conn struct {
2013-10-12 03:18:37 +11:00
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 fully qualified
2012-05-05 07:18:29 +10:00
rtt time.Duration
t time.Time
2013-10-12 03:18:37 +11:00
tsigRequestMAC string
}
// A Client defines parameters for a DNS client. A nil Client is usable for sending queries.
2011-04-13 05:44:56 +10:00
type Client struct {
2013-09-06 19:49:07 +10:00
Net string // if "tcp" a TCP query will be initiated, otherwise an UDP one (default is "" for UDP)
DialTimeout time.Duration // net.DialTimeout (ns), defaults to 2 * 1e9
2013-10-03 05:35:13 +10: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 19:49:07 +10: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.
func Exchange(m *Msg, a string) (r *Msg, err error) {
co := new(Conn)
co.Conn, err = net.DialTimeout("udp", a, dnsTimeout)
if err != nil {
return nil, err
}
2013-09-29 20:46:39 +10:00
defer co.Close()
2013-09-29 20:46:39 +10: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-29 05:31:29 +10: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 20:14:31 +11:00
// co := &dns.Conn{Conn: c} // c is your net.Conn
// co.WriteMsg(m)
// in, _ := co.ReadMsg()
2013-10-18 20:14:31 +11:00
// co.Close()
//
2013-10-12 22:35:09 +11: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 22:35:09 +11: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")
//
func (c *Client) Exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err error) {
2013-09-06 19:49:07 +10: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 {
r1 := r.copy()
r = r1
}
return r, rtt, nil
}
func (c *Client) exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err error) {
co := new(Conn)
timeout := dnsTimeout
if c.DialTimeout != 0 {
timeout = c.DialTimeout
2011-08-04 19:27:56 +10:00
}
if c.Net == "" {
co.Conn, err = net.DialTimeout("udp", a, timeout)
} else {
co.Conn, err = net.DialTimeout(c.Net, a, timeout)
2011-08-08 21:10:35 +10:00
}
if err != nil {
2013-01-29 06:30:13 +11:00
return nil, 0, err
}
2013-09-29 20:46:39 +10:00
timeout = dnsTimeout
if c.ReadTimeout != 0 {
timeout = c.ReadTimeout
2012-05-26 20:02:37 +10:00
}
2013-10-12 03:36:37 +11:00
co.SetReadDeadline(time.Now().Add(timeout))
2013-09-29 20:46:39 +10:00
timeout = dnsTimeout
if c.WriteTimeout != 0 {
2013-10-03 05:35:13 +10:00
timeout = c.WriteTimeout
2011-08-08 21:10:35 +10:00
}
2013-10-12 03:36:37 +11:00
co.SetWriteDeadline(time.Now().Add(timeout))
defer co.Close()
opt := m.IsEdns0()
if opt != nil && opt.UDPSize() >= MinMsgSize {
co.UDPSize = opt.UDPSize()
}
co.TsigSecret = c.TsigSecret
if err = co.WriteMsg(m); err != nil {
2013-01-29 06:30:13 +11:00
return nil, 0, err
}
r, err = co.ReadMsg()
return r, co.rtt, err
2011-08-08 21:10:35 +10:00
}
// ReadMsg reads a message from the connection co.
2013-10-12 08:34:04 +11: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-19 02:29:46 +10:00
p = make([]byte, MaxMsgSize)
} else {
if co.UDPSize >= 512 {
p = make([]byte, co.UDPSize)
} else {
p = make([]byte, MinMsgSize)
}
2011-04-19 06:08:12 +10:00
}
n, err := co.Read(p)
if err != nil && n == 0 {
2011-04-19 06:08:12 +10:00
return nil, err
}
p = p[:n]
if err := m.Unpack(p); err != nil {
return nil, err
2011-04-19 06:08:12 +10:00
}
co.rtt = time.Since(co.t)
if t := m.IsTsig(); t != nil {
if _, ok := co.TsigSecret[t.Hdr.Name]; !ok {
2012-10-16 18:42:38 +11:00
return m, ErrSecret
2011-04-23 00:37:26 +10:00
}
2012-02-26 07:42:08 +11:00
// Need to work on the original message p, as that was used to calculate the tsig.
2013-10-12 03:18:37 +11:00
err = TsigVerify(p, co.TsigSecret[t.Hdr.Name], co.tsigRequestMAC, false)
2011-04-23 00:37:26 +10:00
}
return m, err
}
2011-04-16 07:55:27 +10:00
// Read implements the net.Conn read method.
func (co *Conn) Read(p []byte) (n int, err error) {
if co.Conn == nil {
2011-11-03 09:06:54 +11:00
return 0, ErrConnEmpty
}
if len(p) < 2 {
2012-05-26 18:24:47 +10:00
return 0, io.ErrShortBuffer
}
2013-10-13 04:32:53 +11: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-13 04:32:53 +11:00
n, err = t.Read(p[:l])
if err != nil {
return n, err
}
i := n
for i < int(l) {
2013-10-13 04:32:53 +11:00
j, err := t.Read(p[i:int(l)])
2011-04-19 06:08:12 +10:00
if err != nil {
return i, err
}
i += j
2011-04-19 06:08:12 +10:00
}
n = i
2013-10-13 04:32:53 +11:00
return n, err
}
// assume udp connection
2013-10-13 04:47:11 +11:00
n, err = co.Conn.Read(p)
2013-10-13 04:32:53 +11:00
if err != nil {
return n, err
2011-04-17 18:54:34 +10:00
}
return n, err
2011-04-16 07:55:27 +10:00
}
2013-10-12 08:34:04 +11:00
// WriteMsg sends a message throught the connection co.
2011-04-19 06:08:12 +10:00
// If the message m contains a TSIG record the transaction
// signature is calculated.
func (co *Conn) WriteMsg(m *Msg) (err error) {
2012-03-03 07:19:37 +11:00
var out []byte
if t := m.IsTsig(); t != nil {
2012-03-03 07:19:37 +11:00
mac := ""
if _, ok := co.TsigSecret[t.Hdr.Name]; !ok {
return ErrSecret
2011-04-23 00:37:26 +10:00
}
2013-10-12 03:18:37 +11: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-12 03:18:37 +11:00
co.tsigRequestMAC = mac
2012-03-03 07:19:37 +11:00
} else {
out, err = m.Pack()
}
if err != nil {
return err
2011-04-16 07:55:27 +10:00
}
co.t = time.Now()
if _, err = co.Write(out); err != nil {
2012-03-03 07:19:37 +11:00
return err
}
2011-04-16 07:55:27 +10: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 {
2011-04-16 07:55:27 +10:00
if len(p) < 2 {
return 0, io.ErrShortBuffer
}
l := make([]byte, 2)
l[0], l[1] = packUint16(uint16(len(p)))
p = append(l, p...)
n, err := t.Write(p)
if err != nil {
return n, err
}
i := n
if i < len(p) {
j, err := t.Write(p[i:len(p)])
2011-04-16 07:55:27 +10:00
if err != nil {
return i, err
}
i += j
2011-04-16 07:55:27 +10:00
}
n = i
return n, err
2011-04-16 07:55:27 +10:00
}
n, err = co.Conn.(*net.UDPConn).Write(p)
return n, err
2011-04-16 07:55:27 +10:00
}
2012-05-05 07:18:29 +10:00
// Close implements the net.Conn Close method.
func (co *Conn) Close() error { return co.Conn.Close() }
2012-05-26 18:24:47 +10: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) }