
    h1                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  SSKJrJrJr  SSKJrJr  SSKJr  SSKJrJrJrJrJr  SS	KJrJr  SS
KJr  SSKJ r   SSK!J"r"  SSK#J$r$J%r%  / SQr&Sr'\RP                  " 5       S:X  dJ  \RP                  " 5       RS                  S5      (       d%  \RP                  " 5       RS                  S5      (       a  Sr*Sr+OSr*Sr+Sr, " S S\5      r-\" \R\                  5       " S S\%\$5      5       r/ " S S\" SS5      5      r0 " S S \5      r1 " S! S"5      r2\" \Rf                  5       " S# S$\Rh                  5      5       r5g)%zc
Support for Linux ethernet and IP tunnel devices.

@see: U{https://en.wikipedia.org/wiki/TUN/TAP}
    N)
namedtuple)Tuple)	Attribute	Interfaceimplementer)FlagConstantFlags)Version)abstractdefererror
interfacestask)ethernetraw)log)
deprecated)fullyQualifiedName)FancyEqMixinFancyStrMixin)TunnelFlagsTunnelAddress
TuntapPort   pariscppcsparcl   T  iT@iT@l   T  s   /dev/net/tunc                       \ rS rSrSr\" S5      r\" S5      r\" S5      r\" S5      r	\" S5      r
\" S5      r\" S	5      r\" S
5      r\" S5      r\" S5      r\" S5      r\" S5      rSrg)r   6   aV  
L{TunnelFlags} defines more flags which are used to configure the behavior
of a tunnel device.

@cvar IFF_TUN: This indicates a I{tun}-type device.  This type of tunnel
    carries IP datagrams.  This flag is mutually exclusive with C{IFF_TAP}.

@cvar IFF_TAP: This indicates a I{tap}-type device.  This type of tunnel
    carries ethernet frames.  This flag is mutually exclusive with C{IFF_TUN}.

@cvar IFF_NO_PI: This indicates the I{protocol information} header will
    B{not} be included in data read from the tunnel.

@see: U{https://www.kernel.org/doc/Documentation/networking/tuntap.txt}
      r       @         i   i       i @  i    N)__name__
__module____qualname____firstlineno____doc__r   IFF_TUNIFF_TAP
TUN_FASYNCTUN_NOCHECKSUM	TUN_NO_PITUN_ONE_QUEUETUN_PERSISTTUN_VNET_HDR	IFF_NO_PIIFF_ONE_QUEUEIFF_VNET_HDRIFF_TUN_EXCL__static_attributes__r'       Z/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/pair/tuntap.pyr   r   6   s      6"G6"Gf%J!&)NV$I (Mv&K'LV$I (M'L'Lr:   r   c                   F    \ rS rSrSrSrSS 4S4r\S 5       rS r	S	 r
S
rg)r   W   zM
A L{TunnelAddress} represents the tunnel to which a L{TuntapPort} is bound.
)
_typeValuenametypec                     U R                   $ N)r?   )flags    r;   <lambda>TunnelAddress.<lambda>^   s    DIIr:   r?   c                 .    U R                   R                  $ )zu
Return the integer value of the C{type} attribute.  Used to produce
correct results in the equality implementation.
)r@   valueselfs    r;   r>   TunnelAddress._typeValue`   s     yyr:   c                     Xl         X l        g)z
@param type: Either L{TunnelFlags.IFF_TUN} or L{TunnelFlags.IFF_TAP},
    representing the type of this tunnel.

@param name: The system name of the tunnel.
@type name: L{bytes}
N)r@   r?   )rI   r@   r?   s      r;   __init__TunnelAddress.__init__i   s     		r:   c                 X    [         R                  " S[        SS9  SU R                  4U   $ )zC
Deprecated accessor for the tunnel name.  Use attributes instead.
zUTunnelAddress.__getitem__ is deprecated since Twisted 14.0.0  Use attributes instead.r!   )category
stacklevelTUNTAP)warningswarnDeprecationWarningr?   )rI   indexs     r;   __getitem__TunnelAddress.__getitem__t   s1     	&'		
 $))$U++r:   )r?   r@   N)r(   r)   r*   r+   r,   compareAttributesshowAttributespropertyr>   rL   rV   r9   r'   r:   r;   r   r   W   s;     /56?N 	
,r:   r   c                       \ rS rSrSrSrg)_TunnelDescription   z
Describe an existing tunnel.

@ivar fileno: the file descriptor associated with the tunnel
@type fileno: L{int}

@ivar name: the name of the tunnel
@type name: L{bytes}
r'   N)r(   r)   r*   r+   r,   r9   r'   r:   r;   r\   r\      s    r:   r\   zfileno namec                   z    \ rS rSrSr\" S5      r\" S5      r\" S5      rSS jr	SS jr
S	 rS
 rS rS rS rSrg)_IInputOutputSystem   z
An interface for performing some basic kinds of I/O (particularly that I/O
which might be useful for L{twisted.pair.tuntap}-using code).
z@see: L{os.O_RDWR}z@see: L{os.O_NONBLOCK}z@see: L{os.O_CLOEXEC}c                     g)z
@see: L{os.open}
Nr'   )filenamerC   modes      r;   open_IInputOutputSystem.open       r:   Nc                     g)z
@see: L{fcntl.ioctl}
Nr'   )fdoptargmutate_flags       r;   ioctl_IInputOutputSystem.ioctl   rf   r:   c                     g)z
@see: L{os.read}
Nr'   )rh   limits     r;   read_IInputOutputSystem.read   rf   r:   c                     g)z
@see: L{os.write}
Nr'   )rh   datas     r;   write_IInputOutputSystem.write   rf   r:   c                     g)z
@see: L{os.close}
Nr'   )rh   s    r;   close_IInputOutputSystem.close   rf   r:   c                     g)aL  
Send a datagram to a certain address.

@param datagram: The payload of a UDP datagram 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: The local address from which the datagram was sent.
@rtype: L{tuple} of (L{bytes}, L{int})
Nr'   )datagramaddresss     r;   sendUDP_IInputOutputSystem.sendUDP   rf   r:   c                     g)a  
Return a socket which can be used to receive datagrams sent to the
given address.

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

@param host: The IPv4 address at which the datagram will be received.
@type host: L{bytes}

@param port: The UDP port number at which the datagram will be
    received.
@type port: L{int}

@return: A L{socket.socket} which can be used to receive the specified
    datagram.
Nr'   )filenohostports      r;   
receiveUDP_IInputOutputSystem.receiveUDP   rf   r:   r'   )i  )NN)r(   r)   r*   r+   r,   r   O_RDWR
O_NONBLOCK	O_CLOEXECrd   rl   rp   rt   rw   r|   r   r9   r'   r:   r;   r_   r_      sM    
 +,F34J12I




r:   r_   c                      \ rS rSrSr\" \R                  5      r\" \R                  5      r\" \R                  5      r	\" \R                  5      r
\" \R                  5      r\R                  r\R                  r\" \SS5      rSrg)_RealSystem   z
An interface to the parts of the operating system which L{TuntapPort}
relies on.  This is most of an implementation of L{_IInputOutputSystem}.
r   i   r'   N)r(   r)   r*   r+   r,   staticmethodosrd   rp   rt   rw   fcntlrl   r   r   getattrr   r9   r'   r:   r;   r   r      sq    
  D D"E"E%EYYFJ K3Ir:   r   c                       \ rS rSrSrSrSS jrS\4S jrS r	S	 r
S
 rS rS rS rS rS r\" \" SSSS5      \5      S 5       rSS jrS rS rSrg)r      z@
A Port that reads and writes packets from/to a TUN/TAP-device.
i   Nc                 
   [         R                  R                  U5      (       a  SU l         [        R                  U l        OBSU l         [        R                  U l        [        R                  R                  U5      (       d   eUc
  [        5       nXPl
        [        R                  R                  X5        Xl        X l        X0l        U R#                  U R                  5      nU SU R
                  R$                   S3U l        g )Nr    r   z ())r   IEthernetProtocol
providedByr   r.   _moder-   r   IRawPacketProtocolr   _systemr   FileDescriptorrL   	interfaceprotocolmaxPacketSize_getLogPrefixr?   logstr)rI   r   protor   reactorsystem	logPrefixs          r;   rL   TuntapPort.__init__   s    %%0077DM$,,DJDM$,,DJ))44U;;;;> ]F((7"*&&t}}5	"2djjoo%6a8r:   returnc                     [        U R                  R                  5      4nU R                  (       a  US-   nOUS-   nXR                  R
                  U R                  4-   nSU-  $ )N) )znot z<%s %slistening on %s/%s>)r   r   	__class__	connectedr   r?   r   )rI   argss     r;   __repr__TuntapPort.__repr__  sW    !3DMM4K4K!L N>>%<D)#Dzz77*T11r:   c                 z    U R                  5         U R                  R                  U 5        U R                  5         g)z
Create and bind my socket, and begin listening on it.

This must be called after creating a server to begin listening on the
specified tunnel.
N)_bindSocketr   makeConnectionstartReadingrH   s    r;   startListeningTuntapPort.startListening  s.     	$$T*r:   c                    U R                   R                  U R                   R                  -  U R                   R                  -  n[        R
                  " S[        4-  XR                  5      nU R                   R                  [        U5      nU R                   R                  U[        U5      n[        XVS[         R                  S5      5      $ )a.  
Open the named tunnel using the given mode.

@param name: The name of the tunnel to open.
@type name: L{bytes}

@param mode: Flags from L{TunnelFlags} with exactly one of
    L{TunnelFlags.IFF_TUN} or L{TunnelFlags.IFF_TAP} set.

@return: A L{_TunnelDescription} representing the newly opened tunnel.
z%dsHN    )r   r   r   r   structpack	_IFNAMSIZrG   rd   _TUN_KO_PATHrl   
_TUNSETIFFr\   strip)rI   r?   rc   flagsconfigr   results          r;   _openTunnelTuntapPort._openTunnel  s     ##dll&<&<<t||?V?VVVyl2D**E""<7##FJ?!&)*<*B*B7*KLLr:   c                 n   [         R                  " SU R                  R                  U R                  S9   U R                  U R                  U R                  [        R                  -  5      u  pX l        Xl        SU l        g! [         a'  n[        R                  " SU R                  U5      eSnAff = f)z
Open the tunnel.
z&%(protocol)s starting on %(interface)s)formatr   r   Nr    )r   msgr   r   r   r   r   r   r5   OSErrorr   CannotListenError_filenor   )rI   r   r   es       r;   r   TuntapPort._bindSocket)  s     	;]],,nn	

	C $ 0 0

[-B-B B!F #  	C))$BB	Cs   9B 
B4"B//B4c                     U R                   $ rB   )r   rH   s    r;   r   TuntapPort.fileno>  s    ||r:   c                 P   SnXR                   :  al   U R                  R                  U R                  U R                  5      nU[        U5      -  n U R                  R                  USS9  XR                   :  a  Mk  gg! [
         aH  nUR                  [        R                  [        R                  [        R                  4;   a   SnAge SnAf[         a    e f = f! [         a=    [        U R                  R                  5      n[         R"                  " SSU S35         Nf = f)z-
Called when my socket is ready for reading.
r   N)partialzUnhandled exception from z.datagramReceived)maxThroughputr   rp   r   r   r   errnoEWOULDBLOCKEAGAINEINTRBaseExceptionlenr   datagramReceivedr   r   r   err)rI   rp   rs   r   clss        r;   doReadTuntapPort.doReadA  s     '''||((t7I7IJ CIDR..tQ.? '''  77u00%,,LL   ! R()@)@A 9#>OPQRs0   0A? C ?
C	=CCCAD%$D%c                      U R                   R                  U R                  U5      $ ! [         a:  nUR                  [        R
                  :X  a  U R                  U5      s SnA$ e SnAff = f)z
Write the given data as a single datagram.

@param datagram: The data that will make up the complete datagram to be
    written.
@type datagram: L{bytes}
N)r   rt   r   r   r   r   )rI   rz   r   s      r;   rt   TuntapPort.writeX  sT    	<<%%dllH== 	ww%++%zz(++	s    %( 
A,.A' A,&A''A,c                 D    U R                  SR                  U5      5        g)z
Write a datagram constructed from a L{list} of L{bytes}.

@param seq: The data that will make up the complete datagram to be
    written.
@type seq: L{list} of L{bytes}
r:   N)rt   join)rI   seqs     r;   writeSequenceTuntapPort.writeSequenceg  s     	

388C=!r:   c                 2   U R                  5         U R                  (       a  U R                  $ U R                  (       aD  [        R
                  " U R                  SU R                  5      U l        SU l        U R                  $ [        R                  " S5      $ )z
Stop accepting connections on this port.

This will shut down my socket and call self.connectionLost().

@return: A L{Deferred} that fires when this port has stopped.
r   TN)
stopReadingdisconnecting_stoppedDeferredr   r   
deferLaterr   connectionLostr   succeedrH   s    r;   stopListeningTuntapPort.stopListeningq  ss     	(((^^$(OOa!4!4%D! "&D(((==&&r:   Twisted   r   c                 ^    U R                  5       R                  [        R                  5        g)z>
Close this tunnel.  Use L{TuntapPort.stopListening} instead.
N)r   
addErrbackr   r   rH   s    r;   loseConnectionTuntapPort.loseConnection  s    
 	''0r:   c                 "   [         R                  " SU R                  -  5        [        R                  R                  X5        U R                  R                  5         SU l        U R                  R                  U R                  5        SU l        g)zA
Cleans up my socket.

@param reason: Ignored.  Do not use this.
z(Tuntap %s Closed)r   N)r   r   r   r   r   r   r   doStopr   r   rw   r   )rI   reasons     r;   r   TuntapPort.connectionLost  sb     	$t~~56..t<4<<(r:   c                     U R                   $ )z;
Returns the name of my class, to prefix log entries with.
)r   rH   s    r;   r   TuntapPort.logPrefix  s     {{r:   c                 B    [        U R                  U R                  5      $ )z
Get the local address of this L{TuntapPort}.

@return: A L{TunnelAddress} which describes the tunnel device to which
    this object is bound.
@rtype: L{TunnelAddress}
)r   r   r   rH   s    r;   getHostTuntapPort.getHost  s     TZZ88r:   )r   r   r   r   r   r   r   r   r   r   r   )r&   NNrB   )r(   r)   r*   r+   r,   r   rL   strr   r   r   r   r   r   rt   r   r   r   r
   r   r   r   r   r9   r'   r:   r;   r   r      s~     M9*2# 2	M$*R."'( 	2q!,m<1 =19r:   r   )6r,   r   r   r   platformr   rR   collectionsr   typingr   zope.interfacer   r   r   
constantlyr   r	   incrementalr
   twisted.internetr   r   r   r   r   twisted.pairr   r   twisted.pythonr   twisted.python.deprecater   twisted.python.reflectr   twisted.python.utilr   r   __all__r   machine
startswithr   
_TUNGETIFFr   r   IAddressr   r\   r_   r   IListeningPortr   r   r'   r:   r;   <module>r     sE  
   	    "  < < *  E E &  / 5 ; 	("$$U++$$W-- JJJJ(% (B Z  !&,M< &, "&,R	$8-H 	C) CL4 4& Z&&'@9(( @9 (@9r:   