Better handle error in pack/unpacking

This commit is contained in:
Miek Gieben 2010-12-30 17:15:59 +01:00
parent b6244eaa04
commit ec2e732ad3
4 changed files with 30 additions and 12 deletions

4
TODO
View File

@ -8,5 +8,9 @@ Todo:
* query-time, server in string ouput of dns.Msg
Issues:
* completely fix EDNS
* shortened ipv6 addresses are not parsed correctly
* quoted quotes in txt records
* Convience functions?
- for new(RR*)
- ...

View File

@ -1,5 +1,8 @@
package main
// TODO
// error handling and dns errors should be displayed
import (
"net"
"dns"
@ -10,13 +13,13 @@ import (
"strings"
)
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [@server] [qtype] [qclass] [name ...]\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
var dnssec *bool = flag.Bool("dnssec", false, "Set the DO (DNSSEC OK) bit and set the bufsize to 4096")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [@server] [qtype] [qclass] [name ...]\n", os.Args[0])
flag.PrintDefaults()
}
nameserver := "127.0.0.1" // Default nameserver
qtype := uint16(dns.TypeA) // Default qtype
qclass := uint16(dns.ClassINET) // Default qclass
@ -24,9 +27,6 @@ func main() {
flag.Parse()
if *dnssec {
/* */
}
FLAGS:
for i := 0; i < flag.NArg(); i++ {
// If it starts with @ it is a nameserver
@ -67,6 +67,15 @@ FLAGS:
m := new(dns.Msg)
m.Question = make([]dns.Question, 1)
if *dnssec {
opt := new(dns.RR_OPT)
opt.Hdr = dns.RR_Header{Name: "", Rrtype: dns.TypeOPT}
opt.Version(0, true)
opt.DoBit(true, true)
opt.UDPSize(4096, true)
m.Extra = make([]dns.RR, 1)
m.Extra[0] = opt
}
for _, v := range qname {
m.Question[0] = dns.Question{v, qtype, qclass}
qr <- resolver.DnsMsg{m, nil}

10
msg.go
View File

@ -476,6 +476,10 @@ func unpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) {
// Resource record packer.
func packRR(rr RR, msg []byte, off int) (off2 int, ok bool) {
if rr == nil {
return len(msg), false
}
var off1 int
// pack twice, once to find end of header
// and again to find end of packet.
@ -647,10 +651,10 @@ func (dns *Msg) Pack() (msg []byte, ok bool) {
off := 0
off, ok = packStruct(&dh, msg, off)
for i := 0; i < len(question); i++ {
off, ok = packStruct(&question[i], msg, off)
off, ok = packStruct(&question[i], msg, off)
}
for i := 0; i < len(answer); i++ {
off, ok = packRR(answer[i], msg, off)
off, ok = packRR(answer[i], msg, off)
}
for i := 0; i < len(ns); i++ {
off, ok = packRR(ns[i], msg, off)
@ -661,7 +665,7 @@ func (dns *Msg) Pack() (msg []byte, ok bool) {
if !ok {
return nil, false
}
return msg[0:off], true
return msg[:off], true
}
func (dns *Msg) Unpack(msg []byte) bool {

View File

@ -88,8 +88,9 @@ func query(res *Resolver, msg chan DnsMsg) {
out.Dns.Id = uint16(rand.Int()) ^ uint16(time.Nanoseconds())
sending, ok := out.Dns.Pack()
if !ok {
println("pack failed")
msg <- DnsMsg{nil, nil} // todo error
return
continue;
}
for i := 0; i < len(res.Servers); i++ {