diff --git a/dnssec.go b/dnssec.go index 9127e87f..c49ac7f2 100644 --- a/dnssec.go +++ b/dnssec.go @@ -117,7 +117,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) + n, ok := PackStruct(keywire, wire, 0) if !ok { return 0 } @@ -155,7 +155,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) + n, ok := PackStruct(keywire, wire, 0) if !ok { return nil } @@ -236,7 +236,7 @@ func (s *RR_RRSIG) Sign(k PrivateKey, rrset []RR) error { // Create the desired binary blob signdata := make([]byte, DefaultMsgSize) - n, ok := packStruct(sigwire, signdata, 0) + n, ok := PackStruct(sigwire, signdata, 0) if !ok { return ErrPack } @@ -348,7 +348,7 @@ func (s *RR_RRSIG) Verify(k *RR_DNSKEY, rrset []RR) error { sigwire.SignerName = strings.ToLower(s.SignerName) // Create the desired binary blob signeddata := make([]byte, DefaultMsgSize) - n, ok := packStruct(sigwire, signeddata, 0) + n, ok := PackStruct(sigwire, signeddata, 0) if !ok { return ErrPack } @@ -683,7 +683,7 @@ func rawSignatureData(rrset []RR, s *RR_RRSIG) (buf []byte) { } // 6.2. Canonical RR Form. (5) - origTTL wire := make([]byte, r.Len()*2) - off, ok1 := packRR(r1, wire, 0, nil, false) + off, ok1 := PackRR(r1, wire, 0, nil, false) if !ok1 { return nil } diff --git a/msg.go b/msg.go index 5cb8f274..c0e3a0b4 100644 --- a/msg.go +++ b/msg.go @@ -649,7 +649,7 @@ func structValue(any interface{}) reflect.Value { return reflect.ValueOf(any).Elem() } -func packStruct(any interface{}, msg []byte, off int) (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 } @@ -1010,7 +1010,7 @@ func unpackUint16(msg []byte, off int) (v uint16, off1 int) { return } -func unpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { +func UnpackStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { off, ok = unpackStructValue(structValue(any), msg, off) return off, ok } @@ -1056,7 +1056,7 @@ func packBase32(s []byte) ([]byte, error) { } // Resource record packer. -func packRR(rr RR, msg []byte, off int, compression map[string]int, compress bool) (off1 int, ok bool) { +func PackRR(rr RR, msg []byte, off int, compression map[string]int, compress bool) (off1 int, ok bool) { if rr == nil { return len(msg), false } @@ -1070,11 +1070,11 @@ func packRR(rr RR, msg []byte, off int, compression map[string]int, compress boo } // Resource record unpacker. -func unpackRR(msg []byte, off int) (rr RR, off1 int, ok bool) { +func UnpackRR(msg []byte, off int) (rr RR, off1 int, ok bool) { // unpack just the header, to find the rr type and length var h RR_Header off0 := off - if off, ok = unpackStruct(&h, msg, off); !ok { + if off, ok = UnpackStruct(&h, msg, off); !ok { return nil, len(msg), false } end := off + int(h.Rdlength) @@ -1085,7 +1085,7 @@ func unpackRR(msg []byte, off int) (rr RR, off1 int, ok bool) { } else { rr = mk() } - off, ok = unpackStruct(rr, msg, off0) + off, ok = UnpackStruct(rr, msg, off0) if off != end { return &h, end, true } @@ -1219,13 +1219,13 @@ func (dns *Msg) Pack() (msg []byte, ok bool) { off, ok = packStructCompress(&question[i], msg, off, compression, dns.Compress) } for i := 0; i < len(answer); i++ { - off, ok = packRR(answer[i], msg, off, compression, dns.Compress) + off, ok = PackRR(answer[i], msg, off, compression, dns.Compress) } for i := 0; i < len(ns); i++ { - off, ok = packRR(ns[i], msg, off, compression, dns.Compress) + off, ok = PackRR(ns[i], msg, off, compression, dns.Compress) } for i := 0; i < len(extra); i++ { - off, ok = packRR(extra[i], msg, off, compression, dns.Compress) + off, ok = PackRR(extra[i], msg, off, compression, dns.Compress) } if !ok { return nil, false @@ -1239,7 +1239,7 @@ func (dns *Msg) Unpack(msg []byte) bool { var dh Header off := 0 var ok bool - if off, ok = unpackStruct(&dh, msg, off); !ok { + if off, ok = UnpackStruct(&dh, msg, off); !ok { return false } dns.Id = dh.Id @@ -1261,16 +1261,16 @@ func (dns *Msg) Unpack(msg []byte) bool { dns.Extra = make([]RR, dh.Arcount) for i := 0; i < len(dns.Question); i++ { - off, ok = unpackStruct(&dns.Question[i], msg, off) + off, ok = UnpackStruct(&dns.Question[i], msg, off) } for i := 0; i < len(dns.Answer); i++ { - dns.Answer[i], off, ok = unpackRR(msg, off) + dns.Answer[i], off, ok = UnpackRR(msg, off) } for i := 0; i < len(dns.Ns); i++ { - dns.Ns[i], off, ok = unpackRR(msg, off) + dns.Ns[i], off, ok = UnpackRR(msg, off) } for i := 0; i < len(dns.Extra); i++ { - dns.Extra[i], off, ok = unpackRR(msg, off) + dns.Extra[i], off, ok = UnpackRR(msg, off) } if !ok { return false diff --git a/nsecx.go b/nsecx.go index 7df9565a..7561a948 100644 --- a/nsecx.go +++ b/nsecx.go @@ -38,7 +38,7 @@ func HashName(label string, ha uint8, iter uint16, salt string) string { saltwire := new(saltWireFmt) saltwire.Salt = salt wire := make([]byte, DefaultMsgSize) - n, ok := packStruct(saltwire, wire, 0) + n, ok := PackStruct(saltwire, wire, 0) if !ok { return "" } diff --git a/tsig.go b/tsig.go index d28d4925..c02c5e52 100644 --- a/tsig.go +++ b/tsig.go @@ -193,7 +193,7 @@ func TsigGenerate(m *Msg, secret, requestMAC string, timersOnly bool) ([]byte, s t.OrigId = m.MsgHdr.Id tbuf := make([]byte, t.Len()) - if off, ok := packRR(t, tbuf, 0, nil, false); ok { + if off, ok := PackRR(t, tbuf, 0, nil, false); ok { tbuf = tbuf[:off] // reset to actual size used } else { return nil, "", ErrPack @@ -257,7 +257,7 @@ func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool) m.MACSize = uint16(len(requestMAC) / 2) m.MAC = requestMAC buf = make([]byte, len(requestMAC)) // long enough - n, _ := packStruct(m, buf, 0) + n, _ := PackStruct(m, buf, 0) buf = buf[:n] } @@ -266,7 +266,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) + n, _ := PackStruct(tsig, tsigvar, 0) tsigvar = tsigvar[:n] } else { tsig := new(tsigWireFmt) @@ -279,7 +279,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) + n, _ := PackStruct(tsig, tsigvar, 0) tsigvar = tsigvar[:n] } @@ -302,7 +302,7 @@ func stripTsig(msg []byte) ([]byte, *RR_TSIG, error) { off := 0 tsigoff := 0 var ok bool - if off, ok = unpackStruct(&dh, msg, off); !ok { + if off, ok = UnpackStruct(&dh, msg, off); !ok { return nil, nil, ErrUnpack } if dh.Arcount == 0 { @@ -320,17 +320,17 @@ func stripTsig(msg []byte) ([]byte, *RR_TSIG, error) { dns.Extra = make([]RR, dh.Arcount) for i := 0; i < len(dns.Question); i++ { - off, ok = unpackStruct(&dns.Question[i], msg, off) + off, ok = UnpackStruct(&dns.Question[i], msg, off) } for i := 0; i < len(dns.Answer); i++ { - dns.Answer[i], off, ok = unpackRR(msg, off) + dns.Answer[i], off, ok = UnpackRR(msg, off) } for i := 0; i < len(dns.Ns); i++ { - dns.Ns[i], off, ok = unpackRR(msg, off) + dns.Ns[i], off, ok = UnpackRR(msg, off) } for i := 0; i < len(dns.Extra); i++ { tsigoff = off - dns.Extra[i], off, ok = unpackRR(msg, off) + dns.Extra[i], off, ok = UnpackRR(msg, off) if dns.Extra[i].Header().Rrtype == TypeTSIG { rr = dns.Extra[i].(*RR_TSIG) // Adjust Arcount. diff --git a/zscan.go b/zscan.go index f1b92393..f636280f 100644 --- a/zscan.go +++ b/zscan.go @@ -105,7 +105,7 @@ func NewRR(s string) (RR, error) { // ReadRR reads the RR contained in q. Only the first RR is returned. // The class defaults to IN and TTL defaults to DefaultTtl. func ReadRR(q io.Reader, filename string) (RR, error) { - r := <-ParseZone(q, ".", filename) + r := <-parseZoneHelper(q, ".", filename, 1) if r.Error != nil { return nil, r.Error } @@ -129,9 +129,14 @@ func ReadRR(q io.Reader, filename string) (RR, error) { // } // } func ParseZone(r io.Reader, origin, file string) chan Token { - t := make(chan Token) + return parseZoneHelper(r, origin, file, 10000) +} + +func parseZoneHelper(r io.Reader, origin, file string, chansize int) chan Token { + t := make(chan Token, chansize) go parseZone(r, origin, file, t, 0) return t + } func parseZone(r io.Reader, origin, f string, t chan Token, include int) { @@ -141,7 +146,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { } }() s := scanInit(r) - c := make(chan lex) + c := make(chan lex, 1000) // Start the lexer go zlexer(s, c) // 6 possible beginnings of a line, _ is a space