B
    u9a4&                @   s  d Z ddlZdZdZG dd deZG dd deZd	d
 Z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d Zd d! ZG d"d# d#ZejG d$d% d%eZejG d&d' d'eZG d(d) d)ZG d*d+ d+eeZG d,d- d-eZG d.d/ d/eeZG d0d1 d1Zee_G d2d3 d3ZG d4d5 d5eeZG d6d7 d7eZG d8d9 d9eeZ G d:d; d;Z!e!e_dS )=z1.0    N       c               @   s   e Zd ZdS )AddressValueErrorN)__name__
__module____qualname__ r   r   /usr/lib/python3.7/ipaddress.pyr      s   r   c               @   s   e Zd ZdS )NetmaskValueErrorN)r   r   r   r   r   r   r	   r
      s   r
   c          	   C   sT   yt | S  ttfk
r    Y nX yt| S  ttfk
rB   Y nX td|  d S )Nz0%r does not appear to be an IPv4 or IPv6 address)IPv4Addressr   r
   IPv6Address
ValueError)addressr   r   r	   
ip_address   s    r   Tc          	   C   sX   y
t | |S  ttfk
r"   Y nX y
t| |S  ttfk
rF   Y nX td|  d S )Nz0%r does not appear to be an IPv4 or IPv6 network)IPv4Networkr   r
   IPv6Networkr   )r   strictr   r   r	   
ip_network9   s    

r   c          	   C   sT   yt | S  ttfk
r    Y nX yt| S  ttfk
rB   Y nX td|  d S )Nz2%r does not appear to be an IPv4 or IPv6 interface)IPv4Interfacer   r
   IPv6Interfacer   )r   r   r   r	   ip_interfaceW   s    r   c             C   s.   y|  ddS  tk
r(   tdY nX d S )N   bigz&Address negative or too large for IPv4)to_bytesOverflowErrorr   )r   r   r   r	   v4_int_to_packedz   s    r   c             C   s.   y|  ddS  tk
r(   tdY nX d S )N   r   z&Address negative or too large for IPv6)r   r   r   )r   r   r   r	   v6_int_to_packed   s    
r   c             C   s*   t | d}t|dkr&td|  |S )N/   zOnly one '/' permitted in %r)strsplitlenr   )r   addrr   r   r	   _split_optional_netmask   s    r$   c             c   sR   t | }t| }}x.|D ]&}|j|jd kr<||fV  |}|}qW ||fV  d S )N   )iternext_ip)	addressesitfirstlastipr   r   r	   _find_address_range   s    


r.   c             C   s$   | dkr|S t ||  | d @  S )Nr   r%   )min
bit_length)Znumberbitsr   r   r	   _count_righthand_zero_bits   s    r2   c             c   s   t | trt |tstd| j|jkr8td| |f | |krHtd| jdkrXt}n| jdkrht}ntd| j}| j}|j}x^||krt	t
|||| d  d }|||| f}|V  |d|> 7 }|d |jkrP qW d S )Nz1first and last must be IP addresses, not networksz%%s and %s are not of the same versionz*last IP address must be greater than firstr      zunknown IP versionr%   )
isinstance_BaseAddress	TypeErrorversionr   r   r   _max_prefixlenr(   r/   r2   r0   	_ALL_ONES)r+   r,   r-   Zip_bitsZ	first_intZlast_intZnbitsnetr   r   r	   summarize_address_range   s0    





r;   c             c   s   t | }i }xL|rX| }| }||}|d kr>|||< q||kr||= || qW d }x4t| D ]$}|d k	r|j|jkrql|V  |}qlW d S )N)listpopsupernetgetappendsortedvaluesbroadcast_address)r)   Zto_mergesubnetsr:   r>   Zexistingr,   r   r   r	   _collapse_addresses_internal  s$    

rE   c          	   C   s8  g }g }g }x| D ]}t |trT|rH|d j|jkrHtd||d f || q|j|jkr|r|d j|jkrtd||d f y||j W q tk
r   ||j	 Y qX q|r|d j|jkrtd||d f || qW t
t|}|r,x&t|D ]\}}|t|| qW t|| S )Nz%%s and %s are not of the same version)r4   r5   _versionr6   r@   
_prefixlenr8   r-   AttributeErrornetwork_addressrA   setr.   extendr;   rE   )r)   ZaddrsZipsZnetsr-   r+   r,   r   r   r	   collapse_addresses2  s4    

rM   c             C   s(   t | tr|  S t | tr$|  S tS )N)r4   _BaseNetwork_get_networks_keyr5   _get_address_keyNotImplemented)objr   r   r	   get_mixed_type_keyh  s
    

rS   c               @   s   e Zd ZdZedd Zedd Zedd Zedd	 Zd
d Z	dd Z
edd Zedd Zedd Zedd Zedd Zdd ZdS )_IPAddressBaser   c             C   s   |   S )N)_explode_shorthand_ip_string)selfr   r   r	   exploded  s    z_IPAddressBase.explodedc             C   s   t | S )N)r    )rV   r   r   r	   
compressed  s    z_IPAddressBase.compressedc             C   s   |   S )N)_reverse_pointer)rV   r   r   r	   reverse_pointer  s    	z_IPAddressBase.reverse_pointerc             C   s   dt | f }t|d S )Nz%200s has no version specified)typeNotImplementedError)rV   msgr   r   r	   r7     s    z_IPAddressBase.versionc             C   sF   |dk rd}t ||| jf || jkrBd}t ||| j| jf d S )Nr   z-%d (< 0) is not permitted as an IPv%d addressz2%d (>= 2**%d) is not permitted as an IPv%d address)r   rG   r9   r8   )rV   r   r]   r   r   r	   _check_int_address  s    

z!_IPAddressBase._check_int_addressc             C   s.   t |}||kr*d}t||||| jf d S )Nz6%r (len %d != %d) is not permitted as an IPv%d address)r"   r   rG   )rV   r   Zexpected_lenZaddress_lenr]   r   r   r	   _check_packed_address  s
    z$_IPAddressBase._check_packed_addressc             C   s   | j | j |? A S )N)r9   )cls	prefixlenr   r   r	   _ip_int_from_prefix  s    z"_IPAddressBase._ip_int_from_prefixc       	      C   s\   t || j}| j| }||? }d|> d }||krX| jd }||d}d}t|| |S )Nr%      r   z&Netmask pattern %r mixes zeroes & ones)r2   r8   r   r   )	r`   ip_intZtrailing_zeroesra   Zleading_onesZall_onesZbyteslenZdetailsr]   r   r   r	   _prefix_from_ip_int  s    

z"_IPAddressBase._prefix_from_ip_intc             C   s   d| }t |d d S )Nz%r is not a valid netmask)r
   )r`   Znetmask_strr]   r   r   r	   _report_invalid_netmask  s    z&_IPAddressBase._report_invalid_netmaskc             C   sh   t j|s| | yt|}W n tk
r@   | | Y nX d|  krX| jksdn | | |S )Nr   )_BaseV4_DECIMAL_DIGITS
issupersetrf   intr   r8   )r`   Zprefixlen_strra   r   r   r	   _prefix_from_prefix_string  s    

z)_IPAddressBase._prefix_from_prefix_stringc             C   s   y|  |}W n tk
r,   | | Y nX y
| |S  tk
rL   Y nX || jN }y
| |S  tk
r   | | Y nX d S )N)_ip_int_from_stringr   rf   re   r   r9   )r`   ip_strrd   r   r   r	   _prefix_from_ip_string  s    


z%_IPAddressBase._prefix_from_ip_stringc             C   s   | j t| ffS )N)	__class__r    )rV   r   r   r	   
__reduce__  s    z_IPAddressBase.__reduce__N)r   r   r   	__slots__propertyrW   rX   rZ   r7   r^   r_   classmethodrb   re   rf   rk   rn   rp   r   r   r   r	   rT     s   	"rT   c               @   s`   e Zd Z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d Zdd ZdS )r5   r   c             C   s   | j S )N)r(   )rV   r   r   r	   __int__&  s    z_BaseAddress.__int__c             C   s2   y| j |j ko| j|jkS  tk
r,   tS X d S )N)r(   rG   rI   rQ   )rV   otherr   r   r	   __eq__)  s
    z_BaseAddress.__eq__c             C   sF   t |tstS | j|jkr*td| |f | j|jkrB| j|jk S dS )Nz%%s and %s are not of the same versionF)r4   r5   rQ   rG   r6   r(   )rV   ru   r   r   r	   __lt__0  s    
z_BaseAddress.__lt__c             C   s    t |tstS | t| | S )N)r4   rj   rQ   ro   )rV   ru   r   r   r	   __add__<  s    
z_BaseAddress.__add__c             C   s    t |tstS | t| | S )N)r4   rj   rQ   ro   )rV   ru   r   r   r	   __sub__A  s    
z_BaseAddress.__sub__c             C   s   d| j jt| f S )Nz%s(%r))ro   r   r    )rV   r   r   r	   __repr__F  s    z_BaseAddress.__repr__c             C   s   t | | jS )N)r    _string_from_ip_intr(   )rV   r   r   r	   __str__I  s    z_BaseAddress.__str__c             C   s   t tt| jS )N)hashhexrj   r(   )rV   r   r   r	   __hash__L  s    z_BaseAddress.__hash__c             C   s
   | j | fS )N)rG   )rV   r   r   r	   rP   O  s    z_BaseAddress._get_address_keyc             C   s   | j | jffS )N)ro   r(   )rV   r   r   r	   rp   R  s    z_BaseAddress.__reduce__N)r   r   r   rq   rt   rv   rw   rx   ry   rz   r|   r   rP   rp   r   r   r   r	   r5     s   	r5   c               @   s`  e Z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d Zdd Zdd Zedd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zed%d& Zd'd( Zd)d* Zd+d, ZdGd/d0ZdHd1d2Zed3d4 Zed5d6 Zd7d8 Zd9d: Z ed;d< Z!ed=d> Z"ed?d@ Z#edAdB Z$edCdD Z%edEdF Z&d.S )IrN   c             C   s
   i | _ d S )N)_cache)rV   r   r   r   r	   __init___  s    z_BaseNetwork.__init__c             C   s   d| j jt| f S )Nz%s(%r))ro   r   r    )rV   r   r   r	   rz   b  s    z_BaseNetwork.__repr__c             C   s   d| j | jf S )Nz%s/%d)rJ   ra   )rV   r   r   r	   r|   e  s    z_BaseNetwork.__str__c             c   s<   t | j}t | j}x"t|d |D ]}| |V  q$W d S )Nr%   )rj   rJ   rC   range_address_class)rV   network	broadcastxr   r   r	   hostsh  s    

z_BaseNetwork.hostsc             c   s<   t | j}t | j}x"t||d D ]}| |V  q$W d S )Nr%   )rj   rJ   rC   r   r   )rV   r   r   r   r   r   r	   __iter__t  s    

z_BaseNetwork.__iter__c             C   sl   t | j}t | j}|dkr>|| |kr0td| || S |d7 }|| |k rZtd| || S d S )Nr   zaddress out of ranger%   )rj   rJ   rC   
IndexErrorr   )rV   nr   r   r   r   r	   __getitem__z  s    

z_BaseNetwork.__getitem__c             C   s^   t |tstS | j|jkr*td| |f | j|jkrB| j|jk S | j|jkrZ| j|jk S dS )Nz%%s and %s are not of the same versionF)r4   rN   rQ   rG   r6   rJ   netmask)rV   ru   r   r   r	   rw     s    
z_BaseNetwork.__lt__c             C   sF   y,| j |j ko,| j|jko,t| jt|jkS  tk
r@   tS X d S )N)rG   rJ   rj   r   rI   rQ   )rV   ru   r   r   r	   rv     s    z_BaseNetwork.__eq__c             C   s   t t| jt| jA S )N)r}   rj   rJ   r   )rV   r   r   r	   r     s    z_BaseNetwork.__hash__c             C   sL   | j |j krdS t|trdS t| jt|j  koBt| jkS   S d S )NF)rG   r4   rN   rj   rJ   r(   rC   )rV   ru   r   r   r	   __contains__  s    
z_BaseNetwork.__contains__c             C   s(   | j |kp&| j|kp&|j | kp&|j| kS )N)rJ   rC   )rV   ru   r   r   r	   overlaps  s    


z_BaseNetwork.overlapsc             C   s<   | j d}|d kr8| t| jt| jB }|| j d< |S )NrC   )r   r?   r   rj   rJ   hostmask)rV   r   r   r   r	   rC     s    
z_BaseNetwork.broadcast_addressc             C   s8   | j d}|d kr4| t| j| jA }|| j d< |S )Nr   )r   r?   r   rj   r   r9   )rV   r   r   r   r	   r     s
    
z_BaseNetwork.hostmaskc             C   s   d| j | jf S )Nz%s/%d)rJ   rH   )rV   r   r   r	   with_prefixlen  s    z_BaseNetwork.with_prefixlenc             C   s   d| j | jf S )Nz%s/%s)rJ   r   )rV   r   r   r	   with_netmask  s    z_BaseNetwork.with_netmaskc             C   s   d| j | jf S )Nz%s/%s)rJ   r   )rV   r   r   r	   with_hostmask  s    z_BaseNetwork.with_hostmaskc             C   s   t | jt | j d S )Nr%   )rj   rC   rJ   )rV   r   r   r	   num_addresses  s    z_BaseNetwork.num_addressesc             C   s   dt | f }t|d S )Nz%%200s has no associated address class)r[   r\   )rV   r]   r   r   r	   r     s    z_BaseNetwork._address_classc             C   s   | j S )N)rH   )rV   r   r   r	   ra     s    z_BaseNetwork.prefixlenc             c   s  | j |j kstd| |f t|ts2td| || sLtd|| f || krXd S |d|j|jf }| 	 \}}xb||kr||kr||r|V  |	 \}}q|||r|V  |	 \}}q|t
d|||f q|W ||kr|V  n"||kr|V  nt
d|||f d S )Nz%%s and %s are not of the same versionz%s is not a network objectz%s not contained in %sz%s/%sz3Error performing exclusion: s1: %s s2: %s other: %s)rG   r6   r4   rN   	subnet_ofr   ro   rJ   ra   rD   AssertionError)rV   ru   s1s2r   r   r	   address_exclude  s6    $




z_BaseNetwork.address_excludec             C   s`   | j |j krtd| |f | j|jk r,dS | j|jkr<dS | j|jk rLdS | j|jkr\dS dS )Nz"%s and %s are not of the same typerF   r%   r   )rG   r6   rJ   r   )rV   ru   r   r   r	   compare_networks+  s    !z_BaseNetwork.compare_networksc             C   s   | j | j| jfS )N)rG   rJ   r   )rV   r   r   r	   rO   [  s    z_BaseNetwork._get_networks_keyr%   Nc       	      c   s   | j | jkr| V  d S |d k	rJ|| j k r0td|dkr@td|| j  }|dk rZtd| j | }|| jkr~td|| f t| j}t| jd }t| jd |? }x(t|||D ]}| ||f}|V  qW d S )Nznew prefix must be longerr%   z(cannot set prefixlen_diff and new_prefixr   zprefix length diff must be > 0z0prefix length diff %d is invalid for netblock %s)	rH   r8   r   rj   rJ   rC   r   r   ro   )	rV   prefixlen_diff
new_prefixnew_prefixlenstartendstepZnew_addrZcurrentr   r   r	   rD   e  s,    




z_BaseNetwork.subnetsc             C   s   | j dkr| S |d k	rB|| j kr(td|dkr8td| j | }| j| }|dk rftd| j|f | t| jt| j|> @ |fS )Nr   znew prefix must be shorterr%   z(cannot set prefixlen_diff and new_prefixz;current prefixlen is %d, cannot have a prefixlen_diff of %d)rH   r   ra   ro   rj   rJ   r   )rV   r   r   r   r   r   r	   r>     s     



z_BaseNetwork.supernetc             C   s   | j jo| jjS )N)rJ   is_multicastrC   )rV   r   r   r	   r     s    	z_BaseNetwork.is_multicastc             C   sf   y8| j |j kr"t|  d| d|j| jko8|j| jkS  tk
r`   td|  d| Y nX d S )Nz and z are not of the same versionz*Unable to test subnet containment between )rG   r6   rJ   rC   rI   )abr   r   r	   _is_subnet_of  s    z_BaseNetwork._is_subnet_ofc             C   s   |  | |S )N)r   )rV   ru   r   r   r	   r     s    z_BaseNetwork.subnet_ofc             C   s   |  || S )N)r   )rV   ru   r   r   r	   supernet_of  s    z_BaseNetwork.supernet_ofc             C   s   | j jo| jjS )N)rJ   is_reservedrC   )rV   r   r   r	   r     s    	z_BaseNetwork.is_reservedc             C   s   | j jo| jjS )N)rJ   is_link_localrC   )rV   r   r   r	   r     s    z_BaseNetwork.is_link_localc             C   s   | j jo| jjS )N)rJ   
is_privaterC   )rV   r   r   r	   r     s    	z_BaseNetwork.is_privatec             C   s   | j  S )N)r   )rV   r   r   r	   	is_global  s    	z_BaseNetwork.is_globalc             C   s   | j jo| jjS )N)rJ   is_unspecifiedrC   )rV   r   r   r	   r     s    	z_BaseNetwork.is_unspecifiedc             C   s   | j jo| jjS )N)rJ   is_loopbackrC   )rV   r   r   r	   r     s    	z_BaseNetwork.is_loopback)r%   N)r%   N)'r   r   r   r   rz   r|   r   r   r   rw   rv   r   r   r   rr   rC   r   r   r   r   r   r   ra   r   r   rO   rD   r>   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   r	   rN   V  sD   		K0

5
)rN   c            
   @   s   e Zd ZdZdZde d ZedZedddd	d
ddddh	Z	eZ
i Zdd Zedd Zedd Zedd Zedd Zdd Zdd Zdd Zedd  Zed!d" Zd#S )$rg   r   r   r   r%   
0123456789                     r   r   c             C   s   t | S )N)r    )rV   r   r   r	   rU   A  s    z$_BaseV4._explode_shorthand_ip_stringc             C   s   || j krt|tr<|}d|  kr.| jksjn | | n.y| |}W n tk
rh   | |}Y nX t| 	|}||f| j |< | j | S )Nr   )
_netmask_cacher4   rj   r8   rf   rk   r
   rn   r   rb   )r`   argra   r   r   r   r	   _make_netmaskD  s    	

z_BaseV4._make_netmaskc          
   C   s|   |st d|d}t|dkr.t d| ytt| j|dS  tk
rv } zt d||f d W d d }~X Y nX d S )NzAddress cannot be empty.r   zExpected 4 octets in %rr   z%s in %r)r   r!   r"   rj   
from_bytesmap_parse_octetr   )r`   rm   Zoctetsexcr   r   r	   rl   ^  s    
z_BaseV4._ip_int_from_stringc             C   s   |st d| j|s(d}t || t|dkrDd}t || t|d}|dkrr|d dkrrd	}t || |d
krt d| |S )NzEmpty octet not permittedz#Only decimal digits permitted in %r   z$At most 3 characters permitted in %r
      r   0z3Ambiguous (octal/decimal) value in %r not permittedr   zOctet %d (> 255) not permitted)r   rh   ri   r"   rj   )r`   Z	octet_strr]   Z	octet_intr   r   r	   r   x  s    
z_BaseV4._parse_octetc             C   s   d tt|ddS )Nr   r   r   )joinr   r    r   )r`   rd   r   r   r	   r{     s    z_BaseV4._string_from_ip_intc             C   s   | d}t|dkry$x|D ]}t|| jkrdS qW W n tk
rN   dS X x0t|D ]$\}}|dkrZ|||d  krZdS qZW dS yt|}W n tk
r   dS X d|  ko| jkS   S )Nr   r   Fr   r%   T)r!   r"   rj   _valid_mask_octetsr   	enumerater8   )rV   r   maskr   idxyr   r   r	   _is_valid_netmask  s"    

z_BaseV4._is_valid_netmaskc                sh   | d}y fddtt|D }W n tk
r:   dS X t|t|krPdS |d |d k rddS dS )Nr   c                s   g | ]}| j kr|qS r   )r   ).0r   )rV   r   r	   
<listcomp>  s    z(_BaseV4._is_hostmask.<locals>.<listcomp>Fr   rF   T)r!   r   rj   r   r"   )rV   rm   r1   partsr   )rV   r	   _is_hostmask  s    

z_BaseV4._is_hostmaskc             C   s&   t | dd d d }d|d S )Nr   rF   z.in-addr.arpa)r    r!   r   )rV   Zreverse_octetsr   r   r	   rY     s    z_BaseV4._reverse_pointerc             C   s   | j S )N)r8   )rV   r   r   r	   max_prefixlen  s    z_BaseV4.max_prefixlenc             C   s   | j S )N)rG   )rV   r   r   r	   r7     s    z_BaseV4.versionN)r   r   r   rq   rG   
IPV4LENGTHr9   	frozensetrh   r   r8   r   rU   rs   r   rl   r   r{   r   r   rY   rr   r   r7   r   r   r   r	   rg   *  s"   	%	rg   c               @   s   e Zd ZdZdd Zedd Zedd Zee	 dd	 Z
ee	 d
d Zedd Zedd Zedd Zedd ZdS )r   )r(   __weakref__c             C   sr   t |tr| | || _d S t |trF| |d t|d| _d S t|}d|krbtd| | 	|| _d S )Nr   r   r   zUnexpected '/' in %r)
r4   rj   r^   r(   bytesr_   r   r    r   rl   )rV   r   addr_strr   r   r	   r     s    


zIPv4Address.__init__c             C   s
   t | jS )N)r   r(   )rV   r   r   r	   packed  s    zIPv4Address.packedc             C   s   | | j jkS )N)
_constants_reserved_network)rV   r   r   r	   r     s    	zIPv4Address.is_reservedc                s   t  fdd jjD S )Nc             3   s   | ]} |kV  qd S )Nr   )r   r:   )rV   r   r	   	<genexpr>3  s    z)IPv4Address.is_private.<locals>.<genexpr>)anyr   _private_networks)rV   r   )rV   r	   r   )  s    
zIPv4Address.is_privatec             C   s   | | j jko| j S )N)r   _public_networkr   )rV   r   r   r	   r   5  s    zIPv4Address.is_globalc             C   s   | | j jkS )N)r   _multicast_network)rV   r   r   r	   r   :  s    	zIPv4Address.is_multicastc             C   s   | | j jkS )N)r   _unspecified_address)rV   r   r   r	   r   E  s    	zIPv4Address.is_unspecifiedc             C   s   | | j jkS )N)r   _loopback_network)rV   r   r   r	   r   P  s    zIPv4Address.is_loopbackc             C   s   | | j jkS )N)r   _linklocal_network)rV   r   r   r	   r   Z  s    zIPv4Address.is_link_localN)r   r   r   rq   r   rr   r   r   	functools	lru_cacher   r   r   r   r   r   r   r   r   r	   r     s   #
r   c               @   sj   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zej	Z	e
dd Ze
dd Ze
dd Ze
dd ZdS )r   c             C   s   t |ttfr2t| | t| j| _| j| _	d S t |t
rt| |d  t|dkrht|d | _	n| j| _	t|dd| _| jj| _| jj| _d S t|}t| |d  t|dd| _| jj	| _	| jj| _| jj| _d S )Nr   r%   F)r   )r4   r   rj   r   r   r   r(   r   r8   rH   tupler"   r   r   r$   )rV   r   r#   r   r   r	   r   g  s(    




zIPv4Interface.__init__c             C   s   d|  | j| jjf S )Nz%s/%d)r{   r(   r   ra   )rV   r   r   r	   r|     s    zIPv4Interface.__str__c             C   sB   t | |}|r|tkr|S y| j|jkS  tk
r<   dS X d S )NF)r   rv   rQ   r   rI   )rV   ru   address_equalr   r   r	   rv     s    zIPv4Interface.__eq__c             C   sN   t | |}|tkrtS y| j|jk p4| j|jko4|S  tk
rH   dS X d S )NF)r   rw   rQ   r   rI   )rV   ru   address_lessr   r   r	   rw     s    zIPv4Interface.__lt__c             C   s   | j | jA t| jjA S )N)r(   rH   rj   r   rJ   )rV   r   r   r	   r     s    zIPv4Interface.__hash__c             C   s
   t | jS )N)r   r(   )rV   r   r   r	   r-     s    zIPv4Interface.ipc             C   s   d|  | j| jf S )Nz%s/%s)r{   r(   rH   )rV   r   r   r	   r     s    zIPv4Interface.with_prefixlenc             C   s   d|  | j| jf S )Nz%s/%s)r{   r(   r   )rV   r   r   r	   r     s    zIPv4Interface.with_netmaskc             C   s   d|  | j| jf S )Nz%s/%s)r{   r(   r   )rV   r   r   r	   r     s    zIPv4Interface.with_hostmaskN)r   r   r   r   r|   rv   rw   r   rT   rp   rr   r-   r   r   r   r   r   r   r	   r   e  s   r   c               @   s.   e Zd ZeZdddZee dd Z	dS )r   Tc             C   s   t | | t|ttfr&|}| j}n^t|trT|d }t|dkrL|d n| j}n0t|}| 	|d }t|dkr~|d n| j}t
|| _| |\| _| _t| j}|t| j@ |kr|rtd|  nt
|t| j@ | _| j| jd kr| j| _d S )Nr   r%   r   z%s has host bits set)rN   r   r4   rj   r   r8   r   r"   r$   rl   r   rJ   r   r   rH   r   r   r   )rV   r   r   r#   r   argsr   r   r   r	   r     s(    %


zIPv4Network.__init__c             C   s&   | j tdko| jtdk o$| j S )Nz100.64.0.0/10)rJ   r   rC   r   )rV   r   r   r	   r   
  s    
zIPv4Network.is_globalN)T)
r   r   r   r   r   r   rr   r   r   r   r   r   r   r	   r     s   
Cr   c               @   s   e Zd ZedZedZedZedZededededededed	ed
ededededededgZedZ	e
dZdS )_IPv4Constantsz169.254.0.0/16z127.0.0.0/8z224.0.0.0/4z100.64.0.0/10z	0.0.0.0/8z
10.0.0.0/8z172.16.0.0/12z192.0.0.0/29z192.0.0.170/31z192.0.2.0/24z192.168.0.0/16z198.18.0.0/15z198.51.100.0/24z203.0.113.0/24z240.0.0.0/4z255.255.255.255/32z0.0.0.0N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r     s(   
r   c               @   s   e Zd ZdZdZde d ZdZedZ	eZ
i Zedd Zed	d
 Zedd Zedd ZedddZdd Zdd Zedd Zedd ZdS )_BaseV6r   r3   r   r%   rc   Z0123456789ABCDEFabcdefc             C   sl   || j krbt|tr<|}d|  kr.| jksFn | | n
| |}t| |}||f| j |< | j | S )Nr   )r   r4   rj   r8   rf   rk   r   rb   )r`   r   ra   r   r   r   r	   r   O  s    	


z_BaseV6._make_netmaskc          
   C   s  |st d|d}d}t||k r:d||f }t |d|d kryt| j}W n4 t k
r } zt d||f d W d d }~X Y nX |d|d	? d
@   |d|d
@   | jd }t||krd|d |f }t |d }x@tdt|d D ]*}	||	 s|d k	r"d| }t ||	}qW |d k	r|}
t|| d }|d sp|
d8 }
|
rpd}t || |d s|d8 }|rd}t || | j|
|  }|dk r6d}t || jd |f njt|| jkrd}t || j|f |d sd}t || |d s&d}t || t|}
d}d}ytd}x,t|
D ] }	|d	K }|| 	||	 O }qFW |d	| K }x0t| dD ] }	|d	K }|| 	||	 O }qW |S  t
k
r } zt d||f d W d d }~X Y nX d S )NzAddress cannot be empty:r   z At least %d parts expected in %rr   rF   z%s in %rz%xr   i  r%   z!At most %d colons permitted in %rz At most one '::' permitted in %rr   z0Leading ':' only permitted as part of '::' in %rz1Trailing ':' only permitted as part of '::' in %rz/Expected at most %d other parts with '::' in %rz,Exactly %d parts expected without '::' in %r)r   r!   r"   r   r=   r(   r@   _HEXTET_COUNTr   _parse_hextetr   )r`   rm   r   Z
_min_partsr]   Zipv4_intr   Z
_max_partsZ
skip_indexiZparts_hiZparts_loZparts_skippedrd   r   r   r	   rl   c  s    
$







z_BaseV6._ip_int_from_stringc             C   s>   | j |std| t|dkr4d}t|| t|dS )NzOnly hex digits permitted in %rr   z$At most 4 characters permitted in %rr   )_HEX_DIGITSri   r   r"   rj   )r`   Z
hextet_strr]   r   r   r	   r     s    z_BaseV6._parse_hextetc       	      C   s   d}d}d}d}xJt |D ]>\}}|dkrP|d7 }|dkr>|}||krX|}|}qd}d}qW |dkr|| }|t|kr|dg7 }dg|||< |dkrdg| }|S )NrF   r   r   r%    )r   r"   )	r`   hextetsZbest_doublecolon_startZbest_doublecolon_lenZdoublecolon_startZdoublecolon_lenindexZhextetZbest_doublecolon_endr   r   r	   _compress_hextets  s.    

z_BaseV6._compress_hextetsNc                sZ   |d krt | j}|| jkr$tdd|   fddtdddD }| |}d|S )	NzIPv6 address is too largez%032xc                s&   g | ]}d t  ||d  d qS )z%xr   r   )rj   )r   r   )hex_strr   r	   r   *  s    z/_BaseV6._string_from_ip_int.<locals>.<listcomp>r   r   r   r   )rj   r(   r9   r   r   r   r   )r`   rd   r   r   )r   r	   r{     s    


z_BaseV6._string_from_ip_intc                s   t | trt| j}nt | tr,t| j}nt| }| |}d|   fddtdddD }t | ttfrdd	|| j
f S d	|S )	Nz%032xc                s   g | ]} ||d   qS )r   r   )r   r   )r   r   r	   r   B  s    z8_BaseV6._explode_shorthand_ip_string.<locals>.<listcomp>r   r   r   z%s/%dr   )r4   r   r    rJ   r   r-   rl   r   rN   r   rH   )rV   rm   rd   r   r   )r   r	   rU   /  s    



z$_BaseV6._explode_shorthand_ip_stringc             C   s&   | j d d d dd}d|d S )NrF   r   r   r   z	.ip6.arpa)rW   replacer   )rV   Zreverse_charsr   r   r	   rY   G  s    z_BaseV6._reverse_pointerc             C   s   | j S )N)r8   )rV   r   r   r	   r   P  s    z_BaseV6.max_prefixlenc             C   s   | j S )N)rG   )rV   r   r   r	   r7   T  s    z_BaseV6.version)N)r   r   r   rq   rG   
IPV6LENGTHr9   r   r   r   r8   r   rs   r   rl   r   r   r{   rU   rY   rr   r   r7   r   r   r   r	   r   ;  s"   	h0	r   c               @   s   e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	edd Z
ee dd Zedd Zedd Zedd Zedd Zedd Zedd ZdS )r   )r(   r   c             C   sr   t |tr| | || _d S t |trF| |d t|d| _d S t|}d|krbtd| | 	|| _d S )Nr   r   r   zUnexpected '/' in %r)
r4   rj   r^   r(   r   r_   r   r    r   rl   )rV   r   r   r   r   r	   r   _  s    


zIPv6Address.__init__c             C   s
   t | jS )N)r   r(   )rV   r   r   r	   r     s    zIPv6Address.packedc             C   s   | | j jkS )N)r   r   )rV   r   r   r	   r     s    	zIPv6Address.is_multicastc                s   t  fdd jjD S )Nc             3   s   | ]} |kV  qd S )Nr   )r   r   )rV   r   r	   r     s    z*IPv6Address.is_reserved.<locals>.<genexpr>)r   r   _reserved_networks)rV   r   )rV   r	   r     s    	zIPv6Address.is_reservedc             C   s   | | j jkS )N)r   r   )rV   r   r   r	   r     s    zIPv6Address.is_link_localc             C   s   | | j jkS )N)r   _sitelocal_network)rV   r   r   r	   is_site_local  s    zIPv6Address.is_site_localc                s   t  fdd jjD S )Nc             3   s   | ]} |kV  qd S )Nr   )r   r:   )rV   r   r	   r     s    z)IPv6Address.is_private.<locals>.<genexpr>)r   r   r   )rV   r   )rV   r	   r     s    
zIPv6Address.is_privatec             C   s   | j  S )N)r   )rV   r   r   r	   r     s    	zIPv6Address.is_globalc             C   s
   | j dkS )Nr   )r(   )rV   r   r   r	   r     s    	zIPv6Address.is_unspecifiedc             C   s
   | j dkS )Nr%   )r(   )rV   r   r   r	   r     s    	zIPv6Address.is_loopbackc             C   s    | j d? dkrd S t| j d@ S )Nr   i  l    )r(   r   )rV   r   r   r	   ipv4_mapped  s    	zIPv6Address.ipv4_mappedc             C   s4   | j d? dkrd S t| j d? d@ t| j  d@ fS )N`   i   @   l    )r(   r   )rV   r   r   r	   teredo  s    
zIPv6Address.teredoc             C   s$   | j d? dkrd S t| j d? d@ S )Np   i   P   l    )r(   r   )rV   r   r   r	   	sixtofour  s    	zIPv6Address.sixtofourN)r   r   r   rq   r   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r   Y  s   $
r   c               @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zej	Z	e
dd Ze
dd Ze
dd Ze
dd Ze
dd Ze
dd ZdS )r   c             C   s   t |ttfr2t| | t| j| _| j| _	d S t |t
rt| |d  t|dkrht|d | _	n| j| _	t|dd| _| jj| _| jj| _d S t|}t| |d  t|dd| _| jj| _| jj	| _	| jj| _d S )Nr   r%   F)r   )r4   r   rj   r   r   r   r(   r   r8   rH   r   r"   r   r   r$   )rV   r   r#   r   r   r	   r     s(    




zIPv6Interface.__init__c             C   s   d|  | j| jjf S )Nz%s/%d)r{   r(   r   ra   )rV   r   r   r	   r|   '  s    zIPv6Interface.__str__c             C   sB   t | |}|r|tkr|S y| j|jkS  tk
r<   dS X d S )NF)r   rv   rQ   r   rI   )rV   ru   r   r   r   r	   rv   +  s    zIPv6Interface.__eq__c             C   sN   t | |}|tkrtS y| j|jk p4| j|jko4|S  tk
rH   dS X d S )NF)r   rw   rQ   r   rI   )rV   ru   r   r   r   r	   rw   7  s    zIPv6Interface.__lt__c             C   s   | j | jA t| jjA S )N)r(   rH   rj   r   rJ   )rV   r   r   r	   r   C  s    zIPv6Interface.__hash__c             C   s
   t | jS )N)r   r(   )rV   r   r   r	   r-   H  s    zIPv6Interface.ipc             C   s   d|  | j| jf S )Nz%s/%s)r{   r(   rH   )rV   r   r   r	   r   L  s    zIPv6Interface.with_prefixlenc             C   s   d|  | j| jf S )Nz%s/%s)r{   r(   r   )rV   r   r   r	   r   Q  s    zIPv6Interface.with_netmaskc             C   s   d|  | j| jf S )Nz%s/%s)r{   r(   r   )rV   r   r   r	   r   V  s    zIPv6Interface.with_hostmaskc             C   s   | j dko| jjS )Nr   )r(   r   r   )rV   r   r   r	   r   [  s    zIPv6Interface.is_unspecifiedc             C   s   | j dko| jjS )Nr%   )r(   r   r   )rV   r   r   r	   r   _  s    zIPv6Interface.is_loopbackN)r   r   r   r   r|   rv   rw   r   rT   rp   rr   r-   r   r   r   r   r   r   r   r   r	   r     s   r   c               @   s.   e Zd ZeZd	ddZdd Zedd ZdS )
r   Tc             C   s   t | | t|ttfr&|}| j}n^t|trT|d }t|dkrL|d n| j}n0t|}| 	|d }t|dkr~|d n| j}t
|| _| |\| _| _t| j}|t| j@ |kr|rtd|  nt
|t| j@ | _| j| jd kr| j| _d S )Nr   r%   r   z%s has host bits set)rN   r   r4   rj   r   r8   r   r"   r$   rl   r   rJ   r   r   rH   r   r   r   )rV   r   r   r#   r   r   r   r   r   r	   r   t  s(     


zIPv6Network.__init__c             c   s@   t | j}t | j}x&t|d |d D ]}| |V  q(W d S )Nr%   )rj   rJ   rC   r   r   )rV   r   r   r   r   r   r	   r     s    

zIPv6Network.hostsc             C   s   | j jo| jjS )N)rJ   r   rC   )rV   r   r   r	   r     s    zIPv6Network.is_site_localN)T)	r   r   r   r   r   r   r   rr   r   r   r   r   r	   r   d  s   
>r   c               @   s   e Zd ZedZedZededededededed	ed
ededg
ZedededededededededededededededgZedZdS )_IPv6Constantsz	fe80::/10zff00::/8z::1/128z::/128z::ffff:0:0/96z100::/64z	2001::/23z2001:2::/48z2001:db8::/32z2001:10::/28zfc00::/7z::/8z100::/8z200::/7z400::/6z800::/5z1000::/4z4000::/3z6000::/3z8000::/3zA000::/3zC000::/3zE000::/4zF000::/5zF800::/6zFE00::/9z	fec0::/10N)	r   r   r   r   r   r   r   r   r   r   r   r   r	   r     s*   

r   )T)"__version__r   r   r   r   r   r
   r   r   r   r   r   r$   r.   r2   r;   rE   rM   rS   rT   total_orderingr5   rN   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   <module>   sT   
#716 :   V GuSa    5Wj!