Merge branch 'master' of github.com:miekg/dns

This commit is contained in:
Miek Gieben 2012-07-04 07:45:52 +02:00
commit bdf23b5e58
5 changed files with 37 additions and 32 deletions

View File

@ -117,7 +117,7 @@ func (k *RR_DNSKEY) KeyTag() uint16 {
keywire.Algorithm = k.Algorithm keywire.Algorithm = k.Algorithm
keywire.PublicKey = k.PublicKey keywire.PublicKey = k.PublicKey
wire := make([]byte, DefaultMsgSize) wire := make([]byte, DefaultMsgSize)
n, ok := packStruct(keywire, wire, 0) n, ok := PackStruct(keywire, wire, 0)
if !ok { if !ok {
return 0 return 0
} }
@ -155,7 +155,7 @@ func (k *RR_DNSKEY) ToDS(h int) *RR_DS {
keywire.Algorithm = k.Algorithm keywire.Algorithm = k.Algorithm
keywire.PublicKey = k.PublicKey keywire.PublicKey = k.PublicKey
wire := make([]byte, DefaultMsgSize) wire := make([]byte, DefaultMsgSize)
n, ok := packStruct(keywire, wire, 0) n, ok := PackStruct(keywire, wire, 0)
if !ok { if !ok {
return nil return nil
} }
@ -236,7 +236,7 @@ func (s *RR_RRSIG) Sign(k PrivateKey, rrset []RR) error {
// Create the desired binary blob // Create the desired binary blob
signdata := make([]byte, DefaultMsgSize) signdata := make([]byte, DefaultMsgSize)
n, ok := packStruct(sigwire, signdata, 0) n, ok := PackStruct(sigwire, signdata, 0)
if !ok { if !ok {
return ErrPack return ErrPack
} }
@ -348,7 +348,7 @@ func (s *RR_RRSIG) Verify(k *RR_DNSKEY, rrset []RR) error {
sigwire.SignerName = strings.ToLower(s.SignerName) sigwire.SignerName = strings.ToLower(s.SignerName)
// Create the desired binary blob // Create the desired binary blob
signeddata := make([]byte, DefaultMsgSize) signeddata := make([]byte, DefaultMsgSize)
n, ok := packStruct(sigwire, signeddata, 0) n, ok := PackStruct(sigwire, signeddata, 0)
if !ok { if !ok {
return ErrPack return ErrPack
} }
@ -683,7 +683,7 @@ func rawSignatureData(rrset []RR, s *RR_RRSIG) (buf []byte) {
} }
// 6.2. Canonical RR Form. (5) - origTTL // 6.2. Canonical RR Form. (5) - origTTL
wire := make([]byte, r.Len()*2) wire := make([]byte, r.Len()*2)
off, ok1 := packRR(r1, wire, 0, nil, false) off, ok1 := PackRR(r1, wire, 0, nil, false)
if !ok1 { if !ok1 {
return nil return nil
} }

28
msg.go
View File

@ -649,7 +649,7 @@ func structValue(any interface{}) reflect.Value {
return reflect.ValueOf(any).Elem() 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) off, ok = packStructValue(structValue(any), msg, off, nil, false)
return off, ok return off, ok
} }
@ -1010,7 +1010,7 @@ func unpackUint16(msg []byte, off int) (v uint16, off1 int) {
return 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) off, ok = unpackStructValue(structValue(any), msg, off)
return off, ok return off, ok
} }
@ -1056,7 +1056,7 @@ func packBase32(s []byte) ([]byte, error) {
} }
// Resource record packer. // 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 { if rr == nil {
return len(msg), false 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. // 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 // unpack just the header, to find the rr type and length
var h RR_Header var h RR_Header
off0 := off off0 := off
if off, ok = unpackStruct(&h, msg, off); !ok { if off, ok = UnpackStruct(&h, msg, off); !ok {
return nil, len(msg), false return nil, len(msg), false
} }
end := off + int(h.Rdlength) end := off + int(h.Rdlength)
@ -1085,7 +1085,7 @@ func unpackRR(msg []byte, off int) (rr RR, off1 int, ok bool) {
} else { } else {
rr = mk() rr = mk()
} }
off, ok = unpackStruct(rr, msg, off0) off, ok = UnpackStruct(rr, msg, off0)
if off != end { if off != end {
return &h, end, true 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) off, ok = packStructCompress(&question[i], msg, off, compression, dns.Compress)
} }
for i := 0; i < len(answer); i++ { 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++ { 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++ { 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 { if !ok {
return nil, false return nil, false
@ -1239,7 +1239,7 @@ func (dns *Msg) Unpack(msg []byte) bool {
var dh Header var dh Header
off := 0 off := 0
var ok bool var ok bool
if off, ok = unpackStruct(&dh, msg, off); !ok { if off, ok = UnpackStruct(&dh, msg, off); !ok {
return false return false
} }
dns.Id = dh.Id dns.Id = dh.Id
@ -1261,16 +1261,16 @@ func (dns *Msg) Unpack(msg []byte) bool {
dns.Extra = make([]RR, dh.Arcount) dns.Extra = make([]RR, dh.Arcount)
for i := 0; i < len(dns.Question); i++ { 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++ { 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++ { 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++ { 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 { if !ok {
return false return false

View File

@ -38,7 +38,7 @@ func HashName(label string, ha uint8, iter uint16, salt string) string {
saltwire := new(saltWireFmt) saltwire := new(saltWireFmt)
saltwire.Salt = salt saltwire.Salt = salt
wire := make([]byte, DefaultMsgSize) wire := make([]byte, DefaultMsgSize)
n, ok := packStruct(saltwire, wire, 0) n, ok := PackStruct(saltwire, wire, 0)
if !ok { if !ok {
return "" return ""
} }

18
tsig.go
View File

@ -193,7 +193,7 @@ func TsigGenerate(m *Msg, secret, requestMAC string, timersOnly bool) ([]byte, s
t.OrigId = m.MsgHdr.Id t.OrigId = m.MsgHdr.Id
tbuf := make([]byte, t.Len()) 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 tbuf = tbuf[:off] // reset to actual size used
} else { } else {
return nil, "", ErrPack 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.MACSize = uint16(len(requestMAC) / 2)
m.MAC = requestMAC m.MAC = requestMAC
buf = make([]byte, len(requestMAC)) // long enough buf = make([]byte, len(requestMAC)) // long enough
n, _ := packStruct(m, buf, 0) n, _ := PackStruct(m, buf, 0)
buf = buf[:n] buf = buf[:n]
} }
@ -266,7 +266,7 @@ func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool)
tsig := new(timerWireFmt) tsig := new(timerWireFmt)
tsig.TimeSigned = rr.TimeSigned tsig.TimeSigned = rr.TimeSigned
tsig.Fudge = rr.Fudge tsig.Fudge = rr.Fudge
n, _ := packStruct(tsig, tsigvar, 0) n, _ := PackStruct(tsig, tsigvar, 0)
tsigvar = tsigvar[:n] tsigvar = tsigvar[:n]
} else { } else {
tsig := new(tsigWireFmt) tsig := new(tsigWireFmt)
@ -279,7 +279,7 @@ func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool)
tsig.Error = rr.Error tsig.Error = rr.Error
tsig.OtherLen = rr.OtherLen tsig.OtherLen = rr.OtherLen
tsig.OtherData = rr.OtherData tsig.OtherData = rr.OtherData
n, _ := packStruct(tsig, tsigvar, 0) n, _ := PackStruct(tsig, tsigvar, 0)
tsigvar = tsigvar[:n] tsigvar = tsigvar[:n]
} }
@ -302,7 +302,7 @@ func stripTsig(msg []byte) ([]byte, *RR_TSIG, error) {
off := 0 off := 0
tsigoff := 0 tsigoff := 0
var ok bool var ok bool
if off, ok = unpackStruct(&dh, msg, off); !ok { if off, ok = UnpackStruct(&dh, msg, off); !ok {
return nil, nil, ErrUnpack return nil, nil, ErrUnpack
} }
if dh.Arcount == 0 { if dh.Arcount == 0 {
@ -320,17 +320,17 @@ func stripTsig(msg []byte) ([]byte, *RR_TSIG, error) {
dns.Extra = make([]RR, dh.Arcount) dns.Extra = make([]RR, dh.Arcount)
for i := 0; i < len(dns.Question); i++ { 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++ { 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++ { 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++ { for i := 0; i < len(dns.Extra); i++ {
tsigoff = off 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 { if dns.Extra[i].Header().Rrtype == TypeTSIG {
rr = dns.Extra[i].(*RR_TSIG) rr = dns.Extra[i].(*RR_TSIG)
// Adjust Arcount. // Adjust Arcount.

View File

@ -105,7 +105,7 @@ func NewRR(s string) (RR, error) {
// ReadRR reads the RR contained in q. Only the first RR is returned. // ReadRR reads the RR contained in q. Only the first RR is returned.
// The class defaults to IN and TTL defaults to DefaultTtl. // The class defaults to IN and TTL defaults to DefaultTtl.
func ReadRR(q io.Reader, filename string) (RR, error) { func ReadRR(q io.Reader, filename string) (RR, error) {
r := <-ParseZone(q, ".", filename) r := <-parseZoneHelper(q, ".", filename, 1)
if r.Error != nil { if r.Error != nil {
return nil, r.Error 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 { 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) go parseZone(r, origin, file, t, 0)
return t return t
} }
func parseZone(r io.Reader, origin, f string, t chan Token, include int) { 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) s := scanInit(r)
c := make(chan lex) c := make(chan lex, 1000)
// Start the lexer // Start the lexer
go zlexer(s, c) go zlexer(s, c)
// 6 possible beginnings of a line, _ is a space // 6 possible beginnings of a line, _ is a space