o b0@sldZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z ddlmZmZddlmZddlmZmZmZmZmZdd lmZmZdd lmZdd lmZdd l m!Z!dd l"m#Z#m$Z$gdZ%dZ&dZ'dZ(dZ)GdddeZ*e ej+Gddde$e#Z,GdddeddZ-Gddde Z.GdddZ/e ej0Gdddej1Z2dS) 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 TuntapPortiT@lTs /dev/net/tunc@speZdZdZedZedZedZedZedZ edZ edZ ed Z ed Z ed Zed Zed ZdS)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_EXCLr1r15/usr/lib/python3/dist-packages/twisted/pair/tuntap.pyr,s rc@s@eZdZdZdZdddfdfZeddZd d Zd d Z d S)rzU A L{TunnelAddress} represents the tunnel to which a L{TuntapPort} is bound. ) _typeValuenametypecC|jSN)r4)flagr1r1r2TszTunnelAddress.r4cCs|jjS)z Return the integer value of the C{type} attribute. Used to produce correct results in the equality implementation. )r5valueselfr1r1r2r3VszTunnelAddress._typeValuecCs||_||_dS)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)r5r4)r<r5r4r1r1r2__init___s zTunnelAddress.__init__cCstjdtddd|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)warningswarnDeprecationWarningr4)r<indexr1r1r2 __getitem__js zTunnelAddress.__getitem__N) r!r"r#r$compareAttributesshowAttributespropertyr3r=rEr1r1r1r2rMs  rc@seZdZdZdS)_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$r1r1r1r2rIwsrIz fileno namec@sdeZdZdZedZedZedZdddZdd d Z d d Z d dZ ddZ ddZ ddZdS)_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}cCdS)z" @see: L{os.open} Nr1)filenamer8moder1r1r2openz_IInputOutputSystem.openNcCrL)z& @see: L{fcntl.ioctl} Nr1)fdoptarg mutate_flagr1r1r2ioctlrPz_IInputOutputSystem.ioctlcCrL)z" @see: L{os.read} Nr1)rQlimitr1r1r2readrPz_IInputOutputSystem.readcCrL)z# @see: L{os.write} Nr1)rQdatar1r1r2writerPz_IInputOutputSystem.writecCrL)z# @see: L{os.close} Nr1)rQr1r1r2closerPz_IInputOutputSystem.closecCrL)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}) Nr1)datagramaddressr1r1r2sendUDPrPz_IInputOutputSystem.sendUDPcCrL)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. Nr1)filenohostportr1r1r2 receiveUDPrPz_IInputOutputSystem.receiveUDP)rK)NN)r!r"r#r$rO_RDWR O_NONBLOCK O_CLOEXECrOrUrWrYrZr]rar1r1r1r2rJs   rJc@sZeZdZdZeejZeejZeejZeej Z ee j Z ej Z ej Z eeddZdS) _RealSystemz An interface to the parts of the operating system which L{TuntapPort} relies on. This is most of an implementation of L{_IInputOutputSystem}. rdiN)r!r"r#r$ staticmethodosrOrWrYrZfcntlrUrbrcgetattrrdr1r1r1r2res     rec@seZdZdZdZd%ddZdefdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZeeddddeddZd&dd Zd!d"Zd#d$ZdS)'rzH A Port that reads and writes packets from/to a TUN/TAP-device. ir NcCstj|rd|_tj|_nd|_tj|_tj|sJ|dur$t }||_ t j ||||_||_||_||j}|d|jjd|_dS)Nrrz ())rIEthernetProtocol providedByrr&_moder%rIRawPacketProtocolre_systemr FileDescriptorr= interfaceprotocol maxPacketSize _getLogPrefixr4logstr)r<rqprotorsreactorsystem logPrefixr1r1r2r=s   zTuntapPort.__init__returncCs@t|jjf}|jr|d}n|d}||jj|jf}d|S)N))znot z<%s %slistening on %s/%s>)rrr __class__ connectedrmr4rq)r<argsr1r1r2__repr__s  zTuntapPort.__repr__cCs ||j||dS)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) _bindSocketrrmakeConnection startReadingr;r1r1r2startListenings  zTuntapPort.startListeningcCsd|jj|jjB|jjB}tdtf||j}|jt |}|j |t |}t ||dt dS)af 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)rorbrdrcstructpack _IFNAMSIZr:rO _TUN_KO_PATHrU _TUNSETIFFrIstrip)r<r4rNflagsconfigr^resultr1r1r2 _openTunnel s  zTuntapPort._openTunnelc Csttjd|jj|jdz||j|jtjB\}}Wnt y.}zt d|j|d}~ww||_||_ d|_ dS)z" Open the tunnel. z&%(protocol)s starting on %(interface)s)formatrrrqNr)rmsgrrr|rqrrmrr-OSErrorr CannotListenError_filenor})r<r^rqer1r1r2rs   zTuntapPort._bindSocketcCr6r7)rr;r1r1r2r^4szTuntapPort.filenoc Csd}||jkrjz |j|j|j}Wn&ty1}z|jtjtjtj fvr,WYd}~dSd}~wt y8w|t |7}z |j j |ddWnt ybt|j j}tdd|dYnw||jksdSdS)z= Called when my socket is ready for reading. rN)partialzUnhandled exception from z.datagramReceived) maxThroughputrorWrrsrerrno EWOULDBLOCKEAGAINEINTR BaseExceptionlenrrdatagramReceivedrr|rerr)r<rWrXrclsr1r1r2doRead7s(     zTuntapPort.doReadc CsPz |j|j|WSty'}z|jtjkr"||WYd}~Sd}~ww)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)rorYrrrr)r<r[rr1r1r2rYNs zTuntapPort.writecCs|d|dS)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} N)rYjoin)r<seqr1r1r2 writeSequence]szTuntapPort.writeSequencecCsD||jr |jS|jrt|jd|j|_d|_|jSt dS)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_stoppedDeferredr}r deferLaterrwconnectionLostr succeedr;r1r1r2 stopListeninggs  zTuntapPort.stopListeningTwistedrcCs|tjdS)zN Close this tunnel. Use L{TuntapPort.stopListening} instead. N)r addErrbackrrr;r1r1r2loseConnection{szTuntapPort.loseConnectioncCsFtd|jtj|||jd|_|j |j d|_ dS)zY Cleans up my socket. @param reason: Ignored. Do not use this. z(Tuntap %s Closed)rN) rrrqr rprrrdoStopr}rorZr)r<reasonr1r1r2rs   zTuntapPort.connectionLostcCr6)zK Returns the name of my class, to prefix log entries with. )rur;r1r1r2ryszTuntapPort.logPrefixcCst|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} )rrmrqr;r1r1r2getHostszTuntapPort.getHost)r NNr7)r!r"r#r$rr=strrrrrr^rrYrrrr rrryrr1r1r1r2rs$       r)3r$rrhrgrrA collectionsrtypingrzope.interfacerrr constantlyrr incrementalr twisted.internetr r r r r twisted.pairrrtwisted.pythonrtwisted.python.deprecatertwisted.python.reflectrtwisted.python.utilrr__all__rr _TUNGETIFFrrIAddressrrIrJreIListeningPortrprr1r1r1r2s<      !) F