^gfdZddlZddlmZmZmZmZmZm Z ddl m Z m Z m Z dZdZGddeZy) z2Set based operations for IP addresses and subnets.N) IPNetwork IPAddressIPRange cidr_merge cidr_excludeiprange_to_cidrs) _sys_maxint _dict_keys _int_typec,|jj}||}|j|jkDr+|j||j|jdz f|dz }|}|t |krk||}||vrna|j dz|jk(rn.|j||j dz|jdz f|dz }|}|t |krk|j dz}|j } || kr|j||| f|S)aCalculate IPSet([supernet]) - IPSet(subnets). Assumptions: subnets is sorted, subnet_idx points to the first element in subnets that is a subnet of supernet. Results are appended to the ranges parameter as tuples of in format (version, first, last). Return value is the first subnet_idx that does not point to a subnet of supernet (or len(subnets) if all subsequents items are a subnet of supernet). )_moduleversionfirstappendlenlast) supernetsubnets subnet_idxrangesrsubnet prev_subnet cur_subnetrrs 1/usr/lib/python3/dist-packages/netaddr/ip/sets.py _subtractrs&&G Z F ||hnn$ w q0@AB!OJK s7| #Z( X %    a :#3#3 3  MM7K$4$4q$8*:J:JQ:NO Pa   s7| #   q E ==D } wt,- c#K|sy|d\}}}|ddD]6\}}}||dzk(r||k(r|}t||t||f|}|}|}8t||t||fyw)aqIterate over sorted_ranges, merging where possible Sorted ranges must be a sorted iterable of (version, first, last) tuples. Merging occurs for pairs like [(4, 10, 42), (4, 43, 100)] which is merged into (4, 10, 100), and leads to return value ( IPAddress(10, 4), IPAddress(100, 4) ), which is suitable input for the iprange_to_cidrs function. Nrr )r) sorted_rangescurrent_version current_start current_stop next_version next_start next_stops r_iter_merged_rangesr&8s 3@3C0O]L/z%IPSet.__setstate__..s- 5* 7y)7 ; ; 5sTN)r1r2r))r5states r __setstate__zIPSet.__setstate__s$ mm 5.3 5  rc|j}|j}|j}|j|jj k(r3|j D]}||jvs|j|=yng}|jD]s}|jj|k7s||k(r"|j}|j} ||k\r| |kr|j|V||ks\| |k\sb|j|=|rJy|D]} |j| =|jj |jz } |jdk7r|j| z dz} | r|j} n|j} | |jvry|j| =|j|=|xjdzc_ | dz } |j| z | z|_ d|j|<|jdk7ryy)a Same as compact(), but assume that added_network is the only change and that this IPSet was properly compacted before added_network was added. This allows to perform compaction much faster. added_network must already be present in self._cidrs. Nrr T) rrr _prefixlenrwidthrr)rrB_valuepreviousnext)r5 added_network added_first added_last added_versionpotential_supernet to_remover0rritem shift_widththe_bit candidates r_compact_single_networkzIPSet._compact_single_networks$)) "'' %--   # #}'<'<'B'B B'4&<&<&> "%4 M2  I  LL((M9T]=R yyK'DJ,>$$T*k)dj.@ M2!**M  " &KK% &$++11M4K4KK %%*$++{:a?G)224 )..0  + I& M*  # #q ( # 1 K$1$8$8K$GK#WM )-DKK &)%%*rcdt|j}tj|d|_y)zR Compact internal list of `IPNetwork` objects using a CIDR merge. TN)rr)r1r2r5cidrss rcompactz IPSet.compacts$4;;'mmE40 rctd)z Raises ``TypeError`` if this method is called. .. note:: IPSet objects are not hashable and cannot be used as dictionary keys or as members of other sets. zIP sets are unhashable!) TypeErrorr5s r__hash__zIPSet.__hash__s122rct|}||jvry|jr1|xjdzc_||jvry|jr1y)z :param ip: An IP address or subnet. :return: ``True`` if IP address or subnet is a member of this IP set. Tr F)rr)rG)r5iprs r __contains__zIPSet.__contains__sUR= t{{ "!!   1 $ 4;;&!!rc,t|jS)z9Return True if IPSet contains at least one IP, else False)boolr)r]s r __nonzero__zIPSet.__nonzero__sDKK  rcLtjt|jS)zP :return: an iterator over the IP addresses within this IP set. ) _itertoolschainsortedr)r]s r__iter__zIPSet.__iter__s !455rc,t|jS)zU :return: an iterator over individual IP subnets within this IP set. )rhr)r]s rr3zIPSet.iter_cidrssdkk""rct|trRtjt |d|dd}|j j ||jyt|tr |j}n2t|trtt||}n t|}d|j |<|j|y)a< Adds an IP address or subnet or IPRange to this IP set. Has no effect if it is already present. Note that where possible the IP address or subnet is merged with other members of the set to form more concise CIDR blocks. :param addr: An IP address or subnet in either string or object form, or an IPRange object. :param flags: decides which rules are applied to the interpretation of the addr value. See the netaddr.core namespace documentation for supported constant values. rr,TNr-) r/rr1r2rr)updaterZrr0r rrV)r5r8r. new_cidrss raddz IPSet.add s dG $  a$r(3T;I KK  y ) LLN  dI &99D i (Yt59:DT?D  D $$T*rct|tr+t|d|d}|D]}|j|yt|trt ||}n t |}|j|d}d}|jD]}||vst||}|}n|$|j|=|D]}d|j|<yy)a  Removes an IP address or subnet or IPRange from this IP set. Does nothing if it is not already a member. Note that this method behaves more like discard() found in regular Python sets because it doesn't raise KeyError exceptions if the IP address or subnet is question does not exist. It doesn't make sense to fully emulate that behaviour here as IP sets contain groups of individual IP addresses as individual set members using IPNetwork objects. :param addr: An IP address or subnet, or an IPRange. :param flags: decides which rules are applied to the interpretation of the addr value. See the netaddr.core namespace documentation for supported constant values. rr,Nr-T) r/rrremover rrrnr)r)r5r8r.rYr0 remainder matching_cidrs rrpz IPSet.remove*s& dG $$T!Wd2h7E " D! "  dI &T/DT?D   KK Dt|(t4 $      M*! )$( D! ) !rc<|jjdS)z Removes and returns an arbitrary IP address or subnet from this IP set. :return: An IP address or subnet. r)r)popitemr]s rpopz IPSet.popbs{{""$Q''rc*|j|}| S)z :param other: an IP set. :return: ``True`` if this IP set has no elements (IP addresses or subnets) in common with other. Intersection *must* be an empty set. ) intersection)r5otherresults r isdisjointzIPSet.isdisjointks""5)zrcp|j}|jj|j|S)z':return: a shallow copy of this IP set.) __class__r)rl)r5obj_copys rcopyz IPSet.copyvs)>>#t{{+rc Dt|trUtjdt t |j t |j zDd|_yt|ttfr|j|yt|ds tdg}|D]0}t|tr t||}|j|2t t |j |zD]}d|j |<|jy)a~ Update the contents of this IP set with the union of itself and other IP set. :param iterable: an iterable containing IP addresses, subnets or ranges. :param flags: decides which rules are applied to the interpretation of the addr value. See the netaddr.core namespace documentation for supported constant values. c3 K|]}|yw)N)r@r`s rrCzIPSet.update..sIIs TNrizan iterable was expected!r-)r/r(r1r2rr r)rrrnhasattrr\r rrrZr4s rrlz IPSet.update|s h &--IjDKK)@+5hoo+F*GHIJNPDK  9g"6 7 HHX  x,78 8  #D$ * U3   T " # z$++6BC %D $DKK  % rci|_y)z5Remove all IP addresses and subnets from this IP set.Nr)r]s rclearz IPSet.clears  rc` |j|jk(S#t$r tcYSwxYw)z :param other: an IP set :return: ``True`` if this IP set is equivalent to the ``other`` IP set, ``False`` otherwise. r)AttributeErrorNotImplementedr5rxs r__eq__z IPSet.__eq__/ ";;%,,. . "! ! " --c` |j|jk7S#t$r tcYSwxYw)z :param other: an IP set :return: ``False`` if this IP set is equivalent to the ``other`` IP set, ``True`` otherwise. rrs r__ne__z IPSet.__ne__rrc~t|dstS|j|jkxr|j|S)z :param other: an IP set :return: ``True`` if this IP set is less than the ``other`` IP set, ``False`` otherwise. r))rrsizeissubsetrs r__lt__z IPSet.__lt__s5uh'! !yy5::%>$--*>>rc2|jD]}||vsyy)z :param other: an IP set. :return: ``True`` if every IP address and subnet in this IP set is found within ``other``. FTrr5rxr0s rrzIPSet.issubsets'KK D5  rc~t|dstS|j|jkDxr|j|S)z :param other: an IP set. :return: ``True`` if this IP set is greater than the ``other`` IP set, ``False`` otherwise. r))rrr issupersetrs r__gt__z IPSet.__gt__s5uh'! !yy5::%@$//%*@@rcVt|dstS|jD]}||vsyy)z :param other: an IP set. :return: ``True`` if every IP address and subnet in other IP set is found within this one. r)FT)rrr)rs rrzIPSet.issupersets7uh'! !LL D4 rcH|j}|j||S)z :param other: an IP set. :return: the union of this IP set and another as a new IP set (combines IP addresses and subnets from both sets). )r~rl)r5rxip_sets runionz IPSet.unions  e rcxi}t|j}t|j}d}d}t|}t|}||kr]||krX||} ||} | | k(rd|| <|dz }|dz }n.| | vr d|| <|dz }n| | vr d|| <|dz }n| | kr|dz }n|dz }||kr||krXt} || _| S)z :param other: an IP set. :return: the intersection of this IP set and another as a new IP set. (IP addresses and subnets common to both sets). rTr )rhr)rr() r5rx result_cidrsown_nets other_netsown_idx other_idxown_len other_lenown_cur other_currys rrwzIPSet.intersections $++&ELL)  h- O I $9w'G"9-I)#(, W%1 Q I%(, W%1 g%*. Y'Q Y&qLGNI'I $9.$  rc"g}t|j}t|j}d}d}t|}t|}||kr||kr||} ||} | | k(r |dz }|dz }n| | vrt| |||}|dz }n| | vrt| |||}|dz }n| | krB|j | j j | j| jf|dz }nA|j | j j | j| jf|dz }||kr||kr||krL||} |j | j j | j| jf|dz }||krL||krL||} |j | j j | j| jf|dz }||krLt} t|D]'\} } t| | }|D]}d| j|<)| S)z :param other: an IP set. :return: the symmetric difference of this IP set and another as a new IP set (all IP addresses and subnets that are in exactly one of the sets). rr T) rhr)rrrrrrrr(r&r)r5rx result_rangesrrrrrrrrrystartstoprYr0s rsymmetric_differencezIPSet.symmetric_difference+sG $++&ELL)  h- O I $9w'G"9-I)#1 Q I%#Ix-PQ g%%gz9mT 1 Y&!(('//*A*A*1--*GHqLG!(()*;*;*C*C*3//9>>*KLNI-I $94w'G  '//"9"9")--"? @ qLG )#"9-I  )"3"3";";"+//9>>"C D NI )# .}= +KE4$UD1E +&* d# + + rcg}i}t|j}t|j}d}d}t|}t|} ||kra|| kr\||} ||} | | k(r |dz }|dz }n7| | vr|dz }n-| | vrt| |||}|dz }n| | kr d|| <|dz }n|dz }||kr|| kr\||krd|||<|dz }||krt |D]\} } t | | D]}d||< t }||_|S)z :param other: an IP set. :return: the difference between this IP set and another as a new IP set (all IP addresses and subnets that are in this IP set but not found in the other.) rr T)rhr)rrr&rr()r5rxrrrrrrrrrrrrr0rys r differencezIPSet.differenceosj  $++&ELL)  h- O I $9w'G"9-I)#1 Q I%1 g%%gz9&35 1 Y&,0L)qLGNI'I $9..2L'* + qLG/}= *KE4(5 *%) T" * *$  rcT|j}|tkDrtdtz|S)z :return: the cardinality of this IP set (i.e. sum of individual IP addresses). Raises ``IndexError`` if size > maxint (a Python limitation). Use the .size property for subnets of any size. zUrange contains more than %d (sys.maxint) IP addresses!Use the .size property instead.)rr IndexError)r5rs r__len__z IPSet.__len__s9 yy + 24?@A A rcft|jDcgc]}|jc}Scc}w)z The cardinality of this IP set (based on the number of individual IP addresses including those implicitly defined in subnets). )sumr)rr=s rrz IPSet.sizes% $++6$DII6776s.cjdt|jDcgc] }t|c}zScc}w)z8:return: Python statement to create an equivalent objectz IPSet(%r))rhr)str)r5cs r__repr__zIPSet.__repr__s(fT[[.ABc!fBBBBs0c|j}t|dkDr!|dd}|D]}|d|k7ry|ddz}y)z Returns True if the members of the set form a contiguous IP address range (with no gaps), False otherwise. :return: ``True`` if the ``IPSet`` object is contiguous. r rFr,T)r3r)r5rYrJr0s r iscontiguouszIPSet.iscontiguoussX! u:>Qx{H (7h& 8a< (rc|jr+|j}|syt|dd|ddStd)z Generates an IPRange for this IPSet, if all its members form a single contiguous sequence. Raises ``ValueError`` if the set is not contiguous. :return: An ``IPRange`` for all IPs in the IPSet. Nrr,zIPSet is not contiguous)rr3r ValueErrorrXs riprangez IPSet.iprangesK    OO%E58A;b " 6 667 7rc#K|jDcgc]/}|jj|j|jf1}}t |D]\}}t ||ycc}ww)zGenerate the merged IPRanges for this IPSet. In contrast to self.iprange(), this will work even when the IPSet is not contiguous. Adjacent IPRanges will be merged together, so you get the minimal number of IPRanges. N)r3rrrrr&r)r5r0rrrs r iter_iprangeszIPSet.iter_iprangessq"&!24,,.. DIIF4 4/}= 'KE4%& & '4sA24A- )A2)Nr)r)/__name__ __module__ __qualname____doc__ __slots__r9r<rErVrZr^rard__bool__rir3rnrprurzr~rlrrrrr__le__rr__ge__r__or__rw__and__r__xor__r__sub__rpropertyrr__str__rrrrrrr(r(Us *I->DH.T13&!H6 # +B4)p(  !F " " ? F A F F(TG@D#G2hG 88CG 8" 'rr()r itertoolsrf netaddr.iprrrrrrnetaddr.compatr r r rr&objectr(rrrrs8 9$$>=%P5:W 'FW 'r