dns/client.go

238 lines
5.6 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
)
// A Conn represents a connection (which may be short lived) to a DNS server.
2013-09-29 05:31:29 +10:00
type Conn struct {
net.Conn
rtt time.Duration
t time.Time
requestMAC 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)
ReadTimeout time.Duration // the net.Conn.SetReadTimeout value for new connections (ns), defaults to 2 * 1e9
WriteTimeout time.Duration // the net.Conn.SetWriteTimeout value for new connections (ns), defaults to 2 * 1e9
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, 5*1e9)
if err != nil {
return nil, err
}
defer co.Close()
if err = co.WriteMsg(m, nil); err != nil {
return nil, err
}
r, err = co.ReadMsg(nil)
return r, err
2013-09-29 05:31:29 +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")
//
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)
if c.Net == "" {
co.Conn, err = net.DialTimeout("udp", a, 5*1e9)
} else {
co.Conn, err = net.DialTimeout(c.Net, a, 5*1e9)
2011-08-04 19:27:56 +10:00
}
if err != nil {
return nil, 0, err
2011-08-08 21:10:35 +10:00
}
defer co.Close()
if err = co.WriteMsg(m, c.TsigSecret); err != nil {
2013-01-29 06:30:13 +11:00
return nil, 0, err
}
r, err = co.ReadMsg(c.TsigSecret)
return r, co.rtt, err
2011-08-08 21:10:35 +10:00
}
// Add bufsize
func (co *Conn) ReadMsg(tsigSecret map[string]string) (*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 {
// OPT! TODO(miek): needs function change
p = make([]byte, DefaultMsgSize)
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 := tsigSecret[t.Hdr.Name]; !ok {
return m, ErrSecret
}
// Need to work on the original message p, as that was used to calculate the tsig.
err = TsigVerify(p, tsigSecret[t.Hdr.Name], co.requestMAC, false)
}
return m, err
}
2011-04-16 07:55:27 +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
}
if len(p) < 2 {
2012-05-26 18:24:47 +10:00
return 0, io.ErrShortBuffer
}
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
}
n, err = t.Read(p[:l])
if err != nil {
return n, err
}
i := n
for i < int(l) {
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
return n, err
}
// assume udp connection
n, _, err = co.Conn.(*net.UDPConn).ReadFromUDP(p)
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
}
2012-08-07 04:34:09 +10:00
// send sends a dns msg to the address specified in w.
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, tsigSecret map[string]string) (err error) {
2012-03-03 07:19:37 +11:00
var out []byte
if t := m.IsTsig(); t != nil {
mac := ""
if _, ok := tsigSecret[t.Hdr.Name]; !ok {
return ErrSecret
}
out, mac, err = TsigGenerate(m, tsigSecret[t.Hdr.Name], co.requestMAC, false)
// Set for the next read
co.requestMAC = mac
} 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
}
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
/*
2012-05-26 18:24:47 +10:00
func setTimeouts(w *reply) {
2012-08-17 16:45:26 +10:00
if w.client.ReadTimeout == 0 {
2012-05-26 18:24:47 +10:00
w.conn.SetReadDeadline(time.Now().Add(2 * 1e9))
} else {
2012-08-17 16:45:26 +10:00
w.conn.SetReadDeadline(time.Now().Add(w.client.ReadTimeout))
2012-05-26 18:24:47 +10:00
}
2012-08-17 16:45:26 +10:00
if w.client.WriteTimeout == 0 {
2012-05-26 18:24:47 +10:00
w.conn.SetWriteDeadline(time.Now().Add(2 * 1e9))
} else {
2012-08-17 16:45:26 +10:00
w.conn.SetWriteDeadline(time.Now().Add(w.client.WriteTimeout))
2012-05-26 18:24:47 +10:00
}
}
*/