HEX
Server: LiteSpeed
System: Linux us-phx-web1284.main-hosting.eu 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: u300739242 (300739242)
PHP: 8.2.30
Disabled: system, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/alt/python311/lib/python3.11/site-packages/pyroute2/nslink/__pycache__/nslink.cpython-311.pyc
�

�;f�����dZddlZddlZddlZddlZddlmZddlmZddl	m
Z
ddlmZddl
mZmZdd	lmZmZmZeje��ZGd
�de��ZGd�d
ee��ZdS)ae
NetNS objects
=============

A NetNS object is IPRoute-like. It runs in the main network
namespace, but also creates a proxy process running in
the required netns. All the netlink requests are done via
that proxy process.

NetNS supports standard IPRoute API, so can be used instead
of IPRoute, e.g., in IPDB::

    # start the main network settings database:
    ipdb_main = IPDB()
    # start the same for a netns:
    ipdb_test = IPDB(nl=NetNS('test'))

    # create VETH
    ipdb_main.create(ifname='v0p0', kind='veth', peer='v0p1').commit()

    # move peer VETH into the netns
    with ipdb_main.interfaces.v0p1 as veth:
        veth.net_ns_fd = 'test'

    # please keep in mind, that netns move clears all the settings
    # on a VETH interface pair, so one should run netns assignment
    # as a separate operation only

    # assign addresses
    # please notice, that `v0p1` is already in the `test` netns,
    # so should be accessed via `ipdb_test`
    with ipdb_main.interfaces.v0p0 as veth:
        veth.add_ip('172.16.200.1/24')
        veth.up()
    with ipdb_test.interfaces.v0p1 as veth:
        veth.add_ip('172.16.200.2/24')
        veth.up()

Please review also the test code, under `tests/test_netns.py` for
more examples.

By default, NetNS creates requested netns, if it doesn't exist,
or uses existing one. To control this behaviour, one can use flags
as for `open(2)` system call::

    # create a new netns or fail, if it already exists
    netns = NetNS('test', flags=os.O_CREAT | os.O_EXCL)

    # create a new netns or use existing one
    netns = NetNS('test', flags=os.O_CREAT)

    # the same as above, the default behaviour
    netns = NetNS('test')

To remove a network namespace::

    from pyroute2 import NetNS
    netns = NetNS('test')
    netns.close()
    netns.remove()

One should stop it first with `close()`, and only after that
run `remove()`.

�N)�partial)�RTNL_API)�RTMGRP_DEFAULTS)�MarshalRtnl)�remove�setns�)�RemoteSocket�Server�	Transportc� �eZdZd�Zd�Zd�ZdS)�FDc��||_dD]9}t||ttt|��|j�����:dS)N)�read�write�close)�fd�setattrr�getattr�os)�selfr�names   �I/opt/alt/python311/lib/python3.11/site-packages/pyroute2/nslink/nslink.py�__init__zFD.__init__TsQ�����.�	E�	E�D��D�$����D�(9�(9�4�7� C� C�D�D�D�D�	E�	E�c��|jS�N)r�rs r�filenoz	FD.filenoYs	���w�rc��dSr�rs r�flushzFD.flush\s���trN)�__name__�
__module__�__qualname__rrr"r!rrrrSsD������E�E�E�
�������rrc���eZdZdZejddef�fd�	Zd�Zd�Z	e
jf�fd�	Z�fd�Z
�fd�Z�fd	�Zd
�Zd�Z�xZS)�NetNSa8
    NetNS is the IPRoute API with network namespace support.

    **Why not IPRoute?**

    The task to run netlink commands in some network namespace, being in
    another network namespace, requires the architecture, that differs
    too much from a simple Netlink socket.

    NetNS starts a proxy process in a network namespace and uses
    `multiprocessing` communication channels between the main and the proxy
    processes to route all `recv()` and `sendto()` requests/responses.

    **Any specific API calls?**

    Nope. `NetNS` supports all the same, that `IPRoute` does, in the same
    way. It provides full `socket`-compatible API and can be used in
    poll/select as well.

    The only difference is the `close()` call. In the case of `NetNS` it
    is **mandatory** to close the socket before exit.

    Nc
�p��||_||_|p|}d�tj��D��\}|_d�tj��D��\|_}tj��|_|jdk�r|���|���|j	���|j	���	t|j|j|���n�#t$r@}|j�d|d���tj
|j��Yd}~nvd}~wt$rf}|j�dttjt#|��|j��d���tj
d��Yd}~nd}~wwxYw	t%|j|j||���tj
d��n#tj
d��wxYw	|j���|j���t't(|���|||�	��||_n##t$r|����wxYwt/j|j��t3��|_dS)
Nc�F�g|]}tt|������Sr!�rr��.0�xs  r�
<listcomp>z"NetNS.__init__.<locals>.<listcomp>��&��*O�*O�*O��9�R��U�U�+;�+;�*O�*O�*Orc�F�g|]}tt|������Sr!r*r+s  rr.z"NetNS.__init__.<locals>.<listcomp>�r/rr)�libc�init)�stage�error�)�target�groups)r7)�netns�flagsr�pipe�remote_trnsp_out�remote_trnsp_in�fork�childr�file_objr�OSError�send�_exit�errno�	Exception�ECOMM�strr�superr'rr6�atexit�registerr�marshal)
rr8r9r6r1r7�trnsp_in�	trnsp_out�e�	__class__s
         �rrzNetNS.__init__ys������
���
���5��*O�*O�R�W�Y�Y�*O�*O�*O�'��$�'�*O�*O�R�W�Y�Y�*O�*O�*O�'���i��W�Y�Y��
��:��?�?��N�N�����O�O������#�#�%�%�%���$�$�&�&�&�
��d�j�$�*�4�8�8�8�8�8���
"�
"�
"��&�+�+�f�q�,I�,I�J�J�J�����!�!�!�!�!�!�!�!������	
�	
�	
��)�.�.�%+�%,�U�[�#�a�&�&�$�*�%M�%M���������
�
�
�
�
�
�
�
�����	
����
���(��)�!�!�	����������������������	�� �&�&�(�(�(��!�'�'�)�)�)��%����'�'��)�F�'�K�K�K� �D�K�K���	�	�	��J�J�L�L�L��	����	���
�#�#�#�"�}�}����s>�C:�:
F2�6D?�?
F2�AF-�-F2�6G(�(G>�A$I'�' Jc�H�t|��|j|j��Sr)�typer8r9rs r�clonezNetNS.clone�s���t�D�z�z�$�*�d�j�1�1�1rc���ttd��rtj|j��dS	tj�|jdif��dS#t$rYdSwxYw)N�
unregisterr!)�hasattrrHrSr�
_exithandlersr�
ValueErrorrs r�_cleanup_atexitzNetNS._cleanup_atexit�sz���6�<�(�(�	���d�j�)�)�)�)�)�
��$�+�+�T�Z��R�,@�A�A�A�A�A���
�
�
����
���s�'A�
A)�(A)c�$��|���	tt|���|���dS#	|j�ddi��n#t$rYnwxYwt�d��YdSxYw)N)�coder3�shutdownz2forced shutdown procedure, clean up netns manually)	rWrGr'rrLrArD�logr4)rrYrNs  �rrzNetNS.close�s����������	L��%����$�$�$�$�/�/�/�/�/��	L�
���#�#�W�j�$9�:�:�:�:���
�
�
���
�����I�I�J�K�K�K�K�K�K���s/�)A�B�A"�!B�"
A/�,B�.A/�/Bc���|jdkr|�d|��Stt|���|��S)z0Proxy the open_file method if we are the parent.r�	open_file)r>�proxyrGr'r])r�pathrNs  �rr]zNetNS.open_file�s@����:��?�?��:�:�k�4�0�0�0��U�D�!�!�+�+�D�1�1�1rc���|jdkr|�d|��Stt|���|��S)z1Proxy the close_file method if we are the parent.r�
close_file)r>r^rGr'ra)rrrNs  �rrazNetNS.close_file�s@����:��?�?��:�:�l�B�/�/�/��U�D�!�!�,�,�R�0�0�0rc���|jdkr|�d��Stt|�����S)z.Proxy the get_pid method if we are the parent.r�get_pid)r>r^rGr'rc)rrNs �rrcz
NetNS.get_pid�s<����:��?�?��:�:�i�(�(�(��U�D�!�!�)�)�+�+�+rc��dSrr!rs r�	post_initzNetNS.post_init�s���rc�.�t|j��dS)zG
        Try to remove this network namespace from the system.
        N)rr8rs rrzNetNS.remove�s��	�t�z�����r)r#r$r%�__doc__r�O_CREATrrrQrWrC�
ECONNRESETrr]rarcrer�
__classcell__)rNs@rr'r'`s���������6�j��
��
8%�8%�8%�8%�8%�8%�t2�2�2�����)�
L�
L�
L�
L�
L�
L�2�2�2�2�2�1�1�1�1�1�,�,�,�,�,�
�
�
�������rr')rgrHrC�loggingr�	functoolsr�pyroute2.iprouter�pyroute2.netlink.rtnlr�pyroute2.netlink.rtnl.iprsocketr�pyroute2.netnsrr�remote.transportr
rr�	getLoggerr#r[�objectrr'r!rr�<module>rts2��@�@�D�
�
�
���������	�	�	�	�������%�%�%�%�%�%�1�1�1�1�1�1�7�7�7�7�7�7�(�(�(�(�(�(�(�(�>�>�>�>�>�>�>�>�>�>��g���!�!��
�
�
�
�
��
�
�
�G�G�G�G�G�H�l�G�G�G�G�Gr