| 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 |
|
| 20 |
import dns.enum |
| 21 |
import dns.exception |
| 22 |
|
| 23 |
|
| 24 |
class RdataClass(dns.enum.IntEnum): |
| 25 |
"""DNS Rdata Class""" |
| 26 |
|
| 27 |
RESERVED0 = 0 |
| 28 |
IN = 1 |
| 29 |
INTERNET = IN |
| 30 |
CH = 3 |
| 31 |
CHAOS = CH |
| 32 |
HS = 4 |
| 33 |
HESIOD = HS |
| 34 |
NONE = 254 |
| 35 |
ANY = 255 |
| 36 |
|
| 37 |
@classmethod |
| 38 |
def _maximum(cls): |
| 39 |
return 65535 |
| 40 |
|
| 41 |
@classmethod |
| 42 |
def _short_name(cls): |
| 43 |
return "class" |
| 44 |
|
| 45 |
@classmethod |
| 46 |
def _prefix(cls): |
| 47 |
return "CLASS" |
| 48 |
|
| 49 |
@classmethod |
| 50 |
def _unknown_exception_class(cls): |
| 51 |
return UnknownRdataclass |
| 52 |
|
| 53 |
|
| 54 |
_metaclasses = {RdataClass.NONE, RdataClass.ANY} |
| 55 |
|
| 56 |
|
| 57 |
class UnknownRdataclass(dns.exception.DNSException): |
| 58 |
"""A DNS class is unknown.""" |
| 59 |
|
| 60 |
|
| 61 |
def from_text(text: str) -> RdataClass: |
| 62 |
"""Convert text into a DNS rdata class value. |
| 63 |
|
| 64 |
The input text can be a defined DNS RR class mnemonic or |
| 65 |
instance of the DNS generic class syntax. |
| 66 |
|
| 67 |
For example, "IN" and "CLASS1" will both result in a value of 1. |
| 68 |
|
| 69 |
Raises ``dns.rdatatype.UnknownRdataclass`` if the class is unknown. |
| 70 |
|
| 71 |
Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535. |
| 72 |
|
| 73 |
Returns a ``dns.rdataclass.RdataClass``. |
| 74 |
""" |
| 75 |
|
| 76 |
return RdataClass.from_text(text) |
| 77 |
|
| 78 |
|
| 79 |
def to_text(value: RdataClass) -> str: |
| 80 |
"""Convert a DNS rdata class value to text. |
| 81 |
|
| 82 |
If the value has a known mnemonic, it will be used, otherwise the |
| 83 |
DNS generic class syntax will be used. |
| 84 |
|
| 85 |
Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535. |
| 86 |
|
| 87 |
Returns a ``str``. |
| 88 |
""" |
| 89 |
|
| 90 |
return RdataClass.to_text(value) |
| 91 |
|
| 92 |
|
| 93 |
def is_metaclass(rdclass: RdataClass) -> bool: |
| 94 |
"""True if the specified class is a metaclass. |
| 95 |
|
| 96 |
The currently defined metaclasses are ANY and NONE. |
| 97 |
|
| 98 |
*rdclass* is a ``dns.rdataclass.RdataClass``. |
| 99 |
""" |
| 100 |
|
| 101 |
if rdclass in _metaclasses: |
| 102 |
return True |
| 103 |
return False |
| 104 |
|
| 105 |
|
| 106 |
### BEGIN generated RdataClass constants |
| 107 |
|
| 108 |
RESERVED0 = RdataClass.RESERVED0 |
| 109 |
IN = RdataClass.IN |
| 110 |
INTERNET = RdataClass.INTERNET |
| 111 |
CH = RdataClass.CH |
| 112 |
CHAOS = RdataClass.CHAOS |
| 113 |
HS = RdataClass.HS |
| 114 |
HESIOD = RdataClass.HESIOD |
| 115 |
NONE = RdataClass.NONE |
| 116 |
ANY = RdataClass.ANY |
| 117 |
|
| 118 |
### END generated RdataClass constants |