Skip to content

Commit

Permalink
Cleanup of Resolve fix for removed PackStruct (miekg#7)
Browse files Browse the repository at this point in the history
* Cleanup of Resolve fix for removed PackStruct

The main header data is the same in every iteration, so it does not have
to be in the loop itself

* rename buffer vars to CamelCase
  • Loading branch information
tjeb authored and miekg committed Jun 13, 2016
1 parent 0a62612 commit c0c307b
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions unbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,33 +244,34 @@ func (u *Unbound) Resolve(name string, rrtype, rrclass uint16) (*Result, error)
r.Data = make([][]byte, 0)
r.Rr = make([]dns.RR, 0)
b := C.GoBytes(unsafe.Pointer(C.array_elem_char(res.data, C.int(j))), C.array_elem_int(res.len, C.int(j)))

// Create the RR; write out the header details and
// the rdata to a buffer, and unpack it again into an
// actual RR, for ever rr found by resolve
hdrBuf := make([]byte, len(h.Name) + 11)
off, _ := dns.PackDomainName(h.Name, hdrBuf, 0, nil, false)
binary.BigEndian.PutUint16(hdrBuf[off:], h.Rrtype)
off += 2
binary.BigEndian.PutUint16(hdrBuf[off:], h.Class)
off += 2
binary.BigEndian.PutUint32(hdrBuf[off:], h.Ttl)
off += 4

for len(b) != 0 {
// Create the RR; write out the header details and
// the rdata to a buffer, and unpack it again into an
// actual RR
h.Rdlength = uint16(len(b))
// Note: we are rewriting the rdata len so we do not
// increase off anymore.
binary.BigEndian.PutUint16(hdrBuf[off:], h.Rdlength)
rrBuf := append(hdrBuf, b...)

msg := make([]byte, len(h.Name) + 11)
off, _ := dns.PackDomainName(h.Name, msg, 0, nil, false)
binary.BigEndian.PutUint16(msg[off:], h.Rrtype)
off += 2
binary.BigEndian.PutUint16(msg[off:], h.Class)
off += 2
binary.BigEndian.PutUint32(msg[off:], h.Ttl)
off += 4
binary.BigEndian.PutUint16(msg[off:], h.Rdlength)
off += 2
rrbuf := append(msg, b...)

rr, _, err := dns.UnpackRR(rrbuf, 0)
rr, _, err := dns.UnpackRR(rrBuf, 0)
if err == nil {
r.Rr = append(r.Rr, rr)
}

r.Data = append(r.Data, b)
j++
b = C.GoBytes(unsafe.Pointer(C.array_elem_char(res.data, C.int(j))), C.array_elem_int(res.len, C.int(j)))

}
}
return r, err
Expand Down

0 comments on commit c0c307b

Please sign in to comment.