
    il                     <   U d Z ddl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Zej                  ej                  ej                  dZ eed      rej"                  nej$                  ed<    eed      xr ej&                  Zdd
lmZmZmZmZmZmZ ddlmZmZm Z  ddl!m"Z" dZ#e$e%d<   dZ&e$e%d<   dZ'e$e%d<   dZ(e$e%d<   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/      Z0 G d# d$e/      Z1y# e$ r d	ZY w xY w)%zGModule implementing low-level socket communication with MySQL servers.
    N)ABCabstractmethod)deque)TLSv1TLSv1.1TLSv1.2PROTOCOL_TLSTLSv1.3HAS_TLSv1_3F)AnyDequeListOptionalTupleUnion   )InterfaceErrorNotSupportedErrorOperationalError)StrOrBytesPath2   MIN_COMPRESS_LENGTHi MAX_PAYLOAD_LENGTH   PACKET_HEADER_LENGTH   COMPRESSED_PACKET_HEADER_LENGTHerrreturnc                 f    | j                   st        |       S | j                    d| j                   S )z`Reformat the IOError error message.

    This function reformats the IOError error message.
     )errnostrstrerror)r   s    i/var/www/catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/mysql/connector/network.py_strioerrorr&   I   s,    
 993s8GSYYKq*GG    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.sendi       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.
        Nr0   )r1   r*   r+   s      r%   recvzNetworkBroker.recv   r3   r'   NN)__name__
__module____qualname____doc__r   socketr#   bytesr   intr2   	bytearrayr5   r0   r'   r%   r)   r)   Q   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r1   s    r%   __init__zNetworkBrokerPlain.__init__   s	    r'   c                 2    | j                   dz   dz  | _         yzIncrement packet id.r      NrE   rG   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)r1   r*   r+   rM   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  rR   N)r>   
memoryview	recv_intor   )r1   r*   rW   rM   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   )rL   rF   lenr   rangerV   structpack)r1   r*   r+   r,   r-   r.   offset_s           r%   r2   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.rW   ra   r   rb       rO   rP   N)r]   r   re   unpackrF   rT   r   r&   )r1   r*   r+   headerpayload_lenr   s         r%   r5   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   r6   )r7   r8   r9   r:   rH   rL   r;   r#   r<   rV   r=   r>   r]   r   r2   r5   r0   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 )NetworkBrokerCompressedrA   r   Nc                 N    t         |           d| _        t               | _        y rC   )superrH   _compressed_pktnrr   _queue_read)r1   	__class__s    r%   rH   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   r_   r`   r   rK   Nra   rb   )rc   r   rd   appendre   rf   )r,   rw   pktsrg   rh   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rJ   )rt   rG   s    r%   _set_next_compressed_pktnrz2NetworkBrokerCompressed._set_next_compressed_pktnr  s    "&"8"81"<!Cr'   r*   r+   rM   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.ra   r   rb   r`   )zlibcompressre   rf   rc   rt   rs   rV   )r1   r*   r+   rM   compressed_pktrv   s        r%   rV   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   ra   rb   r`   )rL   rF   r}   rt   r>   joinr{   rc   r   r   rd   rV   r   rs   re   rf   )
r1   r*   r+   r,   r-   r.   payload_preprg   rh   rv   s
            r%   r2   zNetworkBrokerCompressed.send*  sv       "'DK#+++-%=D" ~**4+@+@$+++VWw<-0DDD F 3|,0BBC -'<BT9T#U //1,,- 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.rj   r   ra   r   rk   rb   r   r   N)rs   r]   r>   r   
decompressrc   re   rl   r   r   rt   ru   ry   )
r1   r*   r   r   r   rM   rg   pllrm   rv   s
            r%   _recv_compressed_pktz,NetworkBrokerCompressed._recv_compressed_pktg  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.
        rj   ra   r   rb   rk   r   r   rO   rP   N)ru   rs   r]   r   re   rl   rt   r   rT   r   r&   popleftrF   )	r1   r*   r+   rm   r   r   r   rM   rv   s	           r%   r5   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ro   r6   )r7   r8   r9   r:   rH   staticmethodr<   r=   r   r{   r}   r;   r#   rV   r   r2   r   r>   r5   __classcell__rv   s   @r%   rq   rq      s   65
 %  U  2D	5fmm 	5c 	5 	5$ 	5  (,26;mm; ; 	;
  }; #+3-; 
;z.1MM.136.1JM.1	.1`    r'   rq   c                       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dededededede	e   de	ee      dd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@   
_netbrokerrG   s    r%   rH   zMySQLSocket.__init__  s%    
 .2	26 *.);)=r'   c                 "    t               | _        y)zIEnable network layer where transactions are made with compressed packets.N)rq   r   rG   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*   shutdownr;   	SHUT_RDWRcloserU   OSErrorrG   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   rU   r   rG   s    r%   close_connectionzMySQLSocket.close_connection  s,    	IIOO( 		s    //c                 $    | j                          y N)r   rG   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)r1   r   s     r%   set_connection_timeoutz"MySQLSocket.set_connection_timeout  s(    #* 99II  ) r'   cacertkeyverify_certverify_identitycipher_suitestls_versionsc                    | j                   st        d      	 |rt        j                  }n#|rt        j                  }nt        j
                  }||st        j                         }	|sd|	_        n|j                  d       |d   }
t        s|
dk(  rt        |      d	kD  r|d	   }
t        |
   }t        j                  |      }	|
dk(  rud
|vr#|	xj                  t        j                  z  c_        d|vr#|	xj                  t        j                  z  c_        d|vr#|	xj                  t        j                   z  c_        d|	_        ||	_        |	j%                          |r	 |	j'                  |       |r	 |	j/                  ||       |r|	j1                  |       t3        | d      r-|	j5                  | j                   | j6                        | _         n |	j5                  | j                         | _         |rd|	_        | j6                  r| j6                  gng }t8        j:                  dk(  rM| j6                  dk(  r>ddg}t=        j>                  | j6                        }|jA                  |d   g|d	   z          d}g }|D ]4  }	 t        jB                  | j                   jE                         |       d} n |s7| j                   j-                          t        ddjM                  |             yy# t(        t        j*                  f$ r.}| j                   j-                          t        d|       |d}~ww xY w# t(        t        j*                  f$ r.}| j                   j-                          t        d|       |d}~ww xY w# t        jF                  $ r%}|jI                  tK        |             Y d}~9d}~ww xY w# tN        $ r}tQ        d      |d}~wt        j*                  t(        f$ r(}t        d| jR                  tU        |      f      |d}~wt        jF                  $ r}t        tK        |            |d}~wtV        $ r}t        tK        |            |d}~ww xY w)zSwitch the socket to use SSLi   rR   NFT)reverser   r
   r   r   r   r   zInvalid CA Certificate: zInvalid Certificate/Key: r   )server_hostnament	localhost	127.0.0.1z"Unable to verify server identity: z, z&Python installation has no SSL supportrO   rP   ),r*   r   sslCERT_REQUIREDCERT_OPTIONAL	CERT_NONEcreate_default_contextcheck_hostnamesortTLS_V1_3_SUPPORTEDrc   TLS_VERSIONS
SSLContextoptionsOP_NO_TLSv1_2OP_NO_TLSv1_1OP_NO_TLSv1verify_modeload_default_certsload_verify_locationsrT   SSLErrorr   load_cert_chainset_ciphershasattrwrap_socketr   osnamer;   gethostbyaddrextendmatch_hostnamegetpeercertCertificateErrorry   r#   r   	NameErrorr   r+   r&   NotImplementedError)r1   r   r   r   r   r   r   r   	cert_reqscontexttls_versionssl_protocolr   	hostnamesaliasesmatch_founderrshostnames                     r%   switch_to_sslzMySQLSocket.switch_to_ssl  s    yy t,,_	4--	 --	MM	#<446&-2G*!!$!/*1o*#y0L)A-".q/K+K8..6)+ 43+<+<< 43+<+<<l23??:%*G""+G&&(T11"5 U++D#6 ##M2t]+#//IIt/?/? 0 	 $//		:	)-&=A=M=M(8(8'9SU	77d?t'7'7;'F!,k :I$2243C3CDG$$gaj\GAJ%>?# ) H
 **499+@+@+BHM
 '+ #IIOO%(<TYYt_<MN  #- '  . TIIOO%(+CC5)IJPSST  . UIIOO%(+DSE)JKQTTU< // .CH--.  	W#$LMSVVg& 	 DLL+c2B#C ## 	4 S*3" 	4 S*3	4s   EN=  K. 1N= 4L8 C9N=  .N.>N= .L5)L00L55N= 8M?)M::M??N= N:N5/N= 5N::N= =	Q"OQ".#PQ"'P<<Q"QQ"r,   r-   r.   c                 j    | j                   j                  | j                  | j                  |||      S )z#Send `payload` to the MySQL server.)r-   r.   )r   r2   r*   r+   )r1   r,   r-   r.   s       r%   r2   zMySQLSocket.sendU  s7     ##IILL'%= $ 
 	
r'   c                 b    | j                   j                  | j                  | j                        S )z.Get packet from the MySQL server comm channel.)r   r5   r*   r+   rG   s    r%   r5   zMySQLSocket.recvd  s!    ##DIIt||<<r'   c                      y)zOpen the socket.Nr0   rG   s    r%   open_connectionzMySQLSocket.open_connectionh  r3   r'   c                      y)zGet the location of the socket.Nr0   rG   s    r%   r+   zMySQLSocket.addressl  r3   r'   ro   )FFNNr6   )r7   r8   r9   r:   rH   r   r   r   r   r   r=   r   r   boolr#   r   r   r<   r2   r>   r5   r   r   propertyr+   r0   r'   r%   r   r     s-   >4*hsm * * " %'+,0m4m4 m4 	m4
 m4 m4  }m4 tCy)m4 
m4d (,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   )rs   rH   r   _address)r1   r   rv   s     r%   rH   zMySQLUnixSocket.__init__x  s     +(r'   c                     | j                   S r   r   rG   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  rP   )r;   AF_UNIXSOCK_STREAMr*   r   r   connectr   rT   r   r+   r&   	Exceptionr#   )r1   r   s     r%   r   zMySQLUnixSocket.open_connection  s    	4 2 2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)r1   r   r   s      r%   r   zMySQLUnixSocket.switch_to_ssl  s     	@	
r'   )z/tmp/mysql.sockro   )r7   r8   r9   r:   r#   rH   r   r+   r   r   r   r   r   s   @r%   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.
    hostport
force_ipv6r   Nc                 r    t         |           || _        || _        || _        d| _        | d| | _        y )Nr   :)rs   rH   r   server_portr   _familyr   )r1   r   r   r   rv   s       r%   rH   zMySQLTCPSocket.__init__  s@     	 $ $ * $vQtf-r'   c                     | j                   S r   r   rG   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        $ r(}t        d| 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  rP   )r;   getaddrinfor   r   r   SOL_TCPr   AF_INET6AF_INETr   rT   r+   r&   r   r*   r   r   r   r   r   r#   )	r1   addrinfo	addrinfosinfor   socktypeprotorh   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'  	 DLL+c2B#C	  	 $$$$$   	6"3s8,#5	6sC   B
D< 8D< A&E0 <	E-#E((E-0	G9.F''G3GG)r   i  Fro   )r7   r8   r9   r:   r#   r=   r   rH   r   r+   r   r   r   s   @r%   r   r     s^       	.. . 	.
 
.   86r'   r   )2r:   r   r;   re   r   r   abcr   r   collectionsr   r   PROTOCOL_TLSv1PROTOCOL_TLSv1_1PROTOCOL_TLSv1_2r   r   r	   PROTOCOL_SSLv23r   r   ImportErrortypingr   r   r   r   r   r   errorsr   r   r   typesr   r   r=   __annotations__r   r   r   rT   r#   r&   r)   r@   rq   r   r   r   r0   r'   r%   <module>r     sR  > 
     #  ##''''L 3' 	   
 !m4H
 < ; G G ! S # C # c '(  (HW H HDC DN^ ^B}0 }@v.# v.r$
k $
NO6[ O6  s   A#D DD