o
    $i                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZmZ d dl	m
ZmZmZmZ dedeeeef  fdd	Zd
edeeef defddZ
ddee defddZd
edefddZedddefddZd
edefddZe jfde jdefddZdS )    N)closing)	lru_cache)OptionalTupleUnion)build_addressis_ipv6 node_ip_address_from_perspectiveparse_addressaddressreturnc                 C      t | S )zParse a network address string into host and port.

    Args:
        address: The address string to parse (e.g., "localhost:8000", "[::1]:8000").

    Returns:
        Tuple with (host, port) if port found, None if no colon separator.
    )_parse_addressr    r   V/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/_common/network_utils.pyr
         	r
   hostportc                 C   s
   t | |S )zBuild a network address string from host and port.

    Args:
        host: The hostname or IP address.
        port: The port number (int or string).

    Returns:
        Formatted address string (e.g., "localhost:8000" or "[::1]:8000").
    )_build_address)r   r   r   r   r   r      s   

r   c                 C   r   )an  IP address by which the local node can be reached *from* the `address`.

    If no address is given, defaults to public DNS servers for detection.

    Args:
        address: The IP address and port of any known live service on the
            network you care about.

    Returns:
        The IP address by which the local node can be reached from the address.
    )!_node_ip_address_from_perspectiver   r   r   r   r	   '   s   r	   c                 C   r   )zCheck if a host is resolved to IPv6.

    Args:
        host: The IP or domain name to check (must be without port).

    Returns:
        True if the host is resolved to IPv6, False if IPv4.
    )_is_ipv6r   r   r   r   r   6   r   r      )maxsizec               	   C   sP   t jt jfD ]} zt dd| t j}|d d d W   S  ty%   Y qw dS )zgGet localhost loopback ip with IPv4/IPv6 support.

    Returns:
        The localhost loopback IP.
    	localhostNr      	127.0.0.1)socketAF_INETAF_INET6getaddrinfoSOCK_STREAM	Exception)family
dns_resultr   r   r   get_localhost_ipB   s   
r&   c                 C   s   | dv S )zCheck if the given host string represents a localhost address.

    Args:
        host: The hostname or IP address to check.

    Returns:
        True if the host is a localhost address, False otherwise.
    )r   r   z::1r   r   r   r   r   is_localhostW   r   r'   r$   c                 C   sL   t t| tj}|d | d W  d   S 1 sw   Y  dS )zFind a free port on the local machine.

    Args:
        family: The socket address family (AF_INET for IPv4, AF_INET6 for IPv6).
            Defaults to AF_INET.

    Returns:
        An available port number.
    ) r   r   N)r   r   r"   bindgetsockname)r$   sr   r   r   find_free_portc   s   


$r,   )N)r   
contextlibr   	functoolsr   typingr   r   r   ray._rayletr   r   r   r   r	   r   r
   r   strintboolr&   r'   r   AddressFamilyr,   r   r   r   r   <module>   s    