mirror of
https://github.com/miekg/dns.git
synced 2025-08-20 00:11:00 +02:00
Start the goroutine and return the channel to the caller. Only use 1 channel, a nil message signals the end of the routine. Still need a good name for the MsgErr
51 lines
993 B
Go
51 lines
993 B
Go
package main
|
|
|
|
import (
|
|
"dns"
|
|
"time"
|
|
"fmt"
|
|
)
|
|
|
|
const (
|
|
NLOOP = 5
|
|
)
|
|
|
|
func main() {
|
|
res := new(dns.Resolver)
|
|
ch := dns.NewQuerier(res)
|
|
|
|
// configure the resolver
|
|
res.Servers = []string{"192.168.1.2"}
|
|
res.Timeout = 2
|
|
res.Attempts = 1
|
|
|
|
// Setup done, now for some real work
|
|
// Create a new message
|
|
m := new(dns.Msg)
|
|
m.MsgHdr.Recursion_desired = true //only set this bit
|
|
m.Question = make([]dns.Question, 1)
|
|
|
|
for i:=0; i< NLOOP; i++ {
|
|
// ask something
|
|
m.Question[0] = dns.Question{"miek.nl", dns.TypeSOA, dns.ClassINET}
|
|
ch <- dns.MsgErr{m, nil}
|
|
|
|
// wait for an reply
|
|
in := <-ch
|
|
fmt.Printf("%v\n", in.M)
|
|
|
|
m.Question[0] = dns.Question{"a.miek.nl", dns.TypeTXT, dns.ClassINET}
|
|
ch <- dns.MsgErr{m, nil}
|
|
in = <-ch
|
|
fmt.Printf("%v\n", in.M)
|
|
|
|
m.Question[0] = dns.Question{"miek.nl", dns.TypeTXT, dns.ClassINET}
|
|
ch <- dns.MsgErr{m, nil}
|
|
in = <-ch
|
|
fmt.Printf("%v\n", in.M)
|
|
}
|
|
ch <- dns.MsgErr{nil, nil}
|
|
|
|
time.Sleep(2.0e9) // wait for Go routine to do something
|
|
}
|