Fix all the examples

This commit is contained in:
Miek Gieben 2012-05-05 16:09:57 +02:00
parent 86b08d4a0e
commit d36ee08216
6 changed files with 22 additions and 24 deletions

View File

@ -230,15 +230,15 @@ func (c *Client) Do(m *Msg, a string) {
c.QueryChan <- &Request{Client: c, Addr: a, Request: m}
}
// ExchangeBuffer performs a synchronous query. It sends the buffer m to the
// exchangeBuffer performs a synchronous query. It sends the buffer m to the
// address contained in a.
func (c *Client) ExchangeBuffer(inbuf []byte, a string, outbuf []byte) (n int, err error) {
w := new(reply)
func (c *Client) exchangeBuffer(inbuf []byte, a string, outbuf []byte) (n int, w *reply, err error) {
w = new(reply)
w.client = c
w.addr = a
if c.Hijacked == nil {
if err = w.Dial(); err != nil {
return 0, err
return 0, w, err
}
defer w.Close()
}
@ -247,23 +247,23 @@ func (c *Client) ExchangeBuffer(inbuf []byte, a string, outbuf []byte) (n int, e
}
w.t = time.Now()
if n, err = w.writeClient(inbuf); err != nil {
return 0, err
return 0, w, err
}
if n, err = w.readClient(outbuf); err != nil {
return n, err
return n, w, err
}
// This rtt value isn't useful atm, need to return it somehow, TODO(mg)
w.rtt = time.Since(w.t)
return n, nil
return n, w, nil
}
// Exchange performs an synchronous query. It sends the message m to the address
// contained in a and waits for an reply.
func (c *Client) Exchange(m *Msg, a string) (r *Msg, err error) {
func (c *Client) Exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err error) {
var n int
var w *reply
out, ok := m.Pack()
if !ok {
return nil, ErrPack
return nil, 0, ErrPack
}
var in []byte
switch c.Net {
@ -278,14 +278,14 @@ func (c *Client) Exchange(m *Msg, a string) (r *Msg, err error) {
}
in = make([]byte, size)
}
if n, err = c.ExchangeBuffer(out, a, in); err != nil {
return nil, err
if n, w, err = c.exchangeBuffer(out, a, in); err != nil {
return nil, 0, err
}
r = new(Msg)
if ok := r.Unpack(in[:n]); !ok {
return nil, ErrUnpack
return nil, w.rtt, ErrUnpack
}
return r, nil
return r, w.rtt, nil
}
// Dial connects to the address addr for the network set in c.Net

View File

@ -27,14 +27,14 @@ func main() {
}
for _, a := range addr {
m.Question[0] = dns.Question{"version.bind.", dns.TypeTXT, dns.ClassCHAOS}
in, _ := c.Exchange(m, a)
in, rtt, _ := c.Exchange(m, a)
if in != nil && len(in.Answer) > 0 {
fmt.Printf("%v\n", in.Answer[0])
fmt.Printf("(time %.3d µs) %v\n", rtt/1e3, in.Answer[0])
}
m.Question[0] = dns.Question{"hostname.bind.", dns.TypeTXT, dns.ClassCHAOS}
in, _ = c.Exchange(m, a)
in, rtt, _ = c.Exchange(m, a)
if in != nil && len(in.Answer) > 0 {
fmt.Printf("%v\n", in.Answer[0])
fmt.Printf("(time %.3d µs) %v\n", rtt/1e3, in.Answer[0])
}
}
}

View File

@ -13,7 +13,7 @@ import (
// returns a fingerprint.
func probe(c *dns.Client, addr string, f *fingerprint) *fingerprint {
m := f.msg()
r, err := c.Exchange(m, addr)
r, _, err := c.Exchange(m, addr)
if err != nil {
return errorToFingerprint(err)
}

View File

@ -21,7 +21,7 @@ func main() {
m.SetEdns0(2048, true)
c := dns.NewClient()
r, _ := c.Exchange(m, conf.Servers[0] + ":" + conf.Port)
r, _, _ := c.Exchange(m, conf.Servers[0] + ":" + conf.Port)
if r == nil {
fmt.Printf("*** no answer received for %s\n", os.Args[1])
os.Exit(1)

View File

@ -22,7 +22,7 @@ func main() {
m.MsgHdr.RecursionDesired = true
// Simple sync query, nothing fancy
r, err := c.Exchange(m, config.Servers[0] + ":" + config.Port)
r, _, err := c.Exchange(m, config.Servers[0] + ":" + config.Port)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)

View File

@ -27,8 +27,6 @@ func q(w dns.RequestWriter, m *dns.Msg) {
if w.TsigStatus() != nil {
fmt.Printf(";; Couldn't verify TSIG signature: %s\n", w.TsigStatus().Error())
}
// Save the Rtt in the message
// It's better to extend dns.RequestWriter...
w.Write(r)
}
@ -343,7 +341,7 @@ func getKey(name string, keytag uint16, server string, tcp bool) *dns.RR_DNSKEY
m := new(dns.Msg)
m.SetQuestion(name, dns.TypeDNSKEY)
m.SetEdns0(4096, true)
r, err := c.Exchange(m, server)
r, _, err := c.Exchange(m, server)
if err != nil {
return nil
}