Closed hSaria closed 3 years ago
Hi @hSaria π
Thank you for using icmplib! The function used to detect the type of IP address is intentionally simple. It has been designed according to the needs of the library and according to its performance. If you want an example that I'm not looking to verify the correctness of the parameters take a look at the is_ipv6_address
function π Generally, the verification of the values is done upstream by the developer when reading its application configuration file or when it asks the user to enter a value.
I agree with you on the second point. Having an option to indicate a specific IP address family could make the function more generic and useful for other purposes. I will implement this for the next version.
However, passing the family=None
parameter to the getaddrinfo
function is not a good idea. By doing this, you perform two DNS lookups (for A and AAAA records). In this case, the function sends unnecessary packets over the network and degrades the performance of the function. It is better to make two getaddrinfo
separately as currently and don't do the second lookup if an IPv4 address has already been found.
Looking at the
is_ipv4_address
method, it seems it uses a somewhat simple method to test for an IPv4 address. Testing999.999.999.999
would evaluate toTrue
and can lead toresolve
assuming that the address is valid.If I may suggest a solution, you could skip the entirety of the manual work and rely solely on
socket.getaddrinfo
which will return the address immediately if it does not require resolution:This will make it so
is_ipv4_address
andis_ipv6_address
are no longer required as the tests are performed bysocket
.In addition, there is no way to specify a preferred address family and IPv4 is a forced-defaulted. When an OS has IPv4 and IPv6 routing (i.e. default route over both families), it will prefer IPv6 when calling
socket.getaddrinfo
by default as it is the newer version.As such, I would suggest that the
resolve
function have a keyword argument that defaults toNone
, but can be set to either4
or6
to force a specific address family.For example: