%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/cloudinit/sources/helpers/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/cloudinit/sources/helpers/__pycache__/netlink.cpython-312.pyc

�

���f�.���ddlZddlZddlZddlZddlZddlmZddlmZeje
�ZdZdZ
dZdZdZd	ZdZd
ZdZdZej,e�Zej,e�ZeezZd
Zd
ZdZdZdZdZdZ dZ!d
Z"dZ#dZ$edgd��Z%edddg�Z&edgd��Z'Gd�de(�Z)d�Z*d�Z+d$d�Z,d�Z-d�Z.d �Z/d!�Z0d"�Z1d#�Z2y)%�N)�
namedtuple)�util�����i���<�IHHII�BHiII������RTAAttr)�length�rta_type�data�InterfaceOperstate�ifname�	operstate�
NetlinkHeader)r�type�flags�seq�pidc��eZdZdZy)�NetlinkCreateSocketErrorz5Raised if netlink socket fails during create or bind.N)�__name__�
__module__�__qualname__�__doc__���C/usr/lib/python3/dist-packages/cloudinit/sources/helpers/netlink.pyrr5s��?r%rc��	tjtjtjtj�}|j	tj�tf�|jd�tjd�|S#tj$r}d|z}t|�|�d}~wwxYw)auCreates netlink socket and bind on netlink group to catch interface
    down/up events. The socket will bound only on RTMGRP_LINK (which only
    includes RTM_NEWLINK/RTM_DELLINK/RTM_GETLINK events). The socket is set to
    non-blocking mode since we're only receiving messages.

    :returns: netlink socket in non-blocking mode
    :raises: NetlinkCreateSocketError
    rz*Exception during netlink socket create: %sNzCreated netlink socket)
�socket�
AF_NETLINK�SOCK_RAW�
NETLINK_ROUTE�bind�os�getpid�RTMGRP_LINK�setblocking�errorr�LOG�debug)�netlink_socket�e�msgs   r&�create_bound_netlink_socketr79s���3�������v����0D�0D�
��	���R�Y�Y�[�+�6�7��"�"�1�%��I�I�&�'����	�<�<�3�:�Q�>��&�s�+��2��3�s�A;B�B=�'B8�8B=c���|�Jd��t|�tk\sJd��tjt|dt
�\}}}}}tjd|�t|||||�S)aGets netlink message type and length

    :param: data read from netlink socket
    :returns: netlink message type
    :raises: AssertionError if data is None or data is not >= NLMSGHDR_SIZE
    struct nlmsghdr {
               __u32 nlmsg_len;    /* Length of message including header */
               __u16 nlmsg_type;   /* Type of message content */
               __u16 nlmsg_flags;  /* Additional flags */
               __u32 nlmsg_seq;    /* Sequence number */
               __u32 nlmsg_pid;    /* Sender port ID */
    };
    N�data is nonez+data is smaller than netlink message headerzGot netlink msg of type %d)	�len�
NLMSGHDR_SIZE�struct�unpack�NLMSGHDR_FMT�MSG_TYPE_OFFSETr2r3r)r�msg_len�msg_typerrrs      r&�get_netlink_msg_headerrBOs{����+�^�+���D�	�]�"�5�4�5�"�)/����d�+�O�,�*�&�G�X�u�c�3��I�I�*�H�5���(�E�3��<�<r%c���|�Jd��tj|ggg|�\}}}||vrytjd�|jt�}|�tjd�|S)a�Select and read from the netlink socket if ready.

    :param: netlink_socket: specify which socket object to read from
    :param: timeout: specify a timeout value (integer) to wait while reading,
            if none, it will block indefinitely until socket ready for read
    :returns: string of data read (max length = <MAX_SIZE>) from socket,
              if no data read, returns None
    :raises: AssertionError if netlink_socket is None
    N�netlink socket is noneznetlink socket ready for readz,Reading from Netlink socket returned no data)�selectr2r3�recv�MAX_SIZEr1)r4�timeout�read_set�_rs     r&�read_netlink_socketrKhst���%�?�'?�?�%��]�]�N�#3�R��W�E�N�H�a���X�%���I�I�-�.����x�(�D��|��	�	�@�A��Kr%c�L�|�Jd��t|t�sJd��|tk\sJd��dx}}d}	tjd||��d}tjd||dz��d}||tz||z}t|||�S#tj
$rYywxYw)	a(Unpack a single rta attribute.

    :param: data: string of data read from netlink socket
    :param: offset: starting offset of RTA Attribute
    :return: RTAAttr object with length, type and data. On error, return None.
    :raises: AssertionError if data is None or offset is not integer.
    Nr9zoffset is not integerz'rta offset is less than expected lengthr�H)�offsetr)�
isinstance�int�RTATTR_START_OFFSETr<�unpack_fromr1�RTA_DATA_START_OFFSETr)rrNrr�	attr_datas     r&�unpack_rta_attrrUs�����+�^�+���f�c�"�;�$;�;�"��%�%�1�0�1�%���F�X��I���#�#�C��f�=�a�@���%�%�c�4���
�C�A�F��
�V�3�3�f�v�o�F�I��6�8�Y�/�/���<�<����s�9B
�
B#�"B#c�`�|�Jd��t|�tkDsJd��dx}}t}|t|�kr�t||�}|r|jdk(rn�t|jtzz
tz}||j|zz
}|j
tk(rt|j�}nD|j
tk(r1tj|jd�}|jd�}|t|�kr��|r|�ytjd||�t||�S)a�Reads Interface name and operational state from RTA Data.

    :param: data: string of data read from netlink socket
    :returns: InterfaceOperstate object containing if_name and oper_state.
              None if data does not contain valid IFLA_OPERSTATE and
              IFLA_IFNAME messages.
    :raises: AssertionError if data is None or length of data is
             smaller than RTATTR_START_OFFSET.
    Nr9z2length of data is smaller than RTATTR_START_OFFSETrzutf-8�z!rta attrs: ifname %s operstate %d)r:rQrUr�
PAD_ALIGNMENTr�IFLA_OPERSTATE�ordr�IFLA_IFNAMEr�
decode_binary�stripr2r3r)rrrrN�attr�padlen�interface_names       r&�read_rta_oper_statera�s ����+�^�+���D�	�'�'�<�;�<�'���F�Y�
 �F�
�C��I�
��t�V�,���t�{�{�a�'��
�T�[�[�=�8�9����	�$�+�+��&�&���=�=�N�*��D�I�I��I�
�]�]�k�
)�!�/�/��	�	�7�C�N�#�)�)�$�/�F��C��I�
��Y�&���I�I�1�6�9�E��f�i�0�0r%c����tjd�d���fd�}t|dtgtt
g|��S)z�Block until a single nic is attached.

    :param: netlink_socket: netlink_socket to receive events
    :param: existing_nics: List of existing nics so that we can skip them.
    :raises: AssertionError if netlink_socket is none.
    z!Preparing to wait for nic attach.Nc���|�vry|�y)NTFr$)�iname�carrier�prevCarrier�
existing_nicsrs   ��r&�should_continue_cbz5wait_for_nic_attach_event.<locals>.should_continue_cb�s����M�!����r%)r2r3�read_netlink_messages�RTM_NEWLINK�OPER_UP�	OPER_DOWN)r4rgrhrs ` @r&�wait_for_nic_attach_eventrm�sC����I�I�1�2�
�F�����	�
�	�)�����Mr%c�r��tjd�d��fd�}t|dtgtg|��S)z�Block until a single nic is detached and its operational state is down.

    :param: netlink_socket: netlink_socket to receive events.
    z!Preparing to wait for nic detach.Nc�
��|�y)NFr$)rdrerfrs   �r&rhz5wait_for_nic_detach_event.<locals>.should_continue_cb�s�����r%)r2r3ri�RTM_DELLINKrl)r4rhrs  @r&�wait_for_nic_detach_eventrq�s=���
�I�I�1�2�
�F��
���{�m�i�[�:L���Mr%c����|�Jd����Jd��t��dkDsJd���fd�}tjd�t|�tt
gttg|�y)a�Block until media disconnect and connect has happened on an interface.
    Listens on netlink socket to receive netlink events and when the carrier
    changes from 0 to 1, it considers event has happened and
    return from this function

    :param: netlink_socket: netlink_socket to receive events
    :param: ifname: Interface name to lookout for netlink events
    :raises: AssertionError if netlink_socket is None or ifname is None.
    NrDzinterface name is nonerzinterface name cannot be emptyc�`��|tk(xr	|tk(}|rtjd��yy)NzMedia switch happened on %s.FT)rlrkr2r3)rdrerf�isVnetSwitchrs    �r&rhz=wait_for_media_disconnect_connect.<locals>.should_continue_cb�s/���#�y�0�J�w�'�7I����I�I�4�f�=��r%z1Wait for media disconnect and reconnect to happen)r:r2r3rirjrprkrl)r4rrhs ` r&�!wait_for_media_disconnect_connectru�st����%�?�'?�?�%���7�7�7���v�;��?�<�<�<�?���I�I�A�B����	�k�"�	�)���r%c��|�td��t�}t}t}	t|t�}|��t
j
dt|��||z
}t
j
dt|��d}	t|�}
|	|
k�rD||	d}t|�tkrt
j
d��nt|�}t|�|jkrt
j
d�n�|jtzdz
tdz
z}
|	|
z}	t
j
d	|	�|j|vr��t|�}|�t
j
d
|���|�2|j|k7r#t
j
d|j|���|j|vr�� |}|j}||j||�sy|	|
kr��D||	d}���)a�Reads from the netlink socket until the condition specified by
    the continuation callback is met.

    :param: netlink_socket: netlink_socket to receive events.
    :param: ifname_filter: if not None, will only listen for this interface.
    :param: rtm_types: Type of netlink events to listen for.
    :param: operstates: Operational states to listen.
    :param: should_continue_callback: Specifies when to stop listening.
    NzNetlink socket is nonezread %d bytes from socketzLength of data after concat %drz#Data is smaller than netlink headerz*Partial data. Smaller than netlink messagerz"offset to next netlink message: %dz!Failed to read rta attributes: %sz6Ignored netlink event on interface %s. Waiting for %s.)�RuntimeError�bytesrkrK�SELECT_TIMEOUTr2r3r:r;rBrrXrrarr)r4�
ifname_filter�	rtm_types�
operstates�should_continue_callbackrrerf�	recv_datarN�datalen�nl_msg�nlheaderr_�interface_states               r&riris��� ���3�4�4��7�D��G��K�
�'���G�	�����	�	�-�s�9�~�>��	����	�	�2�C��I�>����d�)���w���&�'�]�F��6�{�]�*��	�	�?�@��-�f�5�H��6�{�X�_�_�,��	�	�F�G���o�o�
�5��9���!�>��F��f�_�F��I�I�:�F�C��}�}�I�-��1�&�9�O��&��	�	�=��O���)�#�*�*�m�;��	�	�L�#�*�*�!��
��(�(�
�:��!�K�%�/�/�G�+��&�&�����M�w��N�F�G�}��ar%)N)3�loggingr-rEr(r<�collectionsr�	cloudinitr�	getLoggerr r2r/rjrp�RTM_GETLINK�RTM_SETLINKrGr?ryr>�
IFINFOMSG_FMT�calcsizer;�IFINFOMSG_SIZErQrSrXr[rY�OPER_UNKNOWN�OPER_NOTPRESENTrl�OPER_LOWERLAYERDOWN�OPER_TESTING�OPER_DORMANTrkrrrrwrr7rBrKrUrarmrqrurir$r%r&�<module>r�s6��
�	�
�
�
�"���g����!���������������������
������-�
� �����/��#�n�4�����
���������
�	�������
��
�Y� >�
?��� 4�x��6M�N����>��
�
@�|�@��,=�2�.0�4"1�J�:�&�@Er%

Zerion Mini Shell 1.0