3 �Pf09�@sldZddlmZddlZddlZdZefZy eefZWne k rJYnXye Z Wn$e k rxe Z e e kstt�YnXdOdkr�dd�Zndd�Zy ejZWnek r�d d �ZYnXd d �Zeed �r�dd�Zndd�ZdPdd�ZGdd�de�ZdZdZGdd�de�ZGdd�de�Zdd�ZdQdd �Zd!d"�Z d#d$�Z!d%d&�Z"d'd(�Z#d)d*�Z$d+d,�Z%d-d.�Z&d/d0�Z'd1d2�Z(d3d4�Z)Gd5d6�d6e�Z*Gd7d8�d8e*�Z+Gd9d:�d:e*�Z,Gd;d<�d�d>e-e+�Z.Gd?d@�d@e.�Z/GdAdB�dBe-e,�Z0GdCdD�dDe�Z1e1e._2GdEdF�dFe�Z3GdGdH�dHe3e+�Z4GdIdJ�dJe4�Z5GdKdL�dLe3e,�Z6GdMdN�dNe�Z7e7e4_2dS)Rz�A fast, lightweight IPv4/IPv6 manipulation library in Python. This library is used to create/poke/manipulate IPv4 and IPv6 addresses and networks. �)�unicode_literalsNz1.0.17�cCs|S)N�)�bytrr�/usr/lib/python3.6/ipaddress.py�_compat_bytes_to_byte_valssrcCsdd�|D�S)NcSsg|]}tjd|�d�qS)s!Br)�struct�unpack)�.0�brrr� #sz._compat_bytes_to_byte_vals..r)rrrrr"scCs<|dks t�d}x&|D]}t|t�s(t�|d>|}qW|S)N�bigr�)�AssertionError� isinstance�_compat_int_types)Zbytvals� endianess�resZbvrrr�_compat_int_from_byte_vals's   rcCs�t|t�st�|dkst�|dkrH|dks2|dkrr?)r@�strictrrr� ip_network�s   rEcCsTyt|�Sttfk r YnXyt|�Sttfk rBYnXtd|��dS)agTake an IP string/int and return an object of the correct type. Args: address: A string or integer, the IP address. Either IPv4 or IPv6 addresses may be supplied; integers less than 2**32 will be considered to be IPv4 by default. Returns: An IPv4Interface or IPv6Interface object. Raises: ValueError: if the string passed isn't either a v4 or a v6 address. Notes: The IPv?Interface classes describe an Address on a particular Network, so they're basically a combination of both the Address and Network classes. z2%r does not appear to be an IPv4 or IPv6 interfaceN)� IPv4Interfacer9r;� IPv6Interfacer?)r@rrr� ip_interface�srHc Cs4y t|dd�Stjtfk r.td��YnXdS)a`Represent an address as 4 packed bytes in network (big-endian) order. Args: address: An integer representation of an IPv4 IP address. Returns: The integer address packed as 4 bytes in network (big-endian) order. Raises: ValueError: If the integer is negative or too large to be an IPv4 IP address. rr z&Address negative or too large for IPv4N)rrr� OverflowErrorr?)r@rrr�v4_int_to_packed�s rJc Cs4y t|dd�Stjtfk r.td��YnXdS)z�Represent an address as 16 packed bytes in network (big-endian) order. Args: address: An integer representation of an IPv6 IP address. Returns: The integer address packed as 16 bytes in network (big-endian) order. rr z&Address negative or too large for IPv6N)rrrrIr?)r@rrr�v6_int_to_packeds  rKcCs*t|�jd�}t|�dkr&td|��|S)zAHelper to split the netmask and raise AddressValueError if needed�/rzOnly one '/' permitted in %r)� _compat_str�split�lenr9)r@�addrrrr�_split_optional_netmasks  rQccsRt|�}t|�}}x.|D]&}|j|jdkr<||fV|}|}qW||fVdS)z�Find a sequence of sorted deduplicated IPv#Address. Args: addresses: a list of IPv#Address objects. Yields: A tuple containing the first and last IP addresses in the sequence. r$N)�iter�next�_ip)� addresses�it�first�last�iprrr�_find_address_ranges    rZcCs$|dkr |St|t||d@��S)z�Count the number of zero bits on the right hand side. Args: number: an integer. bits: maximum number of bits to count. Returns: The number of zero bits on the right hand side of the number. rr$)�minr!)Znumber�bitsrrr�_count_righthand_zero_bits0s r]ccs�t|t�ot|t�std��|j|jkr8td||f��||krHtd��|jdkrXt}n|jdkrht}ntd��|j}|j}|j}x^||kr�t t ||�t ||d�d�}||||f�}|V|d|>7}|d|j kr�Pq�WdS) a�Summarize a network range given the first and last IP addresses. Example: >>> list(summarize_address_range(IPv4Address('192.0.2.0'), ... IPv4Address('192.0.2.130'))) ... #doctest: +NORMALIZE_WHITESPACE [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')] Args: first: the first IPv4Address or IPv6Address in the range. last: the last IPv4Address or IPv6Address in the range. Returns: An iterator of the summarized IPv(4|6) network objects. Raise: TypeError: If the first and last objects are not IP addresses. If the first and last objects are not the same version. ValueError: If the last object is not greater than the first. If the version of the first address is not 4 or 6. z1first 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�zunknown IP versionr$N) r� _BaseAddress� TypeError�versionr?rBrC�_max_prefixlenrTr[r]r!� _ALL_ONES)rWrXrYZip_bitsZ first_intZlast_intZnbits�netrrr�summarize_address_range@s0         reccs�t|�}i}xL|rX|j�}|j�}|j|�}|dkr>|||<q||kr||=|j|�qWd}x4t|j��D]$}|dk r�|j|jkr�ql|V|}qlWdS)auLoops through the addresses, collapsing concurrent netblocks. Example: ip1 = IPv4Network('192.0.2.0/26') ip2 = IPv4Network('192.0.2.64/26') ip3 = IPv4Network('192.0.2.128/26') ip4 = IPv4Network('192.0.2.192/26') _collapse_addresses_internal([ip1, ip2, ip3, ip4]) -> [IPv4Network('192.0.2.0/24')] This shouldn't be called directly; it is called via collapse_addresses([]). Args: addresses: A list of IPv4Network's or IPv6Network's Returns: A list of IPv4Network's or IPv6Network's depending on what we were passed. N)�list�pop�supernet�get�append�sorted�values�broadcast_address)rUZto_merge�subnetsrdrhZexistingrXrrr�_collapse_addresses_internalws$   roc Cs8g}g}g}x�|D]�}t|t�rT|rH|dj|jkrHtd||df��|j|�q|j|jkr�|r�|dj|jkr�td||df��y|j|j�Wq�tk r�|j|j �Yq�Xq|r�|dj|jkr�td||df��|j|�qWt t |��}|�r,x&t |�D]\}}|j t||���qWt||�S) a�Collapse a list of IP objects. Example: collapse_addresses([IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]) -> [IPv4Network('192.0.2.0/24')] Args: addresses: An iterator of IPv4Network or IPv6Network objects. Returns: An iterator of the collapsed IPv(4|6)Network objects. Raises: TypeError: If passed a list of mixed version objects. r$z%%s and %s are not of the same version�����rprprprprp)rr_�_versionr`rj� _prefixlenrbrY�AttributeError�network_addressrk�setrZ�extendrero)rUZaddrsZipsZnetsrYrWrXrrr�collapse_addresses�s4     rwcCs(t|t�r|j�St|t�r$|j�StS)a2Return a key suitable for sorting between networks and addresses. Address and Network objects are not sortable by default; they're fundamentally different so the expression IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24') doesn't make any sense. There are some times however, where you may wish to have ipaddress sort these for you anyway. If you need to do this, you can use this function as the key= argument to sorted(). Args: obj: either a Network or Address object. Returns: appropriate key. )r� _BaseNetwork�_get_networks_keyr_�_get_address_keyr-)�objrrr�get_mixed_type_key�s   r|c@s�eZdZdZfZedd��Zedd��Zedd��Zedd ��Z d d �Z d d �Z e dd��Z e dd��Ze dd��Ze dd��Ze dd��Zdd�ZdS)�_IPAddressBasezThe mother class.cCs|j�S)z:Return the longhand version of the IP address as a string.)�_explode_shorthand_ip_string)r*rrr�exploded�sz_IPAddressBase.explodedcCst|�S)z;Return the shorthand version of the IP address as a string.)rM)r*rrr� compressedsz_IPAddressBase.compressedcCs|j�S)aIThe name of the reverse DNS pointer for the IP address, e.g.: >>> ipaddress.ip_address("127.0.0.1").reverse_pointer '1.0.0.127.in-addr.arpa' >>> ipaddress.ip_address("2001:db8::1").reverse_pointer '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa' )�_reverse_pointer)r*rrr�reverse_pointers z_IPAddressBase.reverse_pointercCsdt|�f}t|��dS)Nz%200s has no version specified)�typer)r*�msgrrrrasz_IPAddressBase.versioncCsF|dkrd}t|||jf��||jkrBd}t|||j|jf��dS)Nrz-%d (< 0) is not permitted as an IPv%d addressz2%d (>= 2**%d) is not permitted as an IPv%d address)r9rqrcrb)r*r@r�rrr�_check_int_addresss  z!_IPAddressBase._check_int_addresscCs.t|�}||kr*d}t|||||jf��dS)Nz~%r (len %d != %d) is not permitted as an IPv%d address. Did you pass in a bytes (str in Python 2) instead of a unicode object?)rOr9rq)r*r@Z expected_lenZ address_lenr�rrr�_check_packed_address s z$_IPAddressBase._check_packed_addresscCs|j|j|?AS)z�Turn the prefix length into a bitwise netmask Args: prefixlen: An integer, the prefix length. Returns: An integer. )rc)�cls� prefixlenrrr�_ip_int_from_prefix+s z"_IPAddressBase._ip_int_from_prefixc Cs\t||j�}|j|}||?}d|>d}||krX|jd}t||d�}d}t||��|S)aReturn prefix length from the bitwise netmask. Args: ip_int: An integer, the netmask in expanded bitwise format Returns: An integer, the prefix length. Raises: ValueError: If the input intermingles zeroes & ones r$rr z&Netmask pattern %r mixes zeroes & ones)r]rbrr?) r��ip_intZtrailing_zeroesr�Z leading_onesZall_onesZbyteslenZdetailsr�rrr�_prefix_from_ip_int8s      z"_IPAddressBase._prefix_from_ip_intcCsd|}t|��dS)Nz%r is not a valid netmask)r;)r�Z netmask_strr�rrr�_report_invalid_netmaskQsz&_IPAddressBase._report_invalid_netmaskc Csjtjj|�s|j|�y t|�}Wntk r@|j|�YnXd|koV|jknsf|j|�|S)a Return prefix length from a numeric string Args: prefixlen_str: The string to be converted Returns: An integer, the prefix length. Raises: NetmaskValueError: If the input is not a valid netmask r)�_BaseV4�_DECIMAL_DIGITS� issupersetr��intr?rb)r�Z prefixlen_strr�rrr�_prefix_from_prefix_stringVs    z)_IPAddressBase._prefix_from_prefix_stringcCs�y|j|�}Wntk r,|j|�YnXy |j|�Stk rLYnX||jN}y |j|�Stk r�|j|�YnXdS)aTurn a netmask/hostmask string into a prefix length Args: ip_str: The netmask/hostmask to be converted Returns: An integer, the prefix length. Raises: NetmaskValueError: If the input is not a valid netmask/hostmask N)�_ip_int_from_stringr9r�r�r?rc)r��ip_strr�rrr�_prefix_from_ip_stringos   z%_IPAddressBase._prefix_from_ip_stringcCs|jt|�ffS)N)� __class__rM)r*rrr� __reduce__�sz_IPAddressBase.__reduce__N)r5r6r7r:r8�propertyrr�r�rar�r�� classmethodr�r�r�r�r�r�rrrrr}�s        "r}c@sdeZdZdZfZdd�Zdd�Zdd�Zdd �Zd d �Z d d �Z dd�Z dd�Z dd�Z dd�ZdS)r_z�A generic IP object. This IP class contains the version independent methods which are used by single IP addresses. cCs|jS)N)rT)r*rrr�__int__�sz_BaseAddress.__int__c Cs2y|j|jko|j|jkStk r,tSXdS)N)rTrqrsr-)r*r+rrrr,�s   z_BaseAddress.__eq__cCs`t|t�stSt|t�s(td||f��|j|jkrDtd||f��|j|jkr\|j|jkSdS)Nz"%s and %s are not of the same typez%%s and %s are not of the same versionF)rr}r-r_r`rqrT)r*r+rrrr0�s       z_BaseAddress.__lt__cCs t|t�stS|jt|�|�S)N)rrr-r�r�)r*r+rrr�__add__�s z_BaseAddress.__add__cCs t|t�stS|jt|�|�S)N)rrr-r�r�)r*r+rrr�__sub__�s z_BaseAddress.__sub__cCsd|jjt|�fS)Nz%s(%r))r�r5rM)r*rrr�__repr__�sz_BaseAddress.__repr__cCst|j|j��S)N)rM�_string_from_ip_intrT)r*rrr�__str__�sz_BaseAddress.__str__cCsttt|j���S)N)�hash�hexr�rT)r*rrr�__hash__�sz_BaseAddress.__hash__cCs |j|fS)N)rq)r*rrrrz�sz_BaseAddress._get_address_keycCs|j|jffS)N)r�rT)r*rrrr��sz_BaseAddress.__reduce__N)r5r6r7r:r8r�r,r0r�r�r�r�r�rzr�rrrrr_�sr_c@sXeZdZdZdd�Zdd�Zdd�Zdd �Zd d �Zd d �Z dd�Z dd�Z dd�Z dd�Z dd�Zedd��Zedd��Zedd��Zedd��Zed d!��Zed"d#��Zed$d%��Zed&d'��Zd(d)�Zd*d+�Zd,d-�ZdFd0d1�ZdGd2d3�Zed4d5��Zd6d7�Zd8d9�Zed:d;��Z edd?��Z"ed@dA��Z#edBdC��Z$edDdE��Z%d/S)Hrxz~A generic IP network object. This IP class contains the version independent methods which are used by networks. cCs i|_dS)N)�_cache)r*r@rrr�__init__�sz_BaseNetwork.__init__cCsd|jjt|�fS)Nz%s(%r))r�r5rM)r*rrrr��sz_BaseNetwork.__repr__cCsd|j|jfS)Nz%s/%d)rtr�)r*rrrr��sz_BaseNetwork.__str__ccs<t|j�}t|j�}x"t|d|�D]}|j|�Vq$WdS)z�Generate Iterator over usable hosts in a network. This is like __iter__ except it doesn't return the network or broadcast addresses. r$N)r�rtrmr(�_address_class)r*�network� broadcast�xrrr�hosts�s  z_BaseNetwork.hostsccs<t|j�}t|j�}x"t||d�D]}|j|�Vq$WdS)Nr$)r�rtrmr(r�)r*r�r�r�rrr�__iter__�s  z_BaseNetwork.__iter__cCslt|j�}t|j�}|dkr>|||kr0td��|j||�S|d7}|||krZtd��|j||�SdS)Nrzaddress out of ranger$)r�rtrm� IndexErrorr�)r*�nr�r�rrr� __getitem__�s    z_BaseNetwork.__getitem__cCsxt|t�stSt|t�s(td||f��|j|jkrDtd||f��|j|jkr\|j|jkS|j|jkrt|j|jkSdS)Nz"%s and %s are not of the same typez%%s and %s are not of the same versionF)rr}r-rxr`rqrt�netmask)r*r+rrrr0s         z_BaseNetwork.__lt__c CsFy,|j|jko,|j|jko,t|j�t|j�kStk r@tSXdS)N)rqrtr�r�rsr-)r*r+rrrr,s   z_BaseNetwork.__eq__cCstt|j�t|j�A�S)N)r�r�rtr�)r*rrrr�sz_BaseNetwork.__hash__cCsL|j|jkrdSt|t�rdSt|j�t|j�koBt|j�kSSdS)NF)rqrrxr�rtrTrm)r*r+rrr� __contains__s   z_BaseNetwork.__contains__cCs(|j|kp&|j|kp&|j|kp&|j|kS)z*Tell if self is partly contained in other.)rtrm)r*r+rrr�overlaps)s   z_BaseNetwork.overlapscCs<|jjd�}|dkr8|jt|j�t|j�B�}||jd<|S)Nrm)r�rir�r�rt�hostmask)r*r�rrrrm0s    z_BaseNetwork.broadcast_addresscCs8|jjd�}|dkr4|jt|j�|jA�}||jd<|S)Nr�)r�rir�r�r�rc)r*r�rrrr�9s   z_BaseNetwork.hostmaskcCsd|j|jfS)Nz%s/%d)rtrr)r*rrr�with_prefixlenAsz_BaseNetwork.with_prefixlencCsd|j|jfS)Nz%s/%s)rtr�)r*rrr� with_netmaskEsz_BaseNetwork.with_netmaskcCsd|j|jfS)Nz%s/%s)rtr�)r*rrr� with_hostmaskIsz_BaseNetwork.with_hostmaskcCst|j�t|j�dS)z&Number of hosts in the current subnet.r$)r�rmrt)r*rrr� num_addressesMsz_BaseNetwork.num_addressescCsdt|�f}t|��dS)Nz%%200s has no associated address class)r�r)r*r�rrrr�Rsz_BaseNetwork._address_classcCs|jS)N)rr)r*rrrr�Zsz_BaseNetwork.prefixlenccs|j|jkstd||f��t|t�s2td|��|j|�sLtd||f��||krXdS|jd|j|jf�}|j �\}}xb||kr�||kr�|j|�r�|V|j �\}}q||j|�r�|V|j �\}}q|t d|||f��q|W||kr�|Vn$||k�r|Vnt d|||f��dS)a�Remove an address from a larger block. For example: addr1 = ip_network('192.0.2.0/28') addr2 = ip_network('192.0.2.1/32') list(addr1.address_exclude(addr2)) = [IPv4Network('192.0.2.0/32'), IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.4/30'), IPv4Network('192.0.2.8/29')] or IPv6: addr1 = ip_network('2001:db8::1/32') addr2 = ip_network('2001:db8::1/128') list(addr1.address_exclude(addr2)) = [ip_network('2001:db8::1/128'), ip_network('2001:db8::2/127'), ip_network('2001:db8::4/126'), ip_network('2001:db8::8/125'), ... ip_network('2001:db8:8000::/33')] Args: other: An IPv4Network or IPv6Network object of the same type. Returns: An iterator of the IPv(4|6)Network objects which is self minus other. Raises: TypeError: If self and other are of differing address versions, or if other is not a network object. ValueError: If other is not completely contained by self. z%%s and %s are not of the same versionz%s is not a network objectz%s not contained in %sNz%s/%sz3Error performing exclusion: s1: %s s2: %s other: %s) rqr`rrx� subnet_ofr?r�rtr�rnr)r*r+�s1�s2rrr�address_exclude^s6$           z_BaseNetwork.address_excludecCs`|j|jkrtd||f��|j|jkr,dS|j|jkr other eg: IPv4Network('192.0.2.128/25') > IPv4Network('192.0.2.0/25') IPv6Network('2001:db8::2000/124') > IPv6Network('2001:db8::1000/124') Raises: TypeError if the IP versions are different. z"%s and %s are not of the same typer$rrprp)rqr`rtr�)r*r+rrr�compare_networks�s!      z_BaseNetwork.compare_networkscCs|j|j|jfS)z�Network-only key function. Returns an object that identifies this address' network and netmask. This function is a suitable "key" argument for sorted() and list.sort(). )rqrtr�)r*rrrry�sz_BaseNetwork._get_networks_keyr$Nc cs�|j|jkr|VdS|dk rJ||jkr0td��|dkr@td��||j}|dkrZtd��|j|}||jkr~td||f��t|j�}t|j�d}t|j�d|?}x(t|||�D]}|j||f�}|Vq�WdS)a�The subnets which join to make the current subnet. In the case that self contains only one IP (self._prefixlen == 32 for IPv4 or self._prefixlen == 128 for IPv6), yield an iterator with just ourself. Args: prefixlen_diff: An integer, the amount the prefix length should be increased by. This should not be set if new_prefix is also set. new_prefix: The desired new prefix length. This must be a larger number (smaller prefix) than the existing prefix. This should not be set if prefixlen_diff is also set. Returns: An iterator of IPv(4|6) objects. Raises: ValueError: The prefixlen_diff is too small or too large. OR prefixlen_diff and new_prefix are both set or new_prefix is a smaller number than the current prefix (smaller number means a larger network) Nznew prefix must be longerr$z(cannot set prefixlen_diff and new_prefixrzprefix length diff must be > 0z0prefix length diff %d is invalid for netblock %s) rrrbr?r�rtrmr�r(r�) r*�prefixlen_diff� new_prefix� new_prefixlenr%r&r'Znew_addrZcurrentrrrrn�s,       z_BaseNetwork.subnetscCs�|jdkr|S|dk rB||jkr(td��|dkr8td��|j|}|j|}|dkrftd|j|f��|jt|j�t|j�|>@|f�S)a�The supernet containing the current network. Args: prefixlen_diff: An integer, the amount the prefix length of the network should be decreased by. For example, given a /24 network and a prefixlen_diff of 3, a supernet with a /21 netmask is returned. Returns: An IPv4 network object. Raises: ValueError: If self.prefixlen - prefixlen_diff < 0. I.e., you have a negative prefix length. OR If prefixlen_diff and new_prefix are both set or new_prefix is a larger number than the current prefix (larger number means a smaller network) rNznew prefix must be shorterr$z(cannot set prefixlen_diff and new_prefixz;current prefixlen is %d, cannot have a prefixlen_diff of %d)rrr?r�r�r�rtr�)r*r�r�r�rrrrhs     z_BaseNetwork.supernetcCs|jjo|jjS)z�Test if the address is reserved for multicast use. Returns: A boolean, True if the address is a multicast address. See RFC 2373 2.7 for details. )rt� is_multicastrm)r*rrrr�As z_BaseNetwork.is_multicastcCsP|j|jkrdSt|d�r 255) not permitted)r?r�r�rOr�)r�Z octet_strr�Z octet_intrrrr��s       z_BaseV4._parse_octetcCsdjdd�t|dd�D��S)z�Turns a 32-bit integer into dotted decimal notation. Args: ip_int: An integer, the IP address. Returns: The IP address as a string in dotted decimal notation. r�css0|](}tt|t�r"tjd|�dn|�VqdS)s!BrN)rMrr>rr )r r rrr� -sz._BaseV4._string_from_ip_int..rr )�joinr)r�r�rrrr�"s z_BaseV4._string_from_ip_intc sh|jd�}y�fdd�tt|�D�}Wntk r:dSXt|�t|�krPdS|d|dkrddSdS) z�Test if the IP string is a hostmask (rather than a netmask). Args: ip_str: A string, the potential hostmask. Returns: A boolean, True if the IP string is a hostmask. r�csg|]}|�jkr|�qSr)�_valid_mask_octets)r r�)r*rrr >sz(_BaseV4._is_hostmask..Frr$Trp)rNr�r�r?rO)r*r�r\�partsr)r*r� _is_hostmask2s z_BaseV4._is_hostmaskcCs&t|�jd�ddd�}dj|�dS)z�Return the reverse DNS pointer name for the IPv4 address. This implements the method described in RFC1035 3.5. r�Nr$z .in-addr.arparp)rMrNr�)r*Zreverse_octetsrrrr�Gsz_BaseV4._reverse_pointercCs|jS)N)rb)r*rrr� max_prefixlenPsz_BaseV4.max_prefixlencCs|jS)N)rq)r*rrrraTsz_BaseV4.versionN)r5r6r7r:r8rq� IPV4LENGTHrc� frozensetr�r�rbr�r~r�r�r�r�r�r�r�r�r�rarrrrr��s"    %  r�c@s|eZdZdZdZdd�Zedd��Zedd ��Zed d ��Z ed d ��Z edd��Z edd��Z edd��Z edd��ZdS)r<z/Represent and manipulate single IPv4 Addresses.rT� __weakref__cCsxt|t�r|j|�||_dSt|t�rL|j|d�t|�}t|d�|_dSt|�}d|krht d|��|j |�|_dS)a� Args: address: A string or integer representing the IP Additionally, an integer can be passed, so IPv4Address('192.0.2.1') == IPv4Address(3221225985). or, more generally IPv4Address(int(IPv4Address('192.0.2.1'))) == IPv4Address('192.0.2.1') Raises: AddressValueError: If ipaddress isn't a valid IPv4 address. Nrr rLzUnexpected '/' in %r) rrr�rTr>r�rrrMr9r�)r*r@�bvs�addr_strrrrr�_s      zIPv4Address.__init__cCs t|j�S)z*The binary representation of this address.)rJrT)r*rrr�packed�szIPv4Address.packedcCs ||jjkS)z�Test if the address is otherwise IETF reserved. Returns: A boolean, True if the address is within the reserved IPv4 Network range. )� _constants�_reserved_network)r*rrrr��s zIPv4Address.is_reservedcst�fdd��jjD��S)z�Test if this address is allocated for private networks. Returns: A boolean, True if the address is reserved per iana-ipv4-special-registry. c3s|]}�|kVqdS)Nr)r rd)r*rrr��sz)IPv4Address.is_private..)�anyr��_private_networks)r*r)r*rr��s zIPv4Address.is_privatecCs||jjko|j S)N)r��_public_networkr�)r*rrrr��s zIPv4Address.is_globalcCs ||jjkS)z�Test if the address is reserved for multicast use. Returns: A boolean, True if the address is multicast. See RFC 3171 for details. )r��_multicast_network)r*rrrr��s zIPv4Address.is_multicastcCs ||jjkS)z�Test if the address is unspecified. Returns: A boolean, True if this is the unspecified address as defined in RFC 5735 3. )r��_unspecified_address)r*rrrr��s zIPv4Address.is_unspecifiedcCs ||jjkS)z�Test if the address is a loopback address. Returns: A boolean, True if the address is a loopback per RFC 3330. )r��_loopback_network)r*rrrr��szIPv4Address.is_loopbackcCs ||jjkS)z�Test if the address is reserved for link-local. Returns: A boolean, True if the address is link-local per RFC 3927. )r��_linklocal_network)r*rrrr��szIPv4Address.is_link_localN)rTr�)r5r6r7r:r8r�r�r�r�r�r�r�r�r�r�rrrrr<Ys$   r<c@sjeZdZdd�Zdd�Zdd�Zdd�Zd d �Zej Z e d d ��Z e d d��Z e dd��Z e dd��ZdS)rFcCs�t|ttf�r2tj||�t|j�|_|j|_ dSt|t �r�tj||d�t |�dkrht |d�|_ n|j|_ t|dd�|_|jj |_ |jj|_dSt|�}tj||d�t|dd�|_|jj |_ |jj |_ |jj|_dS)Nrr$F)rD)rr>rr<r�rBrTr�rbrr�tuplerOr�r�r�rQ)r*r@rPrrrr��s(        zIPv4Interface.__init__cCsd|j|j�|jjfS)Nz%s/%d)r�rTr�r�)r*rrrr��s zIPv4Interface.__str__c CsDtj||�}| s|tkr|Sy |j|jkStk r>dSXdS)NF)r<r,r-r�rs)r*r+� address_equalrrrr,�s  zIPv4Interface.__eq__c Cs>tj||�}|tkrtSy |j|jkStk r8dSXdS)NF)r<r0r-r�rs)r*r+� address_lessrrrr0�s  zIPv4Interface.__lt__cCs|j|jAt|jj�AS)N)rTrrr�r�rt)r*rrrr�szIPv4Interface.__hash__cCs t|j�S)N)r<rT)r*rrrrY szIPv4Interface.ipcCsd|j|j�|jfS)Nz%s/%s)r�rTrr)r*rrrr�s zIPv4Interface.with_prefixlencCsd|j|j�|jfS)Nz%s/%s)r�rTr�)r*rrrr�s zIPv4Interface.with_netmaskcCsd|j|j�|jfS)Nz%s/%s)r�rTr�)r*rrrr�s zIPv4Interface.with_hostmaskN)r5r6r7r�r�r,r0r�r}r�r�rYr�r�r�rrrrrF�s     rFc@s*eZdZdZeZddd�Zedd��ZdS) rBaeThis class represents and manipulates 32-bit IPv4 network + addresses.. Attributes: [examples for IPv4Network('192.0.2.0/27')] .network_address: IPv4Address('192.0.2.0') .hostmask: IPv4Address('0.0.0.31') .broadcast_address: IPv4Address('192.0.2.32') .netmask: IPv4Address('255.255.255.224') .prefixlen: 27 TcCs|tj||�t|ttf�rr<rtr�rbr�rrr�rOr�r?rQr�r�r�)r*r@rDr�r�rPrrrr�0sB%           zIPv4Network.__init__cCs&|jtd�ko|jtd�k o$|j S)z�Test if this address is allocated for public networks. Returns: A boolean, True if the address is not reserved per iana-ipv4-special-registry. z 100.64.0.0/10)rtrBrmr�)r*rrrr��s zIPv4Network.is_globalN)T) r5r6r7r:r<r�r�r�r�rrrrrB!s  UrBc@s�eZdZed�Zed�Zed�Zed�Zed�ed�ed�ed�ed�ed�ed �ed �ed �ed �ed �ed�ed�ed�gZed�Z e d�Z dS)�_IPv4Constantsz169.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/29z192.0.0.170/31z 192.0.2.0/24z192.168.0.0/16z 198.18.0.0/15z198.51.100.0/24z203.0.113.0/24z 240.0.0.0/4z255.255.255.255/32z0.0.0.0N) r5r6r7rBr�r�r�r�r�r�r<r�rrrrr��s( r�c@s�eZdZdZfZdZdedZdZe d�Z eZ iZ e dd��Ze d d ��Ze d d ��Ze d d��Ze ddd��Zdd�Zdd�Zedd��Zedd��ZdS)�_BaseV6zyBase IPv6 object. The following methods are used by IPv6 objects in both single IP addresses and networks. r^rr$rZ0123456789ABCDEFabcdefcCsJ||jkr@t|t�r|}n |j|�}t|j|��}||f|j|<|j|S)aMake a (netmask, prefix_len) tuple from the given argument. Argument can be: - an integer (the prefix length) - a string representing the prefix length (e.g. "24") - a string representing the prefix netmask (e.g. "255.255.255.0") )r�rrr�r=r�)r�r�r�r�rrrr��s   z_BaseV6._make_netmaskcCs�|s td��|jd�}d}t|�|kr:d||f}t|��d|dkr�yt|j��j}Wn2tk r�}ztd||f��WYdd}~XnX|jd |d ?d @�|jd |d @�|jd}t|�|kr�d |d|f}t|��d}x@tdt|�d�D]*} || s�|dk �r d |}t|��| }q�W|dk �r�|} t|�|d} |d�sn| d8} | �rnd}t||��|d�s�| d8} | �r�d}t||��|j| | } | dk�r4d}t||jd|f��njt|�|jk�r�d}t||j|f��|d�s d}t||��|d�s$d}t||��t|�} d} d} ytd} x,t | �D] } | d K} | |j || �O} �qDW| d | K} x0t | d�D] } | d K} | |j || �O} �q�W| St k �r�}ztd||f��WYdd}~XnXdS)z�Turn an IPv6 ip_str into an integer. Args: ip_str: A string, the IPv6 ip_str. Returns: An int, the IPv6 address Raises: AddressValueError: if ip_str isn't a valid IPv6 Address. zAddress cannot be empty�:r�z At least %d parts expected in %rr�r$z%s in %rNz%xri��z!At most %d colons permitted in %rz At most one '::' permitted in %rrz0Leading ':' 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 %rrprprp) r9rNrOr<rgrTrj� _HEXTET_COUNTr(�range� _parse_hextetr?)r�r�r�Z _min_partsr�Zipv4_intr�Z _max_partsZ skip_indexr Zparts_hiZparts_loZ parts_skippedr�rrrr��s�    "              z_BaseV6._ip_int_from_stringcCs>|jj|�std|��t|�dkr4d}t||��t|d�S)a&Convert an IPv6 hextet string into an integer. Args: hextet_str: A string, the number to parse. Returns: The hextet as an integer. Raises: ValueError: if the input isn't strictly a hex number from [0..FFFF]. zOnly hex digits permitted in %rrz$At most 4 characters permitted in %rr)� _HEX_DIGITSr�r?rOr�)r�Z hextet_strr�rrrr�Es     z_BaseV6._parse_hextetc Cs�d}d}d}d}xJt|�D]>\}}|dkrP|d7}|dkr>|}||krX|}|}qd}d}qW|dkr�||}|t|�kr�|dg7}dg|||�<|dkr�dg|}|S) a�Compresses a list of hextets. Compresses a list of strings, replacing the longest continuous sequence of "0" in the list with "" and adding empty strings at the beginning or at the end of the string such that subsequently calling ":".join(hextets) will produce the compressed version of the IPv6 address. Args: hextets: A list of strings, the hextets to compress. Returns: A list of strings. r$rr��rprprprp)� enumeraterO) r��hextetsZbest_doublecolon_startZbest_doublecolon_lenZdoublecolon_startZdoublecolon_len�indexZhextetZbest_doublecolon_endrrr�_compress_hextets_s.   z_BaseV6._compress_hextetsNcsZ|dkrt|j�}||jkr$td��d|��fdd�tddd�D�}|j|�}d j|�S) a,Turns a 128-bit integer into hexadecimal notation. Args: ip_int: An integer, the IP address. Returns: A string, the hexadecimal representation of the address. Raises: ValueError: The address is bigger than 128 bits of all ones. NzIPv6 address is too largez%032xcs&g|]}dt�||d�d��qS)z%xrr)r�)r r�)�hex_strrrr �sz/_BaseV6._string_from_ip_int..rrrr�)r�rTrcr?r�r�r�)r�r�r�r)r�rr��s   z_BaseV6._string_from_ip_intcs�t|t�rt|j�}nt|t�r,t|j�}nt|�}|j|�}d|��fdd�tddd�D�}t|ttf�r�ddj |�|j fSdj |�S) z�Expand a shortened IPv6 address. Args: ip_str: A string, the IPv6 address. Returns: A string, the expanded IPv6 address. z%032xcsg|]}�||d��qS)rr)r r�)r�rrr �sz8_BaseV6._explode_shorthand_ip_string..rrrz%s/%dr�) rrCrMrtrGrYr�r�rxr�rr)r*r�r�r�r)r�rr~�s     z$_BaseV6._explode_shorthand_ip_stringcCs&|jddd�jdd�}dj|�dS)z�Return the reverse DNS pointer name for the IPv6 address. This implements the method described in RFC3596 2.5. Nr$r�r�r�z .ip6.arparp)r�replacer�)r*Z reverse_charsrrrr��sz_BaseV6._reverse_pointercCs|jS)N)rb)r*rrrr��sz_BaseV6.max_prefixlencCs|jS)N)rq)r*rrrra�sz_BaseV6.version)N)r5r6r7r:r8rq� IPV6LENGTHrcr�r�r�rbr�r�r�r�r�r�r�r~r�r�r�rarrrrr��s$   i  0  r�c@s�eZdZdZdZdd�Zedd��Zedd ��Zed d ��Z ed d ��Z edd��Z edd��Z edd��Z edd��Zedd��Zedd��Zedd��Zedd��ZdS) r=z/Represent and manipulate single IPv6 Addresses.rTr�cCsxt|t�r|j|�||_dSt|t�rL|j|d�t|�}t|d�|_dSt|�}d|krht d|��|j |�|_dS)aInstantiate a new IPv6 address object. Args: address: A string or integer representing the IP Additionally, an integer can be passed, so IPv6Address('2001:db8::') == IPv6Address(42540766411282592856903984951653826560) or, more generally IPv6Address(int(IPv6Address('2001:db8::'))) == IPv6Address('2001:db8::') Raises: AddressValueError: If address isn't a valid IPv6 address. Nrr rLzUnexpected '/' in %r) rrr�rTr>r�rrrMr9r�)r*r@r�r�rrrr��s      zIPv6Address.__init__cCs t|j�S)z*The binary representation of this address.)rKrT)r*rrrr��szIPv6Address.packedcCs ||jjkS)z�Test if the address is reserved for multicast use. Returns: A boolean, True if the address is a multicast address. See RFC 2373 2.7 for details. )r�r�)r*rrrr�s zIPv6Address.is_multicastcst�fdd��jjD��S)z�Test if the address is otherwise IETF reserved. Returns: A boolean, True if the address is within one of the reserved IPv6 Network ranges. c3s|]}�|kVqdS)Nr)r r�)r*rrr�sz*IPv6Address.is_reserved..)r�r��_reserved_networks)r*r)r*rr�s zIPv6Address.is_reservedcCs ||jjkS)z�Test if the address is reserved for link-local. Returns: A boolean, True if the address is reserved per RFC 4291. )r�r�)r*rrrr�szIPv6Address.is_link_localcCs ||jjkS)a`Test if the address is reserved for site-local. Note that the site-local address space has been deprecated by RFC 3879. Use is_private to test if this address is in the space of unique local addresses as defined by RFC 4193. Returns: A boolean, True if the address is reserved per RFC 3513 2.5.6. )r��_sitelocal_network)r*rrr� is_site_local#s zIPv6Address.is_site_localcst�fdd��jjD��S)z�Test if this address is allocated for private networks. Returns: A boolean, True if the address is reserved per iana-ipv6-special-registry. c3s|]}�|kVqdS)Nr)r rd)r*rrr�:sz)IPv6Address.is_private..)r�r�r�)r*r)r*rr�1s zIPv6Address.is_privatecCs|j S)z�Test if this address is allocated for public networks. Returns: A boolean, true if the address is not reserved per iana-ipv6-special-registry. )r�)r*rrrr�<s zIPv6Address.is_globalcCs |jdkS)z�Test if the address is unspecified. Returns: A boolean, True if this is the unspecified address as defined in RFC 2373 2.5.2. r)rT)r*rrrr�Gs zIPv6Address.is_unspecifiedcCs |jdkS)z�Test if the address is a loopback address. Returns: A boolean, True if the address is a loopback address as defined in RFC 2373 2.5.3. r$)rT)r*rrrr�Rs zIPv6Address.is_loopbackcCs |jd?dkrdSt|jd@�S)z�Return the IPv4 mapped address. Returns: If the IPv6 address is a v4 mapped address, return the IPv4 mapped address. Return None otherwise. ri��Nl��)rTr<)r*rrr� ipv4_mapped]s zIPv6Address.ipv4_mappedcCs4|jd?dkrdSt|jd?d@�t|jd@�fS)z�Tuple of embedded teredo IPs. Returns: Tuple of the (server, client) IPs or None if the address doesn't appear to be a teredo address (doesn't start with 2001::/32) �`i Nrl��)rTr<)r*rrr�teredojs zIPv6Address.teredocCs$|jd?dkrdSt|jd?d@�S)z�Return the IPv4 6to4 embedded address. Returns: The IPv4 6to4-embedded address if present or None if the address doesn't appear to contain a 6to4 embedded address. �pi N�Pl��)rTr<)r*rrr� sixtofourys zIPv6Address.sixtofourN)rTr�)r5r6r7r:r8r�r�r�r�r�r�rr�r�r�r�rrrrrrrr=�s%   r=c@s�eZdZdd�Zdd�Zdd�Zdd�Zd d �Zej Z e d d ��Z e d d��Z e dd��Z e dd��Ze dd��Ze dd��ZdS)rGcCs�t|ttf�r2tj||�t|j�|_|j|_ dSt|t �r�tj||d�t |�dkrht |d�|_ n|j|_ t|dd�|_|jj |_ |jj|_dSt|�}tj||d�t|dd�|_|jj |_ |jj |_ |jj|_dS)Nrr$F)rD)rr>rr=r�rCrTr�rbrrr�rOr�r�r�rQ)r*r@rPrrrr��s(        zIPv6Interface.__init__cCsd|j|j�|jjfS)Nz%s/%d)r�rTr�r�)r*rrrr��s zIPv6Interface.__str__c CsDtj||�}| s|tkr|Sy |j|jkStk r>dSXdS)NF)r=r,r-r�rs)r*r+r�rrrr,�s  zIPv6Interface.__eq__c Cs>tj||�}|tkrtSy |j|jkStk r8dSXdS)NF)r=r0r-r�rs)r*r+r�rrrr0�s  zIPv6Interface.__lt__cCs|j|jAt|jj�AS)N)rTrrr�r�rt)r*rrrr��szIPv6Interface.__hash__cCs t|j�S)N)r=rT)r*rrrrY�szIPv6Interface.ipcCsd|j|j�|jfS)Nz%s/%s)r�rTrr)r*rrrr��s zIPv6Interface.with_prefixlencCsd|j|j�|jfS)Nz%s/%s)r�rTr�)r*rrrr��s zIPv6Interface.with_netmaskcCsd|j|j�|jfS)Nz%s/%s)r�rTr�)r*rrrr��s zIPv6Interface.with_hostmaskcCs|jdko|jjS)Nr)rTr�r�)r*rrrr��szIPv6Interface.is_unspecifiedcCs|jdko|jjS)Nr$)rTr�r�)r*rrrr��szIPv6Interface.is_loopbackN)r5r6r7r�r�r,r0r�r}r�r�rYr�r�r�r�r�rrrrrG�s       rGc@s2eZdZdZeZd dd�Zdd�Zedd��Z d S) rCavThis class represents and manipulates 128-bit IPv6 networks. Attributes: [examples for IPv6('2001:db8::1000/124')] .network_address: IPv6Address('2001:db8::1000') .hostmask: IPv6Address('::f') .broadcast_address: IPv6Address('2001:db8::100f') .netmask: IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0') .prefixlen: 124 TcCs|tj||�t|ttf�rrr=rtr�rbr�rrr�rOr�r?rQr�r�r�)r*r@rDr�r�rPrrrr��sB           zIPv6Network.__init__ccs@t|j�}t|j�}x&t|d|d�D]}|j|�Vq(WdS)z�Generate Iterator over usable hosts in a network. This is like __iter__ except it doesn't return the Subnet-Router anycast address. r$N)r�rtrmr(r�)r*r�r�r�rrrr�< s  zIPv6Network.hostscCs|jjo|jjS)a`Test if the address is reserved for site-local. Note that the site-local address space has been deprecated by RFC 3879. Use is_private to test if this address is in the space of unique local addresses as defined by RFC 4193. Returns: A boolean, True if the address is reserved per RFC 3513 2.5.6. )rtrrm)r*rrrrH s zIPv6Network.is_site_localN)T) r5r6r7r:r=r�r�r�r�rrrrrrC�s   O rCc@s�eZdZed�Zed�Zed�ed�ed�ed�ed�ed�ed �ed �ed �ed�g Zed �ed �ed�ed�ed�ed�ed�ed�ed�ed�ed�ed�ed�ed�ed�gZed�ZdS)�_IPv6Constantsz fe80::/10zff00::/8z::1/128z::/128z ::ffff:0:0/96z100::/64z 2001::/23z 2001:2::/48z 2001:db8::/32z 2001:10::/28zfc00::/7z::/8z100::/8z200::/7z400::/6z800::/5z1000::/4z4000::/3z6000::/3z8000::/3zA000::/3zC000::/3zE000::/4zF000::/5zF800::/6zFE00::/9z fec0::/10N) r5r6r7rCr�r�r�rrrrrrr X s*         r r)r$)T)8r:Z __future__rr"r� __version__r�rZlong� NameErrorZunicoderM�strr>rr� from_bytesrrsrr�r!r(�objectr)r�r�r?r9r;rArErHrJrKrQrZr]rerorwr|r}r_rxr�r<rFrBr�r�r�r=rGrCr rrrr� s�        )$ $#716=a*vRr 5V{!