o
    h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   J/var/www/html/pro-man-master/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 )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-   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rt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.
           N8   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 )zReturn 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 )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_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   !^ 
"="	


