1
0
mirror of https://github.com/golang/go synced 2024-11-20 05:44:44 -07:00

net: add LookupNS(domain string)

Fixes #4224.

R=golang-dev, dave, minux.ma, mikioh.mikioh, alex.brainman, rsc, herbert.fischer
CC=golang-dev
https://golang.org/cl/6675043
This commit is contained in:
Stephen McQuay 2012-10-18 15:39:04 +09:00 committed by Mikio Hara
parent 4c2a1f9559
commit a5b0c67d5f
6 changed files with 67 additions and 0 deletions

View File

@ -244,3 +244,8 @@ func (s byPref) sort() {
} }
sort.Sort(s) sort.Sort(s)
} }
// An NS represents a single DNS NS record.
type NS struct {
Host string
}

View File

@ -47,6 +47,11 @@ func LookupMX(name string) (mx []*MX, err error) {
return lookupMX(name) 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. // LookupTXT returns the DNS TXT records for the given domain name.
func LookupTXT(name string) (txt []string, err error) { func LookupTXT(name string) (txt []string, err error) {
return lookupTXT(name) return lookupTXT(name)

View File

@ -201,6 +201,21 @@ func lookupMX(name string) (mx []*MX, err error) {
return 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) { func lookupTXT(name string) (txt []string, err error) {
lines, err := queryDNS(name, "txt") lines, err := queryDNS(name, "txt")
if err != nil { if err != nil {

View 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) { func TestGmailTXT(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() || !*testExternal {
t.Logf("skipping test to avoid external network") t.Logf("skipping test to avoid external network")

View File

@ -119,6 +119,19 @@ func lookupMX(name string) (mx []*MX, err error) {
return 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) { func lookupTXT(name string) (txt []string, err error) {
_, records, err := lookup(name, dnsTypeTXT) _, records, err := lookup(name, dnsTypeTXT)
if err != nil { if err != nil {

View File

@ -129,6 +129,21 @@ func lookupMX(name string) (mx []*MX, err error) {
return mx, nil 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) { func lookupTXT(name string) (txt []string, err error) {
var r *syscall.DNSRecord var r *syscall.DNSRecord
e := syscall.DnsQuery(name, syscall.DNS_TYPE_TEXT, 0, nil, &r, nil) e := syscall.DnsQuery(name, syscall.DNS_TYPE_TEXT, 0, nil, &r, nil)