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

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

69 statements  

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 

20import dns.enum 

21import dns.exception 

22 

23 

24class Rcode(dns.enum.IntEnum): 

25 #: No error 

26 NOERROR = 0 

27 #: Format error 

28 FORMERR = 1 

29 #: Server failure 

30 SERVFAIL = 2 

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

32 NXDOMAIN = 3 

33 #: Not implemented 

34 NOTIMP = 4 

35 #: Refused 

36 REFUSED = 5 

37 #: Name exists. 

38 YXDOMAIN = 6 

39 #: RRset exists. 

40 YXRRSET = 7 

41 #: RRset does not exist. 

42 NXRRSET = 8 

43 #: Not authoritative. 

44 NOTAUTH = 9 

45 #: Name not in zone. 

46 NOTZONE = 10 

47 #: DSO-TYPE Not Implemented 

48 DSOTYPENI = 11 

49 #: Bad EDNS version. 

50 BADVERS = 16 

51 #: TSIG Signature Failure 

52 BADSIG = 16 

53 #: Key not recognized. 

54 BADKEY = 17 

55 #: Signature out of time window. 

56 BADTIME = 18 

57 #: Bad TKEY Mode. 

58 BADMODE = 19 

59 #: Duplicate key name. 

60 BADNAME = 20 

61 #: Algorithm not supported. 

62 BADALG = 21 

63 #: Bad Truncation 

64 BADTRUNC = 22 

65 #: Bad/missing Server Cookie 

66 BADCOOKIE = 23 

67 

68 @classmethod 

69 def _maximum(cls): 

70 return 4095 

71 

72 @classmethod 

73 def _unknown_exception_class(cls) -> type[Exception]: 

74 return UnknownRcode 

75 

76 

77class UnknownRcode(dns.exception.DNSException): 

78 """A DNS rcode is unknown.""" 

79 

80 

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

82 """Convert text into an rcode. 

83 

84 :param text: The textual rcode or an integer in textual form. 

85 :type text: str 

86 :raises dns.rcode.UnknownRcode: If the rcode mnemonic is unknown. 

87 :rtype: :py:class:`dns.rcode.Rcode` 

88 """ 

89 

90 return Rcode.from_text(text) 

91 

92 

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

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

95 

96 :param flags: The DNS flags field. 

97 :type flags: int 

98 :param ednsflags: The EDNS flags field. 

99 :type ednsflags: int 

100 :raises ValueError: If the rcode is < 0 or > 4095. 

101 :rtype: :py:class:`dns.rcode.Rcode` 

102 """ 

103 

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

105 return Rcode.make(value) 

106 

107 

108def to_flags(value: Rcode) -> tuple[int, int]: 

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

110 

111 :param value: The rcode. 

112 :type value: :py:class:`dns.rcode.Rcode` 

113 :raises ValueError: If the rcode is < 0 or > 4095. 

114 :rtype: tuple[int, int] 

115 """ 

116 

117 if value < 0 or value > 4095: 

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

119 v = value & 0xF 

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

121 return (v, ev) 

122 

123 

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

125 """Convert rcode into text. 

126 

127 :param value: The rcode. 

128 :type value: :py:class:`dns.rcode.Rcode` 

129 :raises ValueError: If the rcode is < 0 or > 4095. 

130 :rtype: str 

131 """ 

132 

133 if tsig and value == Rcode.BADVERS: 

134 return "BADSIG" 

135 return Rcode.to_text(value) 

136 

137 

138### BEGIN generated Rcode constants 

139 

140NOERROR = Rcode.NOERROR 

141FORMERR = Rcode.FORMERR 

142SERVFAIL = Rcode.SERVFAIL 

143NXDOMAIN = Rcode.NXDOMAIN 

144NOTIMP = Rcode.NOTIMP 

145REFUSED = Rcode.REFUSED 

146YXDOMAIN = Rcode.YXDOMAIN 

147YXRRSET = Rcode.YXRRSET 

148NXRRSET = Rcode.NXRRSET 

149NOTAUTH = Rcode.NOTAUTH 

150NOTZONE = Rcode.NOTZONE 

151DSOTYPENI = Rcode.DSOTYPENI 

152BADVERS = Rcode.BADVERS 

153BADSIG = Rcode.BADSIG 

154BADKEY = Rcode.BADKEY 

155BADTIME = Rcode.BADTIME 

156BADMODE = Rcode.BADMODE 

157BADNAME = Rcode.BADNAME 

158BADALG = Rcode.BADALG 

159BADTRUNC = Rcode.BADTRUNC 

160BADCOOKIE = Rcode.BADCOOKIE 

161 

162### END generated Rcode constants