o
    Þý°jD  ã                
   @   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 ddlZ	ddl
Z	ddl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G dd„ deƒZG dd„ deƒZejeejeejeejeejeiZ eeef e!d< dedefdd„Z"dee#B dddefdd„Z$dee#B de%d e&d!e&def
d"d#„Z'd$ededdf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ejZdS )'zEDNS Optionsé    N)ÚAnyÚDictc                   @   sL   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edd„ ƒZdS )Ú
OptionTypezEDNS option type codesé   é   é   é   é   é	   é
   é   é   é   é   é   c                 C   ó   dS ©Niÿÿ  © ©Úclsr   r   ú{/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/dns/edns.pyÚ_maximum?   ó   zOptionType._maximumN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚNSIDÚDAUÚDHUÚN3UÚECSÚEXPIREÚCOOKIEÚ	KEEPALIVEÚPADDINGÚCHAINÚEDEÚREPORTCHANNELÚclassmethodr   r   r   r   r   r   #   s     r   c                   @   s°   e Zd ZdZdeeB fdd„Zd#dedB dedB fdd	„Z	defd
d„Z
d$dd„Zededddd f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__G   s   zOption.__init__NÚfileÚreturnc                 C   ó   t ‚)zUConvert an option to wire format.

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

        ©ÚNotImplementedError©r-   r/   r   r   r   Úto_wireN   s   zOption.to_wirec                 C   r1   ©Nr2   ©r-   r   r   r   Úto_textV   ó   zOption.to_textÚGenericOptionc                 C   s    |   ¡ }|dus
J ‚t| j|ƒS )zrCreates a dns.edns.GenericOption equivalent of this rdata.

        Returns a ``dns.edns.GenericOption``.
        N)r5   r:   r+   )r-   Úwirer   r   r   Ú
to_genericY   s   zOption.to_genericÚparserúdns.wire.Parserc                 C   r1   )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``.
        r2   ©r   r+   r=   r   r   r   Úfrom_wire_parserb   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   é   éÿÿÿÿ)r5   )r-   Úotherr;   Úowirer   r   r   Ú_cmpo   s   zOption._cmpc                 C   s,   t |tƒsdS | j|jkrdS |  |¡dkS )NFr   ©Ú
isinstancer*   r+   rE   ©r-   rC   r   r   r   Ú__eq__|   ó
   
zOption.__eq__c                 C   s,   t |tƒsdS | j|jkrdS |  |¡dkS )NTr   rF   rH   r   r   r   Ú__ne__ƒ   rJ   zOption.__ne__c                 C   s(   t |tƒr| j|jkrtS |  |¡dk S ©Nr   ©rG   r*   r+   ÚNotImplementedrE   rH   r   r   r   Ú__lt__Š   ó   zOption.__lt__c                 C   s(   t |tƒr| j|jkrtS |  |¡dkS rL   rM   rH   r   r   r   Ú__le__�   rP   zOption.__le__c                 C   s(   t |tƒr| j|jkrtS |  |¡dkS rL   rM   rH   r   r   r   Ú__ge__”   rP   zOption.__ge__c                 C   s(   t |tƒr| j|jkrtS |  |¡dkS rL   rM   rH   r   r   r   Ú__gt__™   rP   zOption.__gt__c                 C   s   |   ¡ S r6   )r8   r7   r   r   r   Ú__str__ž   s   zOption.__str__r6   ©r0   r:   )r   r   r   r   r   Ústrr.   r   Úbytesr5   r8   r<   r)   r@   rE   rI   rK   rO   rQ   rR   rS   rT   r   r   r   r   r*   D   s     
	r*   c                       s„   e Zd ZdZdeeB deeB f‡ fdd„ZddedB dedB fd	d
„Z	defdd„Z
ddd„ZedeeB dddefdd„ƒZ‡  ZS )r:   zwGeneric 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ÚRdataÚ	_as_bytesrX   )r-   r+   rX   ©Ú	__class__r   r   r.   ©   s   zGenericOption.__init__Nr/   r0   c                 C   ó   |r
|  | j¡ d S | jS r6   )ÚwriterX   r4   r   r   r   r5   ­   ó   zGenericOption.to_wirec                 C   s   d| j › �S )NzGeneric )r+   r7   r   r   r   r8   ´   ó   zGenericOption.to_textc                 C   s   | S r6   r   r7   r   r   r   r<   ·   r9   zGenericOption.to_genericr=   r>   c                 C   s   | ||  ¡ ƒS r6   ©Úget_remainingr?   r   r   r   r@   º   s   zGenericOption.from_wire_parserr6   rU   )r   r   r   r   r   rV   rW   r.   r   r5   r8   r<   r)   r*   r@   Ú__classcell__r   r   r^   r   r:   ¢   s    
ÿÿþr:   c                       s’   e Zd ZdZddededB def‡ fdd„Zd	efd
d„Zeded	e	fdd„ƒZ
ddedB d	edB fdd„ZedeeB ddd	e	fdd„ƒZ‡  ZS )Ú	ECSOptionz!EDNS Client Subnet (ECS, RFC7871)Nr   ÚaddressÚsrclenÚscopelenc           	         s`  t ƒ  tj¡ tj |¡}|tjkr5d| _	|du rd}tj
j |¡}tj
j |dd¡}tj
j |dd¡}n,|tjkr]d| _	|du rCd}tj
j |¡}tj
j |dd¡}tj
j |dd¡}ntd	ƒ‚|dusgJ ‚|| _|| _|| _tj ||¡}tt |d
 ¡ƒ}|d|… | _|d }|dkr®t dt| jdd… ƒdd| > @ ¡}| jdd… | | _dS 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   é€   rA   é   é    zBad address familyç       @r	   ÚBrB   éÿ   )rY   r.   r   r!   rZ   ÚinetÚaf_for_addressÚsocketÚAF_INET6Úfamilyr[   r\   Ú_as_ipv6_addressÚ_as_intÚAF_INETÚ_as_ipv4_addressÚ
ValueErrorrh   ri   rj   Ú	inet_ptonÚintÚmathÚceilÚaddrdataÚstructÚpackÚord)	r-   rh   ri   rj   Úafr�   ÚnbytesÚnbitsÚlastr^   r   r   r.   Ä   s:   

&þzECSOption.__init__r0   c                 C   s   d| j › d| j› d| j› �S )NzECS ú/z scope/)rh   ri   rj   r7   r   r   r   r8   ó   s   zECSOption.to_textÚtextc           
      C   s$  d}|   ¡ }d}t|ƒdkr|d }n!t|ƒdkr,|d |kr'td| › d�ƒ‚|d }ntd| › d�ƒ‚| d¡}|dkrG|  d¡\}}d	}n|dkrT|  d¡\}}}ntd| › d�ƒ‚zt|ƒ}W n tys   td
d|› d� ƒ‚w zt|ƒ}	W n ty‹   tdd|› d� ƒ‚w 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!   NrA   r   rk   zcould not parse ECS from "ú"r‰   Ú0zinvalid scope z": scope must be an integerzinvalid srclen z": srclen must be an integer)ÚsplitÚlenr|   Úcountr~   rg   )
rŠ   Úoptional_prefixÚtokensÚecs_textÚ	n_slashesrh   ÚtsrclenÚtscopeÚscoperi   r   r   r   Ú	from_textö   s<   


ÿÿÿzECSOption.from_textr/   c                 C   s2   t  d| j| j| j¡| j }|r| |¡ d S |S )Nú!HBB)r‚   rƒ   rw   ri   rj   r�   ra   ©r-   r/   Úvaluer   r   r   r5   -  s   ÿ
zECSOption.to_wirer+   r=   r>   c           
      C   sŒ   |  d¡\}}}tt |d ¡ƒ}| |¡}|dkr)d| }tj |d|  ¡}	n|dkr<d| }tj |d|  ¡}	nt	dƒ‚| |	||ƒS )	Nr˜   rp   rA   é   ó    rk   é   zunsupported family)
Ú
get_structr~   r   r€   Ú	get_bytesrZ   Úipv4Ú	inet_ntoaÚipv6r|   )
r   r+   r=   rw   Úsrcr–   ÚaddrlenÚprefixÚpadÚaddrr   r   r   r@   7  s   
zECSOption.from_wire_parserrL   r6   )r   r   r   r   rV   r~   r.   r8   Ústaticmethodr*   r—   r   rW   r5   r)   r   r@   rf   r   r   r^   r   rg   Á   s     /6
ÿÿþrg   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dZedd„ ƒZdS )ÚEDECodezExtended DNS Error (EDE) codesr   rA   rk   r   r›   r   r   r   r	   r
   r   r   r   r   é   r   r�   é   r   é   é   é   é   é   rn   c                 C   r   r   r   r   r   r   r   r   g  r   zEDECode._maximumN)r   r   r   r   ÚOTHERÚUNSUPPORTED_DNSKEY_ALGORITHMÚUNSUPPORTED_DS_DIGEST_TYPEÚSTALE_ANSWERÚFORGED_ANSWERÚDNSSEC_INDETERMINATEÚDNSSEC_BOGUSÚSIGNATURE_EXPIREDÚSIGNATURE_NOT_YET_VALIDÚDNSKEY_MISSINGÚRRSIGS_MISSINGÚNO_ZONE_KEY_BIT_SETÚNSEC_MISSINGÚCACHED_ERRORÚ	NOT_READYÚBLOCKEDÚCENSOREDÚFILTEREDÚ
PROHIBITEDÚSTALE_NXDOMAIN_ANSWERÚNOT_AUTHORITATIVEÚNOT_SUPPORTEDÚNO_REACHABLE_AUTHORITYÚNETWORK_ERRORÚINVALID_DATAr)   r   r   r   r   r   r©   J  s:    r©   c                       s„   e Zd ZdZh d£ZddeeB dedB f‡ fdd„Zdefd	d
„Zdde	dB de
dB fdd„ZedeeB dddefdd„ƒZ‡  ZS )Ú	EDEOptionz!Extended DNS Error (EDE, RFC8914)>   ÚDSÚNSECÚDNSKEYÚDNSSECÚRRSIGsÚNXDOMAINNÚcoderŠ   c                    s>   t ƒ  tj¡ t |¡| _|durt|tƒst	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)rY   r.   r   r'   r©   r,   rÑ   rG   rV   r|   rŠ   )r-   rÑ   rŠ   r^   r   r   r.   q  s
   
zEDEOption.__init__r0   c                    sn   dˆ j › �}ˆ j tv r(t ˆ j ¡}d ‡ fdd„| d¡D ƒ¡}|d|› d�7 }ˆ jd ur5|dˆ j› �7 }|S )	NzEDE ú c                 3   s&   � | ]}|ˆ j v r|n| ¡ V  qd S r6   )Ú_preserve_caseÚtitle)Ú.0Úwordr7   r   r   Ú	<genexpr>„  s
   € ÿ
ÿz$EDEOption.to_text.<locals>.<genexpr>Ú_z (ú)z: )rÑ   r©   r8   Újoinr�   rŠ   )r-   ÚoutputÚdescr   r7   r   r8   €  s   
þ
zEDEOption.to_textr/   c                 C   s>   t  d| j¡}| jd ur|| j d¡7 }|r| |¡ d S |S )Nz!HÚutf8)r‚   rƒ   rÑ   rŠ   Úencodera   r™   r   r   r   r5   �  s   

zEDEOption.to_wirer+   r=   r>   c                 C   sL   t  | ¡ ¡}| ¡ }|r|d dkr|d d… }| d¡}nd }| ||ƒS )NrB   r   rÝ   )r©   r,   Ú
get_uint16re   Údecode)r   r+   r=   rÑ   rŠ   Úbtextr   r   r   r@   ˜  s   
zEDEOption.from_wire_parserr6   )r   r   r   r   rÓ   r©   rV   r.   r8   r   rW   r5   r)   r   r*   r@   rf   r   r   r^   r   rÊ   l  s     ÿÿþrÊ   c                       sj   e Zd Zdef‡ fdd„ZddededB fdd„Zdefd	d
„Ze	de
eB dejjdefdd„ƒZ‡  ZS )Ú
NSIDOptionÚnsidc                    ó   t ƒ  tj¡ || _d S r6   )rY   r.   r   r   rã   )r-   rã   r^   r   r   r.   ª  ó   
zNSIDOption.__init__Nr/   r0   c                 C   r`   r6   )ra   rã   r4   r   r   r   r5   ®  rb   zNSIDOption.to_wirec                 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 )ro   é~   Nr   )rÕ   Úcr   r   r   r×   ¶  s   € z%NSIDOption.to_text.<locals>.<genexpr>zNSID )Úallrã   rà   ÚbinasciiÚhexlify)r-   rš   r   r   r   r8   µ  s   
zNSIDOption.to_textr+   r=   c                 C   ó   | |  ¡ ƒS r6   rd   r?   r   r   r   r@   ½  ó   zNSIDOption.from_wire_parserr6   ©r   r   r   rW   r.   r   r5   rV   r8   r)   r   rZ   r;   ÚParserr*   r@   rf   r   r   r^   r   râ   ©  s    ÿÿþrâ   c                       sn   e Zd Zdedef‡ fdd„ZddededB fdd	„Zdefd
d„Ze	de
eB dejjdefdd„ƒZ‡  ZS )ÚCookieOptionÚclientÚserverc                    sb   t ƒ  tj¡ || _|| _t|ƒdkrtdƒ‚t|ƒdkr-t|ƒdk s)t|ƒdkr/tdƒ‚d S d S )Nr	   zclient cookie must be 8 bytesr   ro   z5server cookie must be empty or between 8 and 32 bytes)rY   r.   r   r#   rð   rñ   rŽ   r|   ©r-   rð   rñ   r^   r   r   r.   Å  s   $ÿzCookieOption.__init__Nr/   r0   c                 C   s:   |r|  | j¡ t| jƒdkr|  | j¡ d S | j| j S rL   )ra   rð   rŽ   rñ   r4   r   r   r   r5   Î  s   zCookieOption.to_wirec                 C   sB   t  | j¡ ¡ }t| jƒdkrt  | j¡ ¡ }nd}d|› |› �S )Nr   Ú zCOOKIE )ré   rê   rð   rà   rŽ   rñ   rò   r   r   r   r8   ×  s
   zCookieOption.to_textr+   r=   c                 C   s   | |  d¡| ¡ ƒS )Nr	   )rŸ   re   r?   r   r   r   r@   ß  s   zCookieOption.from_wire_parserr6   rí   r   r   r^   r   rï   Ä  s    		ÿÿþrï   c                       sn   e Zd Zdejjf‡ fdd„ZddededB fdd„Z	de
fd	d
„Zedee
B dejjdefdd„ƒZ‡  ZS )ÚReportChannelOptionÚagent_domainc                    rä   r6   )rY   r.   r   r(   rõ   )r-   rõ   r^   r   r   r.   è  rå   zReportChannelOption.__init__Nr/   r0   c                 C   s   | j  |¡S r6   )rõ   r5   r4   r   r   r   r5   ì  rc   zReportChannelOption.to_wirec                 C   s   d| j  ¡  S )NzREPORTCHANNEL )rõ   r8   r7   r   r   r   r8   ï  s   zReportChannelOption.to_textr+   r=   c                 C   rë   r6   )Úget_namer?   r   r   r   r@   ò  rì   z$ReportChannelOption.from_wire_parserr6   )r   r   r   rZ   ÚnameÚNamer.   r   rW   r5   rV   r8   r)   r   r;   rî   r*   r@   rf   r   r   r^   r   rô   æ  s    ÿÿþrô   Ú_type_to_classr+   r0   c                 C   s   t  | ¡}|du 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ù   Úgetr:   )r+   r   r   r   r   Úget_option_class  s   
rû   r=   r>   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;   ÚcurrentÚolenc                 C   sF   t j ||¡}| |¡� t| |ƒW  d  ƒ S 1 sw   Y  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)rZ   r;   rî   Úrestrict_torü   )r+   r;   rý   rþ   r=   r   r   r   Úoption_from_wire   s   $ÿr   Úimplementationc                 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_type5  s   r  )0r   ré   r   ru   r‚   Útypingr   r   Údns.enumrZ   Údns.inetÚdns.ipv4Údns.ipv6Údns.nameÚ	dns.rdataÚdns.wireÚenumÚIntEnumr   r*   r:   rg   r©   rÊ   râ   rï   rô   r!   r'   r   r#   r(   rù   Ú__annotations__rû   rV   rü   rW   r~   r   r  r   r   r    r"   r$   r%   r&   r   r   r   r   Ú<module>   sz   !^ 
"="û	ÿÿ
þÿÿÿÿ
þ
