Fix compression

The compression was fixed. Also the packing of message is been speed
up, by a facter of 3. Now the packing happens once, instead of
3 times.

Two tests still fail. atm
This commit is contained in:
Miek Gieben 2012-01-11 16:04:52 +01:00
parent 76efc275a3
commit f39d939f52
3 changed files with 20 additions and 33 deletions

View File

@ -18,7 +18,6 @@ func TestPackUnpack(t *testing.T) {
t.Log("Failed to pack msg with DNSKEY")
t.Fail()
}
in := new(Msg)
if !in.Unpack(msg) {
t.Log("Failed to unpack msg with DNSKEY")

39
msg.go
View File

@ -222,11 +222,12 @@ func PackDomainName(s string, msg []byte, off int, compression map[string]int) (
msg[off] = bs[j]
off++
}
if compression != nil {
if p, ok := compression[string(bs[begin:])]; !ok {
// Dont try to compress '.'
if string(bs[begin:]) != "." && compression != nil {
if p, ok := compression[string(bs[begin:])]; !ok {
// Only offsets smaller than this can be used.
if offset < maxCompressionOffset {
compression[string(bs[begin:])] = offset
compression[string(bs[begin:])] = offset
}
} else {
// The first hit is the longest matching dname
@ -250,10 +251,11 @@ func PackDomainName(s string, msg []byte, off int, compression map[string]int) (
if pointer != -1 {
// We have two bytes (14 bits) to put the pointer in
msg[nameoffset], msg[nameoffset+1] = packUint16(uint16(pointer ^ 0xC000))
off = nameoffset + 2
return off, true
off = nameoffset + 1
goto End
}
msg[off] = 0
End:
off++
return off, true
}
@ -725,11 +727,12 @@ func unpackStructValue(val reflect.Value, msg []byte, off int) (off1 int, ok boo
}
s = unpackBase64(msg[off : off+rdlength-consumed])
off += rdlength - consumed
case "cdomain-name": fallthrough
case "cdomain-name":
fallthrough
case "domain-name":
s, off, ok = UnpackDomainName(msg, off)
if !ok {
//fmt.Fprintf(os.Stderr, "dns: failure unpacking domain-name")
//println("dns: failure unpacking domain-name")
return lenmsg, false
}
case "size-base32":
@ -866,31 +869,17 @@ func packBase32(s []byte) ([]byte, error) {
}
// Resource record packer.
func packRR(rr RR, msg []byte, off int, compression map[string]int) (off2 int, ok bool) {
func packRR(rr RR, msg []byte, off int, compression map[string]int) (off1 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.
// a bit inefficient but this doesn't need to be fast.
// off1 is end of header
// off2 is end of rr
off1, ok = packStruct(rr.Header(), msg, off, compression)
off2, ok = packStruct(rr, msg, off, compression)
off1, ok = packStruct(rr, msg, off, compression)
if !ok {
return len(msg), false
}
// pack a third time; redo header with correct data length
rr.Header().Rdlength = uint16(off2 - off1)
packStruct(rr.Header(), msg, off, compression)
return off2, true
// rr.Header().Rdlength = uint16(off2 - off1)
// if !rr.Header().RawSetRdlength(msg, off) {
// return len(msg), false
// }
RawSetRdLength(msg, off, off1)
return off1, true
}
// Resource record unpacker.

View File

@ -434,7 +434,7 @@ func (rr *RR_NAPTR) String() string {
func (rr *RR_NAPTR) Len() int {
return rr.Hdr.Len() + 4 + len(rr.Flags) + len(rr.Service) +
len(rr.Regexp) + len(rr.Replacement)+1
len(rr.Regexp) + len(rr.Replacement) + 1
}
// See RFC 4398.
@ -568,9 +568,8 @@ func (rr *RR_RRSIG) String() string {
}
func (rr *RR_RRSIG) Len() int {
l := len(rr.SignerName) + 1
return rr.Hdr.Len() + l + len(rr.Signature) + 18
// base64 string, wordt iets minder dus
return rr.Hdr.Len() + len(rr.SignerName) + 1 + len(rr.Signature) + 18
// TODO base64 string, should be less
}
type RR_NSEC struct {
@ -813,7 +812,7 @@ func (rr *RR_NSEC3PARAM) String() string {
}
func (rr *RR_NSEC3PARAM) Len() int {
return rr.Hdr.Len() + 2+4+1 + len(rr.Salt)/2
return rr.Hdr.Len() + 2 + 4 + 1 + len(rr.Salt)/2
}
// See RFC 4408.
@ -857,8 +856,8 @@ func (rr *RR_TKEY) String() string {
}
func (rr *RR_TKEY) Len() int {
return rr.Hdr.Len() + len(rr.Algorithm)+1+4+4+6+
len(rr.Key) + 2 + len(rr.OtherData)
return rr.Hdr.Len() + len(rr.Algorithm) + 1 + 4 + 4 + 6 +
len(rr.Key) + 2 + len(rr.OtherData)
}
// Unknown RR representation