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 (
|
2014-01-04 19:30:08 +11:00
|
|
|
"bytes"
|
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
|
|
|
)
|
|
|
|
|
2015-01-16 09:11:11 +11:00
|
|
|
const dnsTimeout time.Duration = 2 * time.Second
|
2013-10-20 08:00:17 +11:00
|
|
|
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
|
2014-07-31 18:48:26 +10: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-05 07:18:29 +10:00
|
|
|
rtt time.Duration
|
|
|
|
t time.Time
|
2013-10-12 03:18:37 +11:00
|
|
|
tsigRequestMAC string
|
2011-04-18 05:56:40 +10:00
|
|
|
}
|
|
|
|
|
2013-11-22 00:27:20 +11:00
|
|
|
// A Client defines parameters for a DNS client.
|
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)
|
2014-07-31 18:48:26 +10:00
|
|
|
UDPSize uint16 // minimum receive buffer for UDP messages
|
2015-01-16 09:11:11 +11:00
|
|
|
DialTimeout time.Duration // net.DialTimeout, defaults to 2 seconds
|
|
|
|
ReadTimeout time.Duration // net.Conn.SetReadTimeout value for connections, defaults to 2 seconds
|
|
|
|
WriteTimeout time.Duration // net.Conn.SetWriteTimeout value for connections, defaults to 2 seconds
|
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
|
2013-08-24 08:56:34 +10:00
|
|
|
}
|
|
|
|
|
2013-09-29 19:22:27 +10:00
|
|
|
// Exchange performs a synchronous UDP query. It sends the message m to the address
|
2013-12-27 18:48:30 +11:00
|
|
|
// contained in a and waits for an reply. Exchange does not retry a failed query, nor
|
2013-12-31 03:46:53 +11:00
|
|
|
// will it fall back to TCP in case of truncation.
|
2014-07-31 18:48:26 +10: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()
|
|
|
|
//
|
2013-09-29 06:58:08 +10:00
|
|
|
func Exchange(m *Msg, a string) (r *Msg, err error) {
|
2015-05-06 16:20:46 +10:00
|
|
|
var co *Conn
|
|
|
|
co, err = DialTimeout("udp", a, dnsTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer co.Close()
|
|
|
|
co.SetReadDeadline(time.Now().Add(dnsTimeout))
|
|
|
|
co.SetWriteDeadline(time.Now().Add(dnsTimeout))
|
|
|
|
|
|
|
|
opt := m.IsEdns0()
|
|
|
|
// If EDNS0 is used use that for size.
|
|
|
|
if opt != nil && opt.UDPSize() >= MinMsgSize {
|
|
|
|
co.UDPSize = opt.UDPSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = co.WriteMsg(m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r, err = co.ReadMsg()
|
|
|
|
if err == nil && r.Id != m.Id {
|
|
|
|
err = ErrId
|
|
|
|
}
|
|
|
|
return r, err
|
2013-09-29 05:31:29 +10:00
|
|
|
}
|
|
|
|
|
2013-10-12 22:44:02 +11: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
|
2013-10-12 22:44:02 +11:00
|
|
|
// co.WriteMsg(m)
|
|
|
|
// in, _ := co.ReadMsg()
|
2013-10-18 20:14:31 +11:00
|
|
|
// co.Close()
|
2013-10-12 22:44:02 +11:00
|
|
|
//
|
2013-10-12 22:35:09 +11:00
|
|
|
func ExchangeConn(c net.Conn, m *Msg) (r *Msg, err error) {
|
2013-10-12 22:44:02 +11:00
|
|
|
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()
|
2015-05-06 15:56:42 +10:00
|
|
|
if err == nil && r.Id != m.Id {
|
|
|
|
err = ErrId
|
|
|
|
}
|
2013-10-12 22:35:09 +11:00
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
2013-09-29 06:58:08 +10:00
|
|
|
// 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")
|
|
|
|
//
|
2014-01-04 19:30:08 +11:00
|
|
|
// Exchange does not retry a failed query, nor will it fall back to TCP in
|
2013-12-27 18:48:30 +11:00
|
|
|
// case of truncation.
|
2013-09-29 06:58:08 +10:00
|
|
|
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 {
|
2013-09-29 06:58:08 +10:00
|
|
|
return c.exchange(m, a)
|
2013-08-24 08:56:34 +10:00
|
|
|
}
|
|
|
|
// This adds a bunch of garbage, TODO(miek).
|
|
|
|
t := "nop"
|
2013-08-24 12:28:58 +10:00
|
|
|
if t1, ok := TypeToString[m.Question[0].Qtype]; ok {
|
2013-08-24 08:56:34 +10:00
|
|
|
t = t1
|
|
|
|
}
|
|
|
|
cl := "nop"
|
2013-08-24 12:28:58 +10:00
|
|
|
if cl1, ok := ClassToString[m.Question[0].Qclass]; ok {
|
2013-08-24 08:56:34 +10:00
|
|
|
cl = cl1
|
|
|
|
}
|
2013-08-24 12:28:58 +10:00
|
|
|
r, rtt, err, shared := c.group.Do(m.Question[0].Name+t+cl, func() (*Msg, time.Duration, error) {
|
2013-09-29 06:58:08 +10:00
|
|
|
return c.exchange(m, a)
|
2013-08-24 08:56:34 +10:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return r, rtt, err
|
|
|
|
}
|
|
|
|
if shared {
|
2014-01-11 03:49:45 +11:00
|
|
|
return r.Copy(), rtt, nil
|
2013-08-24 08:56:34 +10:00
|
|
|
}
|
|
|
|
return r, rtt, nil
|
2011-04-18 05:56:40 +10:00
|
|
|
}
|
|
|
|
|
2015-05-06 16:23:53 +10:00
|
|
|
func (c *Client) dialTimeout() time.Duration {
|
2013-09-29 20:21:18 +10:00
|
|
|
if c.DialTimeout != 0 {
|
2015-05-06 16:23:53 +10:00
|
|
|
return c.DialTimeout
|
|
|
|
}
|
|
|
|
return dnsTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) readTimeout() time.Duration {
|
|
|
|
if c.ReadTimeout != 0 {
|
|
|
|
return c.ReadTimeout
|
2011-08-04 19:27:56 +10:00
|
|
|
}
|
2015-05-06 16:23:53 +10:00
|
|
|
return dnsTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) writeTimeout() time.Duration {
|
|
|
|
if c.WriteTimeout != 0 {
|
|
|
|
return c.WriteTimeout
|
|
|
|
}
|
|
|
|
return dnsTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err error) {
|
|
|
|
var co *Conn
|
2013-09-29 06:58:08 +10:00
|
|
|
if c.Net == "" {
|
2015-05-06 16:23:53 +10:00
|
|
|
co, err = DialTimeout("udp", a, c.dialTimeout())
|
2013-09-29 06:58:08 +10:00
|
|
|
} else {
|
2015-05-06 16:23:53 +10:00
|
|
|
co, err = DialTimeout(c.Net, a, c.dialTimeout())
|
2011-08-08 21:10:35 +10:00
|
|
|
}
|
2013-09-29 06:58:08 +10:00
|
|
|
if err != nil {
|
2013-01-29 06:30:13 +11:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
2013-09-29 06:58:08 +10:00
|
|
|
defer co.Close()
|
2015-05-06 16:23:53 +10:00
|
|
|
|
2013-09-29 20:21:18 +10:00
|
|
|
opt := m.IsEdns0()
|
2014-07-31 18:48:26 +10:00
|
|
|
// If EDNS0 is used use that for size.
|
2013-09-29 20:21:18 +10:00
|
|
|
if opt != nil && opt.UDPSize() >= MinMsgSize {
|
|
|
|
co.UDPSize = opt.UDPSize()
|
|
|
|
}
|
2014-07-31 18:48:26 +10:00
|
|
|
// Otherwise use the client's configured UDP size.
|
2014-02-15 09:14:41 +11:00
|
|
|
if opt == nil && c.UDPSize >= MinMsgSize {
|
|
|
|
co.UDPSize = c.UDPSize
|
|
|
|
}
|
2015-05-06 16:23:53 +10:00
|
|
|
|
|
|
|
co.SetReadDeadline(time.Now().Add(c.readTimeout()))
|
|
|
|
co.SetWriteDeadline(time.Now().Add(c.writeTimeout()))
|
|
|
|
|
2013-09-29 20:21:18 +10:00
|
|
|
co.TsigSecret = c.TsigSecret
|
|
|
|
if err = co.WriteMsg(m); err != nil {
|
2013-01-29 06:30:13 +11:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
2013-09-29 20:21:18 +10:00
|
|
|
r, err = co.ReadMsg()
|
2015-05-06 15:56:42 +10:00
|
|
|
if err == nil && r.Id != m.Id {
|
|
|
|
err = ErrId
|
|
|
|
}
|
2013-09-29 06:58:08 +10:00
|
|
|
return r, co.rtt, err
|
2011-08-08 21:10:35 +10:00
|
|
|
}
|
|
|
|
|
2013-09-29 20:21:18 +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.
|
2013-09-29 20:21:18 +10:00
|
|
|
func (co *Conn) ReadMsg() (*Msg, error) {
|
2011-04-18 05:56:40 +10:00
|
|
|
var p []byte
|
|
|
|
m := new(Msg)
|
2013-09-29 06:58:08 +10:00
|
|
|
if _, ok := co.Conn.(*net.TCPConn); ok {
|
2011-04-19 02:29:46 +10:00
|
|
|
p = make([]byte, MaxMsgSize)
|
2013-09-29 06:58:08 +10:00
|
|
|
} else {
|
2015-06-17 11:21:22 +10:00
|
|
|
if co.UDPSize > MinMsgSize {
|
2013-09-29 20:21:18 +10:00
|
|
|
p = make([]byte, co.UDPSize)
|
|
|
|
} else {
|
|
|
|
p = make([]byte, MinMsgSize)
|
|
|
|
}
|
2011-04-19 06:08:12 +10:00
|
|
|
}
|
2013-09-29 06:58:08 +10:00
|
|
|
n, err := co.Read(p)
|
2012-08-29 05:36:25 +10:00
|
|
|
if err != nil && n == 0 {
|
2011-04-19 06:08:12 +10:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p = p[:n]
|
2012-10-10 06:17:54 +11:00
|
|
|
if err := m.Unpack(p); err != nil {
|
|
|
|
return nil, err
|
2011-04-19 06:08:12 +10:00
|
|
|
}
|
2013-09-29 06:58:08 +10:00
|
|
|
co.rtt = time.Since(co.t)
|
2012-08-25 19:24:01 +10:00
|
|
|
if t := m.IsTsig(); t != nil {
|
2013-09-29 20:21:18 +10:00
|
|
|
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
|
|
|
}
|
2013-09-29 19:22:27 +10:00
|
|
|
return m, err
|
2011-04-18 05:56:40 +10:00
|
|
|
}
|
2011-04-16 07:55:27 +10:00
|
|
|
|
2013-09-29 20:21:18 +10:00
|
|
|
// Read implements the net.Conn read method.
|
2013-09-29 06:58:08 +10:00
|
|
|
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
|
2011-04-18 05:56:40 +10:00
|
|
|
}
|
2012-08-29 05:36:25 +10:00
|
|
|
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])
|
2013-01-12 05:20:49 +11:00
|
|
|
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])
|
2013-01-12 05:20:49 +11:00
|
|
|
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 {
|
2013-01-12 05:20:49 +11:00
|
|
|
return i, err
|
2012-11-23 01:06:21 +11:00
|
|
|
}
|
2013-01-12 05:20:49 +11:00
|
|
|
i += j
|
2011-04-19 06:08:12 +10:00
|
|
|
}
|
2013-01-12 05:20:49 +11:00
|
|
|
n = i
|
2013-10-13 04:32:53 +11:00
|
|
|
return n, err
|
|
|
|
}
|
2014-02-15 09:47:53 +11:00
|
|
|
// 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
|
|
|
}
|
2013-01-12 05:20:49 +11: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.
|
2013-09-29 20:21:18 +10:00
|
|
|
func (co *Conn) WriteMsg(m *Msg) (err error) {
|
2012-03-03 07:19:37 +11:00
|
|
|
var out []byte
|
2012-08-25 19:24:01 +10:00
|
|
|
if t := m.IsTsig(); t != nil {
|
2012-03-03 07:19:37 +11:00
|
|
|
mac := ""
|
2013-09-29 20:21:18 +10:00
|
|
|
if _, ok := co.TsigSecret[t.Hdr.Name]; !ok {
|
2011-09-11 00:50:27 +10:00
|
|
|
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)
|
2013-09-29 20:21:18 +10:00
|
|
|
// 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 {
|
2012-10-10 06:17:54 +11:00
|
|
|
out, err = m.Pack()
|
2012-10-16 05:00:49 +11:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2011-04-16 07:55:27 +10:00
|
|
|
}
|
2013-09-29 06:58:08 +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
|
|
|
|
}
|
|
|
|
|
2013-09-29 20:21:18 +10:00
|
|
|
// Write implements the net.Conn Write method.
|
2013-09-29 06:58:08 +10:00
|
|
|
func (co *Conn) Write(p []byte) (n int, err error) {
|
|
|
|
if t, ok := co.Conn.(*net.TCPConn); ok {
|
2014-01-04 22:17:13 +11:00
|
|
|
lp := len(p)
|
|
|
|
if lp < 2 {
|
2011-04-16 07:55:27 +10:00
|
|
|
return 0, io.ErrShortBuffer
|
|
|
|
}
|
2014-01-04 19:30:08 +11:00
|
|
|
if lp > MaxMsgSize {
|
|
|
|
return 0, &Error{err: "message too large"}
|
2011-04-16 07:55:27 +10:00
|
|
|
}
|
2014-01-04 19:30:08 +11: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-16 07:55:27 +10:00
|
|
|
}
|
2013-09-29 06:58:08 +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
|
|
|
|
2013-11-01 20:40:44 +11:00
|
|
|
// Dial connects to the address on the named network.
|
|
|
|
func Dial(network, address string) (conn *Conn, err error) {
|
2013-11-01 20:50:55 +11:00
|
|
|
conn = new(Conn)
|
2013-11-01 20:40:44 +11:00
|
|
|
conn.Conn, err = net.Dial(network, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2015-02-19 20:58:33 +11:00
|
|
|
// DialTimeout acts like Dial but takes a timeout.
|
2013-11-01 20:40:44 +11:00
|
|
|
func DialTimeout(network, address string, timeout time.Duration) (conn *Conn, err error) {
|
2013-11-01 20:50:55 +11:00
|
|
|
conn = new(Conn)
|
2013-11-01 20:40:44 +11:00
|
|
|
conn.Conn, err = net.DialTimeout(network, address, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|