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

31 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 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): 

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 *text*, a ``str``, the textual opcode 

53 

54 Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown. 

55 

56 Returns an ``int``. 

57 """ 

58 

59 return Opcode.from_text(text) 

60 

61 

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

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

64 

65 *flags*, an ``int``, the DNS flags. 

66 

67 Returns an ``int``. 

68 """ 

69 

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

71 

72 

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

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

75 flags. 

76 

77 *value*, an ``int``, the DNS opcode value. 

78 

79 Returns an ``int``. 

80 """ 

81 

82 return (value << 11) & 0x7800 

83 

84 

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

86 """Convert an opcode to text. 

87 

88 *value*, an ``int`` the opcode value, 

89 

90 Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown. 

91 

92 Returns a ``str``. 

93 """ 

94 

95 return Opcode.to_text(value) 

96 

97 

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

99 """Is the opcode in flags UPDATE? 

100 

101 *flags*, an ``int``, the DNS message flags. 

102 

103 Returns a ``bool``. 

104 """ 

105 

106 return from_flags(flags) == Opcode.UPDATE 

107 

108 

109### BEGIN generated Opcode constants 

110 

111QUERY = Opcode.QUERY 

112IQUERY = Opcode.IQUERY 

113STATUS = Opcode.STATUS 

114NOTIFY = Opcode.NOTIFY 

115UPDATE = Opcode.UPDATE 

116 

117### END generated Opcode constants