Ϫf1dZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z m Z mZddlmZmZddlmZddlmZmZmZmZmZdd lmZmZdd lmZdd lm Z dd l!m"Z"dd l#m$Z$m%Z%gdZ&dZ'ejPdk(s@ejPjSds ejPjSdrdZ*dZ+ndZ*dZ+dZ,GddeZ-eej\Gdde%e$Z/GddeddZ0Gdd e Z1Gd!d"Z2eejfGd#d$ejhZ5y)%zc Support for Linux ethernet and IP tunnel devices. @see: U{https://en.wikipedia.org/wiki/TUN/TAP} N) namedtuple)Tuple) Attribute Interface implementer) FlagConstantFlags)Version)abstractdefererror interfacestask)ethernetraw)log) deprecated)fullyQualifiedName) FancyEqMixin FancyStrMixin) TunnelFlags TunnelAddress TuntapPortpariscppcsparclTiT@iT@lTs /dev/net/tunceZdZdZedZedZedZedZedZ edZ edZ ed Z ed Z ed Zed Zed Zy)ra~ 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 @ii i@iN)__name__ __module__ __qualname____doc__rIFF_TUNIFF_TAP TUN_FASYNCTUN_NOCHECKSUM TUN_NO_PI TUN_ONE_QUEUE TUN_PERSIST TUN_VNET_HDR IFF_NO_PI IFF_ONE_QUEUE IFF_VNET_HDR IFF_TUN_EXCL5/usr/lib/python3/dist-packages/twisted/pair/tuntap.pyrr6s 6"G6"Gf%J!&)NV$I (Mv&K'LV$I (M'L'Lr7rc>eZdZdZdZddfdfZedZdZdZ y ) rzU A L{TunnelAddress} represents the tunnel to which a L{TuntapPort} is bound. ) _typeValuenametypec|jSN)r;)flags r8zTunnelAddress.^s DIIr7r;c.|jjS)z Return the integer value of the C{type} attribute. Used to produce correct results in the equality implementation. )r<valueselfs r8r:zTunnelAddress._typeValue`syyr7c ||_||_y)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;)rDr<r;s r8__init__zTunnelAddress.__init__is  r7c\tjdtdd|jf|S)zS Deprecated accessor for the tunnel name. Use attributes instead. zUTunnelAddress.__getitem__ is deprecated since Twisted 14.0.0 Use attributes instead.r )category stacklevelTUNTAP)warningswarnDeprecationWarningr;)rDindexs r8 __getitem__zTunnelAddress.__getitem__ts1   &'  $))$U++r7N) r&r'r(r)compareAttributesshowAttributespropertyr:rFrOr6r7r8rrWs;/56?N   ,r7rceZdZdZy)_TunnelDescriptionz 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} N)r&r'r(r)r6r7r8rTrTsr7rTz fileno namecneZdZdZedZedZedZd dZddZ dZ d Z d Z d Z d Zy)_IInputOutputSystemz 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}cy)z" @see: L{os.open} Nr6)filenamer?modes r8openz_IInputOutputSystem.openr7Ncy)z& @see: L{fcntl.ioctl} Nr6)fdoptarg mutate_flags r8ioctlz_IInputOutputSystem.ioctlr[r7cy)z" @see: L{os.read} Nr6)r]limits r8readz_IInputOutputSystem.readr[r7cy)z# @see: L{os.write} Nr6)r]datas r8writez_IInputOutputSystem.writer[r7cy)z# @see: L{os.close} Nr6)r]s r8closez_IInputOutputSystem.closer[r7cy)a 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}) Nr6)datagramaddresss r8sendUDPz_IInputOutputSystem.sendUDPr[r7cy)af 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. Nr6)filenohostports r8 receiveUDPz_IInputOutputSystem.receiveUDPr[r7)i)NN)r&r'r(r)rO_RDWR O_NONBLOCK O_CLOEXECrZrardrgrirmrrr6r7r8rVrVsM + ,F34J12I       r7rVceZdZdZeej ZeejZeejZeejZ ee jZ ejZ ejZ eeddZy) _RealSystemz An interface to the parts of the operating system which L{TuntapPort} relies on. This is most of an implementation of L{_IInputOutputSystem}. ruiN)r&r'r(r) staticmethodosrZrdrgrifcntlrarsrtgetattrrur6r7r8rwrwsq  D  D  "E  "E  %E YYFJK3Ir7rwceZdZdZdZddZdefdZdZdZ d Z d Z d Z d Z d ZdZeeddddedZddZdZdZy)rzH A Port that reads and writes packets from/to a TUN/TAP-device. iNctjj|rd|_tj|_n=d|_tj |_tjj|sJ| t}||_ tjj||||_||_||_|j#|j}|d|j j$d|_y)Nrrz ())rIEthernetProtocol providedByrr+_moder*rIRawPacketProtocolrw_systemr FileDescriptorrF interfaceprotocol maxPacketSize _getLogPrefixr;logstr)rDrprotorreactorsystem logPrefixs r8rFzTuntapPort.__init__s  % % 0 0 7DM$,,DJDM$,,DJ))44U; ;; > ]F ((w7" *&&t}}5 " 2djjoo%6a8 r7returnct|jjf}|jr|dz}n|dz}||jj |j fz}d|zS)N))znot z<%s %slistening on %s/%s>)rr __class__ connectedrr;r)rDargss r8__repr__zTuntapPort.__repr__sY!3DMM4K4K!L N >>%)rrCs r8rozTuntapPort.fileno>s ||r7cHd}||jkrn |jj|j|j}|t|z } |jj|d||jkrmyy#t $rF}|j t jt jt jfvrYd}~yd}~wt$rwxYw#t$r<t|jj}t!j"dd|dYwxYw)z= Called when my socket is ready for reading. rN)partialzUnhandled exception from z.datagramReceived) maxThroughputrrdrrrerrno EWOULDBLOCKEAGAINEINTR BaseExceptionlenrdatagramReceivedrrrerr)rDrdrfrclss r8doReadzTuntapPort.doReadAsT''' ||((t7I7IJ CI D R ..tQ.?T''' 77u00%,, LL    ! R()@)@A 9#>OPQ Rs00BC C ;C  C  CAD! D!c |jj|j|S#t$r9}|jtj k(r|j|cYd}~Sd}~wwxYw)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)rrgrrrr)rDrkrs r8rgzTuntapPort.writeXsT <<%%dllH= = ww%++%zz(++  s %( A*-A%A*$A%%A*cD|jdj|y)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} r7N)rgjoin)rDseqs r8 writeSequencezTuntapPort.writeSequencegs 388C=!r7c|j|jr |jS|jrCt j |j d|j|_d|_|jStjdS)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. rTN) stopReading disconnecting_stoppedDeferredrr deferLaterrconnectionLostr succeedrCs r8 stopListeningzTuntapPort.stopListeningqss    (( ( ^^$(OO a!4!4%D !"&D (( (==& &r7Twistedrc^|jjtjy)zN Close this tunnel. Use L{TuntapPort.stopListening} instead. N)r addErrbackrrrCs r8loseConnectionzTuntapPort.loseConnections ''0r7c"tjd|jztjj |||j jd|_|jj|jd|_ y)zY Cleans up my socket. @param reason: Ignored. Do not use this. z(Tuntap %s Closed)rN) rrrr rrrdoStoprrrir)rDreasons r8rzTuntapPort.connectionLostsd $t~~56..tV<  4<<( r7c|jS)zK Returns the name of my class, to prefix log entries with. )rrCs r8rzTuntapPort.logPrefixs{{r7cBt|j|jS)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} )rrrrCs r8getHostzTuntapPort.getHostsTZZ88r7)r%NNr>)r&r'r(r)rrFstrrrrrrorrgrrrr rrrrr6r7r8rrs~M9*2#2 M$*R. "'( 2q!,m<1=1  9r7r)6r)rrzryplatformrrK collectionsrtypingrzope.interfacerrr constantlyrr incrementalr twisted.internetr r r rr twisted.pairrrtwisted.pythonrtwisted.python.deprecatertwisted.python.reflectrtwisted.python.utilrr__all__rmachine startswithr _TUNGETIFFrrIAddressrrTrVrwIListeningPortrrr6r7r8rsE    "<<*EE&/5;   H("x$$U+x$$W-JJJJ (%(B Z !&,M<&,"&,R $8-H C )C L44& Z & &'@9((@9(@9r7