1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """DNS TTL conversion."""
17
18 import dns.exception
19
20 -class BadTTL(dns.exception.SyntaxError):
22
24 """Convert the text form of a TTL to an integer.
25
26 The BIND 8 units syntax for TTLs (e.g. '1w6d4h3m10s') is supported.
27
28 @param text: the textual TTL
29 @type text: string
30 @raises dns.ttl.BadTTL: the TTL is not well-formed
31 @rtype: int
32 """
33
34 if text.isdigit():
35 total = int(text)
36 else:
37 if not text[0].isdigit():
38 raise BadTTL
39 total = 0
40 current = 0
41 for c in text:
42 if c.isdigit():
43 current *= 10
44 current += int(c)
45 else:
46 c = c.lower()
47 if c == 'w':
48 total += current * 604800
49 elif c == 'd':
50 total += current * 86400
51 elif c == 'h':
52 total += current * 3600
53 elif c == 'm':
54 total += current * 60
55 elif c == 's':
56 total += current
57 else:
58 raise BadTTL("unknown unit '%s'" % c)
59 current = 0
60 if not current == 0:
61 raise BadTTL("trailing integer")
62 if total < 0 or total > 2147483647:
63 raise BadTTL("TTL should be between 0 and 2^31 - 1 (inclusive)")
64 return total
65