Make members of dns.Msg public

added packtest to see if ipv4 and ipv6 works
This commit is contained in:
Miek Gieben 2010-08-09 12:40:31 +02:00
parent eb05356fa9
commit af26ef2532
3 changed files with 55 additions and 63 deletions

8
dns.go
View File

@ -60,7 +60,7 @@ func Exchange(res *Resolver, c net.Conn, name string, qtype uint16, qclass uint1
}
out := new(Msg)
out.id = uint16(rand.Int()) ^ uint16(time.Nanoseconds())
out.question = []Question{
out.Question = []Question{
Question{name, qtype, qclass},
}
out.recursion_desired = true
@ -102,7 +102,7 @@ func Exchange(res *Resolver, c net.Conn, name string, qtype uint16, qclass uint1
// Find answer for name in dns message.
// On return, if err == nil, addrs != nil.
func answer(name, server string, dns *Msg, qtype uint16) (addrs []RR, err os.Error) {
addrs = make([]RR, 0, len(dns.answer))
addrs = make([]RR, 0, len(dns.Answer))
if dns.rcode == RcodeNameError && dns.recursion_available {
return nil, &DnsError{Error: noSuchHost, Name: name}
@ -123,8 +123,8 @@ func answer(name, server string, dns *Msg, qtype uint16) (addrs []RR, err os.Err
Cname:
for cnameloop := 0; cnameloop < 10; cnameloop++ {
addrs = addrs[0:0]
for i := 0; i < len(dns.answer); i++ {
rr := dns.answer[i]
for i := 0; i < len(dns.Answer); i++ {
rr := dns.Answer[i]
h := rr.Header()
if h.Class == ClassINET && h.Name == name {
switch h.Rrtype {

View File

@ -503,11 +503,11 @@ func (h *MsgHdr) String() string {
type Msg struct {
MsgHdr
question []Question
edns []Edns
answer []RR
ns []RR
extra []RR
Question []Question
Edns []Edns
Answer []RR
Ns []RR
Extra []RR
}
@ -534,10 +534,10 @@ func (dns *Msg) Pack() (msg []byte, ok bool) {
}
// Prepare variable sized arrays.
question := dns.question
answer := dns.answer
ns := dns.ns
extra := dns.extra
question := dns.Question
answer := dns.Answer
ns := dns.Ns
extra := dns.Extra
dh.Qdcount = uint16(len(question))
dh.Ancount = uint16(len(answer))
@ -588,22 +588,22 @@ func (dns *Msg) Unpack(msg []byte) bool {
dns.rcode = int(dh.Bits & 0xF)
// Arrays.
dns.question = make([]Question, dh.Qdcount)
dns.answer = make([]RR, dh.Ancount)
dns.ns = make([]RR, dh.Nscount)
dns.extra = make([]RR, dh.Arcount)
dns.Question = make([]Question, dh.Qdcount)
dns.Answer = make([]RR, dh.Ancount)
dns.Ns = make([]RR, dh.Nscount)
dns.Extra = make([]RR, dh.Arcount)
for i := 0; i < len(dns.question); i++ {
off, ok = unpackStruct(&dns.question[i], msg, off)
for i := 0; i < len(dns.Question); i++ {
off, ok = unpackStruct(&dns.Question[i], msg, off)
}
for i := 0; i < len(dns.answer); i++ {
dns.answer[i], off, ok = unpackRR(msg, off)
for i := 0; i < len(dns.Answer); i++ {
dns.Answer[i], off, ok = unpackRR(msg, off)
}
for i := 0; i < len(dns.ns); i++ {
dns.ns[i], off, ok = unpackRR(msg, off)
for i := 0; i < len(dns.Ns); i++ {
dns.Ns[i], off, ok = unpackRR(msg, off)
}
for i := 0; i < len(dns.extra); i++ {
dns.extra[i], off, ok = unpackRR(msg, off)
for i := 0; i < len(dns.Extra); i++ {
dns.Extra[i], off, ok = unpackRR(msg, off)
}
if !ok {
return false
@ -616,32 +616,32 @@ func (dns *Msg) Unpack(msg []byte) bool {
func (dns *Msg) String() string {
s := dns.MsgHdr.String() + " "
s += "QUERY: " + strconv.Itoa(len(dns.question)) + ", "
s += "ANSWER: " + strconv.Itoa(len(dns.answer)) + ", "
s += "AUTHORITY: " + strconv.Itoa(len(dns.ns)) + ", "
s += "ADDITIONAL: " + strconv.Itoa(len(dns.extra)) + "\n"
if len(dns.question) > 0 {
s += "QUERY: " + strconv.Itoa(len(dns.Question)) + ", "
s += "ANSWER: " + strconv.Itoa(len(dns.Answer)) + ", "
s += "AUTHORITY: " + strconv.Itoa(len(dns.Ns)) + ", "
s += "ADDITIONAL: " + strconv.Itoa(len(dns.Extra)) + "\n"
if len(dns.Question) > 0 {
s += "\n;; QUESTION SECTION:\n"
for i := 0; i < len(dns.question); i++ {
s += dns.question[i].String() + "\n"
for i := 0; i < len(dns.Question); i++ {
s += dns.Question[i].String() + "\n"
}
}
if len(dns.answer) > 0 {
if len(dns.Answer) > 0 {
s += "\n;; ANSWER SECTION:\n"
for i := 0; i < len(dns.answer); i++ {
s += dns.answer[i].String() + "\n"
for i := 0; i < len(dns.Answer); i++ {
s += dns.Answer[i].String() + "\n"
}
}
if len(dns.ns) > 0 {
if len(dns.Ns) > 0 {
s += "\n;; AUTHORITY SECTION:\n"
for i := 0; i < len(dns.ns); i++ {
s += dns.ns[i].String() + "\n"
for i := 0; i < len(dns.Ns); i++ {
s += dns.Ns[i].String() + "\n"
}
}
if len(dns.extra) > 0 {
if len(dns.Extra) > 0 {
s += "\n;; ADDITIONAL SECTION:\n"
for i := 0; i < len(dns.extra); i++ {
s += dns.extra[i].String() + "\n"
for i := 0; i < len(dns.Extra); i++ {
s += dns.Extra[i].String() + "\n"
}
}
return s

View File

@ -7,29 +7,21 @@ import (
)
func main() {
res := new(dns.Resolver)
res.Servers = []string{"192.168.1.2"}
res.Timeout = 2
res.Attempts = 1
out := new(dns.Msg)
a := new(dns.RR_A)
a.A = net.ParseIP("192.168.1.2").To4()
r := new(dns.RR_AAAA)
r.AAAA = net.ParseIP("2001:7b8:206:1:200:39ff:fe59:b187").To16()
// r.AAAA = net.ParseIP("2003::53").To16()
r.Hdr.Name = "a.miek.nl"
r.Hdr.Rrtype = dns.TypeAAAA
r.Hdr.Class = dns.ClassINET
r.Hdr.Ttl = 3600
out.Answer = make([]dns.RR, 1)
out.Answer[0] = r
aaaa := new(dns.RR_AAAA)
aaaa.AAAA = net.ParseIP("2003::53").To16()
msg, _ := out.Pack()
fmt.Printf("%v\n", a)
fmt.Printf("%v\n", aaaa)
// msg, _ := res.Query("miek.nl.", dns.TypeTXT, dns.ClassINET)
// fmt.Printf("%v\n", msg)
//
// msg, _ = res.Query("www.nlnetlabs.nl", dns.TypeAAAA, dns.ClassINET)
// fmt.Printf("%v\n", msg)
//
msg, _ := res.Query("nlnetlabs.nl", dns.TypeDNSKEY, dns.ClassINET)
fmt.Printf("%v\n", msg)
msg, _ = res.Query("jelte.nlnetlabs.nl", dns.TypeDS, dns.ClassINET)
fmt.Printf("%v\n", msg)
in := new(dns.Msg)
in.Unpack(msg)
fmt.Printf("%v\n", in)
}