
                              d Z ddlmZ ddlZddlZddlZdZdZdZ	dZ
dZd	Zd
ZdZdZdZ G d de      Z G d de      Z G d de      ZeeiZd Zd Zy)zEDNS Options    )absolute_importN               	   
            c                   V    e Zd ZdZd Zd Zed        Zd Zd Z	d Z
d Zd	 Zd
 Zd Zy)Optionz%Base class for all EDNS option types.c                     || _         y)zPInitialize an option.

        *otype*, an ``int``, is the option type.
        Notype)selfr   s     lib/third_party/dns/edns.py__init__zOption.__init__4   s    
 
    c                     t         )z*Convert an option to wire format.
        NotImplementedErrorr   files     r   to_wirezOption.to_wire;   s
     "!r   c                     t         )at  Build an EDNS option object from wire format.

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

        *wire*, a ``binary``, 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 a ``dns.edns.Option``.
        r   clsr   wirecurrentolens        r   	from_wirezOption.from_wire@   s
      "!r   c                     t         )zCompare an EDNS option with another option of the same type.

        Returns < 0 if < *other*, 0 if == *other*, and > 0 if > *other*.
        r   r   others     r   _cmpzOption._cmpR   s
    
 "!r   c                     t        |t              sy| j                  |j                  k7  ry| j                  |      dk(  S NFr   
isinstancer   r   r'   r%   s     r   __eq__zOption.__eq__Y   6    %(::$yy1$$r   c                     t        |t              sy| j                  |j                  k7  ry| j                  |      dk7  S r)   r*   r%   s     r   __ne__zOption.__ne__`   r-   r   c                     t        |t              r| j                  |j                  k7  rt        S | j	                  |      dk  S Nr   r+   r   r   NotImplementedr'   r%   s     r   __lt__zOption.__lt__g   6    %(

ekk)!!yy!##r   c                     t        |t              r| j                  |j                  k7  rt        S | j	                  |      dk  S r1   r2   r%   s     r   __le__zOption.__le__m   6    %(

ekk)!!yy1$$r   c                     t        |t              r| j                  |j                  k7  rt        S | j	                  |      dk\  S r1   r2   r%   s     r   __ge__zOption.__ge__s   r8   r   c                     t        |t              r| j                  |j                  k7  rt        S | j	                  |      dkD  S r1   r2   r%   s     r   __gt__zOption.__gt__y   r5   r   N)__name__
__module____qualname____doc__r   r   classmethodr#   r'   r,   r/   r4   r7   r:   r<    r   r   r   r   0   sF    /"
 " """%%$%%$r   r   c                   D     e Zd ZdZ fdZd Zd Zed        Zd Z	 xZ
S )GenericOptionzwGeneric Option Class

    This class is used for EDNS option types for which we have no better
    implementation.
    c                 :    t         t        |   |       || _        y N)superrD   r   data)r   r   rH   	__class__s      r   r   zGenericOption.__init__   s    mT+E2	r   c                 :    |j                  | j                         y rF   )writerH   r   s     r   r   zGenericOption.to_wire   s    

499r   c                      d| j                   z  S )Nz
Generic %dr   r   s    r   to_textzGenericOption.to_text   s    djj((r   c                       | |||||z          S rF   rB   r   s        r   r#   zGenericOption.from_wire   s    5$w$788r   c                 l    | j                   |j                   k(  ry| j                   |j                   kD  ryyNr      )rH   r%   s     r   r'   zGenericOption._cmp   s+    99

"99uzz!r   )r=   r>   r?   r@   r   r   rN   rA   r#   r'   __classcell__rI   s   @r   rD   rD      s0    ) 9 9r   rD   c                   F     e Zd ZdZd fd	Zd Zd Zed        Zd Z	 xZ
S )	ECSOptionz!EDNS Client Subnet (ECS, RFC7871)c                    t         t        |   t               t        j
                  j                  |      }|t        j
                  j                  k(  rd| _        |7d}n4|t        j
                  j                  k(  rd| _        |d}nt        d      || _        || _        || _        t        j
                  j                  ||      }t        t!        j"                  |dz              }|d| | _        |dz  }|d	k7  rJt'        j(                  d
t+        | j$                  dd       d|z  z        }| j$                  dd |z   | _        yy)a  *address*, a ``text``, 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   rR      zBad ip family       @r   r   BrS      )rG   rW   r   ECSdnsinetaf_for_addressAF_INET6familyAF_INET
ValueErroraddresssrclenscopelen	inet_ptonintmathceiladdrdatastructpackord)
r   rg   rh   ri   afrn   nbytesnbitslastrI   s
            r   r   zECSOption.__init__   s     	i',XX$$W-"""DK~388###DK~_-- 88%%b'2TYYvcz*+ !&)
A:;;sCbc(:$;tu}$MND MM#2.5DM r   c                 d    dj                  | j                  | j                  | j                        S )NzECS {}/{} scope/{})formatrg   rh   ri   rM   s    r   rN   zECSOption.to_text   s(    #**4<<+/==: 	:r   c                    |j                  t        j                  d| j                               |j                  t        j                  d| j                  | j
                               |j                  | j                         y )Nz!Hz!BB)rK   ro   rp   rd   rh   ri   rn   r   s     r   r   zECSOption.to_wire   sN    

6;;tT[[12

6;;udkk4==AB

4==!r   c                    t        j                  d|||dz          \  }}}|dz  }t        t        j                  |dz              }|dk(  r t
        j                  j                  }	d|z
  }
n0|dk(  r t
        j                  j                  }	d|z
  }
nt        d      t
        j                  j                  |	||||z    d|
z  z         } | |||      S )	Nz!HBB   r\   rR   rY      zunsupported family    )ro   unpackrk   rl   rm   r`   ra   re   rc   rf   	inet_ntop)r   r   r    curr"   rd   srcscopeaddrlenrr   padaddrs               r   r#   zECSOption.from_wire   s    #]]64CE?CUqdiiC()Q;!!Bg+Cq[""Bw,C122xx!!"d3s7{&;gm&KL4e$$r   c                 l    | j                   |j                   k(  ry| j                   |j                   kD  ryyrQ   )rn   r%   s     r   r'   zECSOption._cmp   s+    ==ENN*==5>>)r   r1   )r=   r>   r?   r@   r   rN   r   rA   r#   r'   rT   rU   s   @r   rW   rW      s/    +&6P:"
 % %$r   rW   c                 @    t         j                  |       }|t        }|S )zReturn the class for the specified option type.

    The GenericOption class is used if a more specific class is not
    known.
    )_type_to_classgetrD   )r   r   s     r   get_option_classr      s#     

U
#C
{Jr   c                 @    t        |       }|j                  | |||      S )as  Build an EDNS option object from wire format.

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

    *wire*, a ``binary``, 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``.
    )r   r#   )r   r    r!   r"   r   s        r   option_from_wirer      s#     5
!C==gt44r   )r@   
__future__r   rl   ro   dns.inetr`   NSIDDAUDHUN3Ur_   EXPIRECOOKIE	KEEPALIVEPADDINGCHAINobjectr   rD   rW   r   r   r   rB   r   r   <module>r      s   $  &    		
		

M$V M$`F <L L^ 	Y
5r   