1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3# Copyright (C) 2001-2017 Nominum, Inc.
4#
5# Permission to use, copy, modify, and distribute this software and its
6# documentation for any purpose with or without fee is hereby granted,
7# provided that the above copyright notice and this permission notice
8# appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18"""DNS Rdata Classes."""
19
20import dns.enum
21import dns.exception
22
23class RdataClass(dns.enum.IntEnum):
24    """DNS Rdata Class"""
25    RESERVED0 = 0
26    IN = 1
27    INTERNET = IN
28    CH = 3
29    CHAOS = CH
30    HS = 4
31    HESIOD = HS
32    NONE = 254
33    ANY = 255
34
35    @classmethod
36    def _maximum(cls):
37        return 65535
38
39    @classmethod
40    def _short_name(cls):
41        return "class"
42
43    @classmethod
44    def _prefix(cls):
45        return "CLASS"
46
47    @classmethod
48    def _unknown_exception_class(cls):
49        return UnknownRdataclass
50
51
52_metaclasses = {RdataClass.NONE, RdataClass.ANY}
53
54
55class UnknownRdataclass(dns.exception.DNSException):
56    """A DNS class is unknown."""
57
58
59def from_text(text):
60    """Convert text into a DNS rdata class value.
61
62    The input text can be a defined DNS RR class mnemonic or
63    instance of the DNS generic class syntax.
64
65    For example, "IN" and "CLASS1" will both result in a value of 1.
66
67    Raises ``dns.rdatatype.UnknownRdataclass`` if the class is unknown.
68
69    Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535.
70
71    Returns an ``int``.
72    """
73
74    return RdataClass.from_text(text)
75
76
77def to_text(value):
78    """Convert a DNS rdata class value to text.
79
80    If the value has a known mnemonic, it will be used, otherwise the
81    DNS generic class syntax will be used.
82
83    Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535.
84
85    Returns a ``str``.
86    """
87
88    return RdataClass.to_text(value)
89
90
91def is_metaclass(rdclass):
92    """True if the specified class is a metaclass.
93
94    The currently defined metaclasses are ANY and NONE.
95
96    *rdclass* is an ``int``.
97    """
98
99    if rdclass in _metaclasses:
100        return True
101    return False
102
103### BEGIN generated RdataClass constants
104
105RESERVED0 = RdataClass.RESERVED0
106IN = RdataClass.IN
107INTERNET = RdataClass.INTERNET
108CH = RdataClass.CH
109CHAOS = RdataClass.CHAOS
110HS = RdataClass.HS
111HESIOD = RdataClass.HESIOD
112NONE = RdataClass.NONE
113ANY = RdataClass.ANY
114
115### END generated RdataClass constants
116