func main() { if len(os.Args) != 2 { fmt.Printf("%s DOMAIN\n", os.Args[0]) os.Exit(1) } // Error checking config, _ := dns.ClientConfigFromFile("/etc/resolv.conf") c := dns.NewClient() m := new(dns.Msg) m.SetQuestion(os.Args[1], dns.TypeMX) m.MsgHdr.RecursionDesired = true // Simple sync query, nothing fancy r := c.Exchange(m, config.Servers[0]) if r == nil { os.Exit(1) } if r.Rcode != dns.RcodeSuccess { fmt.Printf(" *** invalid answer name %s after MX query for %s\n", os.Args[1], os.Args[1]) os.Exit(1) } // Stuff must be in the answer section for _, a := range r.Answer { fmt.Printf("%v\n", a) } }
func main() { if len(os.Args) != 2 { fmt.Printf("%s NAMESERVER\n", os.Args[0]) os.Exit(1) } conf, _ := dns.ClientConfigFromFile("/etc/resolv.conf") m := new(dns.Msg) m.Question = make([]dns.Question, 1) c := dns.NewClient() addr := addresses(conf, c, os.Args[1]) if len(addr) == 0 { fmt.Printf("No address found for %s\n", os.Args[1]) os.Exit(1) } for _, a := range addr { m.Question[0] = dns.Question{"version.bind.", dns.TypeTXT, dns.ClassCHAOS} in := c.Exchange(m, a) if in != nil && in.Answer != nil { fmt.Printf("%v\n", in.Answer[0]) } m.Question[0] = dns.Question{"hostname.bind.", dns.TypeTXT, dns.ClassCHAOS} in = c.Exchange(m, a) if in != nil && in.Answer != nil { fmt.Printf("%v\n", in.Answer[0]) } } }
func main() { client := dns.NewClient() fmt.Println("> dig liulonnie.net @74.220.195.131") resp, err := client.Query( dns.ParseIP("74.220.195.131"), // ns1.hostmonster.com dns.Domain("liulonnie.net"), dns.A, ) if err != nil { fmt.Println(err) return } fmt.Println(resp.Msg) fmt.Println("> dig -recursive google.com a") client.RecurQuery(dns.Domain("google.com"), dns.A, os.Stdout) fmt.Println("> dig -recursive liulonnie.net a") client.RecurQuery(dns.Domain("liulonnie.net"), dns.A, os.Stdout) fmt.Println("(do it again to see if caching works)") fmt.Println("> dig -recursive liulonnie.net a") client.RecurQuery(dns.Domain("liulonnie.net"), dns.A, os.Stdout) }
func main() { var serial *int = flag.Int("serial", 0, "Perform an IXFR with the given serial") var nameserver *string = flag.String("ns", "127.0.0.1:53", "Query this nameserver") // var secret *string = flag.String("secret", "", "Use this secret for TSIG") flag.Parse() zone := flag.Arg(flag.NArg() - 1) client := dns.NewClient() client.Net = "tcp" m := new(dns.Msg) if *serial > 0 { m.SetIxfr(zone, uint32(*serial)) } else { m.SetAxfr(zone) } if err := client.XfrReceive(m, *nameserver); err == nil { for r := range client.ReplyChan { if r.Error != nil { if r.Error == dns.ErrXfrLast { fmt.Printf("%v\n", r.Reply) } break } fmt.Printf("%v\n", r.Reply) } } else { fmt.Printf("Error %v\n", err) } }
func main() { conf, err := dns.ClientConfigFromFile("/etc/resolv.conf") if len(os.Args) != 2 || err != nil { fmt.Printf("%s DOMAIN\n", os.Args[0]) os.Exit(1) } m := new(dns.Msg) m.SetQuestion(dns.Fqdn(os.Args[1]), dns.TypeDNSKEY) m.SetEdns0(2048, true) c := dns.NewClient() 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) } if r.Rcode != dns.RcodeSuccess { fmt.Printf(" *** invalid answer name %s after DNSKEY query for %s\n", os.Args[1], os.Args[1]) os.Exit(1) } for _, k := range r.Answer { if key, ok := k.(*dns.RR_DNSKEY); ok { key.Hdr.Ttl = 0 for _, alg := range []int{dns.SHA1, dns.SHA256, dns.SHA384} { ds := key.ToDS(alg) fmt.Printf("%v; %d\n", ds, key.Flags) } } } }
func main() { listen := flag.String("listen", "127.0.0.1:8053", "set the listener address") server := flag.String("server", "127.0.0.1:53", "remote server address(es), seperate with commas") verbose = flag.Bool("verbose", false, "Print packet as it flows through") // cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file") flag.Usage = func() { flag.PrintDefaults() } flag.Parse() /* if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } */ clients := strings.Split(*server, ",") qr = make([]*FunkClient, len(clients)) for i, ra := range clients { qr[i] = new(FunkClient) qr[i].Client = dns.NewClient() qr[i].Addr = ra } f = NewFunkenSturm() ok := f.Setup() if !ok { println("Setup failed") return } dns.HandleFunc(".", serve) go listenAndServe(*listen, "tcp") go listenAndServe(*listen, "udp") forever: for { select { case <-signal.Incoming: println("Signal received, stopping") break forever } } }
func main() { conf, err := dns.ClientConfigFromFile("/etc/resolv.conf") if len(os.Args) != 2 || err != nil { fmt.Printf("%s DOMAIN\n", os.Args[0]) os.Exit(1) } m := new(dns.Msg) m.SetQuestion(os.Args[1], dns.TypeDNSKEY) // Set EDNS0's Do bit e := new(dns.RR_OPT) e.Hdr.Name = "." e.Hdr.Rrtype = dns.TypeOPT e.SetUDPSize(2048) e.SetDo() m.Extra = append(m.Extra, e) c := dns.NewClient() r := c.Exchange(m, conf.Servers[0]) if r == nil { fmt.Printf("*** no answer received for %s\n", os.Args[1]) os.Exit(1) } if r.Rcode != dns.RcodeSuccess { fmt.Printf(" *** invalid answer name %s after DNSKEY query for %s\n", os.Args[1], os.Args[1]) os.Exit(1) } // Stuff must be in the answer section, check len(r.Answer) for _, k := range r.Answer { // Foreach key would need to provide a DS records, both sha1 and sha256 if key, ok := k.(*dns.RR_DNSKEY); ok { key.Hdr.Ttl = 0 ds := key.ToDS(dns.SHA1) fmt.Printf("%v\n", ds) ds = key.ToDS(dns.SHA256) fmt.Printf("%v\n", ds) ds = key.ToDS(dns.SHA384) fmt.Printf("%v\n", ds) } } }
// Get the key from the DNS (uses the local resolver) and return them. // If nothing is found we return nil func getKey(name string, keytag uint16, server string, tcp bool) *dns.RR_DNSKEY { c := dns.NewClient() if tcp { c.Net = "tcp" } m := new(dns.Msg) m.SetQuestion(name, dns.TypeDNSKEY) r, err := c.Exchange(m, server) if err != nil { return nil } for _, k := range r.Answer { if k1, ok := k.(*dns.RR_DNSKEY); ok { if k1.KeyTag() == keytag { return k1 } } } return nil }
func startParse(addr string) { l := &lexer{ addr: addr, client: dns.NewClient(), fp: new(fingerprint), items: make(chan item), state: dnsAlive, debug: true, } l.run() // Not completely sure about this code.. for { item := <-l.items fmt.Printf("{%s %s}\n", itemString[item.typ], item.val) if l.state == nil { break } } }
func main() { var serial *int = flag.Int("serial", 0, "Perform an IXFR with the given serial") var nameserver *string = flag.String("ns", "127.0.0.1:53", "Query this nameserver") flag.Parse() zone := flag.Arg(flag.NArg() - 1) client := dns.NewClient() client.Net = "tcp" m := new(dns.Msg) if *serial > 0 { m.SetIxfr(zone, uint32(*serial)) } else { m.SetAxfr(zone) } axfr, err := client.XfrReceive(m, *nameserver) if err != nil { println(err.String()) } for _, v := range axfr { fmt.Printf("%v\n", v) } }
func main() { serial := flag.Int("serial", 0, "Perform an IXFR with the given serial") nameserver := flag.String("ns", "127.0.0.1:53", "Query this nameserver") tsig := flag.String("tsig", "", "request tsig with key: name:key (only hmac-md5)") flag.Parse() zone := flag.Arg(flag.NArg() - 1) client := dns.NewClient() client.Net = "tcp" m := new(dns.Msg) if *serial > 0 { m.SetIxfr(zone, uint32(*serial)) } else { m.SetAxfr(zone) } if *tsig != "" { a := strings.SplitN(*tsig, ":", 2) name, secret := a[0], a[1] client.TsigSecret = map[string]string{name: secret} m.SetTsig(name, dns.HmacMD5, 300, m.MsgHdr.Id, time.Now().Unix()) } if err := client.XfrReceive(m, *nameserver); err == nil { for r := range client.ReplyChan { if r.Error != nil { if r.Error == dns.ErrXfrLast { fmt.Printf("%v\n", r.Reply) } break } fmt.Printf("%v\n", r.Reply) } } else { fmt.Printf("Error %v\n", err) } }
func main() { dnssec := flag.Bool("dnssec", false, "request DNSSEC records") query := flag.Bool("question", false, "show question") short := flag.Bool("short", false, "abbreviate long DNSSEC records") check := flag.Bool("check", false, "check internal DNSSEC consistency") anchor := flag.String("anchor", "", "use the DNSKEY in this file for checking consistency") port := flag.Int("port", 53, "port number to use") aa := flag.Bool("aa", false, "set AA flag in query") ad := flag.Bool("ad", false, "set AD flag in query") cd := flag.Bool("cd", false, "set CD flag in query") rd := flag.Bool("rd", true, "unset RD flag in query") tcp := flag.Bool("tcp", false, "TCP mode") nsid := flag.Bool("nsid", false, "ask for NSID") flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s [@server] [qtype] [qclass] [name ...]\n", os.Args[0]) flag.PrintDefaults() } conf, _ := dns.ClientConfigFromFile("/etc/resolv.conf") nameserver := "@" + conf.Servers[0] qtype := uint16(0) qclass := uint16(dns.ClassINET) // Default qclass var qname []string flag.Parse() if *anchor != "" { f, err := os.Open(*anchor) if err != nil { fmt.Fprintf(os.Stderr, "Failure to open %s: %s\n", *anchor, err.Error()) } r, err := dns.ReadRR(f, *anchor) if err != nil { fmt.Fprintf(os.Stderr, "Failure to read an RR from %s: %s\n", *anchor, err.Error()) } if k, ok := r.(*dns.RR_DNSKEY); !ok { fmt.Fprintf(os.Stderr, "No DNSKEY read from %s\n", *anchor) } else { dnskey = k } } Flags: for i := 0; i < flag.NArg(); i++ { // If it starts with @ it is a nameserver if flag.Arg(i)[0] == '@' { nameserver = flag.Arg(i) continue Flags } // First class, then type, to make ANY queries possible // And if it looks like type, it is a type if k, ok := dns.Str_rr[strings.ToUpper(flag.Arg(i))]; ok { qtype = k continue Flags } // If it looks like a class, it is a class if k, ok := dns.Str_class[strings.ToUpper(flag.Arg(i))]; ok { qclass = k continue Flags } // If it starts with TYPExxx it is unknown rr if strings.HasPrefix(flag.Arg(i), "TYPE") { i, e := strconv.Atoi(string([]byte(flag.Arg(i))[4:])) if e == nil { qtype = uint16(i) continue Flags } } // Anything else is a qname qname = append(qname, flag.Arg(i)) } if len(qname) == 0 { qname = make([]string, 1) qname[0] = "." qtype = dns.TypeNS } if qtype == 0 { qtype = dns.TypeA } nameserver = string([]byte(nameserver)[1:]) // chop off @ nameserver += ":" + strconv.Itoa(*port) // ipv6 todo // We use the async query handling, just to show how // it is to be used. dns.HandleQueryFunc(".", q) dns.ListenAndQuery(nil, nil) c := dns.NewClient() if *tcp { c.Net = "tcp" } m := new(dns.Msg) m.MsgHdr.Authoritative = *aa m.MsgHdr.AuthenticatedData = *ad m.MsgHdr.CheckingDisabled = *cd m.MsgHdr.RecursionDesired = *rd m.Question = make([]dns.Question, 1) if *dnssec || *nsid { o := new(dns.RR_OPT) o.Hdr.Name = "." o.Hdr.Rrtype = dns.TypeOPT if *dnssec { o.SetDo() o.SetUDPSize(dns.DefaultMsgSize) } if *nsid { o.SetNsid("") } m.Extra = append(m.Extra, o) } for _, v := range qname { m.Question[0] = dns.Question{v, qtype, qclass} m.Id = dns.Id() if *query { fmt.Printf("%s\n", m.String()) } c.Do(m, nameserver) } i := 0 forever: for { select { case r := <-dns.DefaultReplyChan: if r.Reply != nil { if r.Reply.Rcode == dns.RcodeSuccess { if r.Request.Id != r.Reply.Id { fmt.Printf("Id mismatch\n") } } if *check { sigCheck(r.Reply, nameserver, *tcp) nsecCheck(r.Reply) } if *short { r.Reply = shortMsg(r.Reply) } fmt.Printf("%v", r.Reply) } i++ if i == len(qname) { break forever } } } }
func main() { var dnssec *bool = flag.Bool("dnssec", false, "request DNSSEC records") var query *bool = flag.Bool("question", false, "show question") var short *bool = flag.Bool("short", false, "abbriate long DNSKEY and RRSIG RRs") var aa *bool = flag.Bool("aa", false, "set AA flag in query") var ad *bool = flag.Bool("ad", false, "set AD flag in query") var cd *bool = flag.Bool("cd", false, "set CD flag in query") var rd *bool = flag.Bool("rd", true, "unset RD flag in query") var tcp *bool = flag.Bool("tcp", false, "TCP mode") var nsid *bool = flag.Bool("nsid", false, "ask for NSID") flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s [@server(:port)] [qtype] [qclass] [name ...]\n", os.Args[0]) flag.PrintDefaults() } // Need to think about it... Config conf, _ := dns.ClientConfigFromFile("/etc/resolv.conf") nameserver := "@" + conf.Servers[0] qtype := uint16(0) qclass := uint16(dns.ClassINET) // Default qclass var qname []string flag.Parse() Flags: for i := 0; i < flag.NArg(); i++ { // If it starts with @ it is a nameserver if flag.Arg(i)[0] == '@' { nameserver = flag.Arg(i) continue Flags } // First class, then type, to make ANY queries possible // And if it looks like type, it is a type for k, v := range dns.Rr_str { if v == strings.ToUpper(flag.Arg(i)) { qtype = k continue Flags } } // If it looks like a class, it is a class for k, v := range dns.Class_str { if v == strings.ToUpper(flag.Arg(i)) { qclass = k continue Flags } } // If it starts with TYPExxx it is unknown rr if strings.HasPrefix(flag.Arg(i), "TYPE") { i, e := strconv.Atoi(string([]byte(flag.Arg(i))[4:])) if e == nil { qtype = uint16(i) continue Flags } } // Anything else is a qname qname = append(qname, flag.Arg(i)) } if len(qname) == 0 { qname = make([]string, 1) qname[0] = "." qtype = dns.TypeNS } if qtype == 0 { qtype = dns.TypeA } nameserver = string([]byte(nameserver)[1:]) // chop off @ if !strings.HasSuffix(nameserver, ":53") { nameserver += ":53" } // ipv6 todo // We use the async query handling, just to show how // it is to be used. dns.HandleQueryFunc(".", q) dns.ListenAndQuery(nil, nil) c := dns.NewClient() if *tcp { c.Net = "tcp" } m := new(dns.Msg) m.MsgHdr.Authoritative = *aa m.MsgHdr.AuthenticatedData = *ad m.MsgHdr.CheckingDisabled = *cd m.MsgHdr.RecursionDesired = *rd m.Question = make([]dns.Question, 1) if *dnssec || *nsid { opt := dns.NewRR(dns.TypeOPT).(*dns.RR_OPT) opt.Hdr.Rrtype = 0 opt.SetDo() opt.SetVersion(0) opt.SetUDPSize(dns.DefaultMsgSize) if *nsid { opt.SetNsid("") } m.Extra = make([]dns.RR, 1) m.Extra[0] = opt } for _, v := range qname { m.Question[0] = dns.Question{v, qtype, qclass} m.Id = dns.Id() if *query { fmt.Printf("%s\n", m.String()) } c.Do(m, nameserver) } i := 0 forever: for { select { case r := <-dns.DefaultReplyChan: if r[1] != nil { if r[0].Id != r[1].Id { fmt.Printf("Id mismatch\n") } if *short { r[1] = shortMsg(r[1]) } fmt.Printf("%v", r[1]) } i++ if i == len(qname) { break forever } } } }