From f1a3b3138457e1fd470ce7ff847a2116437ef790 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Wed, 11 Jan 2012 20:33:38 +0100 Subject: [PATCH] Make a packStructCompress() to leave packStruct() simpler --- dnssec.go | 8 ++++---- msg.go | 14 ++++++++++---- nsec3.go | 2 +- tsig.go | 6 +++--- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/dnssec.go b/dnssec.go index f780b21d..b660b87a 100644 --- a/dnssec.go +++ b/dnssec.go @@ -91,7 +91,7 @@ func (k *RR_DNSKEY) KeyTag() uint16 { keywire.Algorithm = k.Algorithm keywire.PublicKey = k.PublicKey wire := make([]byte, DefaultMsgSize) - n, ok := packStruct(keywire, wire, 0, nil, false) + n, ok := packStruct(keywire, wire, 0) if !ok { return 0 } @@ -126,7 +126,7 @@ func (k *RR_DNSKEY) ToDS(h int) *RR_DS { keywire.Algorithm = k.Algorithm keywire.PublicKey = k.PublicKey wire := make([]byte, DefaultMsgSize) - n, ok := packStruct(keywire, wire, 0, nil, false) + n, ok := packStruct(keywire, wire, 0) if !ok { return nil } @@ -205,7 +205,7 @@ func (s *RR_RRSIG) Sign(k PrivateKey, rrset RRset) error { // Create the desired binary blob signdata := make([]byte, DefaultMsgSize) - n, ok := packStruct(sigwire, signdata, 0, nil, false) + n, ok := packStruct(sigwire, signdata, 0) if !ok { return ErrPack } @@ -300,7 +300,7 @@ func (s *RR_RRSIG) Verify(k *RR_DNSKEY, rrset RRset) error { sigwire.SignerName = strings.ToLower(s.SignerName) // Create the desired binary blob signeddata := make([]byte, DefaultMsgSize) - n, ok := packStruct(sigwire, signeddata, 0, nil, false) + n, ok := packStruct(sigwire, signeddata, 0) if !ok { return ErrPack } diff --git a/msg.go b/msg.go index 826bc3d7..2789ac23 100644 --- a/msg.go +++ b/msg.go @@ -542,7 +542,13 @@ func structValue(any interface{}) reflect.Value { return reflect.ValueOf(any).Elem() } -func packStruct(any interface{}, msg []byte, off int, compression map[string]int, compress bool) (off1 int, ok bool) { +func packStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { + off, ok = packStructValue(structValue(any), msg, off, nil, false) + return off, ok +} + + +func packStructCompress(any interface{}, msg []byte, off int, compression map[string]int, compress bool) (off1 int, ok bool) { off, ok = packStructValue(structValue(any), msg, off, compression, compress) return off, ok } @@ -878,7 +884,7 @@ func packRR(rr RR, msg []byte, off int, compression map[string]int, compress boo return len(msg), false } - off1, ok = packStruct(rr, msg, off, compression, compress) + off1, ok = packStructCompress(rr, msg, off, compression, compress) if !ok { return len(msg), false } @@ -1019,9 +1025,9 @@ func (dns *Msg) Pack() (msg []byte, ok bool) { // Pack it in: header and then the pieces. off := 0 - off, ok = packStruct(&dh, msg, off, compression, dns.Compress) + off, ok = packStructCompress(&dh, msg, off, compression, dns.Compress) for i := 0; i < len(question); i++ { - off, ok = packStruct(&question[i], msg, off, compression, dns.Compress) + off, ok = packStructCompress(&question[i], msg, off, compression, dns.Compress) // println("Question", off) } for i := 0; i < len(answer); i++ { diff --git a/nsec3.go b/nsec3.go index 852b9ad0..da42d827 100644 --- a/nsec3.go +++ b/nsec3.go @@ -17,7 +17,7 @@ func HashName(label string, ha, iter int, salt string) string { saltwire := new(saltWireFmt) saltwire.Salt = salt wire := make([]byte, DefaultMsgSize) - n, ok := packStruct(saltwire, wire, 0, nil, false) + n, ok := packStruct(saltwire, wire, 0) if !ok { return "" } diff --git a/tsig.go b/tsig.go index 309facd9..e86ca2ce 100644 --- a/tsig.go +++ b/tsig.go @@ -161,7 +161,7 @@ func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool) m.MACSize = uint16(len(requestMAC) / 2) m.MAC = requestMAC macbuf = make([]byte, len(requestMAC)) // reqmac should be twice as long - n, _ := packStruct(m, macbuf, 0, nil, false) + n, _ := packStruct(m, macbuf, 0) macbuf = macbuf[:n] } @@ -170,7 +170,7 @@ func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool) tsig := new(timerWireFmt) tsig.TimeSigned = rr.TimeSigned tsig.Fudge = rr.Fudge - n, _ := packStruct(tsig, tsigvar, 0, nil, false) + n, _ := packStruct(tsig, tsigvar, 0) tsigvar = tsigvar[:n] } else { tsig := new(tsigWireFmt) @@ -183,7 +183,7 @@ func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool) tsig.Error = rr.Error tsig.OtherLen = rr.OtherLen tsig.OtherData = rr.OtherData - n, _ := packStruct(tsig, tsigvar, 0, nil, false) + n, _ := packStruct(tsig, tsigvar, 0) tsigvar = tsigvar[:n] } if rr.MAC != "" {