Skip to content
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

Merged
merged 11 commits into from
Jun 24, 2021
Merged

Add IPv6 functionality to ip module. #28

merged 11 commits into from
Jun 24, 2021

Conversation

dgjustice
Copy link
Contributor

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.

- 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 Show resolved Hide resolved
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:
Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@itdependsnetworks
Copy link
Contributor

Thanks!! Mostly just a bunch of nit's, but appreciate the addition.

@itdependsnetworks
Copy link
Contributor

Last nit for me, can you remove any single letter variables?

@itdependsnetworks itdependsnetworks merged commit 631a8aa into networktocode:develop Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants