Debian Codegraph @

icmplib/utils.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 import socket, asyncio
30
31 from threading import Lock
32 from sys import platform
33 from os import getpid
34 from re import match
35 from random import choices
36
37 from .exceptions import NameLookupError
38
39
40 PID = getpid()
41 PLATFORM_LINUX = platform == 'linux'
42 PLATFORM_MACOS = platform == 'darwin'
43 PLATFORM_WINDOWS = platform == 'win32'
44
45 _lock_id = Lock()
46 _current_id = PID
47
48
49 def random_byte_message(size):
50 '''
51 Generate a random byte sequence of the specified size.
52
53 '''
54 sequence = choices(
55 b'abcdefghijklmnopqrstuvwxyz'
56 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
57 b'1234567890', k=size)
58
59 return bytes(sequence)
60
61
62 def unique_identifier():
63 '''
64 Generate a unique identifier between 0 and 65535.
65 The first number generated will be equal to the PID + 1.
66
67 '''
68 global _current_id
69
70 with _lock_id:
71 _current_id += 1
72 _current_id &= 0xffff
73
74 return _current_id
75
76
77 def resolve(name, family=None):
78 '''
79 Resolve a hostname or FQDN to an IP address. Depending on the name
80 specified in parameters, several IP addresses may be returned.
81
82 This function relies on the DNS name server configured on your
83 operating system.
84
85 :type name: str
86 :param name: A hostname or a Fully Qualified Domain Name (FQDN).
87
88 :type family: int, optional
89 :param family: The address family. Can be set to `4` for IPv4 or `6`
90 for IPv6 addresses. By default, this function searches for IPv4
91 addresses first for compatibility reasons (A DNS lookup) before
92 searching for IPv6 addresses (AAAA DNS lookup).
93
94 :rtype: list[str]
95 :returns: A list of IP addresses corresponding to the name passed as
96 a parameter.
97
98 :raises NameLookupError: If the requested name does not exist or
99 cannot be resolved.
100
101 '''
102 try:
103 if family == 6:
104 _family = socket.AF_INET6
105 else:
106 _family = socket.AF_INET
107
108 lookup = socket.getaddrinfo(
109 host=name,
110 port=None,
111 family=_family,
112 type=socket.SOCK_DGRAM)
113
114 return [address[4][0] for address in lookup]
115
116 except OSError:
117 if not family:
118 return resolve(name, 6)
119
120 raise NameLookupError(name)
121
122
123 async def async_resolve(name, family=None):
124 '''
125 Resolve a hostname or FQDN to an IP address. Depending on the name
126 specified in parameters, several IP addresses may be returned.
127
128 This function relies on the DNS name server configured on your
129 operating system.
130
131 This function is non-blocking.
132
133 :type name: str
134 :param name: A hostname or a Fully Qualified Domain Name (FQDN).
135
136 :type family: int, optional
137 :param family: The address family. Can be set to `4` for IPv4 or `6`
138 for IPv6 addresses. By default, this function searches for IPv4
139 addresses first for compatibility reasons (A DNS lookup) before
140 searching for IPv6 addresses (AAAA DNS lookup).
141
142 :rtype: list[str]
143 :returns: A list of IP addresses corresponding to the name passed as
144 a parameter.
145
146 :raises NameLookupError: If the requested name does not exist or
147 cannot be resolved.
148
149 '''
150 try:
151 if family == 6:
152 _family = socket.AF_INET6
153 else:
154 _family = socket.AF_INET
155
156 loop = asyncio.get_running_loop()
157
158 lookup = await loop.getaddrinfo(
159 host=name,
160 port=None,
161 family=_family,
162 type=socket.SOCK_DGRAM)
163
164 return [address[4][0] for address in lookup]
165
166 except OSError:
167 if not family:
168 return await async_resolve(name, 6)
169
170 raise NameLookupError(name)
171
172
173 def is_hostname(name):
174 '''
175 Indicate whether the specified name is a hostname or an FQDN.
176 Return a `boolean`.
177
178 '''
179 pattern = r'(?i)^([a-z0-9-]+|([a-z0-9_-]+[.])+[a-z]+)$'
180 return match(pattern, name) is not None
181
182
183 def is_ipv4_address(address):
184 '''
185 Indicate whether the specified address is an IPv4 address.
186 Return a `boolean`.
187
188 '''
189 pattern = r'^([0-9]{1,3}[.]){3}[0-9]{1,3}$'
190 return match(pattern, address) is not None
191
192
193 def is_ipv6_address(address):
194 '''
195 Indicate whether the specified address is an IPv6 address.
196 Return a `boolean`.
197
198 '''
199 return ':' in address