Commit 0444f1bf authored by Alexandre's avatar Alexandre
Browse files

Split logic with IP family retrival

parent 2bd84f92
......@@ -179,6 +179,7 @@ def canonicalize(hostname):
return result
def is_valid_ip_address(addr):
""" Return True and the address family if the IP address is valid. """
try:
baddr = netaddr.IPAddress(addr)
except netaddr.core.AddrFormatError:
......@@ -255,23 +256,41 @@ def _validate_hostname(hostname, cert):
return True
return False
def check_ip_address(addr, dot=False):
repraddress = addr
(is_addr, family) = is_valid_ip_address(addr)
if not is_addr and not dot:
raise CustomException("%s is not IPv4 and not IPv6" % addr)
def get_addrfamily(addr, dot=False):
"""Return the family as a socket object of the address."""
(is_ip, family) = is_valid_ip_address(addr)
# thoses checks between the IP family and the command line option
# might need to land somewhere else
if opts.forceIPv4 and family == 6:
raise CustomException("You cannot force IPv4 with a litteral IPv6 address (%s)" % addr)
elif opts.forceIPv6 and family == 4:
raise CustomException("You cannot force IPv6 with a litteral IPv4 address (%s)" % addr)
if not is_ip and not dot:
raise CustomException("%s is not IPv4 and not IPv6" % addr)
if opts.forceIPv4 or family == 4:
family = socket.AF_INET
repraddress = addr
elif opts.forceIPv6 or family == 6:
family = socket.AF_INET6
repraddress = f'[{addr}]'
else:
family = 0
return family
def get_repraddr(ipaddr, sfamily):
if sfamily == socket.AF_INET6:
return ("[%s]" % ipaddr)
return ipaddr
def check_ip_address(addr, dot=False):
family = get_addrfamily(addr, dot)
if not dot:
repraddress = get_repraddr(addr, family)
else:
repraddress = addr
return (family, repraddress)
def dump_data(data, text="data"):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment