Fix the size -- was counting the labels, not the bytes

This commit is contained in:
Miek Gieben 2012-01-10 13:47:34 +01:00
parent 3b30c6e0b5
commit 15d82f0b34
3 changed files with 38 additions and 36 deletions

4
dns.go
View File

@ -214,9 +214,9 @@ func (h *RR_Header) String() string {
} }
func (h *RR_Header) Len() int { func (h *RR_Header) Len() int {
l, _ := IsDomainName(h.Name) l := len(h.Name) + 1
l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2) l += 10 // rrtype(2) + class(2) + ttl(4) + rdlength(2)
return int(l) return l
} }
func zoneMatch(pattern, zone string) (ok bool) { func zoneMatch(pattern, zone string) (ok bool) {

6
msg.go
View File

@ -463,6 +463,10 @@ func packStructValue(val reflect.Value, msg []byte, off int) (off1 int, ok bool)
//fmt.Fprintf(os.Stderr, "dns: overflow packing (size-)hex string") //fmt.Fprintf(os.Stderr, "dns: overflow packing (size-)hex string")
return lenmsg, false return lenmsg, false
} }
if off+hex.DecodedLen(len(s)) > lenmsg {
// Overflow
return lenmsg, false
}
copy(msg[off:off+hex.DecodedLen(len(s))], h) copy(msg[off:off+hex.DecodedLen(len(s))], h)
off += hex.DecodedLen(len(s)) off += hex.DecodedLen(len(s))
case "size": case "size":
@ -976,7 +980,7 @@ func (dns *Msg) Pack() (msg []byte, ok bool) {
dh.Arcount = uint16(len(extra)) dh.Arcount = uint16(len(extra))
// TODO: still too much, but better than 64K // TODO: still too much, but better than 64K
msg = make([]byte, dns.Len()*8) msg = make([]byte, dns.Len()*6)
// Pack it in: header and then the pieces. // Pack it in: header and then the pieces.
off := 0 off := 0

View File

@ -151,8 +151,8 @@ func (q *Question) String() (s string) {
} }
func (q *Question) Len() int { func (q *Question) Len() int {
l, _ := IsDomainName(q.Name) l := len(q.Name)+1
return int(l) + 4 return l + 4
} }
type RR_ANY struct { type RR_ANY struct {
@ -186,8 +186,8 @@ func (rr *RR_CNAME) String() string {
} }
func (rr *RR_CNAME) Len() int { func (rr *RR_CNAME) Len() int {
l, _ := IsDomainName(rr.Cname) l := len(rr.Cname)+1
return rr.Hdr.Len() + int(l) return rr.Hdr.Len() + l
} }
type RR_HINFO struct { type RR_HINFO struct {
@ -222,8 +222,8 @@ func (rr *RR_MB) String() string {
} }
func (rr *RR_MB) Len() int { func (rr *RR_MB) Len() int {
l, _ := IsDomainName(rr.Mb) l := len(rr.Mb)+1
return rr.Hdr.Len() + int(l) return rr.Hdr.Len() + l
} }
type RR_MG struct { type RR_MG struct {
@ -240,8 +240,8 @@ func (rr *RR_MG) String() string {
} }
func (rr *RR_MG) Len() int { func (rr *RR_MG) Len() int {
l, _ := IsDomainName(rr.Mg) l := len(rr.Mg)+1
return rr.Hdr.Len() + int(l) return rr.Hdr.Len() + l
} }
type RR_MINFO struct { type RR_MINFO struct {
@ -259,9 +259,9 @@ func (rr *RR_MINFO) String() string {
} }
func (rr *RR_MINFO) Len() int { func (rr *RR_MINFO) Len() int {
l, _ := IsDomainName(rr.Rmail) l := len(rr.Rmail)+1
n, _ := IsDomainName(rr.Email) n := len(rr.Email)+1
return rr.Hdr.Len() + int(l) + int(n) return rr.Hdr.Len() + l + n
} }
type RR_MR struct { type RR_MR struct {
@ -278,8 +278,8 @@ func (rr *RR_MR) String() string {
} }
func (rr *RR_MR) Len() int { func (rr *RR_MR) Len() int {
l, _ := IsDomainName(rr.Mr) l := len(rr.Mr)+1
return rr.Hdr.Len() + int(l) return rr.Hdr.Len() + l
} }
type RR_MX struct { type RR_MX struct {
@ -297,8 +297,8 @@ func (rr *RR_MX) String() string {
} }
func (rr *RR_MX) Len() int { func (rr *RR_MX) Len() int {
l, _ := IsDomainName(rr.Mx) l := len(rr.Mx)+1
return rr.Hdr.Len() + int(l) + 2 return rr.Hdr.Len() + l + 2
} }
type RR_NS struct { type RR_NS struct {
@ -315,8 +315,8 @@ func (rr *RR_NS) String() string {
} }
func (rr *RR_NS) Len() int { func (rr *RR_NS) Len() int {
l, _ := IsDomainName(rr.Ns) l := len(rr.Ns)+1
return rr.Hdr.Len() + int(l) return rr.Hdr.Len() + l
} }
type RR_PTR struct { type RR_PTR struct {
@ -333,8 +333,8 @@ func (rr *RR_PTR) String() string {
} }
func (rr *RR_PTR) Len() int { func (rr *RR_PTR) Len() int {
l, _ := IsDomainName(rr.Ptr) l := len(rr.Ptr)+1
return rr.Hdr.Len() + int(l) return rr.Hdr.Len() + l
} }
type RR_SOA struct { type RR_SOA struct {
@ -362,9 +362,9 @@ func (rr *RR_SOA) String() string {
} }
func (rr *RR_SOA) Len() int { func (rr *RR_SOA) Len() int {
l, _ := IsDomainName(rr.Ns) l := len(rr.Ns)+1
n, _ := IsDomainName(rr.Mbox) n := len(rr.Mbox)+1
return rr.Hdr.Len() + int(l) + int(n) + 20 return rr.Hdr.Len() + l + n + 20
} }
type RR_TXT struct { type RR_TXT struct {
@ -404,8 +404,8 @@ func (rr *RR_SRV) String() string {
} }
func (rr *RR_SRV) Len() int { func (rr *RR_SRV) Len() int {
l, _ := IsDomainName(rr.Target) l := len(rr.Target)+1
return rr.Hdr.Len() + int(l) + 6 return rr.Hdr.Len() + l + 6
} }
type RR_NAPTR struct { type RR_NAPTR struct {
@ -475,8 +475,8 @@ func (rr *RR_DNAME) String() string {
} }
func (rr *RR_DNAME) Len() int { func (rr *RR_DNAME) Len() int {
l, _ := IsDomainName(rr.Target) l := len(rr.Target)+1
return rr.Hdr.Len() + int(l) return rr.Hdr.Len() + l
} }
type RR_A struct { type RR_A struct {
@ -567,8 +567,8 @@ func (rr *RR_RRSIG) String() string {
} }
func (rr *RR_RRSIG) Len() int { func (rr *RR_RRSIG) Len() int {
l, _ := IsDomainName(rr.SignerName) l := len(rr.SignerName)+1
return rr.Hdr.Len() + int(l) + len(rr.Signature) + 18 return rr.Hdr.Len() + l + len(rr.Signature) + 18
// base64 string, wordt iets minder dus // base64 string, wordt iets minder dus
} }
@ -595,8 +595,8 @@ func (rr *RR_NSEC) String() string {
} }
func (rr *RR_NSEC) Len() int { func (rr *RR_NSEC) Len() int {
l, _ := IsDomainName(rr.NextDomain) l := len(rr.NextDomain)+1
return rr.Hdr.Len() + int(l) + len(rr.TypeBitMap) return rr.Hdr.Len() + l + len(rr.TypeBitMap)
// This is also shorter due to the windowing // This is also shorter due to the windowing
} }
@ -704,9 +704,7 @@ func (rr *RR_TALINK) String() string {
} }
func (rr *RR_TALINK) Len() int { func (rr *RR_TALINK) Len() int {
l, _ := IsDomainName(rr.PreviousName) return rr.Hdr.Len() + len(rr.PreviousName) + len(rr.NextName) + 2
n, _ := IsDomainName(rr.NextName)
return rr.Hdr.Len() + int(l) + int(n)
} }
type RR_SSHFP struct { type RR_SSHFP struct {