o
    hl                     @   s  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
 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oGejZW n eyT   d	ZY nw 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dZ+G dd deZ,G dd de,Z-G dd de-Z.G d d! d!eZ/G d"d# d#e/Z0G d$d% d%e/Z1dS )&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                 C   s    | 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    r#   R/var/www/html/scripts/venv/lib/python3.10/site-packages/mysql/connector/network.py_strioerrorI   s    r%   c                   @   sb   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	d
Z
edejdedefddZdS )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                 C      dS )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.
        Nr#   )selfr'   r(   r)   r*   r+   r#   r#   r$   sendi       zNetworkBroker.sendc                 C   r,   )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(   r#   r#   r$   recv   r/   zNetworkBroker.recvNN)__name__
__module____qualname____doc__r   socketr!   bytesr   intr.   	bytearrayr0   r#   r#   r#   r$   r&   Q   s(    r&   c                   @   s   e Zd ZdZdddZdddZdejd	ed
eddfddZ	ddejde
defddZ		ddejd	ededee
 dee
 ddfddZdejd	edefddZdS )NetworkBrokerPlain,Broker class for MySQL socket communication.r   Nc                 C   s
   d| _ d S N_pktnrr-   r#   r#   r$   __init__   s   
zNetworkBrokerPlain.__init__c                 C      | j d d | _ dS zIncrement packet id.r      Nr>   r@   r#   r#   r$   _set_next_pktnr      z"NetworkBrokerPlain._set_next_pktnrr'   r(   pktc              
   C   sb   z| | W dS  ty } ztd|t|fd|d}~w ty0 } ztdd|d}~ww )z!Write packet to the comm channel.  r    valuesNi  r    )sendallIOErrorr   r%   AttributeError)r-   r'   r(   rG   r   r#   r#   r$   	_send_pkt   s   zNetworkBrokerPlain._send_pktr   sizec                 C   sV   t |}t|}|r)|||}|dkr|dkrtdd||d }||8 }|s
|S )z(Read `size` bytes from the comm channel.r   i  rK   N)r9   
memoryview	recv_intor   )r-   r'   rP   rG   pkt_viewreadr#   r#   r$   _recv_chunk   s   
zNetworkBrokerPlain._recv_chunkr)   r*   r+   c              
   C   s   |du r	|    n|| _t|tkrCd}tt|t D ] }| ||dtd| j |||t    |    |t7 }q||d }| ||tdt|dd td| j |  dS )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   )rE   r?   lenr   rangerO   structpack)r-   r'   r(   r)   r*   r+   offset_r#   r#   r$   r.      s6   

zNetworkBrokerPlain.sendc              
   C   sz   z%| j |td}td|dd d d |d }| _|| j ||d W S  ty< } ztd|t|fd|d}~ww )	z+Receive `one` packet from the MySQL server.rP   rX   r   rY       rH   rI   N)rU   r   r\   unpackr?   rM   r   r%   )r-   r'   r(   headerpayload_lenr   r#   r#   r$   r0      s   zNetworkBrokerPlain.recvr   N)r   r1   )r2   r3   r4   r5   rA   rE   r6   r!   r7   rO   r8   r9   rU   r   r.   r0   r#   r#   r#   r$   r:      s,    


,r:   c                       s   e Zd ZdZd fddZedededee fdd	Z	dd
dZ
dejdededdf fddZ		ddejdededee dee ddf fddZdejdededdf fddZdejdedef fddZ  ZS )NetworkBrokerCompressedr;   r   Nc                    s   t    d| _t | _d S r<   )superrA   _compressed_pktnrr   _queue_readr@   	__class__r#   r$   rA      s   
z NetworkBrokerCompressed.__init__r)   pktnrc                 C   s   g }t | tkr8d}tt | t D ]}|dtd| | ||t    |d d }|t7 }q| |d } |tdt | dd td| |   |S )	z2Prepare a payload for sending to the MySQL server.r   rV   rW   r   rD   NrX   rY   )rZ   r   r[   appendr\   r]   )r)   rl   pktsr^   r_   r#   r#   r$   _prepare_packets  s$   

&z(NetworkBrokerCompressed._prepare_packetsc                 C   rB   rC   )rh   r@   r#   r#   r$   _set_next_compressed_pktnr  rF   z2NetworkBrokerCompressed._set_next_compressed_pktnrr'   r(   rG   c                    s\   t |}tdt|dd td| j tdt|dd  | }t |||S )z1Compress packet and write it to the comm channel.rX   r   rY   rW   )zlibcompressr\   r]   rZ   rh   rg   rO   )r-   r'   r(   rG   compressed_pktrj   r#   r$   rO     s   
z!NetworkBrokerCompressed._send_pktr*   r+   c           	   	      s"  |du r	|    n|| _|du r|   n|| _td| || j}t|tt	 kr[d}t
t|t D ]}| |||||t   |   |t7 }q6| ||||d  dS t|tkrj| ||| dS t ||tdt|dd td| j tdddd  |  dS )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.
        N    r   rX   rY   rW   )rE   r?   rp   rh   r9   joinro   rZ   r   r   r[   rO   r   rg   r\   r]   )	r-   r'   r(   r)   r*   r+   payload_prepr^   r_   rj   r#   r$   r.   *  s:   


zNetworkBrokerCompressed.sendcompressed_plluncompressed_pllc           	         s.  t  j||d}|dkr|ntt|}d}|t|k rtd|||t d  d d }t| t|| kryt  j|t	d}td|dd d d |d td|dd d d }| _
}t  j||d}||dkrs|nt|7 }| j|||t |   |t| 7 }|t|k sd	S d	S )
z&Handle reading of a compressed packet.r`   r   rX   r   ra   rY   r   r   N)rg   rU   r9   rq   
decompressrZ   r\   rb   r   r   rh   ri   rm   )	r-   r'   rw   rx   rs   rG   r^   pllrc   rj   r#   r$   _recv_compressed_pktg  s>   z,NetworkBrokerCompressed._recv_compressed_pktc              
      s   | j sOz4t j|td}td|dd d d |d td|dd d d }| _}| ||| W n tyN } zt	d|t
|fd	|d
}~ww | j sTd
S | j  }|d | _|S )z{Receive `one` or `several` packets from the MySQL server, enqueue them, and
        return the packet at the head.
        r`   rX   r   rY   ra   r   r   rH   rI   N)ri   rg   rU   r   r\   rb   rh   r{   rM   r   r%   popleftr?   )r-   r'   r(   rc   rw   rx   r   rG   rj   r#   r$   r0     s2   

zNetworkBrokerCompressed.recvre   r1   )r2   r3   r4   r5   rA   staticmethodr7   r8   r   ro   rp   r6   r!   rO   r   r.   r{   r9   r0   __classcell__r#   r#   rj   r$   rf      s@    
 =$0rf   c                   @   s   e Zd ZdZd&ddZd&ddZd&dd	Zd&d
dZd&ddZde	e
 ddfddZ				d'dededededede	e de	ee  ddfddZ		d(dede	e
 de	e
 ddfddZdefd d!Zed&d"d#Zeedefd$d%ZdS ))MySQLSocketzMySQL socket communication interface.

    Examples:
        Subclasses: network.MySQLTCPSocket and network.MySQLUnixSocket.
    r   Nc                 C   s   d| _ d| _d| _t | _dS )zsNetwork layer where transactions are made with plain (uncompressed) packets
        is enabled by default.
        N)r'   _connection_timeoutserver_hostr:   
_netbrokerr@   r#   r#   r$   rA     s   zMySQLSocket.__init__c                 C   s   t  | _dS )zIEnable network layer where transactions are made with compressed packets.N)rf   r   r@   r#   r#   r$   switch_to_compressed_mode  s   z%MySQLSocket.switch_to_compressed_modec              	   C   s8   z| j tj | j   W dS  ttfy   Y dS w )z'Shut down the socket before closing it.N)r'   shutdownr6   	SHUT_RDWRcloserN   OSErrorr@   r#   r#   r$   r     s   zMySQLSocket.shutdownc              	   C   s*   z| j   W dS  ttfy   Y dS w )zClose the socket.N)r'   r   rN   r   r@   r#   r#   r$   close_connection  s
   zMySQLSocket.close_connectionc                 C   s   |    d S N)r   r@   r#   r#   r$   __del__  s   zMySQLSocket.__del__timeoutc                 C   s    || _ | jr| j| dS dS )zSet the connection timeout.N)r   r'   
settimeout)r-   r   r#   r#   r$   set_connection_timeout  s   z"MySQLSocket.set_connection_timeoutFcacertkeyverify_certverify_identitycipher_suitestls_versionsc                 C   sr  | j stddz[|rtj}n	|rtj}ntj}|du s|s)t }	|s(d|	_nK|jdd |d }
t	sC|
dkrCt
|d	krC|d	 }
t|
 }t|}	|
dkrtd
|vr\|	 jtjO  _d|vrh|	 jtjO  _d|vrt|	 jtjO  _d|	_||	_|	  |rz|	| W n ttjfy } z| j   td| |d}~ww |rz|	|| W n ttjfy } z| j   td| |d}~ww |r|	| t| dr|	j| j | jd| _ n|	| j | _ |r_d|	_| jr| jgng }tjdkr| jdkrddg}t| j}| |d g|d	   d}g }|D ],}zt!| j " | W n tj#yI } z|$t%| W Y d}~q d}~ww d} |sb| j   tdd&| W dS W dS  t'yv } zt(d|d}~w tjtfy } ztd| j)t*|fd|d}~w tj#y } ztt%||d}~w t+y } ztt%||d}~ww )zSwitch the socket to use SSLi   rK   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 supportrH   rI   ),r'   r   sslCERT_REQUIREDCERT_OPTIONAL	CERT_NONEcreate_default_contextcheck_hostnamesortTLS_V1_3_SUPPORTEDrZ   TLS_VERSIONS
SSLContextoptionsOP_NO_TLSv1_2OP_NO_TLSv1_1OP_NO_TLSv1verify_modeload_default_certsload_verify_locationsrM   SSLErrorr   load_cert_chainset_ciphershasattrwrap_socketr   osnamer6   gethostbyaddrextendmatch_hostnamegetpeercertCertificateErrorrm   r!   ru   	NameErrorr   r(   r%   NotImplementedError)r-   r   r   r   r   r   r   r   	cert_reqscontexttls_versionssl_protocolr   	hostnamesaliasesmatch_founderrshostnamer#   r#   r$   switch_to_ssl  s   








zMySQLSocket.switch_to_sslr)   r*   r+   c                 C   s   | j j| j| j|||dS )z#Send `payload` to the MySQL server.)r*   r+   )r   r.   r'   r(   )r-   r)   r*   r+   r#   r#   r$   r.   U  s   zMySQLSocket.sendc                 C   s   | j | j| jS )z.Get packet from the MySQL server comm channel.)r   r0   r'   r(   r@   r#   r#   r$   r0   d  s   zMySQLSocket.recvc                 C   r,   )zOpen the socket.Nr#   r@   r#   r#   r$   open_connectionh  r/   zMySQLSocket.open_connectionc                 C   r,   )zGet the location of the socket.Nr#   r@   r#   r#   r$   r(   l  r/   zMySQLSocket.addressre   )FFNNr1   )r2   r3   r4   r5   rA   r   r   r   r   r   r8   r   r   boolr!   r   r   r7   r.   r9   r0   r   r   propertyr(   r#   r#   r#   r$   r     s\    






	
r
r   c                       s^   e Zd ZdZddeddf fddZedefdd	Zdd
dZde	de	ddfddZ
  ZS )MySQLUnixSocketzpMySQL socket class using UNIX sockets.

    Opens a connection through the UNIX socket of the MySQL Server.
    /tmp/mysql.sockunix_socketr   Nc                    s   t    || _|| _d S r   )rg   rA   r   _address)r-   r   rj   r#   r$   rA   x  s   

zMySQLUnixSocket.__init__c                 C      | j S r   r   r@   r#   r#   r$   r(   }     zMySQLUnixSocket.addressc              
   C   s   zt  t jt j| _| j| j | j| j W d S  ty2 } zt	d| j
t|fd|d }~w tyD } zt	t||d }~ww )Ni  rI   )r6   AF_UNIXSOCK_STREAMr'   r   r   connectr   rM   r   r(   r%   	Exceptionr!   )r-   r   r#   r#   r$   r     s"   zMySQLUnixSocket.open_connectionargskwargsc                 O   s   t dt dS )zSwitch the socket to use SSL.z2SSL is disabled when using unix socket connectionsN)warningswarnWarning)r-   r   r   r#   r#   r$   r     s   zMySQLUnixSocket.switch_to_ssl)r   re   )r2   r3   r4   r5   r!   rA   r   r(   r   r   r   r~   r#   r#   rj   r$   r   r  s    
r   c                	       sV   e Zd ZdZ			ddedededd	f fd
dZedefddZ	dddZ
  ZS )MySQLTCPSocketzYMySQL socket class using TCP/IP.

    Opens a TCP/IP connection to the MySQL Server.
    r     Fhostport
force_ipv6r   Nc                    s6   t    || _|| _|| _d| _| d| | _d S )Nr   :)rg   rA   r   server_portr   _familyr   )r-   r   r   r   rj   r#   r$   rA     s   
zMySQLTCPSocket.__init__c                 C   r   r   r   r@   r#   r#   r$   r(     r   zMySQLTCPSocket.addressc           	   
   C   sb  d}zHt | j| jdt jt j}|D ]}| jr"|d t jkr"|} n|d t jkr-|} nq| jr?|d du r?t	d| j |d du rI|d }W n t
yb } zt	d| jt|fd|d}~ww |\| _}}}}zt  | j||| _| j| j | j| W dS  t
y } zt	d| j| jt|fd|d}~w ty } ztt||d}~ww )z/Open the TCP/IP connection to the MySQL server.)NNNNNr   NzNo IPv6 address found for i  rI   )r6   getaddrinfor   r   r   SOL_TCPr   AF_INET6AF_INETr   rM   r(   r%   r   r'   r   r   r   r   r   r!   )	r-   addrinfo	addrinfosinfor   socktypeprotor_   sockaddrr#   r#   r$   r     sf   
zMySQLTCPSocket.open_connection)r   r   Fre   )r2   r3   r4   r5   r!   r8   r   rA   r   r(   r   r~   r#   r#   rj   r$   r     s"    r   )2r5   r   r6   r\   r   rq   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   r8   __annotations__r   r   r   rM   r!   r%   r&   r:   rf   r   r   r   r#   r#   r#   r$   <module>   sL    Ga A :'