agdsn / pycroft

The AG DSN management system
Apache License 2.0
20 stars 9 forks source link

Replace ipaddr use #150

Closed sebschrader closed 3 months ago

sebschrader commented 6 years ago

The ipaddr module we use, was the original basis for the ipadress module that was included in Python 3.3 and backported. The modules are not API-compatible however, so we can't just replace the ipaddr imports with ipaddress.

ipaddr will die, we're currently even using the git master version instead of a PyPI release. We should replace it. I suggest we use the popular netaddr library used by e.g. Ansible and Hades, instead of the standard library ipaddress. netaddr is much easier to use and provides features that ipaddress doesn't, for example mac address types.

The main advantage of netaddr is the handling of IP networks that have hosts bits set. With ipaddress you have to use three different types: IPv4Address (no prefix), IPv4Network (with subnet prefix, but hosts bits must be zero), IPv4Interface (with prefix and host bits may be non-zero). Secondly you have to use different types for IPv6. netaddr has combined types for IPv4 and IPv6, but you can use specific ones if you want to and it's much easier to use addresses with non-zero host bits. There are many more goodies, such as IP range types, supernets and subnets, iteration over IP address and subnets, etc.

Hades already includes custom SQLAlchemy types for PostgreSQL's inet and macaddr types.

This is not a high priority task, but it should be tracked.

sebschrader commented 6 years ago

And I totally forgot, netaddr supports the very important RFC 1924 😉.

lukasjuhrich commented 8 months ago

This would also remove one more library which does not provide wheels (netaddr does).

lukasjuhrich commented 3 months ago

@sebschrader I'm in the process of doing that. However, I am confused as to the difference fo the following two functions:

https://github.com/agdsn/pycroft/blob/3a5551ea1662af63d31e999ee60b5b4eea0800b1/pycroft/helpers/net.py#L103-L110

https://github.com/agdsn/pycroft/blob/3a5551ea1662af63d31e999ee60b5b4eea0800b1/pycroft/lib/net.py#L118-L130

Both functions are unused and seem to want create the reverse record, but ptr_name does some odd thing relative to the netowrk. The first function clearly does the same thing as netaddr.IPAddress.reverse_dns, but what does ptr_name do?

In [1]: from netaddr import *
In [2]: i = IPAddress("141.30.228.39")
In [3]: i.reverse_dns
Out[3]: '39.228.30.141.in-addr.arpa.'