+
    üÐjhC  ã                   ó2  € R t ^ RIt^ RIt^ RIHt ^ RIHtHtHtH	t	H
t
HtHtHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHtHtHtHt ^ RIH t  ^t!R t"Rt#R t$R t%R t& ! R R4      t'R t(]! ]4       ! R R4      4       t) ! R R4      t*R# )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)ÚnativeStringc                ó0   € \         P                  ! RV 4      # )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)Úns   &Ú6/usr/lib/python3/dist-packages/twisted/pair/testing.pyÚ_Hr      s   € ô �;Š;�t˜QÓÐó    é   c                ó@   € W,           \        V4      ,           V,           # )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Úpayloads   &&&&r   Ú	_ethernetr%   .   s   € ð& �9”r˜(“|Õ# gÕ-Ð-r   c                ó:  € R\        ^\        V4      ,           4      ,           R,           \        ^ 4      ,           \        P                  ! \        P                  \        V 4      4      ,           \        P                  ! \        P                  \        V4      4      ,           p\        \        P                  ! RV4      4      pV^,	          pVR,          V,           pVR,          pVR,          \        P                  ! RV4      ,           VR,          ,           pW2,           # )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      @z!10Hiÿÿ  :Né
   Nz!H:é   NN)
r   ÚlenÚsocketÚ	inet_ptonÚAF_INETr   Úsumr   Úunpackr   )r!   r"   r$   ÚipHeaderÚchecksumStep1ÚcarryÚchecksumStep2ÚchecksumStep3s   &&&     r   Ú_ipr4   D   sã   € ð&	ô ˆR”#�g“,ÕÓ
õ		 ð
 &õ	&ô ˆQ‹%õ	ô ×
Ò
œ6Ÿ>™>¬<¸Ó+<Ó
=õ		>ô ×
Ò
œ6Ÿ>™>¬<¸Ó+<Ó
=õ	>ð ô" œŸš f¨hÓ7Ó8€Mà˜RÕ€Eà" VÕ+¨uÕ4€Mà! FÕ*€Mð
 ˜�}œvŸ{š{¨4°Ó?Õ?À(È3Å-ÕO€HàÕÐr   c                ó¨   € \        V 4      \        V4      ,           \        \        V4      ^,           4      ,           \        ^ 4      ,           pW2,           # )aR  
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$   Ú	udpHeaders   &&& r   Ú_udpr7   v   sM   € ô& 	ˆ3‹ä
ˆS‹'õ	ô ŒS�‹\˜AÕÓ
õ		ô ˆQ‹%õ	ð ð ÕÐr   c                   óž   a € ] tR t^”t o RtRt]! ]R4      t]	! ]
R4      t]! ]R4      t]tRtR t]R 4       t]R	 4       tR
 tR tR tRtV tR# )Ú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 unavailablezOperation would blockzInterrupted function calli   c                ó¢   € Wn         W n        RV n        RV n        RV n        \        4       V n        \        4       V n        \        4       V n        R# )zÖ
@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)Úselfr;   r<   ÚfileModes   &&&&r   Ú__init__ÚTunnel.__init__¬   sC   € ð Œð #ŒØˆŒØ!ˆÔØˆŒ	Ü›'ˆŒÜ ›7ˆÔÜ#›gˆÖr   c                óZ   € V P                   V P                  P                  ,          '       * # )z`
If the file descriptor for this tunnel is open in blocking mode,
C{True}.  C{False} otherwise.
)r<   r;   Ú
O_NONBLOCK©rC   s   &r   ÚblockingÚTunnel.blockingÃ   s    € ð —N‘N T§[¡[×%;Ñ%;Õ;Ô<Ð<r   c                ób   € \        V P                  V P                  P                  ,          4      # )zb
If the file descriptor for this tunnel is marked as close-on-exec,
C{True}.  C{False} otherwise.
)Úboolr<   r;   Ú	O_CLOEXECrI   s   &r   ÚcloseOnExecÚTunnel.closeOnExecË   s"   € ô �D—N‘N T§[¡[×%:Ñ%:Õ:Ó;Ð;r   c                óÄ   € V P                   \        P                  P                  ,          '       d   \	        RR\
        VR7      pV P                  P                  V4       R# )a  
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}
r    Ns         s   ÿÿÿÿÿÿ)r=   r   ÚIFF_TAPÚvaluer%   Ú_IPv4r@   Úappend©rC   Údatagrams   &&r   ÚaddToReadBufferÚTunnel.addToReadBufferÓ   sF   € ð �?‰?œ[×0Ñ0×6Ñ6×6Ô6Ü Ø [¼5È(ôˆHð 	�‰×Ñ˜xÖ(r   c                óP  € V P                   '       dn   V P                  \        P                  P                  ,          '       d   RpMR\
        ,          pV^,          pW P                   P                  4       RV ,           # V P                  '       d   \        4       hV P                  h)aª  
Read a datagram out of this tunnel.

@param limit: The maximum number of bytes from the datagram to return.
    If the next datagram is larger than this, extra bytes are dropped
    and lost forever.
@type limit: L{int}

@raise OSError: Any of the usual I/O problems can result in this
    exception being raised with some particular error number set.

@raise IOError: Any of the usual I/O problems can result in this
    exception being raised with some particular error number set.

@return: The datagram which was read from the tunnel.  If the tunnel
    mode does not include L{TunnelFlags.IFF_NO_PI} then the datagram is
    prefixed with a 4 byte PI header.
@rtype: L{bytes}
r   ó    N)
r@   r=   r   Ú	IFF_NO_PIrS   Ú_PI_SIZEÚpopleftrJ   ÚNotImplementedErrorÚnonBlockingExceptionStyle)rC   ÚlimitÚheaders   && r   ÚreadÚTunnel.readä   s}   € ð( �?�?ˆ?Ø�‰¤×!6Ñ!6×!<Ñ!<×<Ô<Ø‘ð
 !¤8Õ+�Ø˜•
�ØŸO™O×3Ñ3Ó5°f°uÐ=Õ=Ð=Ø�]�]ˆ]Ü%Ó'Ð'à×0Ñ0Ð0r   c                ó  € V P                   '       d+   V P                   P                  4        \        \        R4      h\	        V4      V P
                  8”  d   \        \        R4      hV P                  P                  V4       \	        V4      # )a;  
Write a datagram into this tunnel.

@param datagram: The datagram to write.
@type datagram: L{bytes}

@raise IOError: Any of the usual I/O problems can result in this
    exception being raised with some particular error number set.

@return: The number of bytes of the datagram which were written.
@rtype: L{int}
zInterrupted system callzNo buffer space available)	rB   r^   ÚOSErrorr   r)   ÚSEND_BUFFER_SIZEr   rA   rU   rV   s   &&r   ÚwriteÚTunnel.write  sn   € ð ××ÐØ×Ñ×'Ñ'Ô)Üœ%Ð!:Ó;Ð;äˆx‹=˜4×0Ñ0Ô0Üœ'Ð#>Ó?Ð?à×Ñ×Ñ Ô)Ü�8‹}Ðr   )r?   r<   rB   r@   r>   r;   r=   rA   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú_DEVICE_NAMEÚIOErrorr   ÚEAGAIN_STYLErf   r
   ÚEWOULDBLOCK_STYLEr   ÚEINTR_STYLEr`   rg   rE   ÚpropertyrJ   rO   rX   rc   rh   Ú__static_attributes__Ú__classdictcell__©Ú__classdict__s   @r   r9   r9   ”   s‹   ø‡ € ñð #€Lñ ˜6Ð#EÓF€LÙ Ð-DÓEÐñ ˜%Ð!<Ó=€Kà ,ÐàÐò&ð. ñ=ó ð=ð ñ<ó ð<ò)ò"!1÷Fð r   r9   c                ó0   a € \        S 4      V 3R l4       pV# )a`  
Wrap a L{MemoryIOSystem} method with permission-checking logic.  The
returned function will check C{self.permissions} and raise L{IOError} with
L{errno.EPERM} if the function name is not listed as an available
permission.

@param original: The L{MemoryIOSystem} instance to wrap.

@return: A wrapper around C{original} that applies permission checks.
c                 ór   <€ SP                   V P                  9  d   \        \        R 4      hS! V .VO5/ VB # )zOperation not permitted)rj   Úpermissionsrf   r	   )rC   ÚargsÚkwargsÚoriginals   &*,€r   ÚpermissionCheckerÚ&_privileged.<locals>.permissionChecker+  s:   ø€ à×Ñ D×$4Ñ$4Ô4Üœ%Ð!:Ó;Ð;Ù˜Ð.˜tÒ. vÑ.Ð.r   r   )r~   r   s   f r   Ú_privilegedr�     s#   ø€ ô ˆ8ƒ_ô/ó ð/ð
 Ðr   c                   óˆ   a € ] tR tRt o RtRt^t^t^tR t	R t
R t]RR l4       tR	 tR
 tR t]R 4       tR tR tRtV tR# )ÚMemoryIOSystemi4  zÛ
An in-memory implementation of basic I/O primitives, useful in the context
of unit testing as a drop-in replacement for parts of the C{os} module.

@ivar _devices:
@ivar _openFiles:
@ivar permissions:

@ivar _counter:
i    c                ó4   € / V n         / V n        R R0V n        R# )ÚopenÚioctlN©Ú_devicesÚ
_openFilesr{   rI   s   &r   rE   ÚMemoryIOSystem.__init__G  s   € ØˆŒØˆŒØ" GÐ,ˆÖr   c                óD   € V P                   VP                  4       ,          # )a   
Get the L{Tunnel} object associated with the given L{TuntapPort}.

@param port: A L{TuntapPort} previously initialized using this
    L{MemoryIOSystem}.

@return: The tunnel object created by a prior use of C{open} on this
    object on the tunnel special device file.
@rtype: L{Tunnel}
)r‰   Úfileno)rC   Úports   &&r   Ú	getTunnelÚMemoryIOSystem.getTunnelL  s   € ð �‰˜tŸ{™{›}Õ-Ð-r   c                ó"   € W P                   V&   R# )zù
Specify a class which will be used to handle I/O to a device of a
particular name.

@param name: The filesystem path name of the device.
@type name: L{bytes}

@param cls: A class (like L{Tunnel}) to instantiated whenever this
    device is opened.
N)rˆ   )rC   r?   Úclss   &&&r   ÚregisterSpecialDeviceÚ$MemoryIOSystem.registerSpecialDeviceY  s   € ð "�‰�dÓr   Nc                óÞ   € WP                   9   dO   V P                  pV ;P                  ^,          un        V P                   V,          ! WV4      V P                  V&   V# \        \        R4      h)a~  
A replacement for C{os.open}.  This initializes state in this
L{MemoryIOSystem} which will be reflected in the behavior of the other
file descriptor-related methods (eg L{MemoryIOSystem.read},
L{MemoryIOSystem.write}, etc).

@param name: A string giving the name of the file to open.
@type name: C{bytes}

@param flags: The flags with which to open the file.
@type flags: C{int}

@param mode: The mode with which to open the file.
@type mode: C{int}

@raise OSError: With C{ENOSYS} if the file is not a recognized special
    device file.

@return: A file descriptor associated with the newly opened file
    description.
@rtype: L{int}
zFunction not implemented)rˆ   Ú_counterr‰   rf   r   )rC   r?   ÚflagsÚmodeÚfds   &&&& r   r…   ÚMemoryIOSystem.openf  sV   € ð0 —=‘=Ô Ø—‘ˆBØ�MŠM˜QÕ�MØ"&§-¡-°Ö"5°dÀ4Ó"HˆD�O‰O˜BÑØˆIÜ”fÐ8Ó9Ð9r   c                ó†   €  V P                   V,          P                  V4      #   \         d    \        \        R4      hi ; i)z„
Try to read some bytes out of one of the in-memory buffers which may
previously have been populated by C{write}.

@see: L{os.read}
úBad file descriptor)r‰   rc   ÚKeyErrorrf   r   )rC   r˜   ra   s   &&&r   rc   ÚMemoryIOSystem.read…  s>   € ð	8Ø—?‘? 2Õ&×+Ñ+¨EÓ2Ð2øÜô 	8Üœ%Ð!6Ó7Ð7ð	8úó	   ‚!$ ¤A c                ó†   €  V P                   V,          P                  V4      #   \         d    \        \        R4      hi ; i)zr
Try to add some bytes to one of the in-memory buffers to be accessed by
a later C{read} call.

@see: L{os.write}
r›   )r‰   rh   rœ   rf   r   )rC   r˜   Údatas   &&&r   rh   ÚMemoryIOSystem.write‘  s>   € ð	8Ø—?‘? 2Õ&×,Ñ,¨TÓ2Ð2øÜô 	8Üœ%Ð!6Ó7Ð7ð	8úrž   c                ó`   €  V P                   V R#   \         d    \        \        R4      hi ; i)zj
Discard the in-memory buffer and other in-memory state for the given
file descriptor.

@see: L{os.close}
r›   N)r‰   rœ   rf   r   )rC   r˜   s   &&r   ÚcloseÚMemoryIOSystem.close�  s0   € ð	8Ø—‘ Ò#øÜô 	8Üœ%Ð!6Ó7Ð7ð	8ús   ‚ ‘-c                ó”  €  V P                   V,          pT\        8w  d   \        \
        R4      h\        P                  ! R\        3,          T4      w  rVYdn	        YTn
        TR\        ^,
           R,           Tn        \        P                  ! R\        3,          TP                  T4      #   \         d    \        \        R4      hi ; i)zo
Perform some configuration change to the in-memory state for the given
file descriptor.

@see: L{fcntl.ioctl}
r›   zRequest or args is not valid.z%dsHNs   123)r‰   rœ   rf   r   r   r   r   r.   r   r=   r>   r?   r   )rC   r˜   Úrequestr|   Útunnelr?   r—   s   &&&&   r   r†   ÚMemoryIOSystem.ioctl©  s¦   € ð	8Ø—_‘_ RÕ(ˆFð ”jÔ Üœ&Ð"AÓBÐBä—]’] 6¬Y¨LÕ#8¸$Ó?‰
ˆØ ÔØ#ÔØ˜?œY¨�]Ð+¨fÕ4ˆŒä�{Š{˜6¤Y LÕ0°&·+±+¸tÓDÐDøô ô 	8Üœ%Ð!6Ó7Ð7ð	8ús   ‚B+ Â+Cc           
     óÔ   € RpRp\        VV^ ,          \        WB^,          VR7      R7      p\        V P                  P	                  4       4      pV^ ,          P                  V4       W43# )a  
Write an ethernet frame containing an ip datagram containing a udp
datagram containing the given payload, addressed to the given address,
to a tunnel device previously opened on this I/O system.

@param datagram: A UDP datagram payload to send.
@type datagram: L{bytes}

@param address: The destination to which to send the datagram.
@type address: L{tuple} of (L{bytes}, L{int})

@return: A two-tuple giving the address from which gives the address
    from which the datagram was sent.
@rtype: L{tuple} of (L{bytes}, L{int})
z10.1.2.3iaS  )r!   r"   r$   )r4   r7   Úlistr‰   ÚvaluesrX   )rC   rW   ÚaddressÚsrcIPÚsrcPortÚ
serializedÚ	openFiless   &&&    r   ÚsendUDPÚMemoryIOSystem.sendUDPÀ  sd   € ð" ˆØˆäØØ˜•
Ü˜W°!­*¸hÔGô
ˆ
ô ˜Ÿ™×/Ñ/Ó1Ó2ˆ	Ø�!�×$Ñ$ ZÔ0àÐÐr   c                ó   € \        W4      # )aù  
Get a socket-like object which can be used to receive a datagram sent
from the given address.

@param fileno: A file descriptor representing a tunnel device which the
    datagram will be received via.
@type fileno: L{int}

@param host: The IPv4 address to which the datagram was sent.
@type host: L{bytes}

@param port: The UDP port number to which the datagram was sent.
    received.
@type port: L{int}

@return: A L{socket.socket}-like object which can be used to receive
    the specified datagram.
)Ú	_FakePort)rC   rŒ   Úhostr�   s   &&&&r   Ú
receiveUDPÚMemoryIOSystem.receiveUDPß  s   € ô& ˜Ó&Ð&r   r‡   ©N)rj   rk   rl   rm   rn   r•   ÚO_RDWRrH   rN   rE   rŽ   r’   r�   r…   rc   rh   r£   r†   r±   r¶   ru   rv   rw   s   @r   rƒ   rƒ   4  sw   ø‡ € ñ	ð €Hà€FØ€JØ€Iò-ò
.ò"ð ó:ó ð:ò<
8ò
8ò
8ð ñEó ðEò, ÷>'ð 'r   rƒ   c                   ó0   a € ] tR tRt o RtR tR tRtV tR# )r´   iõ  z€
A socket-like object which can be used to read UDP datagrams from
tunnel-like file descriptors managed by a L{MemoryIOSystem}.
c                ó   € Wn         W n        R # r¸   )Ú_systemÚ_fileno)rC   r;   rŒ   s   &&&r   rE   Ú_FakePort.__init__û  s   € ØŒØŽr   c                óÈ  a
a€ V P                   P                  V P                  ,          P                  P	                  4       p. o
\        4       pV
3R lpWCn        \        4       pVP                  RV4       \        4       oSP                  ^V4       V P                   P                  V P                  ,          P                  pV\        P                  P                  ,          '       d*   \        4       pVP                  RS4       VP                  pMV3R lpV\        P                  P                  ,          '       * p	V	'       d
   V\         R pV! V4       S
^ ,          RV # )a'  
Receive a datagram sent to this port using the L{MemoryIOSystem} which
created this object.

This behaves like L{socket.socket.recv} but the data being I{sent} and
I{received} only passes through various memory buffers managed by this
object and L{MemoryIOSystem}.

@see: L{socket.socket.recv}
c                 ó*   <€ SP                  V 4       R # r¸   )rU   )rW   r¬   Ú	datagramss   &&€r   ÚcaptureÚ_FakePort.recv.<locals>.capture  s   ø€ Ø×Ñ˜XÖ&r   i90  r   c                 ó.   <€ SP                  V R R R R 4      # r¸   )ÚdatagramReceived)r    Úips   &€r   Ú<lambda>Ú _FakePort.recv.<locals>.<lambda>   s   ø€ ¨B×,?Ñ,?Ø�d˜D $¨ô-r   N)r¼   r‰   r½   rA   r^   r   rÅ   r   ÚaddProtor   r=   r   rR   rS   r   r\   r]   )rC   Únbytesr    ÚreceiverrÂ   Úudpr—   ÚetherrÅ   Ú	dataHasPIrÁ   rÆ   s   &&        @@r   ÚrecvÚ_FakePort.recvÿ  s  ù€ ð �|‰|×&Ñ& t§|¡|Õ4×@Ñ@×HÑHÓJˆàˆ	Ü#Ó%ˆõ	'ð %,Ô!äÓˆØ�‰�U˜HÔ%ä‹\ˆØ
�‰�B˜Ôà�|‰|×&Ñ& t§|¡|Õ4×?Ñ?ˆØ”+×%Ñ%×+Ñ+×+Ô+Ü$Ó&ˆEØ�N‰N˜5 "Ô%Ø$×5Ñ5Ñô Ðð ¤× 5Ñ 5× ;Ñ ;Õ;Ô<ˆ	çàœ˜	�?ˆDá˜ÔØ˜�|˜G˜VÐ$Ð$r   )r½   r¼   N)	rj   rk   rl   rm   rn   rE   rÏ   ru   rv   rw   s   @r   r´   r´   õ  s   ø‡ € ñò
÷,%ð ,%r   r´   )+rn   r*   r   Úcollectionsr   Úerrnor   r   r   r   r   r   r	   r
   Ú	functoolsr   Úzope.interfacer   Útwisted.internet.protocolr   Útwisted.pair.ethernetr   Útwisted.pair.ipr   Útwisted.pair.rawudpr   Útwisted.pair.tuntapr   r   r   r   Útwisted.python.compatr   r]   r   rT   r%   r4   r7   r9   r�   rƒ   r´   © r   r   Ú<module>rÜ      s›   ðñó Û Ý ß S× SÓ SÝ å &å 6Ý 2Ý &Ý .ß WÓ WÝ .ð €ò
 ð 	€ò.ò,/òd÷<Hñ HòVñ* Ð Ó!÷}'ð }'ó "ð}'÷@6%ó 6%r   