# Path: katprep/exceptions.py
# class InvalidHostnameFormatException(Exception):
#     """
#     Dummy class for invalid hostname formats (non-FQDN)
# 
#     .. class:: InvalidHostnameFormatException
#     """


import socket
from .exceptions import InvalidHostnameFormatException
    :param address: IP address
    :type address: str
    """
    # Friendly inspired by: https://stackoverflow.com/questions/319279/
    # how-to-validate-ip-address-in-python
    try:
        socket.inet_pton(socket.AF_INET6, address)
    except socket.error:
        return False
    return True


def validate_hostname(hostname):
    """
    Validates using a FQDN rather than a short name as some
    APIs are very picky and SSL verification might fail.

    :param hostname: the hostname to validate
    :type hostname: str
    """
    try:
        if hostname == "localhost":
            # get real hostname
            hostname = socket.gethostname()
        if hostname.count(".") != 2:
            # get convert to FQDN if possible
            hostname = socket.getaddrinfo(
                socket.getfqdn(hostname), 0, 0, 0, 0, socket.AI_CANONNAME
            )[0][3]
    except socket.gaierror:

# Path: katprep/exceptions.py
# class InvalidHostnameFormatException(Exception):
#     """
#     Dummy class for invalid hostname formats (non-FQDN)
# 
#     .. class:: InvalidHostnameFormatException
#     """


# Path: katprep/network.py
import socket
from .exceptions import InvalidHostnameFormatException

    :param address: IP address
    :type address: str
    """
    # Friendly inspired by: https://stackoverflow.com/questions/319279/
    # how-to-validate-ip-address-in-python
    try:
        socket.inet_pton(socket.AF_INET6, address)
    except socket.error:
        return False
    return True


def validate_hostname(hostname):
    """
    Validates using a FQDN rather than a short name as some
    APIs are very picky and SSL verification might fail.

    :param hostname: the hostname to validate
    :type hostname: str
    """
    try:
        if hostname == "localhost":
            # get real hostname
            hostname = socket.gethostname()
        if hostname.count(".") != 2:
            # get convert to FQDN if possible
            hostname = socket.getaddrinfo(
                socket.getfqdn(hostname), 0, 0, 0, 0, socket.AI_CANONNAME
            )[0][3]
    except socket.gaierror:

        raise InvalidHostnameFormatException(