iclab / centinel

http://iclab.org/
MIT License
34 stars 17 forks source link

IPv4 prefixes are added to IPv6 #293

Open arianniaki opened 5 years ago

arianniaki commented 5 years ago

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

zackw commented 5 years ago

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 commented 5 years ago

(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.)