QfR2HdZdZddlZdZdZGddeZGdd eZd Zd2d Z d Z d Z dZ dZ dZdZdZdZdZdZGddZdaej,GddeZej,GddeZGddZee_GddZGd d!eeZGd"d#eZGd$d%eeZGd&d'Zee_ee_Gd(d)Z Gd*d+e eZ!Gd,d-e!Z"Gd.d/e eZ#Gd0d1Z$e$e!_e$e#_y)3zA fast, lightweight IPv4/IPv6 manipulation library in Python. This library is used to create/poke/manipulate IPv4 and IPv6 addresses and networks. z1.0N ceZdZdZy)AddressValueErrorz%A Value Error related to the address.N__name__ __module__ __qualname____doc__0/opt/alt/python312/lib64/python3.12/ipaddress.pyrr/r rceZdZdZy)NetmaskValueErrorz%A Value Error related to the netmask.Nrr r rrrrr rc t|S#ttf$rYnwxYw t|S#ttf$rYnwxYwt |d)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 z. does not appear to be an IPv4 or IPv6 address) IPv4Addressrr IPv6Address ValueErroraddresss r ip_addressrsg  7## 0 1    7## 0 1    {"PQ RR  .AAc t||S#ttf$rYnwxYw t||S#ttf$rYnwxYwt |d)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. z. does not appear to be an IPv4 or IPv6 network) IPv4Networkrr IPv6Networkr)rstricts r ip_networkr9sk  7F++ 0 1    7F++ 0 1    {"PQ RRs    0AAc t|S#ttf$rYnwxYw t|S#ttf$rYnwxYwt |d)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. z0 does not appear to be an IPv4 or IPv6 interface) IPv4Interfacerr IPv6Interfacerrs r ip_interfacer"Vsg* W%% 0 1    W%% 0 1    {"RS TTrcV |jdS#t$r tdwxYw)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. z&Address negative or too large for IPv4to_bytes OverflowErrorrrs rv4_int_to_packedr(xs6C"" CABBC(cV |jdS#t$r tdwxYw)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. z&Address negative or too large for IPv6r%rs rv6_int_to_packedr,s6C## CABBCr)crt|jd}t|dkDrtd||S)zAHelper to split the netmask and raise AddressValueError if needed/zOnly one '/' permitted in )strsplitlenr)raddrs r_split_optional_netmaskr4s9 w<  c "D 4y1}">> 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 and  are not of the same versionz*last IP address must be greater than firstr$zunknown IP versionr6N) isinstance _BaseAddress TypeErrorversionrrr_max_prefixlenr9rArErB _ALL_ONES)r<r=r>ip_bits first_intlast_intnbitsnets rsummarize_address_rangerUs(4| , l +KLL }} $&' ' t|EFF }}  !  -..""G IxxH x .y'B )A-99;a?A)Wu_-. Q%Z q=BLL (  x s D$D)'D)c#XKt|}i}|rU|j}|j}|j|}||||<n||k7r||=|j ||rUd}t |j D]$}||j|jk\r||}&yw)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:to_mergesubnetsrTrYexistingr=s r_collapse_addresses_internalrbs2IHG lln<<>;;x(   #GH  _! OOH %  Dgnn&'  %%)>)>> (s A$B*'AB*cg}g}g}|D]}t|trD|r0|dj|jk7rt|d|dd|j |X|j |j k(rO|r0|dj|jk7rt|d|dd |j |j|r0|dj|jk7rt|d|dd|j |tt|}|r.t|D] \}}|jt||"t||zS#t$r|j |jYwxYw)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 iterable 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. rGrH)rJrK_versionrLr[ _prefixlenrNr>AttributeErrornetwork_addressr\setr?extendrUrb)r:addrsipsnetsr>r<r=s rcollapse_addressesrn0sq$ E C D b, 's2w''2;;6!#SW!.// JJrN ]]b// /s2w''2;;6!#SW!.// / 255!R))R[[8!#T"X!/00 KKO%* S C .s3KE4 LL0= >4 ( 55!" / 2--. /s-E!!$F F ct|tr|jSt|tr|j St S)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. )rJ _BaseNetwork_get_networks_keyrK_get_address_keyNotImplemented)objs rget_mixed_type_keyrufs<$#|$$$&& C &##%% r ceZdZdZdZedZedZedZedZ dZ dZ e d Z e d Ze d Ze d Ze d Ze dZdZy)_IPAddressBasezThe mother class.r c"|jS)z:Return the longhand version of the IP address as a string.)_explode_shorthand_ip_stringselfs rexplodedz_IPAddressBase.explodeds0022r ct|S)z;Return the shorthand version of the IP address as a string.r0rzs r compressedz_IPAddressBase.compresseds4yr c"|jS)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_pointerrzs rreverse_pointerz_IPAddressBase.reverse_pointers$$&&r c6dt|fz}t|)Nz%200s has no version specifiedtypeNotImplementedErrorr{msgs rrMz_IPAddressBase.versions.$t*>!#&&r c|dkrd}t|||jfz||jkDr'd}t|||j|jfzy)Nrz-%d (< 0) is not permitted as an IPv%d addressz2%d (>= 2**%d) is not permitted as an IPv%d address)rrerOrN)r{rrs r_check_int_addressz!_IPAddressBase._check_int_addresssg Q;AC#C7DMM*B$BC C T^^ #FC#C7D4G4G+/==+:%:; ; $r c`t|}||k7rd}t|||||jfzy)Nz6%r (len %d != %d) is not permitted as an IPv%d address)r2rre)r{r expected_len address_lenrs r_check_packed_addressz$_IPAddressBase._check_packed_addresssG'l , &JC#C7K+7+H%HI I 'r c:|j|j|z z S)zTurn the prefix length into a bitwise netmask Args: prefixlen: An integer, the prefix length. Returns: An integer. )rO)cls prefixlens r_ip_int_from_prefixz"_IPAddressBase._ip_int_from_prefixs}}  :;;r ct||j}|j|z }||z }d|zdz }||k7r1|jdz}|j|d}d}t||z|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 r6bigz&Netmask pattern %r mixes zeroes & ones)rErNr&r) rip_inttrailing_zeroesr leading_onesall_onesbyteslendetailsrs r_prefix_from_ip_intz"_IPAddressBase._prefix_from_ip_ints5V585G5GI&&8 0 Na' 8 #))Q.Hooh6G:CS7]+ +r c$d|z}t|d)Nz%r is not a valid netmask)r)r netmask_strrs r_report_invalid_netmaskz&_IPAddressBase._report_invalid_netmasks)K7$$.r c|jr|js|j| t|}dcxkr|j ksn|j||S#t$r|j|YFwxYw)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)isasciiisdigitrintrrN)r prefixlen_strrs r_prefix_from_prefix_stringz)_IPAddressBase._prefix_from_prefix_strings%%'M,A,A,C  ' ' 6 7M*IY4#"4"44  ' ' 6  7  ' ' 6 7s A((BBc* |j|} |jS#t$r|j|Y.wxYw#t$rYnwxYw|j z} |j|S#t$r|j|YywxYw)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_stringrrrrrO)rip_strrs r_prefix_from_ip_stringz%_IPAddressBase._prefix_from_ip_strings 0,,V4F **62 2! 0  ' ' / 0    #-- 0**62 2 0  ' ' / 0s2%AAA AA$A55BBct|ttfr||jfSt|ts t |}t |dkDr|S|d|jfS)zHelper function to parse address of Network/Interface. Arg: address: Argument of Network/Interface. Returns: (addr, prefix) tuple. r6r)rJbytesrrNtupler4r2)rrs r_split_addr_prefixz!_IPAddressBase._split_addr_prefixsa gs| ,C... .'5).g6G ws xxr c |j|jk(xr|j|jk(S#t$r tcYSwxYwr)r9rergrsr{others r__eq__z_BaseAddress.__eq__AsF "HH )8 7 9 "! ! "s36AAct|tstS|j|jk7rt |d|d|j |j k7r|j |j kSyNrGrHF)rJrKrsrerLr9rs r__lt__z_BaseAddress.__lt__Hs_%.! ! ==ENN *!5*+ + 88uyy 88eii' 'r cht|tstS|jt||zSrrJrrsrrs r__add__z_BaseAddress.__add__T*%%! !~~c$i%/00r cht|tstS|jt||z Srrrs r__sub__z_BaseAddress.__sub__Yrr cL|jjdt|dSN()rrr0rzs r__repr__z_BaseAddress.__repr__^>>22CI>>r cJt|j|jSr)r0_string_from_ip_intr9rzs r__str__z_BaseAddress.__str__as4++DHH566r cPttt|jSr)hashhexrr9rzs r__hash__z_BaseAddress.__hash__dsCDHH &''r c|j|fSrrerzs rrrz_BaseAddress._get_address_keygs t$$r c4|j|jffSr)rr9rzs rrz_BaseAddress.__reduce__js~~{**r c|r|ddk(rtt||Stddl}|j datj |}|st ||S|j\}}}|dk(r|jdk(rd}nd }|dk(r |j}n|jdz}|r ||dzd z z }|r|d z }tt||d |||S) aReturns an IP address as a formatted string. Supported presentation types are: 's': returns the IP address as a string (default) 'b': converts to binary and returns a zero-padded string 'X' or 'x': converts to upper- or lower-case hex and returns a zero-padded string 'n': the same as 'b' for IPv4 and 'x' for IPv6 For binary and hex presentation types, the alternate form specifier '#' and the grouping option '_' are supported. rdsNrz(#?)(_?)([xbnX])nr$bxr6r/0) formatr0_address_fmt_rerecompile fullmatchsuper __format__groupsrerNr) r{fmtrm alternategroupingfmt_basepadlenrs rrz_BaseAddress.__format__msc"gn#d)S) )  "  jj);>r c^t|jt|jz dzS)z&Number of hosts in the current subnet.r6)rr^rhrzs r num_addressesz_BaseNetwork.num_addresses s*4))*S1E1E-FFJJr c6dt|fz}t|)Nz%%200s has no associated address classrrs rrz_BaseNetwork._address_classs 6d E!#&&r c|jSr)rfrzs rrz_BaseNetwork.prefixlens r c#K|j|jk(st|d|dt|tstd|z|j |st |d|||k(ry|j |jd|j}|j\}}||k7rv||k7rq|j |r||j\}}n=|j |r||j\}}ntd|d|d |||k7r||k7rq||k(r|y||k(r|ytd|d|d |w) 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. rGrHz%s is not a network objectz not contained in Nr.z Error performing exclusion: s1: z s2: z other: ) rerLrJrp subnet_ofrrrhrr`AssertionError)r{rs1s2s raddress_excludez_BaseNetwork.address_excludesTH}}.!5*+ +%.85@A At$EF F D= 5+@+@+0??!<=BEkbEkr"B$B%&("e&566EkbEk ;H 5[H!"$b%"12 2s D'E*(Ec(|j|jk7rt|d|d|j|jkry|j|jkDry|j|jkry|j|jkDryy)aCompare two IP objects. This is only concerned about the comparison of the integer representation of the network addresses. This means that the host bits aren't considered at all in this method. If you want to compare host bits, you can easily enough do a 'HostA._ip < HostB._ip' Args: other: An IP object. Returns: If the IP versions of self and other are the same, returns: -1 if self < other: eg: IPv4Network('192.0.2.0/25') < IPv4Network('192.0.2.128/25') IPv6Network('2001:db8::1000/124') < IPv6Network('2001:db8::2000/124') 0 if self == other eg: IPv4Network('192.0.2.0/24') == IPv4Network('192.0.2.0/24') IPv6Network('2001:db8::1000/124') == IPv6Network('2001:db8::1000/124') 1 if self > 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. rGz are not of the same typerdr6r)rerLrhrrs rcompare_networksz_BaseNetwork.compare_networksgsB ==ENN *!5*+ +   %"7"7 7   %"7"7 7 <<%-- ' <<%-- 'r cH|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(). )rerhrrzs rrqz_BaseNetwork._get_networks_keys t33T\\BBr Nc#K|j|jk(r|y|9||jkr td|dk7r td||jz }|dkr td|j|z}||jkDrtd||fzt|j}t|j dz}t|j dz|z }t|||D]}|j||f}|yw)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 longerr6(cannot set prefixlen_diff and new_prefixrzprefix length diff must be > 0z0prefix length diff %d is invalid for netblock %s) rfrNrrrhr^r rr) r{prefixlen_diff new_prefix new_prefixlenstartendstepnew_addrcurrents rr`z_BaseNetwork.subnetss4 ??d11 1J   !DOO+ !<==" !KLL'$//9N A => >.8 4.. .B!4F))* *D(()$(()A-DMM"Q&>9eS$/Hnnh %>?GM0sD D ct|jdk(r|S|9||jkDr td|dk7r td|j|z }|j|z }|dkrtd|j|fz|jt |j t |j |zz|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) rznew prefix must be shorterr6r#z;current prefixlen is %d, cannot have a prefixlen_diff of %d)rfrrrrrhr)r{r$r%r&s rrYz_BaseNetwork.supernets* ??a K  !DOO+ !=>>" !KLL!__z9N7 1 M012 2~~ $$ %T\\):n)L M  r c^|jjxr|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. )rh is_multicastr^rzs rr/z_BaseNetwork.is_multicasts,$$114&&33 5r c |j|jk7rt|d|d|j|jkxr|j|jk\S#t$rtd|d|wxYw)NrGrHz*Unable to test subnet containment between )rerLrhr^rg)ars r _is_subnet_ofz_BaseNetwork._is_subnet_of s 4zzQZZ'1#U1#-I JKK%%):)::?''1+>+>> @ 4''(cqc34 4 4s AA A;c&|j||S)z1Return True if this network is a subnet of other.r2rs rrz_BaseNetwork.subnet_ofs!!$..r c&|j||S)z3Return True if this network is a supernet of other.r4rs r supernet_ofz_BaseNetwork.supernet_ofs!!%..r c^|jjxr|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. )rh is_reservedr^rzs rr9z_BaseNetwork.is_reserved,$$003&&22 4r c^|jjxr|jjSzTest if the address is reserved for link-local. Returns: A boolean, True if the address is reserved per RFC 4291. )rh is_link_localr^rzs rr=z_BaseNetwork.is_link_local+s,$$225&&44 6r ctfdjjDxr(tfdjjDS)zTest if this network belongs to a private range. Returns: A boolean, True if the network is reserved per iana-ipv4-special-registry or iana-ipv6-special-registry. c3ZK|]"}j|vxrj|v$ywrr ).0 priv_networkr{s r z*_BaseNetwork.is_private..?s=J'H|''<7:))\9:'H(+c3ZK|]"}j|vxrj|v$ywrr )r@rr{s rrBz*_BaseNetwork.is_private..As@R$P((7:**'9:#OrCany _constants_private_networksall_private_networks_exceptionsrzs`r is_privatez_BaseNetwork.is_private6sUJ'+'H'HJJNQR$(??#O#ORO r c|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. rKrzs r is_globalz_BaseNetwork.is_globalGs??""r c^|jjxr|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. )rhis_unspecifiedr^rzs rrQz_BaseNetwork.is_unspecifiedRs,$$336&&55 7r c^|jjxr|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. )rh is_loopbackr^rzs rrTz_BaseNetwork.is_loopback^r:r )r6N))rr r r rrrrrrrrrr  functoolscached_propertyr^r rrrrrrrrr rqr`rYr/ staticmethodr2rr6r9r=rKrNrQrTr r rrprps ?@ )) 6 "C L777GGAA>>??KK''I2V.`C3j'R 5 5 4 4// 4 466 ## 7 7 4 4r rpceZdZgZy)_BaseConstantsN)rr r rHr r rrYrYksr rYceZdZdZdZdZdezdz ZeZiZ dZ e dZ e dZ e d Ze d Zd Zed Zed Zy)_BaseV4zyBase IPv4 object. The following methods are used by IPv4 objects in both single IP addresses and networks. r r$r/r6ct|Srr~rzs rryz$_BaseV4._explode_shorthand_ip_strings 4yr cl||jvrxt|tr+|}d|cxkr|jks&n|j |n |j |}t|j|}||f|j|<|j|S#t $r|j|}YVwxYwaMake 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) _netmask_cacherJrrNrrrrrrrargrrs r _make_netmaskz_BaseV4._make_netmasks c(( (#s# Y<#*<*<<// :@ # > >s CI "#"9"9)"DEG&-y&8C  s #!!#&& )@!$ : :3 ?I@s BB32B3c|s td|jd}t|dk7rtd|z tj t |j |dS#t$r}t|d|dd}~wwxYw)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.r$zExpected 4 octets in %rr in N)rr1r2r from_bytesmap _parse_octetr)rroctetsexcs rrz_BaseV4._ip_int_from_strings#$=> >c" v;! #$=$FG G J>>#c&6&6"?G G J##v$>?T I Js)A&& B/BBc4|s td|jr|jsd}t||zt|dkDrd}t||z|dk7r|ddk(rd}t||zt |d}|d kDrtd |z|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 %rrrz%Leading zeros are not permitted in %r zOctet %d (> 255) not permitted)rrrr2r)r octet_strr octet_ints rriz_BaseV4._parse_octets89 9!!# (9(9(;7CS9_- - y>A 8CS9_- -   !  39CS9_- - 2& s?= IJ Jr c `djtt|jddS)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. rer$r)joinrhr0r&)rrs rrz_BaseV4._string_from_ip_ints%xxCE!:;<.KI'H 'Hc3&K|]}|v ywrr rs rrBz)IPv4Address.is_private..L\/[DO/[rrErzs`rrKzIPv4Address.is_private9s@$ It'H'HI I ]\t/[/[\\ r cP||jjvxr |j Sar``True`` if the address is defined as globally reachable by iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_ (for IPv6) with the following exception: For IPv4-mapped IPv6-addresses the ``is_private`` value is determined by the semantics of the underlying IPv4 addresses and the following condition holds (see :attr:`IPv6Address.ipv4_mapped`):: address.is_global == address.ipv4_mapped.is_global ``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10`` IPv4 range where they are both ``False``. )rG_public_networkrKrzs rrNzIPv4Address.is_globalOs% 4??:::R4???RRr c2||jjvS)zTest if the address is reserved for multicast use. Returns: A boolean, True if the address is multicast. See RFC 3171 for details. rG_multicast_networkrzs rr/zIPv4Address.is_multicastat9999r c4||jjk(S)zTest if the address is unspecified. Returns: A boolean, True if this is the unspecified address as defined in RFC 5735 3. )rG_unspecified_addressrzs rrQzIPv4Address.is_unspecifiedlst;;;;r c2||jjvS)zTest if the address is a loopback address. Returns: A boolean, True if the address is a loopback per RFC 3330. )rG_loopback_networkrzs rrTzIPv4Address.is_loopbackwst8888r c2||jjvS)zTest if the address is reserved for link-local. Returns: A boolean, True if the address is link-local per RFC 3927. rG_linklocal_networkrzs rr=zIPv4Address.is_link_localt9999r N)rr r r rrrrr9rU lru_cacherKrNr/rQrTr=r r rrrs9&I!6F**99Y  (YSS ::<<99::r rceZdZdZej dZdZdZdZ dZ e jZ e dZe dZe d Ze d Zy ) r c|j|\}}tj||t||fd|_|jj |_|jj |_yNF)r)rrrrrrrfr{rr3masks rrzIPv4Interface.__init__Z,,W5 dT4("D$<> ||++ ,,11r c.|jjSrrr rzs rr zIPv4Interface.hostmask||$$$r cVd|j|j|jfzSrrr9rfrzs rrzIPv4Interface.__str__s,$22488<//++ +r ctj||}|tus|s|S |j|jk(S#t$rYywxYwr)rrrsrrgr{r address_equals rrzIPv4Interface.__eq__P#**47 N *-  <<5==0 0   = A A ctj||}|turtS |j|jkxs|j|jk(xr|S#t$rYywxYwr)rrrsrrgr{r address_lesss rrzIPv4Interface.__lt__sl"))$6 > )! ! LL5==0CLLEMM1Bl D  s7A A*)A*ct|j|jt|jj fSrrr9rfrrrhrzs rrzIPv4Interface.__hash__+TXXtDLL4P4P0QRSSr c,t|jSr)rr9rzs rr>zIPv4Interface.ip488$$r cV|j|jd|jSrrrzs rrzIPv4Interface.with_prefixlen%22488<//+ +r cV|j|jd|jSrrr9rrzs rrzIPv4Interface.with_netmask%22488<,,( (r cV|j|jd|jSrrr9r rzs rrzIPv4Interface.with_hostmask%22488<--) )r N)rr r rrUrVr rrrrrwrrr>rrrr r rr r s2%%+  T **J %%++(())r r cTeZdZdZeZddZeejdZ y)raeThis 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 c|j|\}t|_|j|\|_|_t |j}|t |jz|k7r6|rtd|zt|t |jz|_|j |jdz k(r|j|_ y|j |jk(r fd|_ yy)aInstantiate a new IPv4 network object. Args: address: A string or integer representing the IP [& network]. '192.0.2.0/24' '192.0.2.0/255.255.255.0' '192.0.2.0/0.0.0.255' are all functionally the same in IPv4. Similarly, '192.0.2.1' '192.0.2.1/255.255.255.255' '192.0.2.1/32' are also functionally equivalent. That is to say, failing to provide a subnetmask will create an object with a mask of /32. If the mask (portion after the / in the argument) is given in dotted quad form, it is treated as a netmask if it starts with a non-zero field (e.g. /255.0.0.0 == /8) and as a hostmask if it starts with a zero field (e.g. 0.255.255.255 == /8), with the single exception of an all-zero mask which is treated as a netmask == /0. If no mask is given, a default of /32 is used. Additionally, an integer can be passed, so IPv4Network('192.0.2.1') == IPv4Network(3221225985) or, more generally IPv4Interface(int(IPv4Interface('192.0.2.1'))) == IPv4Interface('192.0.2.1') Raises: AddressValueError: If ipaddress isn't a valid IPv4 address. NetmaskValueError: If the netmask isn't valid for an IPv4 address. ValueError: If strict is True and a network address is not supplied. %s has host bits setr6ctgSr)rr3srz&IPv4Network.__init__.. +d"3!4r N) rrrhrbrrfrrrNrrr{rrrrr3s @rrzIPv4Network.__init__sF,,W5 d*40(,(:(:4(@% doT))* C % % / !7$!>??'2636t||3D4E(F$ ??t22Q6 7DJ __!4!4 54DJ6r c|jtdvxr|jtdv xr |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. 100.64.0.0/10)rhrr^rKrzs rrNzIPv4Network.is_globalsJ))[-IIK**k/.JJL$OO# %r NT) rr r r rrrrrUrrNr r rrrs; !N25hY % %r rcTeZdZedZedZedZedZededededededed ed ed ed ed edededgZededgZ edZ e dZ y)_IPv4Constantsz169.254.0.0/16z 127.0.0.0/8z 224.0.0.0/4rz 0.0.0.0/8z 10.0.0.0/8z 172.16.0.0/12z 192.0.0.0/24z192.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/32z 192.0.0.9/32z 192.0.0.10/32z0.0.0.0N) rr r rrrrrrHrJrrrr r rrr!s$%56#M2$]3!/2O K L!M"$%O$N#$%N#$%O$%&$%M"() $ N#O$$ $M2&y1r rceZdZdZdZdZdezdz ZdZe dZ eZ iZ e dZe d Ze d Ze d Ze dd ZdZdZedZedZedZy )_BaseV6zyBase IPv6 object. The following methods are used by IPv6 objects in both single IP addresses and networks. r rIr/r6r0123456789ABCDEFabcdefc*||jvrwt|tr+|}d|cxkr|jks%n|j |n|j |}t |j|}||f|j|<|j|Sr^)r_rJrrNrrrrr`s rrbz_BaseV6._make_netmask_s c(( (#s# Y<#*<*<<// :::3? !#"9"9)"DEG&-y&8C  s #!!#&&r cF|s td|jd}d}t||krd||fz}t|d|dvrU t|j j }|j d |d z d zz|j d |d zz|jd z}t||kDrd |d z |fz}t|d}td t|d z D]} || r |d|z}t|| }||} t||z d z } |ds| d z} | rd}t||z|ds| d z} | rd}t||z|j| | zz } | d krd}t||jd z |fzt||jk7rd}t||j|fz|dsd}t||z|dsd}t||zt|} d} d} d} t| D]} | d z} | |j|| z} | d | zz} t| dD]} | d z} | |j|| z} | S#t$r}t|d|dd}~wwxYw#t$r}t|d|dd}~wwxYw)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. rd:rmz At least %d parts expected in %rrerdrfN%xr+r6z!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) rr1r2rrXr9r[ _HEXTET_COUNTr _parse_hextetr)rrparts _min_partsripv4_intrk _max_parts skip_indexiparts_hiparts_lo parts_skippedrs rrz_BaseV6._ip_int_from_stringss/#$=> > S! u: "4 F7KKC#C( ( %)  N&uyy{377 LL(b.F!:; < LLF!23 4 &&* u: "5Av8NNC#C( ( q#e*q.)A8)?T I Js1 #I9A%J I>(I99I> J  JJ c|jj|std|zt|dkDrd}t||zt |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 %rr$z$At most 4 characters permitted in %rr+) _HEX_DIGITS issupersetrr2r)r hextet_strrs rrz_BaseV6._parse_hextetsW ))*5>KL L z?Q 8CS:-. .:r""r cd}d}d}d}t|D]%\}}|dk(r|dz }|dk(r|}||kDs|}|}"d}d}'|dkDr*||z}|t|k(r|dgz }dg||||dk(rdg|z}|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. rdrrr6) enumerater2) rhextetsbest_doublecolon_startbest_doublecolon_lendoublecolon_startdoublecolon_lenindexhextetbest_doublecolon_ends r_compress_hextetsz_BaseV6._compress_hextetss""$ &w/ME6}1$$*(-%"%99+:(->*"#$&!0 ! #$:$8%9 $s7|3B4DF4G*+? @%*$.r Nc |t|j}||jkDr tdd|z}t dddDcgc]}dt|||dzdz}}|j |}dj |Scc}w) 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. zIPv6 address is too large%032xrrr$rr+r)rr9rOrrrrs)rrhex_strrrs rrz_BaseV6._string_from_ip_int%s >\F CMM !89 9F";@B?K?a4#ga!nb11?K''0xx  LsBct|trt|j}n1t|trt|j }n t|}|j |}d|z}tdddDcgc] }|||dz }}t|ttfr ddj||jfzSdj|Scc}w)zmExpand a shortened IPv6 address. Returns: A string, the expanded IPv6 address. rrrr$rr) rJrr0rhr!r>rrrprsrf)r{rrrrrs rryz$_BaseV6._explode_shorthand_ip_string?s dK (--.F m ,\FYF))&1F").q"a9A1Q39 d\=9 :chhuot?? ?xx:s=Ccn|jdddjdd}dj|dzS)zReturn the reverse DNS pointer name for the IPv6 address. This implements the method described in RFC3596 2.5. Nrdrrrez .ip6.arpa)r|replacers)r{ reverse_charss rrz_BaseV6._reverse_pointerTs7  dd+33C< xx &44r cl|jd\}}}|sd}||fS|rd|vrtd|z||fS)zHelper function to parse IPv6 string address with scope id. See RFC 4007 for details. Args: ip_str: A string, the IPv6 address. Returns: (addr, scope_id) tuple. %NzInvalid IPv6 address: "%r") partitionr)rr3sepscope_ids r_split_scope_idz_BaseV6._split_scope_id]sU%..s3c8HX~SH_#$@6$IJ JX~r c|jSrrwrzs rrxz_BaseV6.max_prefixlenqryr c|jSrrrzs rrMz_BaseV6.versionur{r r)rr r r rre IPV6LENGTHrOr frozensetrrNr_rrbrrrrryrrWrrrxrMr r rrrKsIHJ!#IM45KNN''&eJeJN##2--^!!2*5&##r rc:eZdZdZdZdZfdZdZfdZdZ e dZ e d Z e d Z e d Ze d Ze d Ze ej$dZe dZe dZe dZe dZe dZe dZxZS)rz/Represent and manipulate single IPv6 Addresses.)r9 _scope_idr~ct|tr |j|||_d|_yt|t r5|j |dtj|d|_d|_yt|}d|vrtd||j|\}|_|j||_y)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. Nr+rr.r) rJrrr9rrrrgr0rrrrs rrzIPv6Address.__init__s$ gs #  # #G ,DH!DN  gu %  & &w 3~~gu5DH!DN w< (?#&8 $DE E#'#7#7#A $.++H5r c`t|}|jr|dz|jzS|S)Nr)rrr)r{rrs rrzIPv6Address.__str__s-"04v|dnn,JFJr cDt|j|jfSr)rr9rrzs rrzIPv6Address.__hash__sTXXt~~.//r cxt||}|turtS|sy|jt |ddk(S)NFr)rrrsrgetattr)r{rrrs rrzIPv6Address.__eq__s=u- N *! !~~ T!BBBr c2|jt|ffSrrrzs rrzIPv6Address.__reduce__sT --r c|jS)zIdentifier of a particular zone of the address's scope. See RFC 4007 for details. Returns: A string identifying the zone of the address if specified, else None. )rrzs rrzIPv6Address.scope_ids~~r c,t|jSr)r,r9rzs rrzIPv6Address.packedrr c2||jjvSr.rrzs rr/zIPv6Address.is_multicastrr cTtfdjjDS)r8c3&K|]}|v ywrr )r@rr{s rrBz*IPv6Address.is_reserved..sI&H419&Hr)rFrG_reserved_networksrzs`rr9zIPv6Address.is_reserveds Idoo&H&HIIIr c2||jjvSr<rrzs rr=zIPv6Address.is_link_localrr c2||jjvSa`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. )rG_sitelocal_networkrzs r is_site_localzIPv6Address.is_site_localst9999r cj}| |jStfdjjDxr(t fdjj DS)rc3&K|]}|v ywrr rs rrBz)IPv6Address.is_private..rrc3&K|]}|v ywrr rs rrBz)IPv6Address.is_private..rr) ipv4_mappedrKrFrGrHrIrJr{rs` rrKzIPv6Address.is_privates]"&&  ")) ) It'H'HI I ]\t/[/[\\ r c|j SrrMrzs rrNzIPv6Address.is_globals??""r c |jdk(S)rPrrrzs rrQzIPv6Address.is_unspecified#sxx1}r cT|j}| |jS|jdk(S)rSr6)rrTr9rs rrTzIPv6Address.is_loopback.s/&&  "** *xx1}r cX|jdz dk7ryt|jdzS)zReturn the IPv4 mapped address. Returns: If the IPv6 address is a v4 mapped address, return the IPv4 mapped address. Return None otherwise. rrNr9rrzs rrzIPv6Address.ipv4_mapped<s+ HHNv %488j011r c|jdz dk7ryt|jdz dzt|jdzfS)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 N@rr rzs rteredozIPv6Address.teredoIsI HHNz )TXX^z9:TXXI 235 5r c^|jdz dk7ryt|jdz dzS)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 NPrr rzs r sixtofourzIPv6Address.sixtofourXs/ HHO &DHHNj899r )rr r r rrrrrrrrrr/r9r=rrUrrKrNrQrTrr$r(rrs@rrrzs@93I&6PK0C.  **::JJ:: : :Y  .##    2 2 5 5 : :r rceZdZdZej dZfdZdZdZ dZ e jZ e dZe dZe d Ze d Ze d Ze fd ZxZS) r!c|j|\}}tj||t||fd|_|jj |_|jj |_yr)rrrrrrrfrs rrzIPv6Interface.__init__hrr c.|jjSrrrzs rr zIPv6Interface.hostmaskprr c>dt||jfzSr)rrrfr{rs rrzIPv6Interface.__str__ts%%'/+//++ +r ctj||}|tus|s|S |j|jk(S#t$rYywxYwr)rrrsrrgrs rrzIPv6Interface.__eq__xrrctj||}|tur|S |j|jkxs|j|jk(xr|S#t$rYywxYwr)rrrsrrgrs rrzIPv6Interface.__lt__sl"))$6 > )  LL5==0CLLEMM1Bl D  s7A A&%A&ct|j|jt|jj fSrrrzs rrzIPv6Interface.__hash__rr c,t|jSr)rr9rzs rr>zIPv6Interface.iprr cV|j|jd|jSrrrzs rrzIPv6Interface.with_prefixlenrr cV|j|jd|jSrrrzs rrzIPv6Interface.with_netmaskrr cV|j|jd|jSrrrzs rrzIPv6Interface.with_hostmaskrr cP|jdk(xr|jjS)Nr)r9rrQrzs rrQzIPv6Interface.is_unspecifiedsxx1}<!rrrrQrTrrs@rr!r!fs2%%+  T **J %%++(())==@@r r!c2eZdZdZeZddZdZedZ y)ravThis 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 c|j|\}t|_|j|\|_|_t |j}|t |jz|k7r6|rtd|zt|t |jz|_|j |jdz k(r|j|_ y|j |jk(r fd|_ yy)aInstantiate a new IPv6 Network object. Args: address: A string or integer representing the IPv6 network or the IP and prefix/netmask. '2001:db8::/128' '2001:db8:0000:0000:0000:0000:0000:0000/128' '2001:db8::' are all functionally the same in IPv6. That is to say, failing to provide a subnetmask will create an object with a mask of /128. Additionally, an integer can be passed, so IPv6Network('2001:db8::') == IPv6Network(42540766411282592856903984951653826560) or, more generally IPv6Network(int(IPv6Network('2001:db8::'))) == IPv6Network('2001:db8::') strict: A boolean. If true, ensure that we have been passed A true network address, eg, 2001:db8::1000/124 and not an IP address on a network, eg, 2001:db8::1/124. Raises: AddressValueError: If address isn't a valid IPv6 address. NetmaskValueError: If the netmask isn't valid for an IPv6 address. ValueError: If strict was True and a network address was not supplied. rr6ctgSr)rrsrrz&IPv6Network.__init__..rr N) rrrhrbrrfrrrNrrrs @rrzIPv6Network.__init__s>,,W5 d*40(,(:(:4(@% doT))* C % % / !7$!>??'2636t||3D4E(F$ ??t22Q6 7DJ __ 3 3 34DJ4r c#Kt|j}t|j}t|dz|dzD]}|j |yw)zGenerate Iterator over usable hosts in a network. This is like __iter__ except it doesn't return the Subnet-Router anycast address. r6Nrrs rrzIPv6Network.hostssRd**+../ w{IM2A%%a( (3sAAc^|jjxr|jjSr)rhrr^rzs rrzIPv6Network.is_site_locals,$$225&&44 6r Nr) rr r r rrrrrrr r rrrs- !N.5` ) 6 6r rceZdZedZedZededededededed ed ed edg Zed ed ededededgZededededededededededededededed gZed!Z y")#_IPv6Constantsz fe80::/10zff00::/8z::1/128z::/128z ::ffff:0:0/96z64:ff9b:1::/48z100::/64z 2001::/23z 2001:db8::/32z 2002::/16zfc00::/7z 2001:1::1/128z 2001:1::2/128z 2001:3::/32z2001:4:112::/48z 2001:20::/28z 2001:30::/28z::/8z100::/8z200::/7z400::/6z800::/5z1000::/4z4000::/3z6000::/3z8000::/3zA000::/3zC000::/3zE000::/4zF000::/5zF800::/6zFE00::/9z fec0::/10N) rr r rrrrHrJrrr r rr=r= s6$[1$Z0 IHO$$%JK O$K JK    O$O$M"%&N#N# $  F[3I I 6I J 7JZ!8JZ!8JZ!8JZ!8J %[1r r=r)%r __version__rUr|rrrrrrr"r(r,r4r?rErUrbrnrurwrtotal_orderingrKrprYrGr[rr rrrrr!rr=r r rr@s    0 00 0S:S:UDC( C ( : 4n.b36l2o,o,d hN>hNhNV H4>H4H4V ) JJZI:'<I:X@)K@)FO%'<O%d#2#2L( ' ll^ i:'<i:XH@KH@VY6'<Y6x*2*2Z( ' r