]> Cypherpunks repositories - gostls13.git/commitdiff
net: add LookupNS(domain string)
authorStephen McQuay <stephen@mcquay.me>
Thu, 18 Oct 2012 06:39:04 +0000 (15:39 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Thu, 18 Oct 2012 06:39:04 +0000 (15:39 +0900)
Fixes #4224.

R=golang-dev, dave, minux.ma, mikioh.mikioh, alex.brainman, rsc, herbert.fischer
CC=golang-dev
https://golang.org/cl/6675043

src/pkg/net/dnsclient.go
src/pkg/net/lookup.go
src/pkg/net/lookup_plan9.go
src/pkg/net/lookup_test.go
src/pkg/net/lookup_unix.go
src/pkg/net/lookup_windows.go

index e69cb3188bcce2f18717036c002ccdc70a135251..a7089d04a598e28a4bf6660237cc70330f3ae224 100644 (file)
@@ -244,3 +244,8 @@ func (s byPref) sort() {
        }
        sort.Sort(s)
 }
+
+// An NS represents a single DNS NS record.
+type NS struct {
+       Host string
+}
index 3a44e528eb22dd8f1d7bc80fff1c0b81dee7c547..533b3511a225b009cd85993c874828523a92a556 100644 (file)
@@ -47,6 +47,11 @@ func LookupMX(name string) (mx []*MX, err error) {
        return lookupMX(name)
 }
 
+// LookupNS returns the DNS NS records for the given domain name.
+func LookupNS(name string) (ns []*NS, err error) {
+       return lookupNS(name)
+}
+
 // LookupTXT returns the DNS TXT records for the given domain name.
 func LookupTXT(name string) (txt []string, err error) {
        return lookupTXT(name)
index 2c698304b21d790e79f25bb337de30fa533c6c05..ae7cf794216b5516ecf1475da5f6dca24388c42a 100644 (file)
@@ -201,6 +201,21 @@ func lookupMX(name string) (mx []*MX, err error) {
        return
 }
 
+func lookupNS(name string) (ns []*NS, err error) {
+       lines, err := queryDNS(name, "ns")
+       if err != nil {
+               return
+       }
+       for _, line := range lines {
+               f := getFields(line)
+               if len(f) < 4 {
+                       continue
+               }
+               ns = append(ns, &NS{f[3]})
+       }
+       return
+}
+
 func lookupTXT(name string) (txt []string, err error) {
        lines, err := queryDNS(name, "txt")
        if err != nil {
index 84f089e86970ffbc923e9f2a567906cdba567f4d..990ade9e2104243901c6204ecddd7e6c2bc8406b 100644 (file)
@@ -52,6 +52,20 @@ func TestGmailMX(t *testing.T) {
        }
 }
 
+func TestGmailNS(t *testing.T) {
+       if testing.Short() || !*testExternal {
+               t.Logf("skipping test to avoid external network")
+               return
+       }
+       ns, err := LookupNS("gmail.com")
+       if err != nil {
+               t.Errorf("failed: %s", err)
+       }
+       if len(ns) == 0 {
+               t.Errorf("no results")
+       }
+}
+
 func TestGmailTXT(t *testing.T) {
        if testing.Short() || !*testExternal {
                t.Logf("skipping test to avoid external network")
index d500a1240d49de8993bba7fd0e29c65d2509e4e1..fa98eed5f2675c7c42f1adccd868074f232a1448 100644 (file)
@@ -119,6 +119,19 @@ func lookupMX(name string) (mx []*MX, err error) {
        return
 }
 
+func lookupNS(name string) (ns []*NS, err error) {
+       _, records, err := lookup(name, dnsTypeNS)
+       if err != nil {
+               return
+       }
+       ns = make([]*NS, len(records))
+       for i, r := range records {
+               r := r.(*dnsRR_NS)
+               ns[i] = &NS{r.Ns}
+       }
+       return
+}
+
 func lookupTXT(name string) (txt []string, err error) {
        _, records, err := lookup(name, dnsTypeTXT)
        if err != nil {
index 99783e975679634bea09e95ce99c7996d23749d4..2a8d01ff40a4596a8f7aa6cc2b9d86dc198c2bc8 100644 (file)
@@ -129,6 +129,21 @@ func lookupMX(name string) (mx []*MX, err error) {
        return mx, nil
 }
 
+func lookupNS(name string) (ns []*NS, err error) {
+       var r *syscall.DNSRecord
+       e := syscall.DnsQuery(name, syscall.DNS_TYPE_NS, 0, nil, &r, nil)
+       if e != nil {
+               return nil, os.NewSyscallError("LookupNS", e)
+       }
+       defer syscall.DnsRecordListFree(r, 1)
+       ns = make([]*NS, 0, 10)
+       for p := r; p != nil && p.Type == syscall.DNS_TYPE_NS; p = p.Next {
+               v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0]))
+               ns = append(ns, &NS{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]) + "."})
+       }
+       return ns, nil
+}
+
 func lookupTXT(name string) (txt []string, err error) {
        var r *syscall.DNSRecord
        e := syscall.DnsQuery(name, syscall.DNS_TYPE_TEXT, 0, nil, &r, nil)