File: //usr/lib/python3/dist-packages/twisted/pair/__pycache__/testing.cpython-310.pyc
o
�bhC � @ s d Z ddlZddlZddlmZ ddlmZmZmZm Z m
Z
mZmZm
Z
ddlmZ ddlmZ ddlmZ ddlmZ dd lmZ dd
lmZ ddlmZmZmZmZ ddlm Z d
Z!dd� Z"dZ#dd� Z$dd� Z%dd� Z&G dd� d�Z'dd� Z(ee�G dd� d��Z)G dd� d�Z*dS )zD
Tools for automated testing of L{twisted.pair}-based applications.
� N)�deque)�EAGAIN�EBADF�EINTR�EINVAL�ENOBUFS�ENOSYS�EPERM�EWOULDBLOCK��wraps)�implementer)�DatagramProtocol)�EthernetProtocol)�
IPProtocol)�RawUDPProtocol)� _IFNAMSIZ�
_TUNSETIFF�TunnelFlags�_IInputOutputSystem)�nativeString� c C s t �d| �S )z�
Pack an integer into a network-order two-byte string.
@param n: The integer to pack. Only values that fit into 16 bits are
supported.
@return: The packed representation of the integer.
@rtype: L{bytes}
z>H)�struct�pack)�n� r �6/usr/lib/python3/dist-packages/twisted/pair/testing.py�_H s
r � c C s || t |� | S )a�
Construct an ethernet frame.
@param src: The source ethernet address, encoded.
@type src: L{bytes}
@param dst: The destination ethernet address, encoded.
@type dst: L{bytes}
@param protocol: The protocol number of the payload of this datagram.
@type protocol: L{int}
@param payload: The content of the ethernet frame (such as an IP datagram).
@type payload: L{bytes}
@return: The full ethernet frame.
@rtype: L{bytes}
)r ��src�dst�protocol�payloadr r r � _ethernet. s r$ c C s� dt dt|� � d t d� t�tjt| �� t�tjt|�� }tt�d|��}|d? }|d@ | }|dA }|dd � t� d
|� |dd� }|| S )a�
Construct an IP datagram with the given source, destination, and
application payload.
@param src: The source IPv4 address as a dotted-quad string.
@type src: L{bytes}
@param dst: The destination IPv4 address as a dotted-quad string.
@type dst: L{bytes}
@param payload: The content of the IP datagram (such as a UDP datagram).
@type payload: L{bytes}
@return: An IP datagram header and payload.
@rtype: L{bytes}
s E � s @r z!10H� i�� N�
z!H� )
r �len�socket� inet_pton�AF_INETr �sumr �unpackr )r r! r# �ipHeader�
checksumStep1�carry�
checksumStep2�
checksumStep3r r r �_ipD s$ ��� ���$r4 c C s0 t | �t |� t t|�d � t d� }|| S )a~
Construct a UDP datagram with the given source, destination, and
application payload.
@param src: The source port number.
@type src: L{int}
@param dst: The destination port number.
@type dst: L{int}
@param payload: The content of the UDP datagram.
@type payload: L{bytes}
@return: A UDP datagram header and payload.
@rtype: L{bytes}
� r )r r) )r r! r# � udpHeaderr r r �_udpv s ����
r7 c @ sr e Zd ZdZdZeed�Zee d�Z
eed�ZeZ
dZdd� Zed d
� �Zedd� �Zd
d� Zdd� Zdd� ZdS )�Tunnelz�
An in-memory implementation of a tun or tap device.
@cvar _DEVICE_NAME: A string representing the conventional filesystem entry
for the tunnel factory character special device.
@type _DEVICE_NAME: C{bytes}
s /dev/net/tunz Resource temporarily unavailablezOperation would blockzInterrupted function calli c C s: || _ || _d| _d| _d| _t� | _t� | _t� | _dS )a
@param system: An L{_IInputOutputSystem} provider to use to perform I/O.
@param openFlags: Any flags to apply when opening the tunnel device.
See C{os.O_*}.
@type openFlags: L{int}
@param fileMode: ignored
N) �system� openFlags�
tunnelMode�
requestedName�namer �
readBuffer�writeBuffer�pendingSignals)�selfr9 r: �fileModer r r �__init__� s zTunnel.__init__c C s | j | jj@ S )zx
If the file descriptor for this tunnel is open in blocking mode,
C{True}. C{False} otherwise.
)r: r9 �
O_NONBLOCK�rA r r r �blocking� s zTunnel.blockingc C s t | j| jj@ �S )zz
If the file descriptor for this tunnel is marked as close-on-exec,
C{True}. C{False} otherwise.
)�boolr: r9 � O_CLOEXECrE r r r �closeOnExec� s zTunnel.closeOnExecc C s. | j tjj@ rtddt|d�}| j�|� dS )aI
Deliver a datagram to this tunnel's read buffer. This makes it
available to be read later using the C{read} method.
@param datagram: The IPv4 datagram to deliver. If the mode of this
tunnel is TAP then ethernet framing will be added automatically.
@type datagram: L{bytes}
s s ������r N)r; r �IFF_TAP�valuer$ �_IPv4r>