| 1 | # Copyright (C) 2001-2007, 2009, 2010 Nominum, Inc.
|
|---|
| 2 | #
|
|---|
| 3 | # Permission to use, copy, modify, and distribute this software and its
|
|---|
| 4 | # documentation for any purpose with or without fee is hereby granted,
|
|---|
| 5 | # provided that the above copyright notice and this permission notice
|
|---|
| 6 | # appear in all copies.
|
|---|
| 7 | #
|
|---|
| 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
|
|---|
| 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|---|
| 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
|
|---|
| 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|---|
| 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|---|
| 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|---|
| 14 | # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|---|
| 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 | # We construct the inverse mapping programmatically to ensure that we
|
|---|
| 47 | # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
|
|---|
| 48 | # would cause the mapping not to be true inverse.
|
|---|
| 49 |
|
|---|
| 50 | _by_value = dict([(y, x) for x, y in _by_text.iteritems()])
|
|---|
| 51 |
|
|---|
| 52 | # Now that we've built the inverse map, we can add class aliases to
|
|---|
| 53 | # the _by_text mapping.
|
|---|
| 54 |
|
|---|
| 55 | _by_text.update({
|
|---|
| 56 | 'INTERNET' : IN,
|
|---|
| 57 | 'CHAOS' : CH,
|
|---|
| 58 | 'HESIOD' : HS
|
|---|
| 59 | })
|
|---|
| 60 |
|
|---|
| 61 | _metaclasses = {
|
|---|
| 62 | NONE : True,
|
|---|
| 63 | ANY : True
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | _unknown_class_pattern = re.compile('CLASS([0-9]+)$', re.I);
|
|---|
| 67 |
|
|---|
| 68 | class UnknownRdataclass(dns.exception.DNSException):
|
|---|
| 69 | """Raised when a class is unknown."""
|
|---|
| 70 | pass
|
|---|
| 71 |
|
|---|
| 72 | def from_text(text):
|
|---|
| 73 | """Convert text into a DNS rdata class value.
|
|---|
| 74 | @param text: the text
|
|---|
| 75 | @type text: string
|
|---|
| 76 | @rtype: int
|
|---|
| 77 | @raises dns.rdataclass.UnknownRdataClass: the class is unknown
|
|---|
| 78 | @raises ValueError: the rdata class value is not >= 0 and <= 65535
|
|---|
| 79 | """
|
|---|
| 80 |
|
|---|
| 81 | value = _by_text.get(text.upper())
|
|---|
| 82 | if value is None:
|
|---|
| 83 | match = _unknown_class_pattern.match(text)
|
|---|
| 84 | if match == None:
|
|---|
| 85 | raise UnknownRdataclass
|
|---|
| 86 | value = int(match.group(1))
|
|---|
| 87 | if value < 0 or value > 65535:
|
|---|
| 88 | raise ValueError("class must be between >= 0 and <= 65535")
|
|---|
| 89 | return value
|
|---|
| 90 |
|
|---|
| 91 | def to_text(value):
|
|---|
| 92 | """Convert a DNS rdata class to text.
|
|---|
| 93 | @param value: the rdata class value
|
|---|
| 94 | @type value: int
|
|---|
| 95 | @rtype: string
|
|---|
| 96 | @raises ValueError: the rdata class value is not >= 0 and <= 65535
|
|---|
| 97 | """
|
|---|
| 98 |
|
|---|
| 99 | if value < 0 or value > 65535:
|
|---|
| 100 | raise ValueError("class must be between >= 0 and <= 65535")
|
|---|
| 101 | text = _by_value.get(value)
|
|---|
| 102 | if text is None:
|
|---|
| 103 | text = 'CLASS' + `value`
|
|---|
| 104 | return text
|
|---|
| 105 |
|
|---|
| 106 | def is_metaclass(rdclass):
|
|---|
| 107 | """True if the class is a metaclass.
|
|---|
| 108 | @param rdclass: the rdata class
|
|---|
| 109 | @type rdclass: int
|
|---|
| 110 | @rtype: bool"""
|
|---|
| 111 |
|
|---|
| 112 | if _metaclasses.has_key(rdclass):
|
|---|
| 113 | return True
|
|---|
| 114 | return False
|
|---|