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

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

32 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 Opcodes.""" 

19 

20import dns.enum 

21import dns.exception 

22 

23 

24class Opcode(dns.enum.IntEnum): 

25 #: Query 

26 QUERY = 0 

27 #: Inverse Query (historical) 

28 IQUERY = 1 

29 #: Server Status (unspecified and unimplemented anywhere) 

30 STATUS = 2 

31 #: Notify 

32 NOTIFY = 4 

33 #: Dynamic Update 

34 UPDATE = 5 

35 

36 @classmethod 

37 def _maximum(cls): 

38 return 15 

39 

40 @classmethod 

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

42 return UnknownOpcode 

43 

44 

45class UnknownOpcode(dns.exception.DNSException): 

46 """An DNS opcode is unknown.""" 

47 

48 

49def from_text(text: str) -> Opcode: 

50 """Convert text into an opcode. 

51 

52 :param text: The textual opcode. 

53 :type text: str 

54 :raises dns.opcode.UnknownOpcode: If the opcode is unknown. 

55 :rtype: :py:class:`dns.opcode.Opcode` 

56 """ 

57 

58 return Opcode.from_text(text) 

59 

60 

61def from_flags(flags: int) -> Opcode: 

62 """Extract an opcode from DNS message flags. 

63 

64 :param flags: The DNS message flags. 

65 :type flags: int 

66 :rtype: :py:class:`dns.opcode.Opcode` 

67 """ 

68 

69 return Opcode((flags & 0x7800) >> 11) 

70 

71 

72def to_flags(value: Opcode) -> int: 

73 """Convert an opcode to a value suitable for ORing into DNS message 

74 flags. 

75 

76 :param value: The DNS opcode value. 

77 :type value: :py:class:`dns.opcode.Opcode` 

78 :rtype: int 

79 """ 

80 

81 return (value << 11) & 0x7800 

82 

83 

84def to_text(value: Opcode) -> str: 

85 """Convert an opcode to text. 

86 

87 :param value: The opcode value. 

88 :type value: :py:class:`dns.opcode.Opcode` 

89 :raises dns.opcode.UnknownOpcode: If the opcode is unknown. 

90 :rtype: str 

91 """ 

92 

93 return Opcode.to_text(value) 

94 

95 

96def is_update(flags: int) -> bool: 

97 """Is the opcode in flags UPDATE? 

98 

99 :param flags: The DNS message flags. 

100 :type flags: int 

101 :rtype: bool 

102 """ 

103 

104 return from_flags(flags) == Opcode.UPDATE 

105 

106 

107### BEGIN generated Opcode constants 

108 

109QUERY = Opcode.QUERY 

110IQUERY = Opcode.IQUERY 

111STATUS = Opcode.STATUS 

112NOTIFY = Opcode.NOTIFY 

113UPDATE = Opcode.UPDATE 

114 

115### END generated Opcode constants