Cleanup the identical parsing functions.

Some records are copies from others: DNSKEY, CDNSKEY and KEY are
identical. DS, CDS and DLV are too and even RRSIG and SIG.

The parsing functions and the definition can all be used for parsing
all these identical types.
This commit is contained in:
Miek Gieben 2014-11-02 11:29:55 +00:00
parent 678d31fa77
commit deb8fe381f
3 changed files with 62 additions and 193 deletions

View File

@ -49,7 +49,7 @@ Send pull request if you want to be listed here.
* DNSSEC: signing, validating and key generation for DSA, RSA and ECDSA;
* EDNS0, NSID;
* AXFR/IXFR;
* TSIG;
* TSIG, SIG(0);
* DNS name compression;
* Depends only on the standard library.
@ -136,5 +136,4 @@ Example programs can be found in the `github.com/miekg/exdns` repository.
* CAA parsing is broken;
* NSEC(3) cover/match/closest enclose;
* Replies with TC bit are not parsed to the end;
* SIG(0);
* Create IsMsg to validate a message before fully parsing it.

View File

@ -894,6 +894,14 @@ func (rr *NSEC) len() int {
return l
}
type DLV struct {
DS
}
type CDS struct {
DS
}
type DS struct {
Hdr RR_Header
KeyTag uint16
@ -915,48 +923,6 @@ func (rr *DS) String() string {
" " + strings.ToUpper(rr.Digest)
}
type CDS struct {
Hdr RR_Header
KeyTag uint16
Algorithm uint8
DigestType uint8
Digest string `dns:"hex"`
}
func (rr *CDS) Header() *RR_Header { return &rr.Hdr }
func (rr *CDS) len() int { return rr.Hdr.len() + 4 + len(rr.Digest)/2 }
func (rr *CDS) copy() RR {
return &CDS{*rr.Hdr.copyHeader(), rr.KeyTag, rr.Algorithm, rr.DigestType, rr.Digest}
}
func (rr *CDS) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.KeyTag)) +
" " + strconv.Itoa(int(rr.Algorithm)) +
" " + strconv.Itoa(int(rr.DigestType)) +
" " + strings.ToUpper(rr.Digest)
}
type DLV struct {
Hdr RR_Header
KeyTag uint16
Algorithm uint8
DigestType uint8
Digest string `dns:"hex"`
}
func (rr *DLV) Header() *RR_Header { return &rr.Hdr }
func (rr *DLV) len() int { return rr.Hdr.len() + 4 + len(rr.Digest)/2 }
func (rr *DLV) copy() RR {
return &DLV{*rr.Hdr.copyHeader(), rr.KeyTag, rr.Algorithm, rr.DigestType, rr.Digest}
}
func (rr *DLV) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.KeyTag)) +
" " + strconv.Itoa(int(rr.Algorithm)) +
" " + strconv.Itoa(int(rr.DigestType)) +
" " + strings.ToUpper(rr.Digest)
}
type KX struct {
Hdr RR_Header
Preference uint16
@ -1058,6 +1024,10 @@ type KEY struct {
DNSKEY
}
type CDNSKEY struct {
DNSKEY
}
type DNSKEY struct {
Hdr RR_Header
Flags uint16
@ -1081,29 +1051,6 @@ func (rr *DNSKEY) String() string {
" " + rr.PublicKey
}
type CDNSKEY struct {
Hdr RR_Header
Flags uint16
Protocol uint8
Algorithm uint8
PublicKey string `dns:"base64"`
}
func (rr *CDNSKEY) Header() *RR_Header { return &rr.Hdr }
func (rr *CDNSKEY) len() int {
return rr.Hdr.len() + 4 + base64.StdEncoding.DecodedLen(len(rr.PublicKey))
}
func (rr *CDNSKEY) copy() RR {
return &DNSKEY{*rr.Hdr.copyHeader(), rr.Flags, rr.Protocol, rr.Algorithm, rr.PublicKey}
}
func (rr *CDNSKEY) String() string {
return rr.Hdr.String() + strconv.Itoa(int(rr.Flags)) +
" " + strconv.Itoa(int(rr.Protocol)) +
" " + strconv.Itoa(int(rr.Algorithm)) +
" " + rr.PublicKey
}
type RKEY struct {
Hdr RR_Header
Flags uint16

View File

@ -1066,11 +1066,11 @@ func setOPENPGPKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, strin
}
func setSIG(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
r, e, s := setRRSIG(h, c, o, f)
if r != nil {
return &SIG{*r.(*RRSIG)} , e, s
}
return nil, e, s
r, e, s := setRRSIG(h, c, o, f)
if r != nil {
return &SIG{*r.(*RRSIG)}, e, s
}
return nil, e, s
}
func setRRSIG(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
@ -1460,15 +1460,7 @@ func setSSHFP(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
return rr, nil, ""
}
func setKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
r, e, s := setDNSKEY(h, c, o, f)
if r != nil {
return &KEY{*r.(*DNSKEY)} , e, s
}
return nil, e, s
}
func setDNSKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
func setDNSKEYs(h RR_Header, c chan lex, o, f, typ string) (RR, *ParseError, string) {
rr := new(DNSKEY)
rr.Hdr = h
@ -1477,25 +1469,25 @@ func setDNSKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
return rr, nil, l.comment
}
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad DNSKEY Flags", l}, ""
return nil, &ParseError{f, "bad " + typ + " Flags", l}, ""
} else {
rr.Flags = uint16(i)
}
<-c // _BLANK
l = <-c // _STRING
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad DNSKEY Protocol", l}, ""
return nil, &ParseError{f, "bad " + typ + " Protocol", l}, ""
} else {
rr.Protocol = uint8(i)
}
<-c // _BLANK
l = <-c // _STRING
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad DNSKEY Algorithm", l}, ""
return nil, &ParseError{f, "bad " + typ + " Algorithm", l}, ""
} else {
rr.Algorithm = uint8(i)
}
s, e, c1 := endingToString(c, "bad DNSKEY PublicKey", f)
s, e, c1 := endingToString(c, "bad "+typ+" PublicKey", f)
if e != nil {
return nil, e, c1
}
@ -1503,39 +1495,25 @@ func setDNSKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
return rr, nil, c1
}
func setCDNSKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
rr := new(CDNSKEY)
rr.Hdr = h
func setKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
r, e, s := setDNSKEYs(h, c, o, f, "KEY")
if r != nil {
return &KEY{*r.(*DNSKEY)}, e, s
}
return nil, e, s
}
l := <-c
if l.length == 0 {
return rr, nil, l.comment
func setDNSKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
r, e, s := setDNSKEYs(h, c, o, f, "DNSKEY")
return r, e, s
}
func setCDNSKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
r, e, s := setDNSKEYs(h, c, o, f, "CDNSKEY")
if r != nil {
return &CDNSKEY{*r.(*DNSKEY)}, e, s
}
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad CDNSKEY Flags", l}, ""
} else {
rr.Flags = uint16(i)
}
<-c // _BLANK
l = <-c // _STRING
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad CDNSKEY Protocol", l}, ""
} else {
rr.Protocol = uint8(i)
}
<-c // _BLANK
l = <-c // _STRING
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad CDNSKEY Algorithm", l}, ""
} else {
rr.Algorithm = uint8(i)
}
s, e, c1 := endingToString(c, "bad CDNSKEY PublicKey", f)
if e != nil {
return nil, e, c1
}
rr.PublicKey = s
return rr, nil, c1
return nil, e, s
}
func setRKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
@ -1573,44 +1551,6 @@ func setRKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
return rr, nil, c1
}
func setDS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
rr := new(DS)
rr.Hdr = h
l := <-c
if l.length == 0 {
return rr, nil, l.comment
}
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad DS KeyTag", l}, ""
} else {
rr.KeyTag = uint16(i)
}
<-c // _BLANK
l = <-c
if i, e := strconv.Atoi(l.token); e != nil {
if i, ok := StringToAlgorithm[l.tokenUpper]; !ok {
return nil, &ParseError{f, "bad DS Algorithm", l}, ""
} else {
rr.Algorithm = i
}
} else {
rr.Algorithm = uint8(i)
}
<-c // _BLANK
l = <-c
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad DS DigestType", l}, ""
} else {
rr.DigestType = uint8(i)
}
s, e, c1 := endingToString(c, "bad DS Digest", f)
if e != nil {
return nil, e, c1
}
rr.Digest = s
return rr, nil, c1
}
func setEID(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
rr := new(EID)
rr.Hdr = h
@ -1683,15 +1623,15 @@ func setGPOS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
return rr, nil, ""
}
func setCDS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
rr := new(CDS)
func setDSs(h RR_Header, c chan lex, o, f, typ string) (RR, *ParseError, string) {
rr := new(DS)
rr.Hdr = h
l := <-c
if l.length == 0 {
return rr, nil, l.comment
}
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad CDS KeyTag", l}, ""
return nil, &ParseError{f, "bad " + typ + " KeyTag", l}, ""
} else {
rr.KeyTag = uint16(i)
}
@ -1699,7 +1639,7 @@ func setCDS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
l = <-c
if i, e := strconv.Atoi(l.token); e != nil {
if i, ok := StringToAlgorithm[l.tokenUpper]; !ok {
return nil, &ParseError{f, "bad CDS Algorithm", l}, ""
return nil, &ParseError{f, "bad " + typ + " Algorithm", l}, ""
} else {
rr.Algorithm = i
}
@ -1709,11 +1649,11 @@ func setCDS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
<-c // _BLANK
l = <-c
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad CDS DigestType", l}, ""
return nil, &ParseError{f, "bad " + typ + " DigestType", l}, ""
} else {
rr.DigestType = uint8(i)
}
s, e, c1 := endingToString(c, "bad CDS Digest", f)
s, e, c1 := endingToString(c, "bad " + typ + " Digest", f)
if e != nil {
return nil, e, c1
}
@ -1721,42 +1661,25 @@ func setCDS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
return rr, nil, c1
}
func setDS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
r, e, s := setDSs(h, c, o, f, "DS")
return r, e, s
}
func setDLV(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
rr := new(DLV)
rr.Hdr = h
l := <-c
if l.length == 0 {
return rr, nil, l.comment
r, e, s := setDSs(h, c, o, f, "DLV")
if r != nil {
return &DLV{*r.(*DS)}, e, s
}
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad DLV KeyTag", l}, ""
} else {
rr.KeyTag = uint16(i)
return nil, e, s
}
func setCDS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
r, e, s := setDSs(h, c, o, f, "DLV")
if r != nil {
return &CDS{*r.(*DS)}, e, s
}
<-c // _BLANK
l = <-c
if i, e := strconv.Atoi(l.token); e != nil {
if i, ok := StringToAlgorithm[l.tokenUpper]; !ok {
return nil, &ParseError{f, "bad DLV Algorithm", l}, ""
} else {
rr.Algorithm = i
}
} else {
rr.Algorithm = uint8(i)
}
<-c // _BLANK
l = <-c
if i, e := strconv.Atoi(l.token); e != nil {
return nil, &ParseError{f, "bad DLV DigestType", l}, ""
} else {
rr.DigestType = uint8(i)
}
s, e, c1 := endingToString(c, "bad DLV Digest", f)
if e != nil {
return nil, e, c1
}
rr.Digest = s
return rr, nil, c1
return nil, e, s
}
func setTA(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {