Skip to content

Commit

Permalink
net: add LookupNS(domain string)
Browse files Browse the repository at this point in the history
Fixes #4224.

R=golang-dev, dave, minux.ma, mikioh.mikioh, alex.brainman, rsc, herbert.fischer
CC=golang-dev
https://golang.org/cl/6675043
  • Loading branch information
smcquay authored and cixtor committed Oct 18, 2012
1 parent 4c2a1f9 commit a5b0c67
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pkg/net/dnsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,8 @@ func (s byPref) sort() {
}
sort.Sort(s)
}

// An NS represents a single DNS NS record.
type NS struct {
Host string
}
5 changes: 5 additions & 0 deletions src/pkg/net/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions src/pkg/net/lookup_plan9.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions src/pkg/net/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 13 additions & 0 deletions src/pkg/net/lookup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions src/pkg/net/lookup_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a5b0c67

Please sign in to comment.