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

IPv4 prefixes are added to IPv6 #293

Open
arianniaki opened this issue Oct 4, 2018 · 2 comments
Open

IPv4 prefixes are added to IPv6 #293

arianniaki opened this issue Oct 4, 2018 · 2 comments
Labels

Comments

@arianniaki
Copy link
Collaborator

need to add statement to check if IP is IPv6 in order not to add the /24 prefix to it.

@arianniaki arianniaki added the bug label Oct 4, 2018
@zackw
Copy link
Collaborator

zackw commented Oct 4, 2018

Python 3 has a module ipaddress for validating and working with IP address and network literals. Code to do what we want properly would be something like this:

def mask_host_address(addr_str):
    addr = ipaddress.ip_address(addr_str)
    if addr.version == 4:
        masked = ipaddress.IPv4Network((addr, 24), strict=False)
    elif addr.version == 6:
        masked = ipaddress.IPv6Network((addr, 64), strict=False)
    else:
        raise ValueError("don't know proper mask length for '{}' (IPv{})"
                         .format(addr_str, addr.version))
    return str(masked)

This would replace the existing code to tack .0/24 on the end.

@zackw
Copy link
Collaborator

zackw commented Oct 4, 2018

(According to RFC 5375, under normal circumstances you're not supposed to use a network prefix longer than /64 with IPv6, so that is the proper equivalent of a /24 in IPv4.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants