-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add IPv6 functionality to ip module. #28
Add IPv6 functionality to ip module. #28
Conversation
- remove redundant call to `str` - `get_all_host` list to generator to avoid memory overhead of very large lists
There are only 32 + 128 valid IPv4/6 netmasks, so I created a lookup table for easy validation. If there are concerns about the memory overhead, a computed version is easy enough to implement.
I modified functions where I could to make them compatible with IPv6. Per [RFC4291](https://datatracker.ietf.org/doc/html/rfc4291#section-4), I have assumed any addresses in the ::/8 range are IPv4. The one exception is `cidr_to_netmask`. I added a v6 version of that function.
netutils/ip.py
Outdated
@@ -247,3 +272,25 @@ def get_usable_range(ip_network): | |||
lower_bound = str(net[1]) | |||
upper_bound = str(net[-2]) | |||
return f"{lower_bound} - {upper_bound}" | |||
|
|||
|
|||
def count_bits(i: int) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a private method?
Also, should avoid using single letter variable names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Up to you.
def get_all_host(ip_network): | ||
"""Given a network, return the list of usable IP addresses. | ||
|
||
Args: | ||
ip_network (str): An IP network in string format that is able to be converted by `ipaddress` library. | ||
|
||
Returns: | ||
list: List of usable IP Addresses within network. | ||
generator: Generator of usable IP Addresses within network. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can go either way on this, performance is obviously better as a generator, but just want to make sure it makes sense for the masses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can yank this out if you like.
Thanks!! Mostly just a bunch of nit's, but appreciate the addition. |
Last nit for me, can you remove any single letter variables? |
Adds dual-stack functionality anywhere it was missing. Please see commit comments for more details. The one oddball is
cidr_to_netmask
because of the ambiguity in translating valid masks <= 32.