Add support for @

For this to work ParseZone was extended to accept a starting
origin. This way even zonefiles without $ORIGIN would work
as expected. This is an api change.
This commit is contained in:
Miek Gieben 2012-02-14 13:46:40 +01:00
parent b49e4f2411
commit 7ef88cf5b3
2 changed files with 16 additions and 11 deletions

View File

@ -100,7 +100,7 @@ func TestParseZone(t *testing.T) {
z2.miek.nl. 86400 IN NSEC miek.nl. TXT RRSIG NSEC z2.miek.nl. 86400 IN NSEC miek.nl. TXT RRSIG NSEC
$TTL 100 $TTL 100
z3.miek.nl. IN NSEC miek.nl. TXT RRSIG NSEC` z3.miek.nl. IN NSEC miek.nl. TXT RRSIG NSEC`
to := ParseZone(strings.NewReader(zone), "") to := ParseZone(strings.NewReader(zone), "", "")
i := 0 i := 0
for x := range to { for x := range to {
if x.Error == nil { if x.Error == nil {
@ -314,7 +314,7 @@ func BenchmarkZoneParsing(b *testing.B) {
return return
} }
defer f.Close() defer f.Close()
to := ParseZone(f, "t/miek.nl.signed_test") to := ParseZone(f, "", "t/miek.nl.signed_test")
for x := range to { for x := range to {
x = x x = x
} }
@ -327,7 +327,7 @@ func TestZoneParsing(t *testing.T) {
} }
defer f.Close() defer f.Close()
start := time.Now().UnixNano() start := time.Now().UnixNano()
to := ParseZone(f, "t/miek.nl.signed_test") to := ParseZone(f, "", "t/miek.nl.signed_test")
var i int var i int
for x := range to { for x := range to {
t.Logf("%s\n", x.RR) t.Logf("%s\n", x.RR)

View File

@ -99,12 +99,10 @@ func NewRR(s string) (RR, error) {
return ReadRR(strings.NewReader(s), "") return ReadRR(strings.NewReader(s), "")
} }
// Ioreader here, or filename which *we* open....???
// 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 := <-ParseZone(q, ".", filename)
if r.Error != nil { if r.Error != nil {
return nil, r.Error return nil, r.Error
} }
@ -115,13 +113,13 @@ func ReadRR(q io.Reader, filename string) (RR, error) {
// returned channel, which consist out the parsed RR or an error. // returned channel, which consist out the parsed RR or an error.
// If there is an error the RR is nil. // If there is an error the RR is nil.
// The channel t is closed by ParseZone when the end of r is reached. // The channel t is closed by ParseZone when the end of r is reached.
func ParseZone(r io.Reader, file string) chan Token { func ParseZone(r io.Reader, origin, file string) chan Token {
t := make(chan Token) t := make(chan Token)
go parseZone(r, file, t, 0) go parseZone(r, origin, file, t, 0)
return t return t
} }
func parseZone(r io.Reader, f string, t chan Token, include int) { func parseZone(r io.Reader, origin, f string, t chan Token, include int) {
defer func() { defer func() {
if include == 0 { if include == 0 {
close(t) close(t)
@ -142,11 +140,18 @@ func parseZone(r io.Reader, f string, t chan Token, include int) {
// 5. _OWNER _ _CLASS _ _STRING _ _RRTYPE -> class/ttl (reversed) // 5. _OWNER _ _CLASS _ _STRING _ _RRTYPE -> class/ttl (reversed)
// After detecting these, we know the _RRTYPE so we can jump to functions // After detecting these, we know the _RRTYPE so we can jump to functions
// handling the rdata for each of these types. // handling the rdata for each of these types.
if origin == "" {
origin = "."
}
if _, _, ok := IsDomainName(origin); !ok {
t <- Token{Error: &ParseError{f, "bad origin name", lex{}}}
return
}
st := _EXPECT_OWNER_DIR st := _EXPECT_OWNER_DIR
var h RR_Header var h RR_Header
var ok bool var ok bool
var defttl uint32 = DefaultTtl var defttl uint32 = DefaultTtl
var origin string = "."
for l := range c { for l := range c {
if _DEBUG { if _DEBUG {
fmt.Printf("[%v]\n", l) fmt.Printf("[%v]\n", l)
@ -215,7 +220,7 @@ func parseZone(r io.Reader, f string, t chan Token, include int) {
t <- Token{Error: &ParseError{f, "Too deeply nested $INCLUDE", l}} t <- Token{Error: &ParseError{f, "Too deeply nested $INCLUDE", l}}
return return
} }
parseZone(r1, l.token, t, include+1) parseZone(r1, l.token, origin, t, include+1)
st = _EXPECT_OWNER_DIR st = _EXPECT_OWNER_DIR
case _EXPECT_DIRTTL_BL: case _EXPECT_DIRTTL_BL:
if l.value != _BLANK { if l.value != _BLANK {