Debian Codegraph @

icmplib/exceptions.py

raw
1 '''
2 icmplib
3 ~~~~~~~
4
5 Easily forge ICMP packets and make your own ping and traceroute.
6
7 https://github.com/ValentinBELYN/icmplib
8
9 :copyright: Copyright 2017-2023 Valentin BELYN.
10 :license: GNU LGPLv3, see the LICENSE for details.
11
12 ~~~~~~~
13
14 This program is free software: you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public License
16 as published by the Free Software Foundation, either version 3 of
17 the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with this program. If not, see
26 <https://www.gnu.org/licenses/>.
27 '''
28
29
30 class ICMPLibError(Exception):
31 '''
32 Exception class for the icmplib package.
33
34 '''
35
36
37 class NameLookupError(ICMPLibError):
38 '''
39 Raised when the requested name does not exist or cannot be resolved.
40 This concerns both Fully Qualified Domain Names and hostnames.
41
42 '''
43 def __init__(self, name):
44 message = f'The name \'{name}\' cannot be resolved'
45 super().__init__(message)
46
47
48 class ICMPSocketError(ICMPLibError):
49 '''
50 Base class for ICMP sockets exceptions.
51
52 '''
53
54
55 class SocketAddressError(ICMPSocketError):
56 '''
57 Raised when the requested address cannot be assigned to the socket.
58
59 '''
60 def __init__(self, address):
61 message = f'The requested address ({address}) cannot be ' \
62 'assigned to the socket'
63 super().__init__(message)
64
65
66 class SocketPermissionError(ICMPSocketError):
67 '''
68 Raised when the privileges are insufficient to create the socket.
69
70 '''
71 def __init__(self, privileged):
72 if privileged:
73 message = 'Root privileges are required to create the socket'
74 else:
75 message = 'A prior configuration of your OS is required ' \
76 'to use ICMP sockets without root privileges. ' \
77 'Read more on https://github.com/ValentinBELYN' \
78 '/icmplib'
79
80 super().__init__(message)
81
82
83 class SocketUnavailableError(ICMPSocketError):
84 '''
85 Raised when an action is performed while the socket is closed.
86
87 '''
88 def __init__(self):
89 message = 'The socket can no longer be used after its closure'
90 super().__init__(message)
91
92
93 class SocketBroadcastError(ICMPSocketError):
94 '''
95 Raised when a broadcast address is used and the corresponding option
96 is not enabled on the socket.
97
98 '''
99 def __init__(self):
100 message = 'Broadcast is not allowed: ' \
101 'please use the \'broadcast\' property to allow it'
102 super().__init__(message)
103
104
105 class TimeoutExceeded(ICMPSocketError):
106 '''
107 Raised when a timeout occurs on a socket.
108
109 '''
110 def __init__(self, timeout):
111 message = f'The timeout has been reached ({timeout}s)'
112 super().__init__(message)
113
114
115 class ICMPError(ICMPLibError):
116 '''
117 Base class for ICMP error messages.
118
119 '''
120 def __init__(self, message, reply):
121 super().__init__(message)
122 self._reply = reply
123
124 @property
125 def reply(self):
126 return self._reply
127
128
129 class DestinationUnreachable(ICMPError):
130 '''
131 Base class for ICMP Destination Unreachable messages.
132
133 Destination Unreachable message is generated by the host or its
134 inbound gateway to inform the client that the destination is
135 unreachable for some reason.
136
137 '''
138 _CODES = {}
139
140 def __init__(self, reply):
141 if reply.code in self._CODES:
142 message = self._CODES[reply.code]
143 else:
144 message = f'Destination unreachable, bad code: {reply.code}'
145
146 super().__init__(message, reply)
147
148
149 class ICMPv4DestinationUnreachable(DestinationUnreachable):
150 _CODES = {
151 0: 'Destination network unreachable',
152 1: 'Destination host unreachable',
153 2: 'Destination protocol unreachable',
154 3: 'Destination port unreachable',
155 4: 'Fragmentation needed and DF set',
156 5: 'Source route failed',
157 6: 'Destination network unknown',
158 7: 'Destination host unknown',
159 8: 'Source host isolated',
160 9: 'Destination network prohibed',
161 10: 'Destination host prohibed',
162 11: 'Destination network unreachable for ToS',
163 12: 'Destination host unreachable for ToS',
164 13: 'Packet filtered',
165 14: 'Precedence violation',
166 15: 'Precedence cutoff'
167 }
168
169
170 class ICMPv6DestinationUnreachable(DestinationUnreachable):
171 _CODES = {
172 0: 'No route to destination',
173 1: 'Communication with destination administratively prohibited',
174 2: 'Beyond scope of source address',
175 3: 'Address unreachable',
176 4: 'Port unreachable',
177 5: 'Source address failed ingress/egress policy',
178 6: 'Reject route to destination'
179 }
180
181
182 class TimeExceeded(ICMPError):
183 '''
184 Base class for ICMP Time Exceeded messages.
185
186 Time Exceeded message is generated by a gateway to inform the source
187 of a discarded datagram due to the time to live field reaching zero.
188 A Time Exceeded message may also be sent by a host if it fails to
189 reassemble a fragmented datagram within its time limit.
190
191 '''
192 _CODES = {}
193
194 def __init__(self, reply):
195 if reply.code in self._CODES:
196 message = self._CODES[reply.code]
197 else:
198 message = f'Time exceeded, bad code: {reply.code}'
199
200 super().__init__(message, reply)
201
202
203 class ICMPv4TimeExceeded(TimeExceeded):
204 _CODES = {
205 0: 'Time to live exceeded',
206 1: 'Fragment reassembly time exceeded'
207 }
208
209
210 class ICMPv6TimeExceeded(TimeExceeded):
211 _CODES = {
212 0: 'Hop limit exceeded',
213 1: 'Fragment reassembly time exceeded'
214 }