HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/twisted/internet/__pycache__/udp.cpython-38.pyc
U

�b�H�@s`dZddlZddlZddlZddlmZddlmZddlm	Z	e	dkr�ddl
mZddl
mZdd	l
m
Z
mZmZmZmZmZmZeeeegZe
eeegZeZe
ZeZeZn0dd
l
mZmZmZmZmZmZeeegZegZddlmZmZmZm Z m!Z!m"Z"ddl#m$Z$m%Z%ee"j&e"j'e"j(�Gd
d�dej)��Z*Gdd�d�Z+ee"j,�Gdd�de+e*��Z-dS)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.
�N)�Optional)�implementer)�platformType�win32)�WSAEINPROGRESS)�WSAEWOULDBLOCK)�WSAECONNREFUSED�
WSAECONNRESET�WSAEINTR�WSAEMSGSIZE�WSAENETRESET�WSAENOPROTOOPT�WSAETIMEDOUT)�EAGAIN�ECONNREFUSED�EINTR�EMSGSIZE�ENOPROTOOPT�EWOULDBLOCK)�abstract�address�base�defer�error�
interfaces)�failure�logc@s�eZdZUdZejZejZdZ	dZ
eee
d<dZd2dd�Zed	d
��Zed�dd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zd3dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd4d$d%�Zd&d'�Z d(d)�Z!d*d+�Z"d,d-�Z#d.d/�Z$d0d1�Z%dS)5�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�_realPortNumber�� 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"[sz
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}.
        @type addressFamily: L{int}

        @param protocol: A C{DatagramProtocol} instance which will be
            connected to the C{port}.
        @type protocol: 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%)�socket�fromfd�
socketType�getsockname�_preexistingSocket)	�clsr,�fd�
addressFamilyr$r%r#r&r*r-r-r.�_fromListeningDescriptorys �zPort._fromListeningDescriptor)�returncCs6|jdk	r"d|jj�d|j�d�Sd|jj�d�SdS)N�<z on �>z not connected>)rr$�	__class__�r*r-r-r.�__repr__�s
z
Port.__repr__cCs|jS)z)
        Return a socket object.
        )r/r<r-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�_connectToProtocolr<r-r-r.�startListening�szPort.startListeningc
Cs�|jdkr`z|��}|�|j|jf�Wqltk
r\}zt�|j|j|��W5d}~XYqlXn|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)r3�createInternetSocket�bindr&r#�OSErrorr�CannotListenErrorr2rr�msg�
_getLogPrefixr$�	connectedr/�fileno�r*�skt�ler-r-r.r?�s"

&��zPort._bindSocketcCs|j�|�|��dS�N)r$�makeConnection�startReadingr<r-r-r.r@�szPort._connectToProtocolc
Cs�d}||jkr�z|j�|j�\}}Wnbtk
r�}zD|jd}|tkrRWY�,dS|tkrt|jrj|j	�
�WY�
dS�W5d}~XYqX|t|�7}|jtj
kr�|dd�}z|j	�||�Wqtk
r�t��YqXqdS)z=
        Called when my socket is ready for reading.
        rN�)�
maxThroughputr/�recvfromr%rE�args�_sockErrReadIgnore�_sockErrReadRefuser(r$�connectionRefused�lenr6�AF_INET6�datagramReceived�
BaseExceptionr�err)r*�read�data�addr�se�nor-r-r.�doRead�s(




zPort.doReadc
Cs�|jr�|d|jfkst�z|j�|�WStk
r�}zR|jd}|tkr\|�|�WY�0S|tkrpt	�
d��n|tkr�|j�
�n�W5d}~XYnX�n8|dks�t�t�|d�s�t�|d�s�|ddkr�t	�|dd��t�|d��s�|ddk�r|jtjk�rt	�|dd��t�|d��rJ|jtjk�rJt	�|dd��z|j�||�WStk
�r�}zX|jd}|tk�r�|�||�WY�2S|tk�r�t	�
d��n|tk�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/�sendrErTr�writerr�MessageLengthErrorrr$rWr�isIPAddress�
isIPv6Address�InvalidAddressErrorr6rY�AF_INET�sendto)r*�datagramr_r`rar-r-r.resd
��
���
�
���




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)re�join)r*�seqr_r-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(�RuntimeErrorrrgrhrrir/�connect)r*�hostr#r-r-r.rsOs�
zPort.connectcCs"|��|jr|j�d|j�dS)Nr)�stopReadingrIr,�	callLater�connectionLostr<r-r-r.�_loseConnection\szPort._loseConnectioncCs&|jrt��}|_nd}|��|SrN)rIr�Deferred�drx)r*�resultr-r-r.�
stopListeningas
zPort.stopListeningcCstjdtdd�|��dS)Nz-Please use stopListening() to disconnect portrQ)�
stacklevel)�warnings�warn�DeprecationWarningr|r<r-r-r.�loseConnectionis�zPort.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���rz)rrGrrRrr!rwr$�doStopr/�closerJ�hasattrrz�callback)r*�reasonr-r-r.rwqs


zPort.connectionLostcCs|�|j�}d||_dS)zP
        Initialize the C{logstr} attribute to be used by C{logPrefix}.
        z%s (UDP)N)rHr$�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.
        rqN)
rrhr&r/rYr6rgrjrrir<r-r-r.r)�s

�zPort._setAddressFamilycCs|jS)z0
        Return the prefix to log with.
        )r�r<r-r-r.r��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}
        �UDPNrQ)r�)r�)r/r2r6rjr�IPv4AddressrY�IPv6Address�r*r_r-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�szPort.setBroadcastAllowedcCst|j�tjtj��S)z�
        Checks if broadcast is currently allowed on this port.

        @return: Whether this port may broadcast.
        @rtype: L{bool}
        )�boolr/�
getsockoptr�r�r<r-r-r.�getBroadcastAllowed�szPort.getBroadcastAllowed)rr N)N)N)&�__name__�
__module__�__qualname__�__doc__r/rjr6�
SOCK_DGRAMr1rRrr�int�__annotations__r3r"�classmethodr7�strr=r>rAr?r@rbrerprsrxr|r�rwr'r)r�r�r�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_IF�	inet_ntoa�struct�pack)r*�ir-r-r.�getOutgoingInterface�sz#MulticastMixin.getOutgoingInterfacecCs|j�|��|j�S)zReturns Deferred of success.)r,�resolve�addCallback�
_setInterfacer�r-r-r.�setOutgoingInterface�sz#MulticastMixin.setOutgoingInterfacecCs"t�|�}|j�tjtj|�dS)NrB)r/�	inet_atonr�r�r�)r*r_r�r-r-r.r��s
zMulticastMixin._setInterfacecCs|j�tjtj�SrN)r/r�r��IP_MULTICAST_LOOPr<r-r-r.�getLoopbackMode�szMulticastMixin.getLoopbackModecCs(t�dt|��}|j�tjtj|�dS)N�b)r�r�r�r/r�r�r�)r*�moder-r-r.�setLoopbackMode�szMulticastMixin.setLoopbackModecCs|j�tjtj�SrN)r/r�r��IP_MULTICAST_TTLr<r-r-r.�getTTL�szMulticastMixin.getTTLcCs$t�d|�}|j�tjtj|�dS)N�B)r�r�r/r�r�r�)r*�ttlr-r-r.�setTTL�szMulticastMixin.setTTLrcCs|j�|��|j|d�S)z4Join a multicast group. Returns Deferred of success.rB�r,r�r��
_joinAddr1�r*r_r&r-r-r.�	joinGroup�szMulticastMixin.joinGroupcCs|j�|��|j||�SrN)r,r�r��
_joinAddr2)r*r_r&rnr-r-r.r��szMulticastMixin._joinAddr1c
Cs�t�|�}t�|�}|r tj}ntj}z|j�tj|||�Wn@tk
r�}z"t�t	j
||f|j���WY�Sd}~XYnXdSrN)r/r��IP_ADD_MEMBERSHIP�IP_DROP_MEMBERSHIPr�r�rEr�Failurer�MulticastJoinErrorrT)r*r&r_rn�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"�szMulticastPort.__init__c
Csxt�|�}|jrt|�tjtjd�ttd�rtz|�tjtjd�Wn0t	k
rr}z|j
tkr`n�W5d}~XYnX|S)NrB�SO_REUSEPORT)rrCr�r�r/r��SO_REUSEADDRr�r�rE�errnorrKr-r-r.rCs


z"MulticastPort.createInternetSocket)rr NF)r�r�r�r�r"rCr-r-r-r.r��s�
r�).r�r/r�r~�typingr�zope.interfacer�twisted.python.runtimerr�rrrr	r
rrr
rrrUrVrrrrr�twisted.internetrrrrrr�twisted.pythonrr�IListeningPort�
IUDPTransport�
ISystemHandler!rr��IMulticastTransportr�r-r-r-r.�<module>s@$ 
 �|8