U
    Â@ÛfŸ;  ã                   @   sš  U 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mZm	Z	 ddl
ZddlZddlZddlZG dd„ dejjƒZG dd„ dƒZG dd	„ d	eƒZG d
d„ deƒZG dd„ dejjƒZG dd„ deƒZG dd„ deƒZejeejeejeiZeeef ed< eedœdd„Ze	eef dedœdd„Ze	eef e e!e!edœdd„Z"eeddœdd„Z#ejZej$Z$ej%Z%ej&Z&ejZej'Z'ej(Z(ej)Z)ej*Z*ej+Z+ejZdS ) zEDNS Optionsé    N)ÚAnyÚDictÚOptionalÚUnionc                   @   sD   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZedd„ ƒZdS )Ú
OptionTypeé   é   é   é   é   é	   é
   é   é   é   é   c                 C   s   dS ©Niÿÿ  © ©Úclsr   r   úB/var/www/html/chatgpt/venv/lib/python3.8/site-packages/dns/edns.pyÚ_maximum8   s    zOptionType._maximumN)Ú__name__Ú
__module__Ú__qualname__ÚNSIDÚDAUÚDHUÚN3UÚECSÚEXPIREÚCOOKIEÚ	KEEPALIVEÚPADDINGÚCHAINÚEDEÚclassmethodr   r   r   r   r   r       s   r   c                   @   s¤   e Zd ZdZeeef dœdd„Zd ee	 ee
 dœdd„Zed	œd
d„Zeedd dœdd„ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )!ÚOptionz%Base class for all EDNS option types.©Úotypec                 C   s   t  |¡| _dS )z_Initialize an option.

        *otype*, a ``dns.edns.OptionType``, is the option type.
        N)r   Úmaker)   )Úselfr)   r   r   r   Ú__init__@   s    zOption.__init__N©ÚfileÚreturnc                 C   s   t ‚dS )zUConvert an option to wire format.

        Returns a ``bytes`` or ``None``.

        N©ÚNotImplementedError©r+   r.   r   r   r   Úto_wireG   s    zOption.to_wire©r/   c                 C   s   t ‚d S ©Nr0   ©r+   r   r   r   Úto_textO   s    zOption.to_textúdns.wire.Parser©r)   Úparserr/   c                 C   s   t ‚dS )a  Build an EDNS option object from wire format.

        *otype*, a ``dns.edns.OptionType``, is the option type.

        *parser*, a ``dns.wire.Parser``, the parser, which should be
        restructed to the option length.

        Returns a ``dns.edns.Option``.
        Nr0   ©r   r)   r:   r   r   r   Úfrom_wire_parserR   s    zOption.from_wire_parserc                 C   s,   |   ¡ }|  ¡ }||krdS ||kr(dS dS )z�Compare an EDNS option with another option of the same type.

        Returns < 0 if < *other*, 0 if == *other*, and > 0 if > *other*.
        r   é   éÿÿÿÿ)r3   )r+   ÚotherÚwireZowirer   r   r   Ú_cmp_   s    zOption._cmpc                 C   s,   t |tƒsdS | j|jkrdS |  |¡dkS )NFr   ©Ú
isinstancer'   r)   rA   ©r+   r?   r   r   r   Ú__eq__l   s
    
zOption.__eq__c                 C   s,   t |tƒsdS | j|jkrdS |  |¡dkS )NTr   rB   rD   r   r   r   Ú__ne__s   s
    
zOption.__ne__c                 C   s(   t |tƒr| j|jkrtS |  |¡dk S ©Nr   ©rC   r'   r)   ÚNotImplementedrA   rD   r   r   r   Ú__lt__z   s    zOption.__lt__c                 C   s(   t |tƒr| j|jkrtS |  |¡dkS rG   rH   rD   r   r   r   Ú__le__   s    zOption.__le__c                 C   s(   t |tƒr| j|jkrtS |  |¡dkS rG   rH   rD   r   r   r   Ú__ge__„   s    zOption.__ge__c                 C   s(   t |tƒr| j|jkrtS |  |¡dkS rG   rH   rD   r   r   r   Ú__gt__‰   s    zOption.__gt__c                 C   s   |   ¡ S r5   )r7   r6   r   r   r   Ú__str__Ž   s    zOption.__str__)N)r   r   r   Ú__doc__r   r   Ústrr,   r   r   Úbytesr3   r7   r&   r<   rA   rE   rF   rJ   rK   rL   rM   rN   r   r   r   r   r'   =   s   r'   c                       s~   e Zd ZdZeeef eeef dœ‡ fdd„Zde	e
 e	e dœdd„Zed	œd
d„Zeeeef dedœdd„ƒZ‡  ZS )ÚGenericOptionzwGeneric Option Class

    This class is used for EDNS option types for which we have no better
    implementation.
    )r)   Údatac                    s"   t ƒ  |¡ tjj |d¡| _d S )NT)Úsuperr,   ÚdnsÚrdataÚRdataZ	_as_bytesrS   )r+   r)   rS   ©Ú	__class__r   r   r,   ™   s    zGenericOption.__init__Nr-   c                 C   s   |r|  | j¡ d S | jS d S r5   )ÚwriterS   r2   r   r   r   r3   �   s    zGenericOption.to_wirer4   c                 C   s
   d| j  S )Nz
Generic %dr(   r6   r   r   r   r7   ¤   s    zGenericOption.to_textr8   r9   c                 C   s   | ||  ¡ ƒS r5   ©Úget_remainingr;   r   r   r   r<   §   s    zGenericOption.from_wire_parser)N)r   r   r   rO   r   r   rP   rQ   r,   r   r   r3   r7   r&   r'   r<   Ú__classcell__r   r   rX   r   rR   ’   s   $
 þrR   c                       sŠ   e Zd ZdZdeee edœ‡ fdd„Zedœdd	„Ze	ee
d
œdd„ƒZdee ee dœdd„Zeeeef de
dœdd„ƒZ‡  ZS )Ú	ECSOptionz!EDNS Client Subnet (ECS, RFC7871)Nr   )ÚaddressÚsrclenÚscopelenc           	         s^  t ƒ  tj¡ tj |¡}|tjkrjd| _	|dkr6d}tj
j |¡}tj
j |dd¡}tj
j |dd¡}nX|tjkrºd| _	|dkr†d}tj
j |¡}tj
j |dd¡}tj
j |dd¡}ntd	ƒ‚|dk	sÎt‚|| _|| _|| _tj ||¡}tt |d
 ¡ƒ}|d|… | _|d }|dk�rZt dt| jdd… ƒdd| > @ ¡}| jdd… | | _dS )aŠ  *address*, a ``str``, is the client address information.

        *srclen*, an ``int``, the source prefix length, which is the
        leftmost number of bits of the address to be used for the
        lookup.  The default is 24 for IPv4 and 56 for IPv6.

        *scopelen*, an ``int``, the scope prefix length.  This value
        must be 0 in queries, and should be set in responses.
        é   Né8   r   é€   r=   é   é    zBad address familyç       @r   ÚBr>   éÿ   )rT   r,   r   r   rU   ZinetZaf_for_addressÚsocketÚAF_INET6ÚfamilyrV   rW   Z_as_ipv6_addressÚ_as_intÚAF_INETZ_as_ipv4_addressÚ
ValueErrorÚAssertionErrorr_   r`   ra   Ú	inet_ptonÚintÚmathÚceilÚaddrdataÚstructÚpackÚord)	r+   r_   r`   ra   Úafru   ÚnbytesÚnbitsÚlastrX   r   r   r,   ±   s8    


&zECSOption.__init__r4   c                 C   s   d  | j| j| j¡S )NzECS {}/{} scope/{})Úformatr_   r`   ra   r6   r   r   r   r7   à   s    zECSOption.to_text)Útextr/   c           
      C   s(  d}|   ¡ }d}t|ƒdkr&|d }n>t|ƒdkrV|d |krLtd | ¡ƒ‚|d }ntd | ¡ƒ‚| d¡}|dkrŠ|  d¡\}}d}n(|dkr¤|  d¡\}}}ntd | ¡ƒ‚zt|ƒ}W n& tk
rä   td	d
 |¡ ƒ‚Y nX zt|ƒ}	W n( tk
�r   tdd |¡ ƒ‚Y nX t||	|ƒS )a€  Convert a string into a `dns.edns.ECSOption`

        *text*, a `str`, the text form of the option.

        Returns a `dns.edns.ECSOption`.

        Examples:

        >>> import dns.edns
        >>>
        >>> # basic example
        >>> dns.edns.ECSOption.from_text('1.2.3.4/24')
        >>>
        >>> # also understands scope
        >>> dns.edns.ECSOption.from_text('1.2.3.4/24/32')
        >>>
        >>> # IPv6
        >>> dns.edns.ECSOption.from_text('2001:4b98::1/64/64')
        >>>
        >>> # it understands results from `dns.edns.ECSOption.to_text()`
        >>> dns.edns.ECSOption.from_text('ECS 1.2.3.4/24/32')
        r   Nr=   r   rb   zcould not parse ECS from "{}"ú/Ú0zinvalid scope z"{}": scope must be an integerzinvalid srclen z"{}": srclen must be an integer)ÚsplitÚlenro   r}   Úcountrr   r^   )
r~   Zoptional_prefixÚtokensZecs_textZ	n_slashesr_   ZtsrclenZtscopeÚscoper`   r   r   r   Ú	from_textã   s<    


ÿ
ÿ
zECSOption.from_textr-   c                 C   s6   t  d| j| j| j¡| j }|r.| |¡ d S |S d S )Nú!HBB)rv   rw   rl   r`   ra   ru   rZ   ©r+   r.   Úvaluer   r   r   r3     s    ÿ
zECSOption.to_wirer8   r9   c           
      C   sŒ   |  d¡\}}}tt |d ¡ƒ}| |¡}|dkrRd| }tj |d|  ¡}	n.|dkrxd| }tj |d|  ¡}	nt	dƒ‚| |	||ƒS )	Nr‡   rg   r=   é   ó    rb   é   zunsupported family)
Z
get_structrr   rs   rt   Ú	get_bytesrU   Zipv4Ú	inet_ntoaZipv6ro   )
r   r)   r:   rl   Úsrcr…   ZaddrlenÚprefixÚpadÚaddrr   r   r   r<   &  s    
zECSOption.from_wire_parser)Nr   )N)r   r   r   rO   rP   r   rr   r,   r7   Ústaticmethodr'   r†   r   rQ   r3   r&   r   r   r<   r]   r   r   rX   r   r^   ®   s   /8

 þr^   c                   @   s|   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZedd„ ƒZdS )ÚEDECoder   r=   rb   r   rŠ   r   r	   r
   r   r   r   r   r   r   é   r   rŒ   é   é   é   é   é   é   é   re   c                 C   s   dS r   r   r   r   r   r   r   T  s    zEDECode._maximumN)r   r   r   ÚOTHERZUNSUPPORTED_DNSKEY_ALGORITHMZUNSUPPORTED_DS_DIGEST_TYPEZSTALE_ANSWERZFORGED_ANSWERZDNSSEC_INDETERMINATEZDNSSEC_BOGUSZSIGNATURE_EXPIREDZSIGNATURE_NOT_YET_VALIDZDNSKEY_MISSINGZRRSIGS_MISSINGZNO_ZONE_KEY_BIT_SETZNSEC_MISSINGZCACHED_ERRORZ	NOT_READYZBLOCKEDZCENSOREDZFILTEREDZ
PROHIBITEDZSTALE_NXDOMAIN_ANSWERZNOT_AUTHORITATIVEZNOT_SUPPORTEDZNO_REACHABLE_AUTHORITYZNETWORK_ERRORZINVALID_DATAr&   r   r   r   r   r   r”   9  s6   r”   c                       sŒ   e Zd ZdZddddddhZdeeef ee d	œ‡ fd
d„Z	edœdd„Z
dee ee dœdd„Zeeeef dedœdd„ƒZ‡  ZS )Ú	EDEOptionz!Extended DNS Error (EDE, RFC8914)ZDNSKEYZDSZDNSSECZRRSIGsZNSECZNXDOMAINN)Úcoder~   c                    s>   t ƒ  tj¡ t |¡| _|dk	r4t|tƒs4t	dƒ‚|| _
dS )zÃ*code*, a ``dns.edns.EDECode`` or ``str``, the info code of the
        extended error.

        *text*, a ``str`` or ``None``, specifying additional information about
        the error.
        Nztext must be string or None)rT   r,   r   r%   r”   r*   rŸ   rC   rP   ro   r~   )r+   rŸ   r~   rX   r   r   r,   ^  s
    zEDEOption.__init__r4   c                    sn   dˆ j › �}ˆ j tkrPt ˆ j ¡}d ‡ fdd„| d¡D ƒ¡}|d|› d�7 }ˆ jd k	rj|dˆ j› �7 }|S )	NzEDE ú c                 3   s$   | ]}|ˆ j kr|n| ¡ V  qd S r5   )Ú_preserve_caseÚtitle)Ú.0Úwordr6   r   r   Ú	<genexpr>q  s   ÿz$EDEOption.to_text.<locals>.<genexpr>Ú_z (ú)z: )rŸ   r”   r7   Újoinr�   r~   )r+   ÚoutputÚdescr   r6   r   r7   m  s    
þ
zEDEOption.to_textr-   c                 C   sB   t  d| j¡}| jd k	r(|| j d¡7 }|r:| |¡ d S |S d S )Nz!HÚutf8)rv   rw   rŸ   r~   ÚencoderZ   rˆ   r   r   r   r3   z  s    

zEDEOption.to_wirer8   r9   c                 C   sL   t  | ¡ ¡}| ¡ }|r>|d dkr2|d d… }| d¡}nd }| ||ƒS )Nr>   r   r«   )r”   r*   Z
get_uint16r\   Údecode)r   r)   r:   rŸ   r~   Zbtextr   r   r   r<   …  s    zEDEOption.from_wire_parser)N)N)r   r   r   rO   r¡   r   r”   rP   r   r,   r7   r   rQ   r3   r&   r   r'   r<   r]   r   r   rX   r   rž   Y  s   "
 þrž   c                       sh   e Zd Zedœ‡ fdd„Zdeee dœdd„Zedœd	d
„Z	e
eeef ejjedœdd„ƒZ‡  ZS )Ú
NSIDOption)Únsidc                    s   t ƒ  tj¡ || _d S r5   )rT   r,   r   r   r¯   )r+   r¯   rX   r   r   r,   —  s    zNSIDOption.__init__Nr-   c                 C   s   |r|  | j¡ d S | jS d S r5   )rZ   r¯   r2   r   r   r   r3   ›  s    zNSIDOption.to_wirer4   c                 C   s:   t dd„ | jD ƒƒr | j ¡ }nt | j¡ ¡ }d|› �S )Nc                 s   s   | ]}|d ko|dkV  qdS )rf   é~   Nr   )r£   Úcr   r   r   r¥   £  s     z%NSIDOption.to_text.<locals>.<genexpr>zNSID )Úallr¯   r­   ÚbinasciiÚhexlify)r+   r‰   r   r   r   r7   ¢  s    zNSIDOption.to_textr9   c                 C   s   | |  ¡ ƒS r5   r[   r;   r   r   r   r<   ª  s    zNSIDOption.from_wire_parser)N)r   r   r   rQ   r,   r   r   r3   rP   r7   r&   r   r   rU   r@   ÚParserr'   r<   r]   r   r   rX   r   r®   –  s   
 þr®   Ú_type_to_class)r)   r/   c                 C   s   t  | ¡}|dkrt}|S )z„Return the class for the specified option type.

    The GenericOption class is used if a more specific class is not
    known.
    N)r¶   ÚgetrR   )r)   r   r   r   r   Úget_option_class¸  s    
r¸   r8   r9   c                 C   s   t  | ¡} t| ƒ}| | |¡S )a  Build an EDNS option object from wire format.

    *otype*, an ``int``, is the option type.

    *parser*, a ``dns.wire.Parser``, the parser, which should be
    restricted to the option length.

    Returns an instance of a subclass of ``dns.edns.Option``.
    )r   r*   r¸   r<   )r)   r:   r   r   r   r   Úoption_from_wire_parserÅ  s    
r¹   )r)   r@   ÚcurrentÚolenr/   c              
   C   s:   t j ||¡}| |¡� t| |ƒW  5 Q R £ S Q R X dS )ar  Build an EDNS option object from wire format.

    *otype*, an ``int``, is the option type.

    *wire*, a ``bytes``, is the wire-format message.

    *current*, an ``int``, is the offset in *wire* of the beginning
    of the rdata.

    *olen*, an ``int``, is the length of the wire-format option data

    Returns an instance of a subclass of ``dns.edns.Option``.
    N)rU   r@   rµ   Zrestrict_tor¹   )r)   r@   rº   r»   r:   r   r   r   Úoption_from_wireÖ  s    r¼   )Úimplementationr)   r/   c                 C   s   | t |< dS )z«Register the implementation of an option type.

    *implementation*, a ``class``, is a subclass of ``dns.edns.Option``.

    *otype*, an ``int``, is the option type.
    N)r¶   )r½   r)   r   r   r   Úregister_typeë  s    r¾   ),rO   r³   rs   rj   rv   Útypingr   r   r   r   Zdns.enumrU   Zdns.inetZ	dns.rdataZdns.wireÚenumÚIntEnumr   r'   rR   r^   r”   rž   r®   r   r%   r   r¶   Ú__annotations__r¸   rP   r¹   rQ   rr   r¼   r¾   r   r   r   r    r!   r"   r#   r$   r   r   r   r   Ú<module>   s^   U  =   ý
 þ
   þ