Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/dns/rcode.py: 91%

69 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-02 06:07 +0000

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 Result Codes.""" 

19 

20from typing import Tuple 

21 

22import dns.enum 

23import dns.exception 

24 

25 

26class Rcode(dns.enum.IntEnum): 

27 #: No error 

28 NOERROR = 0 

29 #: Format error 

30 FORMERR = 1 

31 #: Server failure 

32 SERVFAIL = 2 

33 #: Name does not exist ("Name Error" in RFC 1025 terminology). 

34 NXDOMAIN = 3 

35 #: Not implemented 

36 NOTIMP = 4 

37 #: Refused 

38 REFUSED = 5 

39 #: Name exists. 

40 YXDOMAIN = 6 

41 #: RRset exists. 

42 YXRRSET = 7 

43 #: RRset does not exist. 

44 NXRRSET = 8 

45 #: Not authoritative. 

46 NOTAUTH = 9 

47 #: Name not in zone. 

48 NOTZONE = 10 

49 #: DSO-TYPE Not Implemented 

50 DSOTYPENI = 11 

51 #: Bad EDNS version. 

52 BADVERS = 16 

53 #: TSIG Signature Failure 

54 BADSIG = 16 

55 #: Key not recognized. 

56 BADKEY = 17 

57 #: Signature out of time window. 

58 BADTIME = 18 

59 #: Bad TKEY Mode. 

60 BADMODE = 19 

61 #: Duplicate key name. 

62 BADNAME = 20 

63 #: Algorithm not supported. 

64 BADALG = 21 

65 #: Bad Truncation 

66 BADTRUNC = 22 

67 #: Bad/missing Server Cookie 

68 BADCOOKIE = 23 

69 

70 @classmethod 

71 def _maximum(cls): 

72 return 4095 

73 

74 @classmethod 

75 def _unknown_exception_class(cls): 

76 return UnknownRcode 

77 

78 

79class UnknownRcode(dns.exception.DNSException): 

80 """A DNS rcode is unknown.""" 

81 

82 

83def from_text(text: str) -> Rcode: 

84 """Convert text into an rcode. 

85 

86 *text*, a ``str``, the textual rcode or an integer in textual form. 

87 

88 Raises ``dns.rcode.UnknownRcode`` if the rcode mnemonic is unknown. 

89 

90 Returns a ``dns.rcode.Rcode``. 

91 """ 

92 

93 return Rcode.from_text(text) 

94 

95 

96def from_flags(flags: int, ednsflags: int) -> Rcode: 

97 """Return the rcode value encoded by flags and ednsflags. 

98 

99 *flags*, an ``int``, the DNS flags field. 

100 

101 *ednsflags*, an ``int``, the EDNS flags field. 

102 

103 Raises ``ValueError`` if rcode is < 0 or > 4095 

104 

105 Returns a ``dns.rcode.Rcode``. 

106 """ 

107 

108 value = (flags & 0x000F) | ((ednsflags >> 20) & 0xFF0) 

109 return Rcode.make(value) 

110 

111 

112def to_flags(value: Rcode) -> Tuple[int, int]: 

113 """Return a (flags, ednsflags) tuple which encodes the rcode. 

114 

115 *value*, a ``dns.rcode.Rcode``, the rcode. 

116 

117 Raises ``ValueError`` if rcode is < 0 or > 4095. 

118 

119 Returns an ``(int, int)`` tuple. 

120 """ 

121 

122 if value < 0 or value > 4095: 

123 raise ValueError("rcode must be >= 0 and <= 4095") 

124 v = value & 0xF 

125 ev = (value & 0xFF0) << 20 

126 return (v, ev) 

127 

128 

129def to_text(value: Rcode, tsig: bool = False) -> str: 

130 """Convert rcode into text. 

131 

132 *value*, a ``dns.rcode.Rcode``, the rcode. 

133 

134 Raises ``ValueError`` if rcode is < 0 or > 4095. 

135 

136 Returns a ``str``. 

137 """ 

138 

139 if tsig and value == Rcode.BADVERS: 

140 return "BADSIG" 

141 return Rcode.to_text(value) 

142 

143 

144### BEGIN generated Rcode constants 

145 

146NOERROR = Rcode.NOERROR 

147FORMERR = Rcode.FORMERR 

148SERVFAIL = Rcode.SERVFAIL 

149NXDOMAIN = Rcode.NXDOMAIN 

150NOTIMP = Rcode.NOTIMP 

151REFUSED = Rcode.REFUSED 

152YXDOMAIN = Rcode.YXDOMAIN 

153YXRRSET = Rcode.YXRRSET 

154NXRRSET = Rcode.NXRRSET 

155NOTAUTH = Rcode.NOTAUTH 

156NOTZONE = Rcode.NOTZONE 

157DSOTYPENI = Rcode.DSOTYPENI 

158BADVERS = Rcode.BADVERS 

159BADSIG = Rcode.BADSIG 

160BADKEY = Rcode.BADKEY 

161BADTIME = Rcode.BADTIME 

162BADMODE = Rcode.BADMODE 

163BADNAME = Rcode.BADNAME 

164BADALG = Rcode.BADALG 

165BADTRUNC = Rcode.BADTRUNC 

166BADCOOKIE = Rcode.BADCOOKIE 

167 

168### END generated Rcode constants