Implement TTLs in words

1H, 1M, 1W2D, etc. should now be parsed in TTLs
This commit is contained in:
Miek Gieben 2012-02-14 15:14:54 +01:00
parent b3a58abd24
commit 63d8b1feb1
2 changed files with 30 additions and 7 deletions

View File

@ -155,6 +155,8 @@ func TestDomainName(t *testing.T) {
func TestParseDirective(t *testing.T) {
tests := map[string]string{
"$ORIGIN miek.nl.\na IN NS b": "a.miek.nl.\t3600\tIN\tNS\tb.miek.nl.",
"$TTL 2H\nmiek.nl. IN NS b.": "miek.nl.\t7200\tIN\tNS\tb.",
"miek.nl. 1D IN NS b.": "miek.nl.\t86400\tIN\tNS\tb.",
}
for i, o := range tests {
rr, e := NewRR(i)

View File

@ -679,11 +679,32 @@ func typeToInt(token string) (uint16, bool) {
}
func stringToTtl(l lex, f string, t chan Token) (uint32, bool) {
if ttl, ok := strconv.Atoi(l.token); ok != nil {
t <- Token{Error: &ParseError{f, "Not a TTL", l}}
return 0, false
} else {
return uint32(ttl), true
}
panic("not reached")
s := uint32(0)
i := uint32(0)
for _, c := range l.token {
switch c {
case 's', 'S':
s += i
i = 0
case 'm', 'M':
s += i * 60
i = 0
case 'h', 'H':
s += i * 60 * 60
i = 0
case 'd', 'D':
s += i * 60 * 60 * 24
i = 0
case 'w', 'W':
s += i * 60 * 60 * 24 * 7
i = 0
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
i *= 10
i += uint32(c) - '0'
default:
t <- Token{Error: &ParseError{f, "Not a TTL", l}}
return 0, false
}
}
return s+i, true
}