Fix all the examples
This commit is contained in:
parent
86b08d4a0e
commit
d36ee08216
28
client.go
28
client.go
|
@ -230,15 +230,15 @@ func (c *Client) Do(m *Msg, a string) {
|
||||||
c.QueryChan <- &Request{Client: c, Addr: a, Request: m}
|
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.
|
// address contained in a.
|
||||||
func (c *Client) ExchangeBuffer(inbuf []byte, a string, outbuf []byte) (n int, err error) {
|
func (c *Client) exchangeBuffer(inbuf []byte, a string, outbuf []byte) (n int, w *reply, err error) {
|
||||||
w := new(reply)
|
w = new(reply)
|
||||||
w.client = c
|
w.client = c
|
||||||
w.addr = a
|
w.addr = a
|
||||||
if c.Hijacked == nil {
|
if c.Hijacked == nil {
|
||||||
if err = w.Dial(); err != nil {
|
if err = w.Dial(); err != nil {
|
||||||
return 0, err
|
return 0, w, err
|
||||||
}
|
}
|
||||||
defer w.Close()
|
defer w.Close()
|
||||||
}
|
}
|
||||||
|
@ -247,23 +247,23 @@ func (c *Client) ExchangeBuffer(inbuf []byte, a string, outbuf []byte) (n int, e
|
||||||
}
|
}
|
||||||
w.t = time.Now()
|
w.t = time.Now()
|
||||||
if n, err = w.writeClient(inbuf); err != nil {
|
if n, err = w.writeClient(inbuf); err != nil {
|
||||||
return 0, err
|
return 0, w, err
|
||||||
}
|
}
|
||||||
if n, err = w.readClient(outbuf); err != nil {
|
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)
|
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
|
// Exchange performs an synchronous query. It sends the message m to the address
|
||||||
// contained in a and waits for an reply.
|
// 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 n int
|
||||||
|
var w *reply
|
||||||
out, ok := m.Pack()
|
out, ok := m.Pack()
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, ErrPack
|
return nil, 0, ErrPack
|
||||||
}
|
}
|
||||||
var in []byte
|
var in []byte
|
||||||
switch c.Net {
|
switch c.Net {
|
||||||
|
@ -278,14 +278,14 @@ func (c *Client) Exchange(m *Msg, a string) (r *Msg, err error) {
|
||||||
}
|
}
|
||||||
in = make([]byte, size)
|
in = make([]byte, size)
|
||||||
}
|
}
|
||||||
if n, err = c.ExchangeBuffer(out, a, in); err != nil {
|
if n, w, err = c.exchangeBuffer(out, a, in); err != nil {
|
||||||
return nil, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
r = new(Msg)
|
r = new(Msg)
|
||||||
if ok := r.Unpack(in[:n]); !ok {
|
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
|
// Dial connects to the address addr for the network set in c.Net
|
||||||
|
|
|
@ -27,14 +27,14 @@ func main() {
|
||||||
}
|
}
|
||||||
for _, a := range addr {
|
for _, a := range addr {
|
||||||
m.Question[0] = dns.Question{"version.bind.", dns.TypeTXT, dns.ClassCHAOS}
|
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 {
|
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}
|
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 {
|
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])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
// returns a fingerprint.
|
// returns a fingerprint.
|
||||||
func probe(c *dns.Client, addr string, f *fingerprint) *fingerprint {
|
func probe(c *dns.Client, addr string, f *fingerprint) *fingerprint {
|
||||||
m := f.msg()
|
m := f.msg()
|
||||||
r, err := c.Exchange(m, addr)
|
r, _, err := c.Exchange(m, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errorToFingerprint(err)
|
return errorToFingerprint(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ func main() {
|
||||||
m.SetEdns0(2048, true)
|
m.SetEdns0(2048, true)
|
||||||
|
|
||||||
c := dns.NewClient()
|
c := dns.NewClient()
|
||||||
r, _ := c.Exchange(m, conf.Servers[0] + ":" + conf.Port)
|
r, _, _ := c.Exchange(m, conf.Servers[0] + ":" + conf.Port)
|
||||||
if r == nil {
|
if r == nil {
|
||||||
fmt.Printf("*** no answer received for %s\n", os.Args[1])
|
fmt.Printf("*** no answer received for %s\n", os.Args[1])
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -22,7 +22,7 @@ func main() {
|
||||||
m.MsgHdr.RecursionDesired = true
|
m.MsgHdr.RecursionDesired = true
|
||||||
|
|
||||||
// Simple sync query, nothing fancy
|
// 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 {
|
if err != nil {
|
||||||
fmt.Printf("%s\n", err.Error())
|
fmt.Printf("%s\n", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -27,8 +27,6 @@ func q(w dns.RequestWriter, m *dns.Msg) {
|
||||||
if w.TsigStatus() != nil {
|
if w.TsigStatus() != nil {
|
||||||
fmt.Printf(";; Couldn't verify TSIG signature: %s\n", w.TsigStatus().Error())
|
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)
|
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 := new(dns.Msg)
|
||||||
m.SetQuestion(name, dns.TypeDNSKEY)
|
m.SetQuestion(name, dns.TypeDNSKEY)
|
||||||
m.SetEdns0(4096, true)
|
m.SetEdns0(4096, true)
|
||||||
r, err := c.Exchange(m, server)
|
r, _, err := c.Exchange(m, server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue