HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/net/__pycache__/__init__.cpython-310.pyc
o

�Ad;��@s�ddlZddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZddlm
Z
ddlmZmZddlmZmZe�e�ZdZdZgd�Zgd	�Ze�d
�fdd�Zd
d�Zd�dd�Z				d�dd�Zd�dd�Z dd�Z!dd�Z"dd�Z#dd�Z$dd�Z%d d!�Z&d"d#�Z'ej(dd$�d%e)fd&d'��Z*ej(dd$�d%e+fd(d)��Z,d*e-d%e)fd+d,�Z.d�d-d.�Z/d/d0�Z0d1d2�Z1d�d%e)fd3d4�Z2d�d5d6�Z3d�d7d8�Z4d9d:�Z5d;d<�Z6d=d>�Z7d?d@�Z8dAdB�Z9GdCdD�dDe:�Z;dEdF�Z<	d�dGee
e-d%e
e-fdHdI�Z=	d�dGee
e-d%ee-fdJdK�Z>	d�dGee
e-d%e
e-fdLdM�Z?	d�dGee
e-d%ee-fdNdO�Z@	d�dGee
e-d%e
e-fdPdQ�ZA	d�dGee
e-d%ee-fdRdS�ZB	d�dGee
e-d%e
e-fdTdU�ZC	d�dGee
e-d%ee-fdVdW�ZDd�dXdY�ZEdZd[�ZFd�d]d^�ZGd�d`da�ZH	d�dbdc�ZIddde�ZJdfdg�ZKd�d%eLfdhdi�ZMdje-d%ee-fdkdl�ZNd�d%eLfdmdn�ZOd�d%eLfdodp�ZPd�d%eLfdqdr�ZQd�d%eLfdsdt�ZRd�d%e+fdudv�ZSdwdx�ZTdye	e-efd%e)fdzd{�ZUd|ed}e-fd~d�ZVd}e-d%e)fd�d��ZWd}e-d%e)fd�d��ZXd}e-d%e)fd�d��ZYd}e-d%e)fd�d��ZZd}e-d%e)fd�d��Z[d}e-d%e)fd�d��Z\d%e)fd�d��Z]d%e-fd�d��Z^d%e_fd�d��Z`d%e_fd�d��Zad�e-d�e-d%e-fd�d��ZbGd�d��d�ec�ZddS)��N)�Any�Callable�Dict�List�Optional)�urlparse)�subp�util)�UrlError�readurlz/sys/class/net/�eth0)�dhcp6�
ipv6_slaaczipv6_dhcpv6-statelesszipv6_dhcpv6-stateful)�	ovs-vsctlz--format�csvz
--no-headingsz	--timeout�10z	--columns�name�find�	interfacez
type=internalz([0-9]+)cCsdd�t�||�D�S)aDSorting for Humans: natural sort order. Can be use as the key to sort
    functions.
    This will sort ['eth0', 'ens3', 'ens10', 'ens12', 'ens8', 'ens0'] as
    ['ens0', 'ens3', 'ens8', 'ens10', 'ens12', 'eth0'] instead of the simple
    python way which will produce ['ens0', 'ens10', 'ens12', 'ens3', 'ens8',
    'eth0'].cSs$g|]}|��rt|�n|���qS�)�isdigit�int�lower)�.0�textrr�8/usr/lib/python3/dist-packages/cloudinit/net/__init__.py�
<listcomp>3s��z$natural_sort_key.<locals>.<listcomp>)�re�split)�s�_nsrerrr�natural_sort_key,s
�r!cCstS)z3Simple function to return the global SYS_CLASS_NET.)�
SYS_CLASS_NETrrrr�get_sys_class_path9sr#�cCst�|d|S)N�/)r#��devname�pathrrr�sys_dev_path>�r)c

Cs�t||�}zt�|�}Wn?ttfyK}z1t|dd�}	|	tjtjfvr2|dur2||�WYd}~S|	tj	fvrF|durF||�WYd}~S�d}~ww|�
�}|durV|Sz||WSty}}z|durq||�WYd}~St�
d||��d}~ww)N�errnoz5Found unexpected (not translatable) value '%s' in '%s)r)r	�	load_file�OSError�IOError�getattrr+�ENOENT�ENOTDIR�EINVAL�strip�KeyError�LOG�debug)
r'r(�	translate�	on_enoent�on_keyerror�	on_einval�dev_path�contents�e�e_errnorrr�read_sys_netBs<
��	
���r?cCsdd�}t||||||d�S)NcSsdS�NFr)r=rrr�
on_excp_falsehsz(read_sys_net_safe.<locals>.on_excp_false)r9r8r:r7)r?)�iface�fieldr7rArrr�read_sys_net_safegs�rDcCs6t||�}|durdSzt|�WStyYdSwr@)rDr�
ValueError)rBrC�valrrr�read_sys_net_intus

�rGcCsdddd�}t|d|d�S)NTF)�up�unknown�down�	operstate)r7�rD)r'r7rrr�is_upsrMcC�tj�t|d��S)N�bridge��osr(�existsr)�r'rrr�	is_bridge�r*rTcCrN)N�bondingrPrSrrr�is_bond�r*rVcCs t|dd�}tj�|�r|SdS)z8Return the master path for devname, or None if no master�master�r(N)r)rQr(rRr&rrr�
get_master�srYcCsHt|�}|dur
dStj�|d�}tj�|d�}tj�|�p#tj�|�S)z@Return a bool indicating if devname's master is a bridge or bondNFrUrO)rYrQr(�joinrR)r'�master_path�bonding_path�bridge_pathrrr�master_is_bridge_or_bond�sr^cCs,t|�}|dur
dSt|dd�}tj�|�S)z;Return a bool indicating if devname's master is openvswitchNFzupper_ovs-systemrX)rYr)rQr(rR)r'r[�ovs_pathrrr�master_is_openvswitch�s
r`)�maxsize�returncCs tt�d��}|st�d�|S)zDReturn a bool indicating if Open vSwitch is installed in the system.rz<ovs-vsctl not in PATH; not detecting Open vSwitch interfaces)�boolr�whichr5r6)�retrrr�openvswitch_is_installed�s�rfc
CsZzt�t�\}}W|��Stjy,}zd|jvr't�d�gWYd}~S�d}~ww)z�Return a list of the names of OVS internal interfaces on the system.

    These will all be strings, and are used to exclude OVS-specific interface
    from cloud-init's network configuration handling.
    zdatabase connection failedzJOpen vSwitch is not yet up; no interfaces will be detected as OVS-internalN)r�!OVS_INTERNAL_INTERFACE_LOOKUP_CMD�ProcessExecutionError�stderrr5�info�
splitlines)�out�_err�excrrr�get_ovs_internal_interfaces�s
�
���ror'cCs,t�sdSt�}||vrt�d|�dSdS)z�Returns True if this is an OVS internal interface.

    If OVS is not installed or not yet running, this will return False.
    FzDetected %s as an OVS interfaceT)rfror5r6)r'�ovs_bridgesrrr�!is_openvswitch_internal_interface�srqcCs,|durt|�}t||�st||�rdSdS)z�netfailover driver uses 3 nics, master, primary and standby.
    this returns True if the device is either the primary or standby
    as these devices are to be ignored.
    NTF)�
device_driver�is_netfail_primary�is_netfail_standby�r'�driverrrr�is_netfailover�s�rwcCs*d}zt|d�}W|StyY|Sw)zDReturns a str from reading /sys/class/net/<devname>/device/features.r$zdevice/features)r?�	Exception�r'�featuresrrr�get_dev_features�s��r{cCs(t|�}|rt|�dkrdS|ddkS)z� Return True if VIRTIO_NET_F_STANDBY bit (62) is set.

    https://github.com/torvalds/linux/blob/         089cf7f6ecb266b6a4164919a2e69bd2f938374a/         include/uapi/linux/virtio_net.h#L60
    �@F�>�1)r{�lenryrrr�has_netfail_standby_feature�sr�cCs<t|�durdS|durt|�}|dkrdSt|�sdSdS)z�A device is a "netfail master" device if:

    - The device does NOT have the 'master' sysfs attribute
    - The device driver is 'virtio_net'
    - The device has the standby feature bit set

    Return True if all of the above is True.
    NF�
virtio_netT�rYrrr�rurrr�is_netfail_master��	r�cCstt|dd�}tj�|�sdS|durt|�}|dkrdStj�tj�|��}t|�}|dkr0dSt|�}|s8dSdS)a7A device is a "netfail primary" device if:

    - the device has a 'master' sysfs file
    - the device driver is not 'virtio_net'
    - the 'master' sysfs file points to device with virtio_net driver
    - the 'master' device has the 'standby' feature bit set

    Return True if all of the above is True.
    rWrXFNr�T)r)rQr(rRrr�basename�realpathr�)r'rv�master_sysfs_path�master_devname�
master_driver�master_has_standbyrrrrssrscCs<t|�durdS|durt|�}|dkrdSt|�sdSdS)z�A device is a "netfail standby" device if:

    - The device has a 'master' sysfs attribute
    - The device driver is 'virtio_net'
    - The device has the standby feature bit set

    Return True if all of the above is True.
    NFr�Tr�rurrrrt6r�rtcCst|d�}|r
|dvr
dSdS)a�
    /* interface name assignment types (sysfs name_assign_type attribute) */
    #define NET_NAME_UNKNOWN      0  /* unknown origin (not exposed to user) */
    #define NET_NAME_ENUM         1  /* enumerated by kernel */
    #define NET_NAME_PREDICTABLE  2  /* predictably named by the kernel */
    #define NET_NAME_USER         3  /* provided by user-space */
    #define NET_NAME_RENAMED      4  /* renamed by user-space */
    �name_assign_type)�3�4TFrL)r'r�rrr�
is_renamedNs
	r�cCstt|d��}d|��vS)N�ueventzDEVTYPE=vlan)�strrDrk)r'r�rrr�is_vlan]sr�cCs0d}t|d�}tj�|�rtj�t�|��}|S)z8Return the device driver for net device named 'devname'.Nz
device/driver)r)rQr(�islinkr��readlink)r'rv�driver_pathrrrrrbs

rrcCst|d�}|durdS|S)z;Return the device id string for net device named 'devname'.z
device/deviceFNrL)r'�dev_idrrr�device_devidms
r�c
Cslt��st��rtt����Sz	t�t��}W|St	y5}z|j
t
jkr)g}n�WYd}~|Sd}~ww�N)r	�
is_FreeBSD�is_DragonFlyBSD�list�get_interfaces_by_mac�valuesrQ�listdirr#r-r+r0)�devsr=rrr�get_devicelistvs�
���r�c@seZdZdZdS)�ParserErrorz6Raised when a parser has issue parsing a file/content.N)�__name__�
__module__�__qualname__�__doc__rrrrr��sr�cCs |rt|t�s	dS|�d�dkS)NF�config�disabled)�
isinstance�dict�get)�cfgrrr�is_disabled_cfg�sr��blacklist_driverscC�8t��st��rt|�St��st��rt|�St|�S)zqGet the list of network interfaces viable for networking.

    @return List of interfaces, sorted naturally.
    )r	r�r��find_candidate_nics_on_freebsd�	is_NetBSD�
is_OpenBSD�(find_candidate_nics_on_netbsd_or_openbsd�find_candidate_nics_on_linux�r�rrr�find_candidate_nics�s
r�cCr�)z.Get the name of the 'fallback' network device.)r	r�r��find_fallback_nic_on_freebsdr�r��&find_fallback_nic_on_netbsd_or_openbsd�find_fallback_nic_on_linuxr�rrr�find_fallback_nic�s
r�cCstt���td�S)z�Get the names of the candidate network devices on NetBSD/OpenBSD.

    @param blacklist_drivers: currently ignored
    @return list of sorted interfaces
    ��key)�sortedr�r�r!r�rrrr��sr�cC�t|�}|r
|dSdS)z�Get the 'fallback' network device name on NetBSD/OpenBSD.

    @param blacklist_drivers: currently ignored
    @return default interface, or None
    rN)r��r��namesrrrr���r�cCs4t�gd��\}}|��}|r|Stt���td�S)z�Get the names of the candidate network devices on FreeBSD.

    @param blacklist_drivers: Currently ignored.
    @return List of sorted interfaces.
    )�ifconfigz-lz-u�etherr�)rrr�r�r�r!)r��stdout�_stderrr�rrrr��s
r�cCr�)z�Get the 'fallback' network device name on FreeBSD.

    @param blacklist_drivers: Currently ignored.
    @return List of sorted interfaces.
    rN)r�r�rrrr��r�r�cCs�|sg}dt��vrt�d�ndd�t�D�}t|�r.t�d|�d}tjtj|tjd�g}g}t�D]�}|dkr<q5t|�}||vrLt�d	||�q5t	|d
�sXt�d|�q5|�
d�rdt�d
|�q5t|�rot�d|�q5t|�rzt�d|�q5t
|�r�t�d|�q5t|d�}|r�|�|�q5t�d|�t|d�}|r�|�|�q5t	|d�}	|	dvr�|�|�q5t�d|�q5g}
||fD]}t|td�}t|vr�|�t�|�dt�|
|7}
q�|
S)z�Get the names of the candidate network devices on Linux.

    @param blacklist_drivers: Filter out NICs with these drivers.
    @return List of sorted interfaces.
    z
net.ifnames=0z9Stable ifnames disabled by net.ifnames=0 in /proc/cmdlinecSs g|]}|dkrt|�s|�qS)�lo)r�)r�devicerrrr�s���z0find_candidate_nics_on_linux.<locals>.<listcomp>z4Found unstable nic names: %s; calling udevadm settlez!Waiting for udev events to settle)�funcr�z%Ignoring interface with %s driver: %s�addressz"Ignoring interface without mac: %s�vethzIgnoring veth interface: %szIgnoring bridge interface: %szIgnoring bond interface: %szIgnoring failover interface: %s�carrierzInterface has no carrier: %s�dormantrK)r�rJ�lowerlayerdownrIzInterface ignored: %sr�r)r	�get_cmdliner5r6r�r�log_time�udevadm_settlerrrD�
startswithrTrVrwrG�appendr�r!�DEFAULT_PRIMARY_INTERFACE�remove�insert)r��unstable�msg�	connected�possibly_connectedrrvr�r�rK�sorted_interfaces�
interfacesrrrr��s|��
�









r�cCr�)z�Get the 'fallback' network device name on Linux.

    @param blacklist_drivers: Ignore devices with these drivers.
    @return List of sorted interfaces.
    rN)r�r�rrrr�>r�r�cCsx|sd}t|d�}|s
dSt|�rd|i}n	dt|d���i}d||d�}|r3t|�}|r3||d	d
<||idd�}|S)
zBGenerate network cfg v2 for dhcp on the NIC most likely connected.Fr�Nr�
macaddressr�T)�dhcp4�set-name�matchr�rv�)�	ethernets�version)r�r�rDrrr)r��
config_driver�target_namer�r�rv�nconfrrr�generate_fallback_configMs 

�r�cCsFdd�}dd�}|�d�}|dkr||�S|dkr||�Std|��)	NcSs�g}|�di�D]>}|�d�dkrq|�d�}|sq|�d�}|�di��d�}|�di��d�}|s7t|�}|s=t|�}|�||||g�q|S)	Nr��type�physical�mac_addressr�paramsrv�	device_id)r�rrr�r�)�netcfg�physdevs�ent�macrrvr�rrr�
_version_1hs 

z$extract_physdevs.<locals>._version_1cSs�g}|�di���D]=}|�d�}|sq
|�di��d�}|s q
|�di��d�}|�di��d�}|s8t|�}|s>t|�}|�||||g�q
|S)Nr�r�r�r�rvr�)r�r�rrr�r�)r�r�r�rr�rvr�rrr�
_version_2zs 
z$extract_physdevs.<locals>._version_2r��r�z"Unknown network config version: %s)r��RuntimeError)r�r�r�r�rrr�extract_physdevsgs
r�FcCs*t|d�}|dur|rtd��dS|dvS)a�return True if the provided interface has its own address.

    Based on addr_assign_type in /sys.  Return true for any interface
    that does not have a 'stolen' address. Examples of such devices
    are bonds or vlans that inherit their mac from another device.
    Possible values are:
      0: permanent address    2: stolen from another device
      1: randomly generated   3: set using dev_set_mac_address�addr_assign_typeNz%s had no addr_assign_type.T)rr��)rGrE)�ifname�strict�assign_typerrr�interface_has_own_mac�s

r�Tc
Cs�i}t�D]\}}}}d|||��|t|�d�||<q|r_t�d�}tjgd�dd�\}}tjgd�dd�\}	}t�}
||	fD]
}|
�|�|��q?|�	�D]}|dd	up[|d
|
v|d<qN|S)a�Collect information necessary for rename_interfaces.

    returns a dictionary by mac address like:
       {name:
         {
          'downable': None or boolean indicating that the
                      device has only automatically assigned ip addrs.
          'device_id': Device id value (if it has one)
          'driver': Device driver (if it has one)
          'mac': mac address (in lower case)
          'name': name
          'up': boolean: is_up(name)
         }}
    N)�downabler�rvr�rrHz[0-9]+:\s+(\w+)[@:])�ipz-6�addr�show�	permanent�scope�globalT��capture)r�z-4r�r�rHFrr�)
�get_interfacesrrMr�compiler�set�update�findallr�)
�check_downable�cur_inforr�rvr��nmatch�ipv6rm�ipv4�nics_with_addresses�	bytes_out�drrr�_get_current_rename_info�s.�	

��rcs�t|�st�d�dS|durt�}i�|��D]\}}|��}|�d�r-|d��|d<||d<|�|<qt�d���fdd�}dd�}d	d
�}	dd�}
g}g}g}
|��}d
}d}dd����fdd�}|D]�\}}}}|ru|��}g}||||�}|s�|r�|�d||f�qi|�d�}||kr�qi|s�|r�|�d||f�qi|dr�d}|ds�|r�|�||||f�qid|d<|�d|||ff�|
�d|||ff�||v�r<||}|dr�d}|ds�|r�|�||||f�qi|�d|||ff�d}|du�s||v�r|d7}||}|du�s||v�s|�d||||ff�||d<|��}|d�r<|
�d|||ff�|�d|||d|ff�||d<|��}||7}qi||	|
d�}t|�t|
�dk�rzt|��rst�d|�nEt�d|�n>t�d |||
�||
D]0\}}}}z
|�|�|�W�q�t	�y�}z|�d!|||||f�WYd}~�q�d}~wwt|��r�t	d"�
|���dS)#Nzno interfaces to renamer�rzDetected interfaces %scstdd����D��S)Ncss�|]	}|d|fVqdS)rNr�r�datarrr�	<genexpr>�s�z<_rename_interfaces.<locals>.update_byname.<locals>.<genexpr>)r�r�)�bymac)rrr�
update_byname�sz)_rename_interfaces.<locals>.update_bynamecSstjddd|d|gdd�dS)Nr��linkrrTr�r)�cur�newrrr�rename�sz"_rename_interfaces.<locals>.renamecS�tjddd|dgdd�dS)Nr�rrrJTrr�rrrrrJ��z _rename_interfaces.<locals>.downcSr)Nr�rrrHTrrrrrrrH�rz_rename_interfaces.<locals>.upz
cirename%d���cSsd|r|r|r|d|ko|d|ko|d|kS|r(|r(|d|ko'|d|kS|r0|d|kSdS)zmatch if set and in datar�rvr�Fr)rr�rvr�rrr�entry_matchs
�
�z'_rename_interfaces.<locals>.entry_matchcsT����fdd����D�}t|�r(t|�dkr$d|���f}t|��|dSdS)Ncsg|]}�|����r|�qSrrr)r�rvrr�rrrs��z:_rename_interfaces.<locals>.find_entry.<locals>.<listcomp>r�zjFailed to match a single device. Matched devices "%s" with search values "(mac:%s driver:%s device_id:%s)"r)r�rrE)r�rvr�r�r��rr)r�rvr�r�
find_entrys�
��z&_rename_interfaces.<locals>.find_entryz<[nic not present] Cannot rename mac=%s to %s, not available.rHz*[busy] Error renaming mac=%s from %s to %sr�FrJz2[busy-target] Error renaming mac=%s from %s to %s.r�r)rrJrHrz(unable to do any work for renaming of %sz$no work necessary for renaming of %sz$achieving renaming of %s with ops %sz.[unknown] Error performing %s%s for %s, %s: %s�
)rr5r6r�items�copyr�rr�rxrZ)�renames�strict_present�strict_busy�current_inforrrrrrJrH�ops�errors�ups�
cur_byname�tmpname_fmt�tmpir r��new_namervr��cur_ops�cur_namer��target�tmp_name�opmap�opr�r=rrr�_rename_interfaces�s�


��
��
�


����
�r5cCs$d}tj�t|d��r
d}t||�S)z6Returns the string value of an interface's MAC Addressr��
bonding_slavezbonding_slave/perm_hwaddr)rQr(�isdirr)rD)r�r(rrr�get_interface_macxs
r8cCs>t|d�dkrt|�}|r|r|dd�|dd�}|SdS)z�Returns the string value of an Infiniband interface's hardware
    address. If ethernet_format is True, an Ethernet MAC-style 6 byte
    representation of the address will be returned.
    r��32�$i��3N)rDr8)r��ethernet_formatr�rrr�get_ib_interface_hwaddr�s�r=cCsHt��st��r
t|d�St��rt|d�St��rt|d�St|d�S)Nr�)	r	r�r�� get_interfaces_by_mac_on_freebsdr��get_interfaces_by_mac_on_netbsdr�� get_interfaces_by_mac_on_openbsd�get_interfaces_by_mac_on_linuxr�rrrr��s����r�r�cCs0t���D]\}}|��|��kr|SqdSr�)r�r"r)r��
interface_mac�interface_namerrr�find_interface_name_from_mac�s
�rDcCs<t�gd��\}}dd�}dd�}dd�|||��D�}|S)N)r��-ar�cssB�d}|�d�D]}|�d�r||7}q|r|V|}q|VdS)Nr$r!�	)rr�)rl�
curr_block�linerrr�flatten�s�


z1get_interfaces_by_mac_on_freebsd.<locals>.flattencss6�|D]}t�d|�}|r|�d�|�d�fVqdS)Nz2^(?P<ifname>\S*): .*ether\s(?P<mac>[\da-f:]{17}).*r�r�)r�search�group)�	flat_list�block�mrrr�find_mac�s����z2get_interfaces_by_mac_on_freebsd.<locals>.find_maccSsi|]\}}||�qSrr)rr�r�rrr�
<dictcomp>�sz4get_interfaces_by_mac_on_freebsd.<locals>.<dictcomp>r)r�rl�_rIrO�resultsrrrr>�s
r>c	C�bi}d}t�ddg�\}}t�dd|���}|D]}t�||�}|r.|��}|d||d<q|S)NzE(?P<ifname>\w+).*address:\s(?P<mac>([\da-f]{2}[:-]){5}([\da-f]{2})).*r�rE�\n\s+� r�r��rr�subrkr��	groupdict�	r�re�re_field_matchrlrQ�if_linesrHrN�fieldsrrrr?����r?c	CrS)NzC(?P<ifname>\w+).*lladdr\s(?P<mac>([\da-f]{2}[:-]){5}([\da-f]{2})).*r�rErTrUr�r�rVrYrrrr@�r]r@c
	Csi}i}t|d�D]�\}}}}||vrhd}d||||f}|||krO||dkr9t�|d||||f�q	|dkrOd}t�|d|||||f�|dksW|dkrbt�d	||||�q	|rht|��|||<|||<t|d�}	|	r�|	|vr�|||	<q	t�d
|||	|	�q	|S)zmBuild a dictionary of tuples {mac: name}.

    Bridges and any devices that have a 'stolen' mac are excluded.r�Tz6duplicate mac found! both '%s' and '%s' have mac '%s'.�	hv_netvsczC Ignoring '%s' due to driver '%s' and '%s' having driver hv_netvsc.F�
mscc_felix�	fsl_enetcz>Ignoring duplicate macs from '%s' and '%s' due to driver '%s'.z^Ethernet and InfiniBand interfaces have the same address both '%s' and '%s' have address '%s'.)rr5�warningr6r�r=)
r�re�
driver_maprr�rv�_devid�raise_duplicate_mac_errorr��ib_macrrrrA�sf�������

��rAcCs�g}|durg}t�}d�d�}|D]Y}t|�sqt|�rqt|�r#qt|�r(qt|�dur7t|�s7t|�s7qt	|�r<qt
|�}|sCq|dkrR||dt|��krRqt|�rWqt
|�}||vr`q|�|||t|�f�q|S)z�Return list of interface tuples (name, mac, driver, device_id)

    Bridges and any devices that have a 'stolen' mac are excluded.N�:)�00rgrgrgrgrgrgrgrgrgrgrgrgrgrgrgr�)r�rZr�rTr�rVrYr^r`rwr8rrqrrr�r�)r�rer��zero_macrr�rvrrrrIsF
��rcCsPi}t�D] \}}}}t|d�}|r%||vr!td||||f��|||<q|S)zTBuild a dictionary mapping Infiniband interface names to their hardware
    address.Fz5duplicate mac found! both '%s' and '%s' have mac '%s')rr=r�)rerrQrerrr�get_ib_hwaddrs_by_interfacets
���ri�url_datac
Cs�d|vrt�d|�dS|d}zt|�}t|jdk|jdkg�s+t�d|j�WdSWntyE}z
t�d|�WYd}~dSd}~wwd	|vrNd
|d	<z
tdi|��WdStybYdSw)
aDReturn true when the instance has access to the provided URL.

    Logs a warning if url is not the expected format.

    url_data is a dictionary of kwargs to send to readurl. E.g.:

    has_url_connectivity({
        "url": "http://example.invalid",
        "headers": {"some": "header"},
        "timeout": 10
    })
    �urlz4Ignoring connectivity check. No 'url' to check in %sF�http�httpsz2Ignoring connectivity check. Invalid URL scheme %sz+Ignoring connectivity check. Invalid URL %sN�timeout�Tr)r5rar�any�schemerErr
)rjrk�result�errrrr�has_url_connectivity�s8
�������rt�convert_to_addressr�cKs(z	||fi|��WStyYdSw)aUse a function to return an address. If conversion throws a ValueError
    exception return False.

    :param check_cb:
        Test function, must return a truthy value
    :param address:
        The string to test.

    :return:
        Address or False

    F)rE)rur��kwargsrrr�maybe_get_address�s

�rwcC�tttj|��S)z�Returns a bool indicating if ``s`` is an IP address.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IP address or not.
    )rcrw�	ipaddress�
ip_address�r�rrr�
is_ip_address��	r|cCrx)z�Returns a bool indicating if ``s`` is an IPv4 address.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    )rcrwry�IPv4Addressr{rrr�is_ipv4_address�r}rcCrx)z�Returns a bool indicating if ``s`` is an IPv6 address.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    )rcrwry�IPv6Addressr{rrr�is_ipv6_address�r}r�cC�tttj|dd��S)z�Returns a bool indicating if ``s`` is an IPv4 or IPv6 network.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    F�r�)rcrwry�
ip_networkr{rrr�
is_ip_network�s	r�cCr�)z�Returns a bool indicating if ``s`` is an IPv4 network.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    Fr�)rcrwry�IPv4Networkr{rrr�is_ipv4_network��	�r�cCr�)z�Returns a bool indicating if ``s`` is an IPv6 network.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    Fr�)rcrwry�IPv6Networkr{rrr�is_ipv6_network�r�r�cCs@|d�d�s
|dtvrdS|ddkrt|�d��rdSdS)z:Common helper for checking network_state subnets for ipv6.r��6T�staticr�F)�endswith�IPV6_DYNAMIC_TYPESr�r�)�subnetrrr�subnet_is_ipv6
s
r�cCstt�d|���j�S)z�Convert a network prefix to an ipv4 netmask.

    This is the inverse of ipv4_mask_to_net_prefix.
        24 -> "255.255.255.0"
    Also supports input as a string.�0.0.0.0/)r�ryr��netmask)�prefixrrr�net_prefix_to_ipv4_masksr�cCst�d|���jS)aConvert an ipv4 netmask into a network prefix length.

    If the input is already an integer or a string representation of
    an integer, then int(mask) will be returned.
       "255.255.255.0" => 24
       str(24)         => 24
       "24"            => 24
    r�)ryr��	prefixlen)�maskrrr�ipv4_mask_to_net_prefixs	r�cCs�zt�d|���j}|WStyYnwt�|�}t|�}|dkr%|Sttj||d@���}||?}tj|}d|>d}||krKtd|��|S)z�Convert an ipv6 netmask (very uncommon) or prefix (64) to prefix.

    If the input is already an integer or a string representation of
    an integer, then int(mask) will be returned.
       "ffff:ffff:ffff::"  => 48
       "48"                => 48
    z::/rr�zInvalid network mask '%s')	ryr�r�rErzr�min�
IPV6LENGTH�
bit_length)r�r�r��mask_int�trailing_zeroes�leading_ones�all_onesrrr�ipv6_mask_to_net_prefix+s&�
�
r�r�r�cCsttj|�d|��dd�j�S)zCGet string representation of broadcast address from an ip/mask pairr%Fr�)r�ryr��broadcast_address)r�r�rrr�mask_and_ipv4_to_bcast_addrNs�r�c@seZdZdS)�RendererNotFoundErrorN)r�r�r�rrrrr�Usr�)r$)NNNNr�)NN)F)T)TTN)er+�	functoolsry�loggingrQr�typingrrrrr�urllib.parser�	cloudinitrr	�cloudinit.url_helperr
r�	getLoggerr�r5r"r�r�rgrr!r#r)r?rDrGrMrTrVrYr^r`�	lru_cachercrfr�ror�rqrwr{r�r�rsrtr�r�rrr�r�rxr�r�r�r�r�r�r�r�r�r�r�r�r�rr5r8r=r�r�rDr>r?r@rArrirtrwr|rr�r�r�r�r�r�rr�r�r�r�r�rrrr�<module>s


�
%


	





!	�
�
��
�
�
�
�
��
�
��
�
��
�
��
�
�[�
�
�

0
/
� 
c+'	#