1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """DNS Rdata Classes.
17
18 @var _by_text: The rdata class textual name to value mapping
19 @type _by_text: dict
20 @var _by_value: The rdata class value to textual name mapping
21 @type _by_value: dict
22 @var _metaclasses: If an rdataclass is a metaclass, there will be a mapping
23 whose key is the rdatatype value and whose value is True in this dictionary.
24 @type _metaclasses: dict"""
25
26 import re
27
28 import dns.exception
29
30 RESERVED0 = 0
31 IN = 1
32 CH = 3
33 HS = 4
34 NONE = 254
35 ANY = 255
36
37 _by_text = {
38 'RESERVED0' : RESERVED0,
39 'IN' : IN,
40 'CH' : CH,
41 'HS' : HS,
42 'NONE' : NONE,
43 'ANY' : ANY
44 }
45
46
47
48
49
50 _by_value = dict([(y, x) for x, y in _by_text.items()])
51
52
53
54
55 _by_text.update({
56 'INTERNET' : IN,
57 'CHAOS' : CH,
58 'HESIOD' : HS
59 })
60
61 _metaclasses = frozenset([NONE, ANY])
62
63 _unknown_class_pattern = re.compile('CLASS([0-9]+)$', re.I);
64
66 """Raised when a class is unknown."""
67 pass
68
70 """Convert text into a DNS rdata class value.
71 @param text: the text
72 @type text: string
73 @rtype: int
74 @raises dns.rdataclass.UnknownRdataclass: the class is unknown
75 @raises ValueError: the rdata class value is not >= 0 and <= 65535
76 """
77
78 value = _by_text.get(text.upper())
79 if value is None:
80 match = _unknown_class_pattern.match(text)
81 if match == None:
82 raise UnknownRdataclass
83 value = int(match.group(1))
84 if value < 0 or value > 65535:
85 raise ValueError("class must be between >= 0 and <= 65535")
86 return value
87
89 """Convert a DNS rdata class to text.
90 @param value: the rdata class value
91 @type value: int
92 @rtype: string
93 @raises ValueError: the rdata class value is not >= 0 and <= 65535
94 """
95
96 if value < 0 or value > 65535:
97 raise ValueError("class must be between >= 0 and <= 65535")
98 text = _by_value.get(value)
99 if text is None:
100 text = 'CLASS' + str(value)
101 return text
102
112