Fix setting the rdlength for compressed names

RawSetRdlength was working with a 0xC00 bitmask which
should have been 0xC0.
This commit is contained in:
Miek Gieben 2012-02-26 18:23:21 +01:00
parent 7047ab5809
commit b7f997cc6e
4 changed files with 21 additions and 12 deletions

View File

@ -8,6 +8,10 @@ need to be fixed.
* Add handy zone data structure (r/b tree)? Or not...
* Use the Exchange structure to deal with errors when resolving, esp. Timeout
* Add tsig check in 'q'?
* tsig and ip blocks should be handled in the library! Other configs may be
stored outside and are up to the programmer
* Query source address, for axfr n stuff is something others
* Compression is not working correctly
## Examples to add

View File

@ -29,7 +29,10 @@ import (
"strconv"
)
var printf *bool
var (
printf *bool
compress *bool
)
const dom = "whoami.miek.nl."
@ -42,6 +45,7 @@ func handleReflect(w dns.ResponseWriter, r *dns.Msg) {
)
m := new(dns.Msg)
m.SetReply(r)
m.Compress = *compress
if ip, ok := w.RemoteAddr().(*net.UDPAddr); ok {
str = "Port: " + strconv.Itoa(ip.Port) + " (udp)"
a = ip.IP
@ -103,6 +107,7 @@ func serve(net string) {
func main() {
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
printf = flag.Bool("print", false, "print replies")
compress = flag.Bool("compress", false, "compress replies")
flag.Usage = func() {
flag.PrintDefaults()
}

20
msg.go
View File

@ -193,26 +193,20 @@ var Rcode_str = map[int]string{
// PackDomainName packs a domain name s into msg[off:].
// If compression is wanted compress must be true and the compression
// map, needs to hold a mapping between domain names and offsets
// map needs to hold a mapping between domain names and offsets
// pointing into msg[].
func PackDomainName(s string, msg []byte, off int, compression map[string]int, compress bool) (off1 int, ok bool) {
// Add trailing dot to canonicalize name.
lenmsg := len(msg)
if n := len(s); n == 0 || s[n-1] != '.' {
//println("hier? s", s)
//return lenmsg, false // This is an error, should be fqdn
// Make it fully qualified
s += "."
}
// Each dot ends a segment of the name.
// We trade each dot byte for a length byte.
// Except for escaped dots (\.), which are normal dots.
// There is also a trailing zero.
// Check that we have all the space we need.
tot := len(s) + 1 // TODO: this too much for compression...
if off+tot > lenmsg {
return lenmsg, false
}
// Compression
nameoffset := -1
pointer := -1
@ -238,9 +232,15 @@ func PackDomainName(s string, msg []byte, off int, compression map[string]int, c
msg[off] = byte(i - begin)
offset := off
off++
if off > lenmsg {
return lenmsg, false
}
for j := begin; j < i; j++ {
msg[off] = bs[j]
off++
if off > lenmsg {
return lenmsg, false
}
}
// Dont try to compress '.'
if string(bs[begin:]) != "." && compression != nil {
@ -259,7 +259,7 @@ func PackDomainName(s string, msg []byte, off int, compression map[string]int, c
if pointer == -1 && compress {
pointer = p // Where to point to
nameoffset = offset // Where to point from
// println("Compressing:", string(bs[begin:]))
break
}
}
}

View File

@ -24,7 +24,7 @@ Loop:
}
c := int(msg[off])
off++
switch c & 0xC00 {
switch c & 0xC0 {
case 0x00:
if c == 0x00 {
// End of the domainname