Move RR header packing out of generated code (#885)

This commit is contained in:
Tom Thorogood 2019-01-04 10:09:14 +10:30 committed by GitHub
parent 813bd39114
commit b955100a79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 368 additions and 650 deletions

10
dns.go
View File

@ -41,8 +41,9 @@ type RR interface {
// size will be returned and domain names will be added to the map for future compression.
len(off int, compression map[string]struct{}) int
// pack packs an RR into wire format.
pack(msg []byte, off int, compression compressionMap, compress bool) (headerEnd int, off1 int, err error)
// pack packs the records RDATA into wire format. The header will
// already have been packed into msg.
pack(msg []byte, off int, compression compressionMap, compress bool) (off1 int, err error)
// unpack unpacks an RR from wire format.
//
@ -87,6 +88,11 @@ func (h *RR_Header) len(off int, compression map[string]struct{}) int {
return l
}
func (h *RR_Header) pack(msg []byte, off int, compression compressionMap, compress bool) (off1 int, err error) {
// RR_Header has no RDATA to pack.
return off, nil
}
func (h *RR_Header) unpack(msg []byte, off int) (int, error) {
panic("dns: internal error: unpack should never be called on RR_Header")
}

7
msg.go
View File

@ -621,7 +621,12 @@ func packRR(rr RR, msg []byte, off int, compression compressionMap, compress boo
return len(msg), len(msg), &Error{err: "nil rr"}
}
headerEnd, off1, err = rr.pack(msg, off, compression, compress)
headerEnd, err = rr.Header().packHeader(msg, off, compression, compress)
if err != nil {
return headerEnd, len(msg), err
}
off1, err = rr.pack(msg, headerEnd, compression, compress)
if err != nil {
return headerEnd, len(msg), err
}

View File

@ -80,17 +80,12 @@ func main() {
o := scope.Lookup(name)
st, _ := getTypeStruct(o.Type(), scope)
fmt.Fprintf(b, "func (rr *%s) pack(msg []byte, off int, compression compressionMap, compress bool) (int, int, error) {\n", name)
fmt.Fprint(b, `headerEnd, off, err := rr.Hdr.pack(msg, off, compression, compress)
if err != nil {
return headerEnd, off, err
}
`)
fmt.Fprintf(b, "func (rr *%s) pack(msg []byte, off int, compression compressionMap, compress bool) (off1 int, err error) {\n", name)
for i := 1; i < st.NumFields(); i++ {
o := func(s string) {
fmt.Fprintf(b, s, st.Field(i).Name())
fmt.Fprint(b, `if err != nil {
return headerEnd, off, err
return off, err
}
`)
}
@ -144,7 +139,7 @@ return headerEnd, off, err
if rr.%s != "-" {
off, err = packStringHex(rr.%s, msg, off)
if err != nil {
return headerEnd, off, err
return off, err
}
}
`, field, field)
@ -176,7 +171,7 @@ if rr.%s != "-" {
log.Fatalln(name, st.Field(i).Name(), st.Tag(i))
}
}
fmt.Fprintln(b, "return headerEnd, off, nil }\n")
fmt.Fprintln(b, "return off, nil }\n")
}
fmt.Fprint(b, "// unpack*() functions\n\n")

View File

@ -99,34 +99,34 @@ func unpackHeader(msg []byte, off int) (rr RR_Header, off1 int, truncmsg []byte,
return hdr, off, msg, err
}
// pack packs an RR header, returning the offset to the end of the header.
// packHeader packs an RR header, returning the offset to the end of the header.
// See PackDomainName for documentation about the compression.
func (hdr RR_Header) pack(msg []byte, off int, compression compressionMap, compress bool) (int, int, error) {
func (hdr RR_Header) packHeader(msg []byte, off int, compression compressionMap, compress bool) (int, error) {
if off == len(msg) {
return off, off, nil
return off, nil
}
off, err := packDomainName(hdr.Name, msg, off, compression, compress)
if err != nil {
return off, len(msg), err
return len(msg), err
}
off, err = packUint16(hdr.Rrtype, msg, off)
if err != nil {
return off, len(msg), err
return len(msg), err
}
off, err = packUint16(hdr.Class, msg, off)
if err != nil {
return off, len(msg), err
return len(msg), err
}
off, err = packUint32(hdr.Ttl, msg, off)
if err != nil {
return off, len(msg), err
return len(msg), err
}
off, err = packUint16(0, msg, off) // The RDLENGTH field will be set later in packRR.
if err != nil {
return off, len(msg), err
return len(msg), err
}
return off, off, nil
return off, nil
}
// helper helper functions.

View File

@ -70,17 +70,13 @@ func (r *PrivateRR) copy() RR {
return rr
}
func (r *PrivateRR) pack(msg []byte, off int, compression compressionMap, compress bool) (int, int, error) {
headerEnd, off, err := r.Hdr.pack(msg, off, compression, compress)
if err != nil {
return off, off, err
}
func (r *PrivateRR) pack(msg []byte, off int, compression compressionMap, compress bool) (int, error) {
n, err := r.Data.Pack(msg[off:])
if err != nil {
return headerEnd, len(msg), err
return len(msg), err
}
off += n
return headerEnd, off, nil
return off, nil
}
func (r *PrivateRR) unpack(msg []byte, off int) (int, error) {

960
zmsg.go

File diff suppressed because it is too large Load Diff