
    m[hn                        d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZmZmZ 	 ddlZej$                  ej&                  ej(                  ej*                  dZ eed      xr ej0                  Zd	d
lmZmZmZmZ dZ dZ!dZ"dZ#de$de%fdZ& G d de      Z' G d de'      Z( G d de(      Z) G d de      Z* G d de*      Z+ G d de*      Z,y# e$ r dZdZY jw xY w)zGModule implementing low-level socket communication with MySQL servers.
    N)ABCabstractmethod)deque)AnyDequeListOptionalTupleUnion)TLSv1TLSv1.1TLSv1.2TLSv1.3HAS_TLSv1_3F   )InterfaceErrorNotSupportedErrorOperationalErrorProgrammingError2   i       errreturnc                 h    | j                   st        |       S d| j                    d| j                   S )z`Reformat the IOError error message.

    This function reformats the IOError error message.
    zErrno z: )errnostrstrerror)r   s    c/var/www/html/p-interativo-gestao-midia/env/lib/python3.12/site-packages/mysql/connector/network.py_strioerrorr    E   s.    
 993s8NF399+R~*NN    c                       e Zd ZdZe	 	 ddej
                  dededee	   dee	   ddfd	       Z
edej
                  dedefd
       Zy)NetworkBrokeraP  Broker class interface.

    The network object is a broker used as a delegate by a socket object. Whenever the
    socket wants to deliver or get packets to or from the MySQL server it needs to rely
    on its network broker (netbroker).

    The netbroker sends `payloads` and receives `packets`.

    A packet is a bytes sequence, it has a header and body (referred to as payload).
    The first `PACKET_HEADER_LENGTH` or `COMPRESSED_PACKET_HEADER_LENGTH`
    (as appropriate) bytes correspond to the `header`, the remaining ones represent the
    `payload`.

    The maximum payload length allowed to be sent per packet to the server is
    `MAX_PAYLOAD_LENGTH`. When  `send` is called with a payload whose length is greater
    than `MAX_PAYLOAD_LENGTH` the netbroker breaks it down into packets, so the caller
    of `send` can provide payloads of arbitrary length.

    Finally, data received by the netbroker comes directly from the server, expect to
    get a packet for each call to `recv`. The received packet contains a header and
    payload, the latter respecting `MAX_PAYLOAD_LENGTH`.
    Nsockaddresspayloadpacket_numbercompressed_packet_numberr   c                      y)a  Send `payload` to the MySQL server.

        If provided a payload whose length is greater than `MAX_PAYLOAD_LENGTH`, it is
        broken down into packets.

        Args:
            sock: Object holding the socket connection.
            address: Socket's location.
            payload: Packet's body to send.
            packet_number: Sequence id (packet ID) to attach to the header when sending
                           plain packets.
            compressed_packet_number: Same as `packet_number` but used when sending
                                      compressed packets.

        Raises:
            :class:`OperationalError`: If something goes wrong while sending packets to
                                       the MySQL server.
        N )selfr$   r%   r&   r'   r(   s         r   sendzNetworkBroker.sende       r!   c                      y)a)  Get the next available packet from the MySQL server.

        Args:
            sock: Object holding the socket connection.
            address: Socket's location.

        Returns:
            packet: A packet from the MySQL server.

        Raises:
            :class:`OperationalError`: If something goes wrong while receiving packets
                                       from the MySQL server.
            :class:`InterfaceError`: If something goes wrong while receiving packets
                                     from the MySQL server.
        Nr*   )r+   r$   r%   s      r   recvzNetworkBroker.recv   r-   r!   NN)__name__
__module____qualname____doc__r   socketr   bytesr	   intr,   	bytearrayr/   r*   r!   r   r#   r#   M   s    .  (,26mm  	
  } #+3- 
 6     r!   r#   c                       e Zd ZdZddZddZdej                  dededdfd	Z	ddej                  d
e
defdZ	 	 ddej                  dededee
   dee
   ddfdZdej                  dedefdZy)NetworkBrokerPlain,Broker class for MySQL socket communication.r   Nc                     d| _         y N_pktnrr+   s    r   __init__zNetworkBrokerPlain.__init__   s	    r!   c                 2    | j                   dz   dz  | _         yzIncrement packet id.r      Nr?   rA   s    r   _set_next_pktnrz"NetworkBrokerPlain._set_next_pktnr   s    {{Q#-r!   r$   r%   pktc                     	 |j                  |       y# t        $ r}t        d|t        |      f      |d}~wt        $ r}t        d      |d}~ww xY w)z!Write packet to the comm channel.  r   valuesNi  r   )sendallIOErrorr   r    AttributeError)r+   r$   r%   rG   r   s        r   	_send_pktzNetworkBrokerPlain._send_pkt   sZ    	8LL 	"G[-=#>  	8".C7	8s    	A6AAAsizec                     t        |      }t        |      }|r5|j                  ||      }|dk(  r|dkD  rt        d      ||d }||z  }|r5|S )z(Read `size` bytes from the comm channel.r   i  rL   N)r8   
memoryview	recv_intor   )r+   r$   rQ   rG   pkt_viewreads         r   _recv_chunkzNetworkBrokerPlain._recv_chunk   s_    oc?>>(D1DqyTAX$400HDLD  
r!   r&   r'   r(   c           
         || j                          n|| _        t        |      t        k\  rd}t	        t        |      t        z        D ]\  }| j                  ||dt        j                  d| j                        z   |||t        z    z          | j                          |t        z  }^ ||d }| j                  ||t        j                  dt        |            dd t        j                  d| j                        z   |z          y)zSend payload to the MySQL server.

        If provided a payload whose length is greater than `MAX_PAYLOAD_LENGTH`, it is
        broken down into packets.
        Nr      <B<I   )rF   r@   lenMAX_PAYLOAD_LENGTHrangerP   structpack)r+   r$   r%   r&   r'   r(   offset_s           r   r,   zNetworkBrokerPlain.send   s        "'DK w<--F3w<+==>#kk$45fv0B'BCD $$&,, ? fg&GKKc'l+Aa0kk$,-	
r!   c                    	 | j                  |t              }t        j                  d|dd dz         d   |d   c}| _        || j                  ||      z   S # t
        $ r}t        d|t        |      f      |d}~ww xY w)	z+Receive `one` packet from the MySQL server.rQ   r[   r   r\       rI   rJ   N)rW   PACKET_HEADER_LENGTHr`   unpackr@   rN   r   r    )r+   r$   r%   headerpayload_lenr   s         r   r/   zNetworkBrokerPlain.recv   s    	%%d1E%FF dF1QK'$9:1=q	 %K D,,T,DDD 	"G[-=#>	s   AA 	B "A;;B r   N)r   r0   )r1   r2   r3   r4   rB   rF   r5   r   r6   rP   r7   r8   rW   r	   r,   r/   r*   r!   r   r:   r:      s    6.	8fmm 	8c 	8 	8$ 	8
 
S 
 
" (,26*
mm*
 *
 	*

  }*
 #+3-*
 
*
X   r!   r:   c                   &    e Zd ZdZd fdZedededee   fd       Z	ddZ
d	ej                  d
ededdf fdZ	 	 dd	ej                  d
ededee   dee   ddf fdZd	ej                  dededdf fdZd	ej                  d
edef fdZ xZS )NetworkBrokerCompressedr;   r   Nc                 N    t         |           d| _        t               | _        y r=   )superrB   _compressed_pktnrr   _queue_read)r+   	__class__s    r   rB   z NetworkBrokerCompressed.__init__   s     !#-2Wr!   r&   pktnrc                    g }t        |       t        k\  rmd}t        t        |       t        z        D ]H  }|j                  dt	        j
                  d|      z   | ||t        z    z          |dz   dz  }|t        z  }J | |d } |j                  t	        j
                  dt        |             dd t	        j
                  d|      z   | z          |S )	z2Prepare a payload for sending to the MySQL server.r   rY   rZ   r   rE   Nr[   r\   )r]   r^   r_   appendr`   ra   )r&   rs   pktsrb   rc   s        r   _prepare_packetsz(NetworkBrokerCompressed._prepare_packets   s      w<--F3w<+==>#kk$./fv0B'BCD
 c),, ? fg&GKKc'l+Aa06;;tU3KKgU	
 r!   c                 2    | j                   dz   dz  | _         yrD   )rp   rA   s    r   _set_next_compressed_pktnrz2NetworkBrokerCompressed._set_next_compressed_pktnr  s    "&"8"81"<!Cr!   r$   r%   rG   c                 "   t        j                  |      }t        j                  dt	        |            dd t        j                  d| j
                        z   t        j                  dt	        |            dd z   |z   }t        |   |||      S )z1Compress packet and write it to the comm channel.r[   r   r\   rZ   )zlibcompressr`   ra   r]   rp   ro   rP   )r+   r$   r%   rG   compressed_pktrr   s        r   rP   z!NetworkBrokerCompressed._send_pkt  s    s+KKc.121Q7kk$ 6 678kk$C)!A./  	 w w44r!   r'   r(   c           
         || j                          n|| _        || j                          n|| _        t	        d      j                  | j                  || j                              }t        |      t        t        z
  k\  rod}t        t        |      t        z        D ]8  }| j                  |||||t        z           | j                          |t        z  }: | j                  ||||d        yt        |      t        kD  r| j                  |||       yt        	| -  ||t        j                  dt        |            dd t        j                  d| j                        z   t        j                  dd      dd z   |z          y)zSend `payload` as compressed packets to the MySQL server.

        If provided a payload whose length is greater than `MAX_PAYLOAD_LENGTH`, it is
        broken down into packets.
        Nr!   r   r[   r\   rZ   )rF   r@   ry   rp   r8   joinrw   r]   r^   rg   r_   rP   MIN_COMPRESS_LENGTHro   r`   ra   )
r+   r$   r%   r&   r'   r(   payload_preprb   rc   rr   s
            r   r,   zNetworkBrokerCompressed.send&  sr       "'DK#+++-%=D" ~**4+@+@$+++VWw<-0DDD F 3|,0BBC'<BT9T#U //1,, D NN4,vw*?@ 7|11tWl; !KKc,&781=kk$(>(>?@kk$*1Q/0 ##r!   compressed_plluncompressed_pllc                    t         	|   ||      }|dk(  r|nt        t        j                  |            }d}|t        |      k  rt        j                  d|||t        z   dz
   dz         d   }t        |z   t        |      |z
  kD  rt         	|   |t              }t        j                  d|dd dz         d   |d   t        j                  d|dd dz         d   	 c}| _
        }t         	|   ||      }||dk(  r|nt        j                  |      z  }| j                  j                  |||t        z   |z           |t        |z   z  }|t        |      k  ry	y	)
z&Handle reading of a compressed packet.re   r   r[   r   rf   r\   r   r   N)ro   rW   r8   r{   
decompressr]   r`   rh   rg   COMPRESSED_PACKET_HEADER_LENGTHrp   rq   ru   )
r+   r$   r   r   r}   rG   rb   pllri   rr   s
            r   _recv_compressed_pktz,NetworkBrokerCompressed._recv_compressed_pktc  s    ,T,G  1$ 4??>:; 	 s3x--c&6,@#@1#DEOC $c)CHv,== ,T8W,X MM$qg(=>qA1IMM$qg(=>qA	"*$ "'!4T!4!O '1, #8 ##C:N1NQT1T$UV*S00F? s3xr!   c                    | j                   ss	 t        | 	  |t              }t	        j
                  d|dd dz         d   |d   t	        j
                  d|dd dz         d   	 c}| _        }| j                  |||       | j                   sy
| j                   j                         }|d   | _        |S # t        $ r}t        d|t        |      f	      |d
}~ww xY w)z{Receive `one` or `several` packets from the MySQL server, enqueue them, and
        return the packet at the head.
        re   r[   r   r\   rf   r   r   rI   rJ   N)rq   ro   rW   r   r`   rh   rp   r   rN   r   r    popleftr@   )	r+   r$   r%   ri   r   r   r   rG   rr   s	           r   r/   zNetworkBrokerCompressed.recv  s     ,T8W,X MM$qg(=>qA1IMM$qg(=>qA	"*$ ))$@PQ &&(!f
  &S1A'Bs   A2B4 4	C=CCrk   r0   )r1   r2   r3   r4   rB   staticmethodr6   r7   r   rw   ry   r5   r   rP   r	   r,   r   r8   r/   __classcell__rr   s   @r   rm   rm      s   65
 %  U  2D	5fmm 	5c 	5 	5$ 	5  (,26;mm; ; 	;
  }; #+3-; 
;z.1MM.136.1JM.1	.1`    r!   rm   c                   8   e Zd ZdZddZddZddZddZddZd	e	e
   ddfd
ZdededdfdZ	 	 	 	 	 	 	 dde	e   de	e   de	e   de	e   de	e   de	ee      de	ee      defdZ	 	 ddede	e
   de	e
   ddfdZdefdZedd       Zeedefd              Zy) MySQLSocketzMySQL socket communication interface.

    Examples:
        Subclasses: network.MySQLTCPSocket and network.MySQLUnixSocket.
    r   Nc                 L    d| _         d| _        d| _        t               | _        y)zsNetwork layer where transactions are made with plain (uncompressed) packets
        is enabled by default.
        N)r$   _connection_timeoutserver_hostr:   
_netbrokerrA   s    r   rB   zMySQLSocket.__init__  s%    
 .2	26 *.);)=r!   c                 "    t               | _        y)zIEnable network layer where transactions are made with compressed packets.N)rm   r   rA   s    r   switch_to_compressed_modez%MySQLSocket.switch_to_compressed_mode  s    13r!   c                     	 | j                   j                  t        j                         | j                   j	                          y# t
        t        f$ r Y yw xY w)z'Shut down the socket before closing it.N)r$   shutdownr5   	SHUT_RDWRcloserO   OSErrorrA   s    r   r   zMySQLSocket.shutdown  sD    	IIv//0IIOO( 		s   AA AAc                 d    	 | j                   j                          y# t        t        f$ r Y yw xY w)zClose the socket.N)r$   r   rO   r   rA   s    r   close_connectionzMySQLSocket.close_connection  s,    	IIOO( 		s    //c                 $    | j                          y N)r   rA   s    r   __del__zMySQLSocket.__del__  s    r!   timeoutc                 b    || _         | j                  r| j                  j                  |       yy)zSet the connection timeout.N)r   r$   
settimeout)r+   r   s     r   set_connection_timeoutz"MySQLSocket.set_connection_timeout  s(    #* 99II  ) r!   ssl_contexthostc                     | j                   J | j                   j                  dk(  rt        d      t        t	        d      	 |j                  | j                   |      | _         y# t        $ r}t	        d      |d}~wt        j                  t        f$ r(}t        d| j                  t        |      f      |d}~wt        j                  $ r}t        t        |            |d}~wt        $ r}t        t        |            |d}~ww xY w)a  Upgrade an existing connection to TLS.

        Args:
            ssl_context (ssl.SSLContext): The SSL Context to be used.
            host (str): Server host name.

        Returns:
            None.

        Raises:
            ProgrammingError: If the transport does not expose the socket instance.
            NotSupportedError: If Python installation has no SSL support.
        Nr   z,SSL is not supported when using Unix sockets&Python installation has no SSL support)server_hostnamerI   rJ   )r$   familyr   sslr   wrap_socket	NameErrorSSLErrorrN   r   r%   r    CertificateErrorr   NotImplementedError)r+   r   r   r   s       r   switch_to_sslzMySQLSocket.switch_to_ssl  s     yy$$$99q "#QRR;#$LMM	4#//		4/PDI 	W#$LMSVVg& 	 DLL+c2B#C ## 	4 S*3" 	4 S*3	4s<   "A( (	D1A==D#B<<DC''D3DDssl_cassl_certssl_keyssl_verify_certssl_verify_identitytls_versionstls_cipher_suitesc                    d}| j                   st        d      t        t        d      |g }|g }	 |r|j	                  d       |d   }t
        |   }	t        j                  |	      }
|dk(  rd	|vr#|
xj                  t        j                  z  c_        d
|vr#|
xj                  t        j                  z  c_        d|vr8|
xj                  t        j                  z  c_        nt        j                         }
||
_        |rt        j                  |
_        n-|rt        j                  |
_        nt        j                   |
_        |
j#                          |r	 |
j%                  |       |r	 |
j+                  ||       |r%|d	k(  r |
j-                  dj/                  |             |
S # t&        t        j(                  f$ r}t        d|       |d}~ww xY w# t&        t        j(                  f$ r}t        d|       |d}~ww xY w# t0        $ r}t        d      |d}~wt&        t2        t        j4                  t        j(                  f$ r}t        t7        |            |d}~ww xY w)a  Build a SSLContext.

        Args:
            ssl_ca: Certificate authority, opptional.
            ssl_cert: SSL certificate, optional.
            ssl_key: Private key, optional.
            ssl_verify_cert: Verify the SSL certificate if `True`.
            ssl_verify_identity: Verify host identity if `True`.
            tls_versions: TLS protocol versions, optional.
            tls_cipher_suites: Set of steps that helps to establish a secure connection.

        Returns:
            ssl_context (ssl.SSLContext): An SSL Context ready be used.

        Raises:
            NotSupportedError: Python installation has no SSL support.
            InterfaceError: Socket undefined or invalid ssl data.
        Ni   rL   r   T)reverser   r   r   r   r   zInvalid CA Certificate: zInvalid Certificate/Key: :)r$   r   r   r   sortTLS_VERSIONS
SSLContextoptionsOP_NO_TLSv1_2OP_NO_TLSv1_1OP_NO_TLSv1create_default_contextcheck_hostnameCERT_REQUIREDverify_modeCERT_OPTIONAL	CERT_NONEload_default_certsload_verify_locationsrN   r   load_cert_chainset_ciphersr   r   r   r   r   )r+   r   r   r   r   r   r   r   tls_versionssl_protocolcontextr   s               r   build_ssl_contextzMySQLSocket.build_ssl_context  s>   8 &*yy t,,;#$LMML$ "6	4!!$!/*1o+K8..6)+ 43+<+<< 43+<+<<l23??: 446%8G"&)&7&7#$&)&7&7#&)mm#&&(T11&9 U++Hg> ![I%=##CHH->$?@N  . T(+CC5)IJPSST
  . U(+DSE)JKQTTU  	W#$LMSVV  LL	
 	4 !S*3	4sl   D$H	 F) +H	 .G  (H	 )GGGH	 H2HHH	 		I(H0I(I##I(r&   r'   r(   c                 j    | j                   j                  | j                  | j                  |||      S )at  Send `payload` to the MySQL server.

        NOTE: if `payload` is an instance of `bytearray`, then `payload` might be
        changed by this method - `bytearray` is similar to passing a variable by
        reference.

        If you're sure you won't read `payload` after invoking `send()`,
        then you can use `bytearray.` Otherwise, you must use `bytes`.
        )r'   r(   )r   r,   r$   r%   )r+   r&   r'   r(   s       r   r,   zMySQLSocket.sendh  s7     ##IILL'%= $ 
 	
r!   c                 b    | j                   j                  | j                  | j                        S )z.Get packet from the MySQL server comm channel.)r   r/   r$   r%   rA   s    r   r/   zMySQLSocket.recv  s!    ##DIIt||<<r!   c                      y)zOpen the socket.Nr*   rA   s    r   open_connectionzMySQLSocket.open_connection  r-   r!   c                      y)zGet the location of the socket.Nr*   rA   s    r   r%   zMySQLSocket.address  r-   r!   rk   )NNNFFNNr0   )r1   r2   r3   r4   rB   r   r   r   r   r	   r7   r   r   r   r   boolr   r   r6   r,   r8   r/   r   r   propertyr%   r*   r!   r   r   r     sj   >4*hsm * *"4 "4C "4D "4L !%"&!%*/.3,015`4`4 3-`4 #	`4
 "$`4 &d^`4 tCy)`4 $DI.`4 
`4J (,26	

  }
 #+3-	

 

.=i =   . .  .r!   r   c                   `     e Zd ZdZddeddf fdZedefd       ZddZde	d	e	ddfd
Z
 xZS )MySQLUnixSocketzpMySQL socket class using UNIX sockets.

    Opens a connection through the UNIX socket of the MySQL Server.
    unix_socketr   Nc                 >    t         |           || _        || _        y r   )ro   rB   r   _address)r+   r   rr   s     r   rB   zMySQLUnixSocket.__init__  s     +(r!   c                     | j                   S r   r   rA   s    r   r%   zMySQLUnixSocket.address      }}r!   c                    	 t        j                   t         j                  t         j                        | _        | j                  j	                  | j
                         | j                  j                  | j                         y # t        $ r(}t        d| j                  t        |      f      |d }~wt        $ r}t        t        |            |d }~ww xY w)Ni  rJ   )r5   AF_UNIXSOCK_STREAMr$   r   r   connectr   rN   r   r%   r    	Exceptionr   )r+   r   s     r   r   zMySQLUnixSocket.open_connection  s    	4""DI
 II  !9!9:IId../ 	 DLL+c2B#C  	4 S*3	4s$   BB 	C#B00C<CCargskwargsc                 8    t        j                  dt               y)zSwitch the socket to use SSL.z2SSL is disabled when using unix socket connectionsN)warningswarnWarning)r+   r   r   s      r   r   zMySQLUnixSocket.switch_to_ssl  s     	@	
r!   )z/tmp/mysql.sockrk   )r1   r2   r3   r4   r   rB   r   r%   r   r   r   r   r   s   @r   r   r     sX    
)C ) )
   4 

$'
	
r!   r   c            	       Z     e Zd ZdZ	 	 	 d
dedededdf fdZedefd       Z	dd	Z
 xZS )MySQLTCPSocketzYMySQL socket class using TCP/IP.

    Opens a TCP/IP connection to the MySQL Server.
    r   port
force_ipv6r   Nc                 r    t         |           || _        || _        || _        d| _        | d| | _        y )Nr   r   )ro   rB   r   server_portr   _familyr   )r+   r   r   r   rr   s       r   rB   zMySQLTCPSocket.__init__  s@     	 $ $ * $vQtf-r!   c                     | j                   S r   r   rA   s    r   r%   zMySQLTCPSocket.address  r   r!   c           	         d}	 t        j                  | j                  | j                  dt         j                  t         j
                        }|D ]B  }| j                  r|d   t         j                  k(  r|} n|d   t         j                  k(  s@|} n | j                  r|d   t        d| j                         |d   |d   }|\  | _        }}}}	 t        j                   | j                  ||      | _        | j                  j                  | j                         | j                  j!                  |       y# t        $ r3}t        d| j                  | j                  t        |      f      |d}~ww xY w# t        $ r3}t        d| j                  | j                  t        |      f      |d}~wt"        $ r}t%        t'        |            |d}~ww xY w)z/Open the TCP/IP connection to the MySQL server.)NNNNNr   NzNo IPv6 address found for i  rJ   )r5   getaddrinfor   r   r   SOL_TCPr   AF_INET6AF_INETr   rN   r    r   r$   r   r   r   r   r   r   )	r+   addrinfo	addrinfosinfor   socktypeprotorc   sockaddrs	            r   r   zMySQLTCPSocket.open_connection  s    + 		**    ""I "??tAw&//'A#H7fnn,#H " 8A;#6$'A$BRBRAS%TUU{"$Q< 8@4x8	6dllHeDDIII  !9!9:IIh'  	 (($*:*:K<LM 	  	 $$$$$   	6"3s8,#5	6sC   B
D< 8D< A&E; <	E8.E33E8;	G.F22G>GG)z	127.0.0.1i  Frk   )r1   r2   r3   r4   r   r7   r   rB   r   r%   r   r   r   s   @r   r   r     s^       	.. . 	.
 
.   96r!   r   )-r4   r5   r`   r   r{   abcr   r   collectionsr   typingr   r   r   r	   r
   r   r   PROTOCOL_TLSv1PROTOCOL_TLSv1_1PROTOCOL_TLSv1_2PROTOCOL_TLSr   hasattrr   TLS_V1_3_SUPPORTEDImportErrorerrorsr   r   r   r   r   r^   rg   r   rN   r   r    r#   r:   rm   r   r   r   r*   r!   r   <module>r     s   :     #  ; ; ##''''##	L !m4H     "# OW O ODC DN^ ^B}0 }@U.# U.p&
k &
RP6[ P6G  
Cs   A
C 	C'&C'