U .e7@s`dZddlmZddlZddlZdZefZz eefZWne k rJYnXze Z Wne k rle Z YnXddkrddZ nddZ z ejZWnek rdd ZYnXd d Zeed rd dZnddZdNddZGdddeZdZdZGdddeZGdddeZddZdOddZd d!Zd"d#Zd$d%Z d&d'Z!d(d)Z"d*d+Z#d,d-Z$d.d/Z%d0d1Z&d2d3Z'Gd4d5d5eZ(Gd6d7d7e(Z)Gd8d9d9e(Z*Gd:d;d;eZ+Gdd?d?e,Z-Gd@dAdAe+e*Z.GdBdCdCeZ/e/e,_0GdDdEdEeZ1GdFdGdGe1e)Z2GdHdIdIe2Z3GdJdKdKe1e*Z4GdLdMdMeZ5e5e2_0dS)PzA 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.22cCs|SNZbytrr9/usr/lib/python3.8/site-packages/pip/_vendor/ipaddress.py_compat_bytes_to_byte_valssrcCsdd|DS)NcSsg|]}td|dqS)!Br)structunpack.0brrr #sz._compat_bytes_to_byte_vals..rrrrrr"scCsd}|D]}|d>|}q|S)Nrr)Zbytvals endianessresZbvrrr_compat_int_from_byte_vals'srcCst|dkr.|dks|dkr"tdtd|S|dkrj|dksJ|ddkrTtd td |d ?|d @StdS) Nrlz(integer out of range for 'I' format codes!Iz)integer out of range for 'QQ' format codes!QQ@l)r errorpackNotImplementedError)ZintvalZlengthrrrr_compat_to_bytes0s   r bit_lengthcCs|Sr)r)irrr_compat_bit_lengthAsrcCs&tD]}||?dkr|SqdSNr) itertoolscount)rrrrrrDs  ccs |}||kr|V||7}qdSrr)startendsteprrrr _compat_rangeJsr&c@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)_TotalOrderingMixinrcCstdSrrselfotherrrr__eq__Ysz_TotalOrderingMixin.__eq__cCs||}|tkrtS| Sr)r,NotImplemented)r*r+equalrrr__ne__\s z_TotalOrderingMixin.__ne__cCstdSrr(r)rrr__lt__bsz_TotalOrderingMixin.__lt__cCs$||}|tks|s ||S|Srr0r-r,r*r+lessrrr__le__es   z_TotalOrderingMixin.__le__cCs6||}|tkrtS||}|tkr,tS|p2| Srr1)r*r+r3r.rrr__gt__ks  z_TotalOrderingMixin.__gt__cCs||}|tkrtS| Sr)r0r-r2rrr__ge__ts z_TotalOrderingMixin.__ge__N) __name__ __module__ __qualname__ __slots__r,r/r0r4r5r6rrrrr'Rs r' rc@seZdZdZdS)AddressValueErrorz%A Value Error related to the address.Nr7r8r9__doc__rrrrr<sr<c@seZdZdZdS)NetmaskValueErrorz%A Value Error related to the netmask.Nr=rrrrr?sr?c Csnz t|WSttfk r"YnXz t|WSttfk rFYnXt|tr^td|td|dS)aTake 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 IPv4Address or IPv6Address object. Raises: ValueError: if the *address* passed isn't either a v4 or a v6 address zx%r does not appear to be an IPv4 or IPv6 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?z0%r does not appear to be an IPv4 or IPv6 addressN) IPv4Addressr<r? IPv6Address isinstancebytes ValueErroraddressrrr ip_addresss"   rGTc Csrz t||WSttfk r$YnXz t||WSttfk rJYnXt|trbtd|td|dS)aTake an IP string/int and return an object of the correct type. Args: address: A string or integer, the IP network. Either IPv4 or IPv6 networks may be supplied; integers less than 2**32 will be considered to be IPv4 by default. Returns: An IPv4Network or IPv6Network object. Raises: ValueError: if the string passed isn't either a v4 or a v6 address. Or if the network has host bits set. zx%r does not appear to be an IPv4 or IPv6 network. Did you pass in a bytes (str in Python 2) instead of a unicode object?z0%r does not appear to be an IPv4 or IPv6 networkN) IPv4Networkr<r? IPv6NetworkrBrCrD)rFstrictrrr ip_networks"   rKc CsXz t|WSttfk r"YnXz t|WSttfk rFYnXtd|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) IPv4Interfacer<r? IPv6InterfacerDrErrr ip_interfaces  rNc Cs6zt|ddWStjtfk r0tdYnXdS)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. rbigz&Address negative or too large for IPv4Nrr r OverflowErrorrDrErrrv4_int_to_packedsrRc Cs6zt|ddWStjtfk r0tdYnXdS)zRepresent 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. rrOz&Address negative or too large for IPv6NrPrErrrv6_int_to_packeds rScCs*t|d}t|dkr&td||S)zAHelper to split the netmask and raise AddressValueError if needed/rzOnly one '/' permitted in %r) _compat_strsplitlenr<)rFaddrrrr_split_optional_netmasks  rYccsNt|}t|}}|D]&}|j|jdkr:||fV|}|}q||fVdS)zFind 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)iternext_ip) addressesitfirstlastiprrr_find_address_ranges   rbcCs$|dkr |St|t||d@S)zCount 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)Znumberbitsrrr_count_righthand_zero_bits2s reccst|trt|tstd|j|jkr8td||f||krHtd|jdkrXt}n|jdkrht}ntd|j}|j}|j}||krt t ||t ||dd}||||f}|V|d|>7}|d|j krqqdS) aSummarize 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 networks%%s and %s are not of the same versionz*last IP address must be greater than firstrzunknown IP versionr"N) rB _BaseAddress TypeErrorversionrDrHrI_max_prefixlenr\rcrer _ALL_ONES)r_r`raZip_bitsZ first_intZlast_intZnbitsnetrrrsummarize_address_rangeBs8      rnccst|}i}|rV|}|}||}|dkr<|||<q ||kr ||=||q d}t|D]$}|dk r|j|jkrqf|V|}qfdS)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)listpopsupernetgetappendsortedvaluesbroadcast_address)r]Zto_mergesubnetsrmrqZexistingr`rrr_collapse_addresses_internalys$    rxc Cs0g}g}g}|D]}t|trR|rF|dj|jkrFtd||df||q|j|jkr|r|dj|jkrtd||dfz||jWqtk r||j YqXq|r|dj|jkrtd||df||qt t |}|r$t |D]\}}| t||qt||S)aCollapse 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. rf)rBrh_versionrirs _prefixlenrkraAttributeErrornetwork_addressrtsetrbextendrnrx)r]ZaddrsZipsZnetsrar_r`rrrcollapse_addressess@     rcCs(t|tr|St|tr$|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. )rB _BaseNetwork_get_networks_keyrh_get_address_keyr-)objrrrget_mixed_type_keys   rc@seZdZdZdZeddZeddZeddZed d Z d d Z d dZ e ddZ e ddZe ddZe ddZe ddZddZdS)_IPAddressBasezThe mother class.rcCs|S)z:Return the longhand version of the IP address as a string.)_explode_shorthand_ip_stringr*rrrexplodedsz_IPAddressBase.explodedcCst|S)z;Return the shorthand version of the IP address as a string.rUrrrr compressedsz_IPAddressBase.compressedcCs|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_pointerrrrrreverse_pointer s z_IPAddressBase.reverse_pointercCsdt|f}t|dS)Nz%200s has no version specifiedtyperr*msgrrrrjsz_IPAddressBase.versioncCsF|dkrd}t|||jf||jkrBd}t|||j|jfdS)Nrz-%d (< 0) is not permitted as an IPv%d addressz2%d (>= 2**%d) is not permitted as an IPv%d address)r<rzrlrk)r*rFrrrr_check_int_addresss  z!_IPAddressBase._check_int_addresscCs.t|}||kr*d}t|||||jfdS)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?)rWr<rz)r*rFZ expected_lenZ address_lenrrrr_check_packed_address"sz$_IPAddressBase._check_packed_addresscCs|j|j|?AS)zTurn the prefix length into a bitwise netmask Args: prefixlen: An integer, the prefix length. Returns: An integer. )rl)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"rrOz&Netmask pattern %r mixes zeroes & ones)rerkrrD) rip_intZtrailing_zeroesrZ leading_onesZall_onesZbyteslenZdetailsrrrr_prefix_from_ip_int9s      z"_IPAddressBase._prefix_from_ip_intcCsd|}t|dS)Nz%r is not a valid netmask)r?)rZ netmask_strrrrr_report_invalid_netmaskRsz&_IPAddressBase._report_invalid_netmaskcCshtj|s||z t|}Wntk r@||YnXd|krX|jksdn|||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 issupersetrintrDrk)rZ prefixlen_strrrrr_prefix_from_prefix_stringWs    z)_IPAddressBase._prefix_from_prefix_stringcCsz||}Wntk r,||YnXz ||WStk rNYnX||jN}z ||WStk r||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_stringr<rrrDrl)rip_strrrrr_prefix_from_ip_stringps   z%_IPAddressBase._prefix_from_ip_stringcCs|jt|ffSr) __class__rUrrrr __reduce__sz_IPAddressBase.__reduce__N)r7r8r9r>r:propertyrrrrjrr classmethodrrrrrrrrrrrs.           !rc@sdeZdZdZdZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZdS)rhzA generic IP object. This IP class contains the version independent methods which are used by single IP addresses. rcCs|jSrr\rrrr__int__sz_BaseAddress.__int__cCs8z|j|jko|j|jkWStk r2tYSXdSr)r\rzr|r-r)rrrr,s   z_BaseAddress.__eq__cCs`t|tstSt|ts(td||f|j|jkrDtd||f|j|jkr\|j|jkSdSN"%s and %s are not of the same typerfF)rBrr-rhrirzr\r)rrrr0s     z_BaseAddress.__lt__cCs t|tstS|t||SrrB_compat_int_typesr-rrr)rrr__add__s z_BaseAddress.__add__cCs t|tstS|t||Srrr)rrr__sub__s z_BaseAddress.__sub__cCsd|jjt|fSNz%s(%r)rr7rUrrrr__repr__sz_BaseAddress.__repr__cCst||jSr)rU_string_from_ip_intr\rrrr__str__sz_BaseAddress.__str__cCsttt|jSr)hashhexrr\rrrr__hash__sz_BaseAddress.__hash__cCs |j|fSrrzrrrrrsz_BaseAddress._get_address_keycCs|j|jffSr)rr\rrrrrsz_BaseAddress.__reduce__N)r7r8r9r>r:rr,r0rrrrrrrrrrrrhsrhc@sdeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZeddZeddZeddZeddZed d!Zed"d#Zed$d%Zed&d'Zd(d)Zd*d+Zd,d-ZdHd0d1ZdId2d3Zed4d5Zed6d7Zd8d9Z d:d;Z!edd?Z#ed@dAZ$edBdCZ%edDdEZ&edFdGZ'd/S)Jrz~A generic IP network object. This IP class contains the version independent methods which are used by networks. cCs i|_dSr)_cache)r*rFrrr__init__sz_BaseNetwork.__init__cCsd|jjt|fSrrrrrrrsz_BaseNetwork.__repr__cCsd|j|jfSN%s/%d)r}rrrrrrsz_BaseNetwork.__str__ccs8t|j}t|j}t|d|D]}||Vq"dS)zGenerate Iterator over usable hosts in a network. This is like __iter__ except it doesn't return the network or broadcast addresses. r"Nrr}rvr&_address_classr*network broadcastxrrrhostss  z_BaseNetwork.hostsccs8t|j}t|j}t||dD]}||Vq"dSNr"rrrrr__iter__s  z_BaseNetwork.__iter__cCslt|j}t|j}|dkr>|||kr0td|||S|d7}|||krZtd|||SdS)Nrzaddress out of ranger")rr}rv IndexErrorr)r*nrrrrr __getitem__s    z_BaseNetwork.__getitem__cCsxt|tstSt|ts(td||f|j|jkrDtd||f|j|jkr\|j|jkS|j|jkrt|j|jkSdSr)rBrr-rrirzr}netmaskr)rrrr0s"       z_BaseNetwork.__lt__cCsLz.|j|jko,|j|jko,t|jt|jkWStk rFtYSXdSr)rzr}rrr|r-r)rrrr,s  z_BaseNetwork.__eq__cCstt|jt|jASr)rrr}rrrrrrsz_BaseNetwork.__hash__cCsL|j|jkrdSt|trdSt|jt|jkoBt|jkSSdSNF)rzrBrrr}r\rvr)rrr __contains__s  z_BaseNetwork.__contains__cCs(|j|kp&|j|kp&|j|kp&|j|kS)z*Tell if self is partly contained in other.)r}rvr)rrroverlaps*s    z_BaseNetwork.overlapscCs<|jd}|dkr8|t|jt|jB}||jd<|S)Nrv)rrrrrr}hostmaskr*rrrrrv1s   z_BaseNetwork.broadcast_addresscCs8|jd}|dkr4|t|j|jA}||jd<|S)Nr)rrrrrrrlrrrrr:s   z_BaseNetwork.hostmaskcCsd|j|jfSr)r}r{rrrrwith_prefixlenBsz_BaseNetwork.with_prefixlencCsd|j|jfSN%s/%s)r}rrrrr with_netmaskFsz_BaseNetwork.with_netmaskcCsd|j|jfSr)r}rrrrr with_hostmaskJsz_BaseNetwork.with_hostmaskcCst|jt|jdS)z&Number of hosts in the current subnet.r")rrvr}rrrr num_addressesNsz_BaseNetwork.num_addressescCsdt|f}t|dS)Nz%%200s has no associated address classrrrrrrSsz_BaseNetwork._address_classcCs|jSr)r{rrrrr[sz_BaseNetwork.prefixlenccs|j|jkstd||ft|ts2td|||sLtd||f||krXdS|d|j|jf}| \}}||kr||kr||r|V| \}}qz||r|V| \}}qzt d|||fqz||kr|Vn"||kr|Vnt d|||fdS)aRemove 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. rfz%s is not a network objectz%s not contained in %sNrz3Error performing exclusion: s1: %s s2: %s other: %s) rzrirBr subnet_ofrDrr}rrwAssertionError)r*r+s1s2rrraddress_exclude_s@$        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. rryr"r)rzrir}rr)rrrcompare_networkss!     z_BaseNetwork.compare_networkscCs|j|j|jfS)zNetwork-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(). )rzr}rrrrrrsz_BaseNetwork._get_networks_keyr"Nc cs|j|jkr|VdS|dk rJ||jkr0td|dkr@td||j}|dkrZtd|j|}||jkr~td||ft|j}t|jd}t|jd|?}t|||D]}|||f}|VqdS)aThe 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"(cannot set prefixlen_diff and new_prefixrzprefix length diff must be > 0z0prefix length diff %d is invalid for netblock %s) r{rkrDrr}rvrr&r) r*prefixlen_diff new_prefix new_prefixlenr#r$r%Znew_addrZcurrentrrrrws2      z_BaseNetwork.subnetscCs|jdkr|S|dk rB||jkr(td|dkr8td|j|}|j|}|dkrftd|j|f|t|jt|j|>@|fS)aThe 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"rz;current prefixlen is %d, cannot have a prefixlen_diff of %d)r{rDrrrr}r)r*rrrrrrrqs&    z_BaseNetwork.supernetcCs|jjo|jjSzTest 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} is_multicastrvrrrrrAs z_BaseNetwork.is_multicastcCs^z4|j|jkrtd|||j|jko2|j|jkWStk rXtd||fYnXdS)Nrfz3Unable to test subnet containment between %s and %s)rzrir}rvr|)ar rrr _is_subnet_ofMs   z_BaseNetwork._is_subnet_ofcCs |||S)z1Return True if this network is a subnet of other.rr)rrrrYsz_BaseNetwork.subnet_ofcCs |||S)z3Return True if this network is a supernet of other.rr)rrr supernet_of]sz_BaseNetwork.supernet_ofcCs|jjo|jjS)Test if the address is otherwise IETF reserved. Returns: A boolean, True if the address is within one of the reserved IPv6 Network ranges. )r} is_reservedrvrrrrras z_BaseNetwork.is_reservedcCs|jjo|jjSzTest if the address is reserved for link-local. Returns: A boolean, True if the address is reserved per RFC 4291. )r} is_link_localrvrrrrrmsz_BaseNetwork.is_link_localcCs|jjo|jjS)zTest if this address is allocated for private networks. Returns: A boolean, True if the address is reserved per iana-ipv4-special-registry or iana-ipv6-special-registry. )r} is_privatervrrrrrxs z_BaseNetwork.is_privatecCs|j S)zTest if this address is allocated for public networks. Returns: A boolean, True if the address is not reserved per iana-ipv4-special-registry or iana-ipv6-special-registry. rrrrr is_globals z_BaseNetwork.is_globalcCs|jjo|jjS)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}is_unspecifiedrvrrrrrs z_BaseNetwork.is_unspecifiedcCs|jjo|jjS)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} is_loopbackrvrrrrrs z_BaseNetwork.is_loopback)r"N)r"N)(r7r8r9r>rrrrrrr0r,rrrrrvrrrrrrrrrrrwrqr staticmethodrrrrrrrrrrrrrrsf           K0 5 (       rc @seZdZdZdZdZdedZedZ eddd d d d d ddg Z eZ iZ ddZ eddZeddZeddZeddZddZddZeddZed d!Zd"S)#rzyBase IPv4 object. The following methods are used by IPv4 objects in both single IP addresses and networks. rrrr" 0123456789rrcCst|Srrrrrrrsz$_BaseV4._explode_shorthand_ip_stringcCsn||jkrdt|tr|}n.z||}Wntk rF||}YnXt||}||f|j|<|j|SaMake 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") )_netmask_cacherBrrr?rr@rrargrrrrr _make_netmasks  z_BaseV4._make_netmaskc Csz|s td|d}t|dkr.td|ztt|j|dWStk rt}ztd||fW5d}~XYnXdS)aTurn the given IP string into an integer for comparison. Args: ip_str: A string, the IP ip_str. Returns: The IP ip_str as an integer. Raises: AddressValueError: if ip_str isn't a valid IPv4 Address. Address cannot be empty.rzExpected 4 octets in %rrO%s in %rN)r<rVrWrmap _parse_octetrD)rrZoctetsexcrrrrs    z_BaseV4._ip_int_from_stringcCs|s td|j|s(d}t||t|dkrDd}t||t|d}|dkrr|ddkrrd }t|||d krtd ||S) a Convert a decimal octet into an integer. Args: octet_str: A string, the number to parse. Returns: The octet as an integer. Raises: ValueError: if the octet isn't strictly a decimal from [0..255]. zEmpty octet not permittedz#Only decimal digits permitted in %rz$At most 3 characters permitted in %r r0z3Ambiguous (octal/decimal) value in %r not permittedrzOctet %d (> 255) not permitted)rDrrrWr)rZ octet_strrZ octet_intrrrrs       z_BaseV4._parse_octetcCsdddt|ddDS)zTurns 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. rcss0|](}tt|tr"td|dn|VqdS)rrN)rUrBrCr r r rrr %s z._BaseV4._string_from_ip_int..rrO)joinr)rrrrrrs  z_BaseV4._string_from_ip_intcsj|d}zfddtt|D}Wntk r<YdSXt|t|krRdS|d|dkrfdSdS)zTest 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. rcsg|]}|jkr|qSr)_valid_mask_octetsr rrrrr6s z(_BaseV4._is_hostmask..FrryT)rVrrrDrW)r*rrdpartsrrr _is_hostmask*s z_BaseV4._is_hostmaskcCs&t|dddd}d|dS)zReturn the reverse DNS pointer name for the IPv4 address. This implements the method described in RFC1035 3.5. rNryz .in-addr.arpa)rUrVr)r*Zreverse_octetsrrrr?sz_BaseV4._reverse_pointercCs|jSrrkrrrr max_prefixlenHsz_BaseV4.max_prefixlencCs|jSrrrrrrrjLsz_BaseV4.versionN)r7r8r9r>r:rz IPV4LENGTHrl frozensetrrrkrrrrrrrr rrr rjrrrrrs.    $   rc@s|eZdZdZdZddZeddZeddZed d Z ed d Z ed dZ eddZ eddZ eddZdS)r@z/Represent and manipulate single IPv4 Addresses.r\ __weakref__cCsxt|tr||||_dSt|trL||dt|}t|d|_dSt|}d|krht d|| ||_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. NrrOrTUnexpected '/' in %r rBrrr\rCrrrrUr<rr*rFZbvsZaddr_strrrrrWs      zIPv4Address.__init__cCs t|jSz*The binary representation of this address.)rRr\rrrrpacked{szIPv4Address.packedcCs ||jjkS)zTest if the address is otherwise IETF reserved. Returns: A boolean, True if the address is within the reserved IPv4 Network range. ) _constants_reserved_networkrrrrrs zIPv4Address.is_reservedcstfddjjDS)zTest if this address is allocated for private networks. Returns: A boolean, True if the address is reserved per iana-ipv4-special-registry. c3s|]}|kVqdSrrr rmrrrrsz)IPv4Address.is_private..anyr_private_networksrrrrrs zIPv4Address.is_privatecCs||jjko|j Sr)r_public_networkrrrrrrs zIPv4Address.is_globalcCs ||jjkS)zTest if the address is reserved for multicast use. Returns: A boolean, True if the address is multicast. See RFC 3171 for details. r_multicast_networkrrrrrs zIPv4Address.is_multicastcCs ||jjkS)zTest if the address is unspecified. Returns: A boolean, True if this is the unspecified address as defined in RFC 5735 3. )r_unspecified_addressrrrrrs zIPv4Address.is_unspecifiedcCs ||jjkS)zTest if the address is a loopback address. Returns: A boolean, True if the address is a loopback per RFC 3330. )r_loopback_networkrrrrrszIPv4Address.is_loopbackcCs ||jjkS)zTest if the address is reserved for link-local. Returns: A boolean, True if the address is link-local per RFC 3927. r_linklocal_networkrrrrrszIPv4Address.is_link_localN)r7r8r9r>r:rrrrrrrrrrrrrrr@Qs&$       r@c@sjeZdZddZddZddZddZd d Zej Z e d d Z e d dZ e ddZ e ddZdS)rLcCst|ttfr2t||t|j|_|j|_ dSt|t rt||dt |dkrht |d|_ n|j|_ t|dd|_|jj |_ |jj|_dSt|}t||dt|dd|_|jj |_ |jj |_ |jj|_dSNrr"F)rJ)rBrCrr@rrHr\rrkr{tuplerWrrrrYr*rFrXrrrrs(        zIPv4Interface.__init__cCsd||j|jjfSrrr\rrrrrrrs zIPv4Interface.__str__cCsFt||}|r|tkr|Sz|j|jkWStk r@YdSXdSr)r@r,r-rr|r*r+Z address_equalrrrr,s  zIPv4Interface.__eq__cCsRt||}|tkrtSz|j|jkp4|j|jko4|WStk rLYdSXdSr)r@r0r-rr|r*r+Z address_lessrrrr0s  zIPv4Interface.__lt__cCs|j|jAt|jjASrr\r{rrr}rrrrrszIPv4Interface.__hash__cCs t|jSr)r@r\rrrrraszIPv4Interface.ipcCsd||j|jfSrrr\r{rrrrr s zIPv4Interface.with_prefixlencCsd||j|jfSrrr\rrrrrrs zIPv4Interface.with_netmaskcCsd||j|jfSrrr\rrrrrrs zIPv4Interface.with_hostmaskN)r7r8r9rrr,r0rrrrrarrrrrrrrLs     rLc@s*eZdZdZeZdddZeddZdS) rHaeThis 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|t||t|ttfrr@rrrrrrrrrHs   UrHc@seZdZedZedZedZedZededededededed ed ed ed ed edededgZedZ e dZ dS)_IPv4Constantsz169.254.0.0/16z 127.0.0.0/8z 224.0.0.0/4r0z 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) r7r8r9rHr#r!rrrrr@r rrrrr1s*r1c@seZdZdZdZdZdedZdZe dZ eZ iZ e dd Ze d d Ze d d Ze ddZe dddZddZddZeddZeddZdS)_BaseV6zyBase IPv6 object. The following methods are used by IPv6 objects in both single IP addresses and networks. rrgrr"rZ0123456789ABCDEFabcdefcCsJ||jkr@t|tr|}n ||}t||}||f|j|<|j|Sr)rrBrrrArrrrrrs   z_BaseV6._make_netmaskc Cs|s td|d}d}t||kr:d||f}t|d|dkrzt|j}Wn2tk r}ztd||fW5d}~XYnX|d |d ?d @|d |d @|jd }t||krd |d |f}t|d}td t|d D]*} || s|dk rd|}t|| }q|dk r|} t||d } |dsj| d 8} | rjd}t|||ds| d 8} | rd}t|||j| | } | d kr0d}t||jd |fnjt||jkrd}t||j|f|dsd}t|||ds d}t||t|} d} d} znd} t | D] } | d K} | | || O} q>| d | K} t | dD] } | d K} | | || O} qx| WSt k r}ztd||fW5d}~XYnXdS)zTurn 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. r:rz At least %d parts expected in %rrryrN%xrr"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 %r) r<rVrWr@rpr\rs _HEXTET_COUNTr&range _parse_hextetrD)rrr Z _min_partsrZipv4_intrZ _max_partsZ skip_indexrZparts_hiZparts_loZ parts_skippedrrrrrs    "               z_BaseV6._ip_int_from_stringcCs>|j|std|t|dkr4d}t||t|dS)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_DIGITSrrDrWr)rZ hextet_strrrrrr8>s     z_BaseV6._parse_hextetc Csd}d}d}d}t|D]>\}}|dkrN|d7}|dkr<|}||krV|}|}qd}d}q|dkr||}|t|kr~|dg7}dg|||<|dkrdg|}|S)aCompresses 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. ryrrr") enumeraterW) rhextetsZbest_doublecolon_startZbest_doublecolon_lenZdoublecolon_startZdoublecolon_lenindexZhextetZbest_doublecolon_endrrr_compress_hextetsXs0   z_BaseV6._compress_hextetsNcsZ|dkrt|j}||jkr$tdd|fddtdddD}||}d |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 large%032xcs&g|]}dt||ddqS)r4rr)rr Zhex_strrrrsz/_BaseV6._string_from_ip_int..rr;rr3)rr\rlrDr7r>r)rrr<rr@rrs   z_BaseV6._string_from_ip_intcst|trt|j}nt|tr,t|j}nt|}||}d|fddtdddD}t|ttfrdd ||j fSd |S) zExpand a shortened IPv6 address. Args: ip_str: A string, the IPv6 address. Returns: A string, the expanded IPv6 address. r?csg|]}||dqS)rrr r@rrrsz8_BaseV6._explode_shorthand_ip_string..rr;rrr3) rBrIrUr}rMrarr7rrr{)r*rrr rr@rrs     z$_BaseV6._explode_shorthand_ip_stringcCs&|jddddd}d|dS)zReturn the reverse DNS pointer name for the IPv6 address. This implements the method described in RFC3596 2.5. Nryr3r:rz .ip6.arpa)rreplacer)r*Z reverse_charsrrrrsz_BaseV6._reverse_pointercCs|jSrr rrrrr sz_BaseV6.max_prefixlencCs|jSrrrrrrrjsz_BaseV6.version)N)r7r8r9r>r:rz IPV6LENGTHrlr6rr9rkrrrrr8r>rrrrr rjrrrrr2s0   h  /   r2c@seZdZdZdZddZeddZeddZed d Z ed d Z ed dZ eddZ eddZ eddZeddZeddZeddZeddZdS)rAz/Represent and manipulate single IPv6 Addresses.rcCsxt|tr||||_dSt|trL||dt|}t|d|_dSt|}d|krht d|| ||_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. NrrOrTrrrrrrrs      zIPv6Address.__init__cCs t|jSr)rSr\rrrrrszIPv6Address.packedcCs ||jjkSrrrrrrrs zIPv6Address.is_multicastcstfddjjDS)rc3s|]}|kVqdSrrr rrrrsz*IPv6Address.is_reserved..)rr_reserved_networksrrrrrs zIPv6Address.is_reservedcCs ||jjkSrr"rrrrrszIPv6Address.is_link_localcCs ||jjkSa`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_networkrrrr is_site_locals zIPv6Address.is_site_localcstfddjjDS)zTest if this address is allocated for private networks. Returns: A boolean, True if the address is reserved per iana-ipv6-special-registry. c3s|]}|kVqdSrrrrrrr3sz)IPv6Address.is_private..rrrrrr*s zIPv6Address.is_privatecCs|j S)zTest if this address is allocated for public networks. Returns: A boolean, true if the address is not reserved per iana-ipv6-special-registry. rrrrrr5s zIPv6Address.is_globalcCs |jdkS)rrrrrrrr@s zIPv6Address.is_unspecifiedcCs |jdkS)rr"rrrrrrKs zIPv6Address.is_loopbackcCs |jd?dkrdSt|jd@S)zReturn the IPv4 mapped address. Returns: If the IPv6 address is a v4 mapped address, return the IPv4 mapped address. Return None otherwise. r;r5Nr\r@rrrr ipv4_mappedVs zIPv6Address.ipv4_mappedcCs4|jd?dkrdSt|jd?d@t|jd@fS)zTuple 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 NrrGrHrrrrteredocs zIPv6Address.teredocCs$|jd?dkrdSt|jd?d@S)zReturn 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 NPrGrHrrrr sixtofourrs zIPv6Address.sixtofourN)r7r8r9r>r:rrrrrrrFrrrrrIrKrNrrrrrAs6%           rAc@seZdZddZddZddZddZd d Zej Z e d d Z e d dZ e ddZ e ddZe ddZe ddZdS)rMcCst|ttfr2t||t|j|_|j|_ dSt|t rt||dt |dkrht |d|_ n|j|_ t|dd|_|jj |_ |jj|_dSt|}t||dt|dd|_|jj |_ |jj |_ |jj|_dSr$)rBrCrrArrIr\rrkr{r%rWrrrrYr&rrrrs(        zIPv6Interface.__init__cCsd||j|jjfSrr'rrrrrs zIPv6Interface.__str__cCsFt||}|r|tkr|Sz|j|jkWStk r@YdSXdSr)rAr,r-rr|r(rrrr,s  zIPv6Interface.__eq__cCsRt||}|tkrtSz|j|jkp4|j|jko4|WStk rLYdSXdSr)rAr0r-rr|r)rrrr0s  zIPv6Interface.__lt__cCs|j|jAt|jjASrr*rrrrrszIPv6Interface.__hash__cCs t|jSr)rAr\rrrrraszIPv6Interface.ipcCsd||j|jfSrr+rrrrrs zIPv6Interface.with_prefixlencCsd||j|jfSrr,rrrrrs zIPv6Interface.with_netmaskcCsd||j|jfSrr-rrrrrs zIPv6Interface.with_hostmaskcCs|jdko|jjSr)r\rrrrrrrszIPv6Interface.is_unspecifiedcCs|jdko|jjSr)r\rrrrrrrszIPv6Interface.is_loopbackN)r7r8r9rrr,r0rrrrrarrrrrrrrrrMs$       rMc@s2eZdZdZeZd ddZddZeddZ d S) rIavThis 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|t||t|ttfrrArrrrrFrrrrrIs   O rIc@seZdZedZedZededededededed ed ed edg Zed ed edededededededededededededgZedZdS)_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) r7r8r9rIr#rrrCrErrrrrOR s< rO)r")T)6r>Z __future__rr r __version__rrZlong NameErrorZunicoderUstrr from_bytesrr|rhasattrrr&objectr'rrBrDr<r?rGrKrNrRrSrYrbrernrxrrrrhrrr@rLrHr1rr2rArMrIrOrrrrs        )$ $#716=X*vSr 5W{!