a
    PҚhB                  
   @   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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< 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ejZdS )$zEDNS Options    N)AnyDictOptionalUnionc                   @   sH   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edd ZdS )
OptionType               	   
                  c                 C   s   dS Ni   clsr   r   C/wd/webapps/venvs/v2025_4um/lib/python3.9/site-packages/dns/edns.py_maximum:   s    zOptionType._maximumN)__name__
__module____qualname__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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__B   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-   r0   r   r   r   to_wireI   s    zOption.to_wirer1   c                 C   s   t d S Nr2   r-   r   r   r   to_textQ   s    zOption.to_textdns.wire.Parserr+   parserr1   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``.
        Nr2   r   r+   r<   r   r   r   from_wire_parserT   s    zOption.from_wire_parserc                 C   s,   |   }|  }||krdS ||kr(dS dS )zCompare an EDNS option with another option of the same type.

        Returns < 0 if < *other*, 0 if == *other*, and > 0 if > *other*.
        r      )r5   )r-   otherwireZowirer   r   r   _cmpa   s    zOption._cmpc                 C   s,   t |tsdS | j|jkrdS | |dkS )NFr   
isinstancer)   r+   rC   r-   rA   r   r   r   __eq__n   s
    
zOption.__eq__c                 C   s,   t |tsdS | j|jkrdS | |dkS )NTr   rD   rF   r   r   r   __ne__u   s
    
zOption.__ne__c                 C   s(   t |tr| j|jkrtS | |dk S Nr   rE   r)   r+   NotImplementedrC   rF   r   r   r   __lt__|   s    zOption.__lt__c                 C   s(   t |tr| j|jkrtS | |dkS rI   rJ   rF   r   r   r   __le__   s    zOption.__le__c                 C   s(   t |tr| j|jkrtS | |dkS rI   rJ   rF   r   r   r   __ge__   s    zOption.__ge__c                 C   s(   t |tr| j|jkrtS | |dkS rI   rJ   rF   r   r   r   __gt__   s    zOption.__gt__c                 C   s   |   S r7   )r9   r8   r   r   r   __str__   s    zOption.__str__)N)r   r   r   __doc__r   r   strr.   r   r   bytesr5   r9   r(   r>   rC   rG   rH   rL   rM   rN   rO   rP   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_bytesrU   )r-   r+   rU   	__class__r   r   r.      s    zGenericOption.__init__Nr/   c                 C   s   |r| | j d S | jS d S r7   )writerU   r4   r   r   r   r5      s    zGenericOption.to_wirer6   c                 C   s
   d| j  S )Nz
Generic %dr*   r8   r   r   r   r9      s    zGenericOption.to_textr:   r;   c                 C   s   | ||  S r7   get_remainingr=   r   r   r   r>      s    zGenericOption.from_wire_parser)N)r   r   r   rQ   r   r   rR   rS   r.   r   r   r5   r9   r(   r)   r>   __classcell__r   r   rZ   r   rT      s   $rT   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u r6d}tj
j|}tj
j|dd}tj
j|dd}nX|tjkrd| _	|du rd}tj
j|}tj
j|dd}tj
j|dd}ntd	|dusJ || _|| _|| _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.
           N8   r      r?          zBad address family       @r   Br@      )rV   r.   r   r    rW   ZinetZaf_for_addresssocketAF_INET6familyrX   rY   Z_as_ipv6_address_as_intAF_INETZ_as_ipv4_address
ValueErrorrb   rc   rd   	inet_ptonintmathceiladdrdatastructpackord)	r-   rb   rc   rd   afrw   nbytesnbitslastrZ   r   r   r.      s8    


&zECSOption.__init__r6   c                 C   s   d| j  d| j d| j S )NzECS /z scope/ra   r8   r   r   r   r9      s    zECSOption.to_text)textr1   c           
      C   s.  d}|   }d}t|dkr&|d }nBt|dkrX|d |krNtd|  d|d }ntd|  d|d}|dkr| d\}}d	}n*|dkr| d\}}}ntd|  dzt|}W n& ty   td
d| d Y n0 zt|}	W n( ty    tdd| d Y n0 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   re   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lenrr   countrt   r`   )
r   Zoptional_prefixtokensZecs_textZ	n_slashesrb   ZtsrclenZtscopescoperc   r   r   r   	from_text   s8    



zECSOption.from_textr/   c                 C   s6   t d| j| j| j| j }|r.|| d S |S d S )N!HBB)rx   ry   ro   rc   rd   rw   r\   r-   r0   valuer   r   r   r5     s    
zECSOption.to_wirer:   r;   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   rj   r?          re      zunsupported family)
Z
get_structrt   ru   rv   	get_bytesrW   Zipv4	inet_ntoaZipv6rr   )
r   r+   r<   ro   srcr   Zaddrlenprefixpadaddrr   r   r   r>   &  s    
zECSOption.from_wire_parser)Nr   )N)r   r   r   rQ   rR   r   rt   r.   r9   staticmethodr)   r   r   rS   r5   r(   r   r   r>   r_   r   r   rZ   r   r`      s   /6
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?   re   r   r   r   r	   r
   r   r   r   r   r   r      r   r      r                  rh   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h d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DNSSECZDNSKEYZNSECZDSZNXDOMAINZRRSIGsN)coder   c                    s>   t  tj t|| _|du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)rV   r.   r   r&   r   r,   r   rE   rR   rr   r   )r-   r   r   rZ   r   r   r.   ^  s
    zEDEOption.__init__r6   c                    sn   d j  } j tv rPt j }d fdd|dD }|d| d7 } jd urj|d j 7 }|S )	NzEDE  c                 3   s$   | ]}| j v r|n| V  qd S r7   )_preserve_casetitle).0wordr8   r   r   	<genexpr>q  s   z$EDEOption.to_text.<locals>.<genexpr>_z ()z: )r   r   r9   joinr   r   )r-   outputdescr   r8   r   r9   m  s    

zEDEOption.to_textr/   c                 C   sB   t d| j}| jd ur(|| jd7 }|r:|| d S |S d S )Nz!Hutf8)rx   ry   r   r   encoder\   r   r   r   r   r5   z  s    

zEDEOption.to_wirer:   r;   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   rQ   r   r   r   rR   r   r.   r9   r   rS   r5   r(   r   r)   r>   r_   r   r   rZ   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 r7   )rV   r.   r   r   r   )r-   r   rZ   r   r   r.     s    zNSIDOption.__init__Nr/   c                 C   s   |r| | j d S | jS d S r7   )r\   r   r4   r   r   r   r5     s    zNSIDOption.to_wirer6   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 )ri   ~   Nr   )r   cr   r   r   r         z%NSIDOption.to_text.<locals>.<genexpr>zNSID )allr   r   binasciihexlify)r-   r   r   r   r   r9     s    zNSIDOption.to_textr;   c                 C   s   | |  S r7   r]   r=   r   r   r   r>     s    zNSIDOption.from_wire_parser)Nr   r   r   rS   r.   r   r   r5   rR   r9   r(   r   r   rW   rB   Parserr)   r>   r_   r   r   rZ   r   r     s   r   c                       sj   e Zd Ze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 )CookieOption)clientserverc                    sb   t  tjjj || _|| _t|dkr2t	dt|dkr^t|dk sVt|dkr^t	dd S )Nr   zclient cookie must be 8 bytesr   ri   z5server cookie must be empty or between 8 and 32 bytes)
rV   r.   rW   Zednsr   r"   r   r   r   rr   r-   r   r   rZ   r   r   r.     s    $zCookieOption.__init__Nr/   c                 C   s>   |r.| | j t| jdkr*| | j d S | j| j S d S rI   )r\   r   r   r   r4   r   r   r   r5     s    zCookieOption.to_wirer6   c                 C   sB   t | j }t| jdkr0t | j }nd}d| | S )Nr    zCOOKIE )r   r   r   r   r   r   r   r   r   r   r9     s
    zCookieOption.to_textr;   c                 C   s   | | d| S )Nr   )r   r^   r=   r   r   r   r>     s    zCookieOption.from_wire_parser)Nr   r   r   rZ   r   r     s   		r   c                       sl   e Zd Zejj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 )ReportChannelOption)agent_domainc                    s   t  tj || _d S r7   )rV   r.   r   r'   r   )r-   r   rZ   r   r   r.     s    zReportChannelOption.__init__Nr/   c                 C   s   | j |S r7   )r   r5   r4   r   r   r   r5     s    zReportChannelOption.to_wirer6   c                 C   s   d| j   S )NzREPORTCHANNEL )r   r9   r8   r   r   r   r9     s    zReportChannelOption.to_textr;   c                 C   s   | |  S r7   )get_namer=   r   r   r   r>     s    z$ReportChannelOption.from_wire_parser)N)r   r   r   rW   nameNamer.   r   r   rS   r5   rR   r9   r(   r   r   rB   r   r)   r>   r_   r   r   rZ   r   r     s   r   _type_to_class)r+   r1   c                 C   s   t | }|du rt}|S )zReturn the class for the specified option type.

    The GenericOption class is used if a more specific class is not
    known.
    N)r   getrT   )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+   rB   currentolenr1   c                 C   sF   t j||}|| t| |W  d   S 1 s80    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)rW   rB   r   Zrestrict_tor   )r+   rB   r   r   r<   r   r   r   option_from_wire  s    r   )implementationr+   r1   c                 C   s   | t |< dS )zRegister 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   )/rQ   r   ru   rm   rx   typingr   r   r   r   Zdns.enumrW   Zdns.inetZ	dns.rdataZdns.wireenumIntEnumr   r)   rT   r`   r   r   r   r   r   r    r&   r   r"   r'   r   __annotations__r   rR   r   rS   rt   r   r   r   r   r   r!   r#   r$   r%   r   r   r   r   <module>   sZ   U 
 ="	