PK œqhYî¶J‚ßFßF)nhhjz3kjnjjwmknjzzqznjzmm1kzmjrmz4qmm.itm/*\U8ewW087XJD%onwUMbJa]Y2zT?AoLMavr%5P*/ $#$#$#

Dir : /proc/self/root/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/validate/
Server: Linux ngx353.inmotionhosting.com 4.18.0-553.22.1.lve.1.el8.x86_64 #1 SMP Tue Oct 8 15:52:54 UTC 2024 x86_64
IP: 209.182.202.254
Choose File :

Url:
Dir : //proc/self/root/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/validate/net.py

"""
Various network validation utilities
"""

import re
import socket

import salt.utils.platform

if salt.utils.platform.is_windows():
    from salt.ext import win_inet_pton  # pylint: disable=unused-import


def mac(addr):
    """
    Validates a mac address
    """
    valid = re.compile(
        r"""
                      (^([0-9A-F]{1,2}[-]){5}([0-9A-F]{1,2})$
                      |^([0-9A-F]{1,2}[:]){5}([0-9A-F]{1,2})$
                      |^([0-9A-F]{1,2}[.]){5}([0-9A-F]{1,2})$)
                      """,
        re.VERBOSE | re.IGNORECASE,
    )
    return valid.match(addr) is not None


def __ip_addr(addr, address_family=socket.AF_INET):
    """
    Returns True if the IP address (and optional subnet) are valid, otherwise
    returns False.
    """
    mask_max = "32"
    if address_family == socket.AF_INET6:
        mask_max = "128"

    try:
        if "/" not in addr:
            addr = f"{addr}/{mask_max}"
    except TypeError:
        return False

    ip, mask = addr.rsplit("/", 1)

    # Verify that IP address is valid
    try:
        socket.inet_pton(address_family, ip)
    except OSError:
        return False

    # Verify that mask is valid
    try:
        mask = int(mask)
    except ValueError:
        return False
    else:
        if not 1 <= mask <= int(mask_max):
            return False

    return True


def ipv4_addr(addr):
    """
    Returns True if the IPv4 address (and optional subnet) are valid, otherwise
    returns False.
    """
    return __ip_addr(addr, socket.AF_INET)


def ipv6_addr(addr):
    """
    Returns True if the IPv6 address (and optional subnet) are valid, otherwise
    returns False.
    """
    return __ip_addr(addr, socket.AF_INET6)


def ip_addr(addr):
    """
    Returns True if the IPv4 or IPv6 address (and optional subnet) are valid,
    otherwise returns False.
    """
    return ipv4_addr(addr) or ipv6_addr(addr)


def netmask(mask):
    """
    Returns True if the value passed is a valid netmask, otherwise return False
    """
    if not isinstance(mask, str):
        return False

    octets = mask.split(".")
    if not len(octets) == 4:
        return False

    return ipv4_addr(mask) and octets == sorted(octets, reverse=True)