HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/internet/__pycache__/udp.cpython-38.pyc
U

��W[�H�@s�dZddlmZmZddlZddlZddlZddlZddlm	Z	ddl
mZedkr�ddlm
Z
ddlmZmZmZdd	lmZmZmZdd
lmZddlmZee
eegZeeeegZeZeZe
ZeZn8ddlmZmZmZmZmZdd
lmZeeegZegZddlmZm Z m!Z!ddl"m#Z#m$Z$ddl%m&Z&ddlm'Z'm(Z(m)Z)e	e)j*e)j+e)j,�Gdd�dej-��Z.e&Gdd�d��Z/e	e)j0�Gdd�de/e.��Z1dS)a�
Various asynchronous UDP classes.

Please do not use this module directly.

@var _sockErrReadIgnore: list of symbolic error constants (from the C{errno}
    module) representing socket errors where the error is temporary and can be
    ignored.

@var _sockErrReadRefuse: list of symbolic error constants (from the C{errno}
    module) representing socket errors that indicate connection refused.
�)�division�absolute_importN)�implementer)�platformTypeZwin32)�WSAEWOULDBLOCK)�WSAEINTR�WSAEMSGSIZE�WSAETIMEDOUT)�WSAECONNREFUSED�
WSAECONNRESET�WSAENETRESET)�WSAEINPROGRESS)�WSAENOPROTOOPT)�EWOULDBLOCK�EINTR�EMSGSIZE�ECONNREFUSED�EAGAIN)�ENOPROTOOPT)�base�defer�address)�log�failure)�	_oldStyle)�abstract�error�
interfacesc@s�eZdZdZejZejZdZ	dZ
dZd0dd�Ze
dd	��Zd
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zd1dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd2d"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Z d.d/�Z!dS)3�Porta�
    UDP port, listening for packets.

    @ivar maxThroughput: Maximum number of bytes read in one event
        loop iteration.

    @ivar addressFamily: L{socket.AF_INET} or L{socket.AF_INET6}, depending on
        whether this port is listening on an IPv4 address or an IPv6 address.

    @ivar _realPortNumber: Actual port number being listened on. The
        value will be L{None} until this L{Port} is listening.

    @ivar _preexistingSocket: If not L{None}, a L{socket.socket} instance which
        was created and initialized outside of the reactor and will be used to
        listen for connections (instead of a new socket being created by this
        L{Port}).
    iN�� cCs@tj�||�||_||_||_||_|��d|_|�	�dS)a
        @param port: A port number on which to listen.
        @type port: L{int}

        @param proto: A C{DatagramProtocol} instance which will be
            connected to the given C{port}.
        @type proto: L{twisted.internet.protocol.DatagramProtocol}

        @param interface: The local IPv4 or IPv6 address to which to bind;
            defaults to '', ie all IPv4 addresses.
        @type interface: L{str}

        @param maxPacketSize: The maximum packet size to accept.
        @type maxPacketSize: L{int}

        @param reactor: A reactor which will notify this C{Port} when
            its socket is ready for reading or writing. Defaults to
            L{None}, ie the default global reactor.
        @type reactor: L{interfaces.IReactorFDSet}
        N)
r�BasePort�__init__�port�protocol�
maxPacketSize�	interface�	setLogStr�_connectedAddr�_setAddressFamily)�selfr#�protor&r%�reactor�r-�6/usr/lib/python3/dist-packages/twisted/internet/udp.pyr"Ysz
Port.__init__c	Cs8t�|||j�}|��d}|d||||d�}||_|S)a�
        Create a new L{Port} based on an existing listening
        I{SOCK_DGRAM} socket.

        @param reactor: A reactor which will notify this L{Port} when
            its socket is ready for reading or writing. Defaults to
            L{None}, ie the default global reactor.
        @type reactor: L{interfaces.IReactorFDSet}

        @param fd: An integer file descriptor associated with a listening
            socket.  The socket must be in non-blocking mode.  Any additional
            attributes desired, such as I{FD_CLOEXEC}, must also be set already.
        @type fd: L{int}

        @param addressFamily: The address family (sometimes called I{domain}) of
            the existing socket.  For example, L{socket.AF_INET}.
        @param addressFamily: L{int}

        @param protocol: A C{DatagramProtocol} instance which will be
            connected to the C{port}.
        @type proto: L{twisted.internet.protocol.DatagramProtocol}

        @param maxPacketSize: The maximum packet size to accept.
        @type maxPacketSize: L{int}

        @return: A new instance of C{cls} wrapping the socket given by C{fd}.
        @rtype: L{Port}
        rN)r&r,r%)�socketZfromfd�
socketType�getsockname�_preexistingSocket)	�clsr,�fd�
addressFamilyr$r%r#r&r*r-r-r.�_fromListeningDescriptorxs
�zPort._fromListeningDescriptorcCs.|jdk	rd|jj|jfSd|jjfSdS)Nz
<%s on %s>z<%s not connected>)�_realPortNumberr$�	__class__�r*r-r-r.�__repr__�s
z
Port.__repr__cCs|jS)z)
        Return a socket object.
        )r/r9r-r-r.�	getHandle�szPort.getHandlecCs|��|��dS)z�
        Create and bind my socket, and begin listening on it.

        This is called on unserialization, and must be called after creating a
        server to begin listening on the specified port.
        N)�_bindSocket�_connectToProtocolr9r-r-r.�startListening�szPort.startListeningc
Cs�|jdkrbz|��}|�|j|jf�Wqntjk
r^}zt�|j|j|��W5d}~XYqnXn|j}d|_|��d|_	t
�d|�|j
�|j	f�d|_||_|jj|_dS)aJ
        Prepare and assign a L{socket.socket} instance to
        C{self.socket}.

        Either creates a new SOCK_DGRAM L{socket.socket} bound to
        C{self.interface} and C{self.port} or takes an existing
        L{socket.socket} provided via the
        L{interfaces.IReactorSocket.adoptDatagramPort} interface.
        N�z%s starting on %s)r2�createInternetSocketZbindr&r#r/rZCannotListenErrorr1r7r�msg�
_getLogPrefixr$�	connected�fileno�r*Zskt�ler-r-r.r<�s 

&
�zPort._bindSocketcCs|j�|�|��dS�N)r$ZmakeConnectionZstartReadingr9r-r-r.r=�szPort._connectToProtocolc
Cs�d}||jkr�z|j�|j�\}}Wndtjk
r�}zD|jd}|tkrTWY�,dS|tkrv|jrl|j	�
�WY�
dS�W5d}~XYqX|t|�7}|jtj
kr�|dd�}z|j	�||�Wqt��YqXqdS)z=
        Called when my socket is ready for reading.
        rN�)�
maxThroughputr/Zrecvfromr%r�args�_sockErrReadIgnore�_sockErrReadRefuser(r$�connectionRefused�lenr5�AF_INET6ZdatagramReceivedr�err)r*�read�data�addr�se�nor-r-r.�doRead�s(




zPort.doReadc
Cs�|jr�|d|jfkst�z|j�|�WStjk
r�}zR|jd}|tkr^|�|�WY�0S|tkrrt�	d��n|t
kr�|j��n�W5d}~XYnX�n:|dks�t�t
�|d�s�t
�|d�s�|ddkr�t�|dd��t
�|d��s|ddk�r|jtjk�rt�|dd��t
�|d��rL|jtjk�rLt�|dd��z|j�||�WStjk
�r�}zX|jd}|tk�r�|�||�WY�2S|tk�r�t�	d��n|t
k�r�WY�
dS�W5d}~XYnXdS)az
        Write a datagram.

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

        @type addr: L{tuple} containing L{str} as first element and L{int} as
            second element, or L{None}
        @param addr: A tuple of (I{stringified IPv4 or IPv6 address},
            I{integer port number}); can be L{None} in connected mode.
        Nrzmessage too longz<broadcast>z0write() only accepts IP addresses, not hostnamesz7IPv6 port write() called with IPv4 or broadcast addressz*IPv4 port write() called with IPv6 address)r(�AssertionErrorr/�sendrrJr�writerZMessageLengthErrorrr$rMr�isIPAddress�
isIPv6Address�InvalidAddressErrorr5rO�AF_INETZsendto)r*ZdatagramrSrTrUr-r-r.rY�s`
�
��
��
��




z
Port.writecCs|�d�|�|�dS)a�
        Write a datagram constructed from an iterable of L{bytes}.

        @param seq: The data that will make up the complete datagram to be
            written.
        @type seq: an iterable of L{bytes}

        @type addr: L{tuple} containing L{str} as first element and L{int} as
            second element, or L{None}
        @param addr: A tuple of (I{stringified IPv4 or IPv6 address},
            I{integer port number}); can be L{None} in connected mode.
        �N)rY�join)r*�seqrSr-r-r.�
writeSequence:s
zPort.writeSequencecCsL|jrtd��t�|�s.t�|�s.t�|d��||f|_|j�||f�dS)z-
        'Connect' to remote server.
        z:already connected, reconnecting is not currently supported�not an IPv4 or IPv6 address.N)	r(�RuntimeErrorrrZr[rr\r/�connect)r*Zhostr#r-r-r.rdJs�
zPort.connectcCs"|��|jr|j�d|j�dS)Nr)ZstopReadingrCr,Z	callLater�connectionLostr9r-r-r.�_loseConnectionWszPort._loseConnectioncCs&|jrt��}|_nd}|��|SrG)rCrZDeferred�drf)r*�resultr-r-r.�
stopListening]s
zPort.stopListeningcCstjdtdd�|��dS)Nz-Please use stopListening() to disconnect portrH)�
stacklevel)�warnings�warn�DeprecationWarningrir9r-r-r.�loseConnectionfszPort.loseConnectioncCsdt�d|j�d|_d|_tj�||�|j��|j	�
�|`	|`t|d�r`|j
�d�|`
dS)z&
        Cleans up my socket.
        z(UDP Port %s Closed)N���rg)rrAr7rIrr!rer$ZdoStopr/�closerD�hasattrrg�callback)r*�reasonr-r-r.reks


zPort.connectionLostcCs|�|j�}d||_dS)zP
        Initialize the C{logstr} attribute to be used by C{logPrefix}.
        z%s (UDP)N)rBr$�logstr)r*�	logPrefixr-r-r.r'|szPort.setLogStrcCsDt�|j�rtj|_n*t�|j�r,tj|_n|jr@t�	|jd��dS)z8
        Resolve address family for the socket.
        rbN)
rr[r&r/rOr5rZr]rr\r9r-r-r.r)�s

�zPort._setAddressFamilycCs|jS)z0
        Return the prefix to log with.
        )rtr9r-r-r.ru�szPort.logPrefixcCsJ|j��}|jtjkr$tjd|��S|jtjkrFtjd|dd���SdS)z�
        Return the local address of the UDP connection

        @returns: the local address of the UDP connection
        @rtype: L{IPv4Address} or L{IPv6Address}
        �UDPNrH)rv)rv)r/r1r5r]rZIPv4AddressrOZIPv6Address�r*rSr-r-r.�getHost�s

zPort.getHostcCs|j�tjtj|�dS)z�
        Set whether this port may broadcast. This is disabled by default.

        @param enabled: Whether the port may broadcast.
        @type enabled: L{bool}
        N)r/�
setsockopt�
SOL_SOCKET�SO_BROADCAST)r*�enabledr-r-r.�setBroadcastAllowed�s
�zPort.setBroadcastAllowedcCst�|j�tjtj��S)z�
        Checks if broadcast is currently allowed on this port.

        @return: Whether this port may broadcast.
        @rtype: L{bool}
        )�operator�truthr/�
getsockoptrzr{r9r-r-r.�getBroadcastAllowed�s�zPort.getBroadcastAllowed)rr N)N)N)"�__name__�
__module__�__qualname__�__doc__r/r]r5Z
SOCK_DGRAMr0rIr7r2r"�classmethodr6r:r;r>r<r=rVrYrardrfrirnrer'r)rurxr}r�r-r-r-r.r<s8

&"!
<
	

rc@sleZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
ddd�Zdd�Zdd�Z
ddd�ZdS)�MulticastMixinz,
    Implement multicast functionality.
    cCs$|j�tjtj�}t�t�d|��S)Nz@i)r/r��
IPPROTO_IP�IP_MULTICAST_IFZ	inet_ntoa�struct�pack)r*�ir-r-r.�getOutgoingInterface�sz#MulticastMixin.getOutgoingInterfacecCs|j�|��|j�S)zReturns Deferred of success.)r,�resolve�addCallback�
_setInterfacerwr-r-r.�setOutgoingInterface�sz#MulticastMixin.setOutgoingInterfacecCs"t�|�}|j�tjtj|�dS)Nr?)r/�	inet_atonryr�r�)r*rSr�r-r-r.r��s
zMulticastMixin._setInterfacecCs|j�tjtj�SrG)r/r�r��IP_MULTICAST_LOOPr9r-r-r.�getLoopbackMode�szMulticastMixin.getLoopbackModecCs*t�dt�|��}|j�tjtj|�dS)N�b)r�r�r~rr/ryr�r�)r*�moder-r-r.�setLoopbackMode�szMulticastMixin.setLoopbackModecCs|j�tjtj�SrG)r/r�r��IP_MULTICAST_TTLr9r-r-r.�getTTL�szMulticastMixin.getTTLcCs$t�d|�}|j�tjtj|�dS)N�B)r�r�r/ryr�r�)r*Zttlr-r-r.�setTTL�szMulticastMixin.setTTLrcCs|j�|��|j|d�S)z4Join a multicast group. Returns Deferred of success.r?�r,r�r��
_joinAddr1�r*rSr&r-r-r.�	joinGroup�szMulticastMixin.joinGroupcCs|j�|��|j||�SrG)r,r�r��
_joinAddr2)r*rSr&r_r-r-r.r��szMulticastMixin._joinAddr1c
Cs�t�|�}t�|�}|r tj}ntj}z|j�tj|||�WnBtjk
r�}z"t�tj	||f|j
���WY�Sd}~XYnXdSrG)r/r�ZIP_ADD_MEMBERSHIPZIP_DROP_MEMBERSHIPryr�rrZFailureZMulticastJoinErrorrJ)r*r&rSr_�cmd�er-r-r.r��s

zMulticastMixin._joinAddr2cCs|j�|��|j|d�S)z2Leave multicast group, return Deferred of success.rr�r�r-r-r.�
leaveGroup�szMulticastMixin.leaveGroupN)r)r)r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r-r-r-r.r��s

r�c@s"eZdZdZd
dd�Zdd	�ZdS)�
MulticastPortz.
    UDP Port that supports multicasting.
    rr NFcCst�||||||�||_dS)zX
        @see: L{twisted.internet.interfaces.IReactorMulticast.listenMulticast}
        N)rr"�listenMultiple)r*r#r+r&r%r,r�r-r-r.r"szMulticastPort.__init__c
Cszt�|�}|jrv|�tjtjd�ttd�rvz|�tjtjd�Wn2tj	k
rt}z|j
tkrbn�W5d}~XYnX|S)Nr?�SO_REUSEPORT)rr@r�ryr/rzZSO_REUSEADDRrqr�r�errnorrEr-r-r.r@s


z"MulticastPort.createInternetSocket)rr NF)r�r�r�r�r"r@r-r-r-r.r�s�
	r�)2r�Z
__future__rrr/r~r�rkZzope.interfacerZtwisted.python.runtimerr�rrrr	r
rrr
rrrKrLrrrrrZtwisted.internetrrrZtwisted.pythonrrZtwisted.python._oldstylerrrrZIListeningPortZ
IUDPTransportZ
ISystemHandler!rr�ZIMulticastTransportr�r-r-r-r.�<module>sT
�
�C