1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """DNS Rdata Classes."""
19
20 import re
21
22 import dns.exception
23
24 RESERVED0 = 0
25 IN = 1
26 CH = 3
27 HS = 4
28 NONE = 254
29 ANY = 255
30
31 _by_text = {
32 'RESERVED0': RESERVED0,
33 'IN': IN,
34 'CH': CH,
35 'HS': HS,
36 'NONE': NONE,
37 'ANY': ANY
38 }
39
40
41
42
43
44 _by_value = {y: x for x, y in _by_text.items()}
45
46
47
48
49 _by_text.update({
50 'INTERNET': IN,
51 'CHAOS': CH,
52 'HESIOD': HS
53 })
54
55 _metaclasses = {
56 NONE: True,
57 ANY: True
58 }
59
60 _unknown_class_pattern = re.compile('CLASS([0-9]+)$', re.I)
61
62
64 """A DNS class is unknown."""
65
66
68 """Convert text into a DNS rdata class value.
69
70 The input text can be a defined DNS RR class mnemonic or
71 instance of the DNS generic class syntax.
72
73 For example, "IN" and "CLASS1" will both result in a value of 1.
74
75 Raises ``dns.rdatatype.UnknownRdataclass`` if the class is unknown.
76
77 Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535.
78
79 Returns an ``int``.
80 """
81
82 value = _by_text.get(text.upper())
83 if value is None:
84 match = _unknown_class_pattern.match(text)
85 if match is None:
86 raise UnknownRdataclass
87 value = int(match.group(1))
88 if value < 0 or value > 65535:
89 raise ValueError("class must be between >= 0 and <= 65535")
90 return value
91
92
94 """Convert a DNS rdata type value to text.
95
96 If the value has a known mnemonic, it will be used, otherwise the
97 DNS generic class syntax will be used.
98
99 Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535.
100
101 Returns a ``str``.
102 """
103
104 if value < 0 or value > 65535:
105 raise ValueError("class must be between >= 0 and <= 65535")
106 text = _by_value.get(value)
107 if text is None:
108 text = 'CLASS' + repr(value)
109 return text
110
111
123