Remove packLen() and fix bug in Len()

packLen() was a featureless mirror of Len(). Remove it, and just use
Len() internally too.
Fix bug in Len() too, where the length of the additional section was
not counted.
This commit is contained in:
Miek Gieben 2014-02-10 12:23:53 +00:00
parent 4f6fef6777
commit 56e0bb46d8
2 changed files with 8 additions and 22 deletions

View File

@ -65,7 +65,7 @@ func TestPackUnpack2(t *testing.T) {
m.Answer[0] = rr
_, err := m.Pack()
if err != nil {
t.Log("Packing failed")
t.Log("Packing failed: " + err.Error())
t.Fail()
return
}
@ -93,7 +93,7 @@ func TestPackUnpack3(t *testing.T) {
m.Answer[0] = rr
b, err := m.Pack()
if err != nil {
t.Log("Packing failed")
t.Log("Packing failed: " + err.Error())
t.Fail()
return
}

26
msg.go
View File

@ -1366,10 +1366,14 @@ func (dns *Msg) PackBuffer(buf []byte) (msg []byte, err error) {
dh.Nscount = uint16(len(ns))
dh.Arcount = uint16(len(extra))
// We need the uncompressed length here, because we first pack it and then compress it.
msg = buf
if packLen := dns.packLen() + 1; len(msg) < packLen {
compress := dns.Compress
dns.Compress = false
if packLen := dns.Len() + 1; len(msg) < packLen {
msg = make([]byte, packLen)
}
dns.Compress = compress
// Pack it in: header and then the pieces.
off := 0
@ -1514,25 +1518,6 @@ func (dns *Msg) String() string {
return s
}
// packLen returns the message length when in UNcompressed wire format.
func (dns *Msg) packLen() int {
// Message header is always 12 bytes
l := 12
for i := 0; i < len(dns.Question); i++ {
l += dns.Question[i].len()
}
for i := 0; i < len(dns.Answer); i++ {
l += dns.Answer[i].len()
}
for i := 0; i < len(dns.Ns); i++ {
l += dns.Ns[i].len()
}
for i := 0; i < len(dns.Extra); i++ {
l += dns.Extra[i].len()
}
return l
}
// Len returns the message length when in (un)compressed wire format.
// If dns.Compress is true compression it is taken into account. Len()
// is provided to be a faster way to get the size of the resulting packet,
@ -1576,6 +1561,7 @@ func (dns *Msg) Len() int {
}
}
for i := 0; i < len(dns.Extra); i++ {
l += dns.Extra[i].len()
if dns.Compress {
k, ok := compressionLenSearch(compression, dns.Extra[i].Header().Name)
if ok {