Skip to content

Instantly share code, notes, and snippets.

@andrewtj
Created October 21, 2016 20:44
Show Gist options
  • Save andrewtj/056ed6225898d652288f0d416eb3cc3f to your computer and use it in GitHub Desktop.
Save andrewtj/056ed6225898d652288f0d416eb3cc3f to your computer and use it in GitHub Desktop.
package main
import (
"log"
"strings"
"github.com/miekg/dns"
)
func main() {
qname := "github.com."
ns_path := []string{"g.root-servers.net."}
client := new(dns.Client)
q := new(dns.Msg).SetQuestion(qname, dns.TypeNS)
q.MsgHdr.RecursionDesired = false
var ns, extra []dns.RR
query_loop:
for {
q.Id = dns.Id()
target := ns_path[len(ns_path)-1]
r, _, err := client.Exchange(q, target+":53")
switch {
case err != nil:
log.Fatalf("error querying %s: %s", err, target)
case r.MsgHdr.Rcode != dns.RcodeSuccess:
log.Fatalf("unexpected rcode from %s:\n%s", target, r)
case r.MsgHdr.Authoritative:
log.Fatalf("ran into an authoritative server. problem with lookup logic?\n%s", r)
case len(r.Ns) == 0:
log.Fatalf("expected referral from %s:\n%s", target, r)
default:
ns, extra = r.Ns, r.Extra
if rr, ok := ns[0].(*dns.NS); ok {
if rr.Hdr.Name == qname {
break query_loop
}
ns_path = append(ns_path, rr.Ns)
} else {
log.Fatalf("expected an NS rr - message:\n%s", target, r)
}
}
}
glue := make(map[string][]string)
for _, rr := range extra {
switch rr := rr.(type) {
case *dns.A:
glue[rr.Hdr.Name] = append(glue[rr.Hdr.Name], rr.A.String())
case *dns.AAAA:
glue[rr.Hdr.Name] = append(glue[rr.Hdr.Name], rr.AAAA.String())
}
}
log.Printf("followed path: %s\n", strings.Join(ns_path, " -> "))
for _, rr := range ns {
if rr, ok := rr.(*dns.NS); ok {
if len(glue[rr.Ns]) == 0 {
log.Printf(" * %s <no glue supplied>\n", rr.Ns)
} else {
log.Printf(" * %s %v\n", rr.Ns, glue[rr.Ns])
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment