-=Mc@s2dZdZddlZddlZi dd6dd6dd6dd 6dd 6dd 6dd 6dd 6dd6ZiPdd6ddd6ddd6dddd 6dddd d6dddd6dd6dd6dd6dd6dd 6dd!6d"d#6d$d%6d&d'6d(d)dd6d*d+6d,d-6d.d/6d0d16d0d26d0d36d,d46d5d66d,d76d8d96d0d:6d0d;6d.d<6d0d=6d0d>6d0d?6d0d@6d0dA6ddB6d0dC6dDdE6d,dF6d0dG6d.dH6d0dI6d0dJ6d0dK6d,dL6d,dM6d,dN6dOdP6d0dQ6d,dR6d.dS6d.dT6d.dU6d8dV6d0dW6dDdX6ddY6ddZ6dd[6dd\6dd]6dd^6dd_6dd`6dda6ddb6ddc6ddde6ddf6dgdh6ddi6djdk6dldm6dndo6dpdq6drds6dtdu6dvdw6dxdy6dzd{6d|d}6Zd~fdYZdefdYZdZdZ dZ dZ dZ idd6d!d 6dd6d[d6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dad6dd6Z dZdZdZdZdZdZdZdZedkr.ddlZej\ZZer+ddlZejdnndS(s IPy - class and tools for handling of IPv4 and IPv6 addresses and networks. See README file for learn how to use IPy. Further Information might be available at: https://github.com/haypo/python-ipy s0.75iNtPUBLICt0tPRIVATEt00000000t00001010t01111111t1t1010100111111110t 101011000001t1100000010101000tRESERVEDt111i`t UNSPECIFIEDitLOOPBACKitIPV4MAPiPit WKP46TRANSt 00000000011001001111111110011011i@t UNASSIGNEDt00000001t0000001t0000010t0000011t00001t0001sGLOBAL-UNICASTt001tSPECIALPURPOSEt00100000000000010000000tTEREDOt 00100000000000010000000000000000tBMWGt 00100000000000010000000000000010tORCHIDt0010000000000001000000000001sALLOCATED APNICt00100000000000010000001sALLOCATED ARINt00100000000000010000010sALLOCATED RIPE NCCt00100000000000010000011t00100000000000010000100t00100000000000010000101t00100000000000010000110t DOCUMENTATIONt 00100000000000010000110110111000t00100000000000010000111sALLOCATED LACNICt00100000000000010001001t00100000000000010001010t00100000000000010001011t00100000000000010001100t00100000000000010001101t0010000000000001000111t00100000000000010010t001000000000000100110t0010000000000001001110t0010000000000001001111t00100000000000010100000sALLOCATED AFRINICt00100000000000010100001t00100000000000010100010t00100000000000010100011t00100000000000010100100t00100000000000010100101t00100000000000010100110t00100000000000010101t0010000000000001100t00100000000000011010t00100000000000011011t6TO4t0010000000000010t001000000000001100t 001001000000t 001001100000t00100110000100000000000t00100110001000000000000t 001010000000t 001010100000t 001011000000t00101101t0010111t0011t010t011t100t101t110t1110t11110t111110tULAt1111110t 111111100t LINKLOCALt 1111111010t 1111111011t MULTICASTt11111111sNODE-LOCAL MULTICASTt1111111100000001sLINK-LOCAL MULTICASTt1111111100000010sADMIN-LOCAL MULTICASTt1111111100000100sSITE-LOCAL MULTICASTt1111111100000101sORG-LOCAL MULTICASTt1111111100001000sGLOBAL MULTICASTt1111111100001110sRESERVED MULTICASTt1111111100001111sPREFIX-BASED MULTICASTt 111111110011sRP-EMBEDDED MULTICASTt 111111110111tIPintcBs"eZdZdddZdZdZdZdZdZdZ dd Z dd Z dd Z dd Zdd ZddZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZRS(sHandling of IP addresses returning integers. Use class IP instead because some features are not implemented for IPint.icCsWd|_d|_d}d}t|ttfrt||_|dkro|jdkrfd}qod}n|dkr|jdkrtd|jnd }nF|dkr|jd krtd |jnd }n td ||_||_ nOt|t r:|j|_|j |_ |j|_nt|t t fr=|j d}t|dkrR|\}}t|\|_} | dkrtdnt|\}} | dkrtdn||jkrtdn||j} t| }td|d |fjj|krFtd|qFnt|dkr|j d}t|dkr|d}d}qFt|dkrtdqF|\}}|jddkrFt|\} } | dkrtdnt| }qFn-t|dkr:tdn tdt|\|_} |dkrp| }n|dkr|dkrd |}q|dkrd |}qtd n||_t||_ |r|jt|j |j@|_nt|j|j |jsStdt||j fqSntdt|dS(s-Create an instance of an IP object. Data can be a network specification or a single IP. IP addresses can be specified in all forms understood by parseAddress(). The size of a network can be specified as /prefixlen a.b.c.0/24 2001:658:22a:cafe::/64 -lastIP a.b.c.0-a.b.c.255 2001:658:22a:cafe::-2001:658:22a:cafe:ffff:ffff:ffff:ffff /decimal netmask a.b.c.d/255.255.255.0 not supported for IPv6 If no size specification is given a size of 1 address (/32 for IPv4 and /128 for IPv6) is assumed. If make_net is True, an IP address will be transformed into the network address by applying the specified netmask. >>> print(IP('127.0.0.0/8')) 127.0.0.0/8 >>> print(IP('127.0.0.0/255.0.0.0')) 127.0.0.0/8 >>> print(IP('127.0.0.0-127.255.255.255')) 127.0.0.0/8 >>> print(IP('127.0.0.1/255.0.0.0', make_net=True)) 127.0.0.0/8 See module documentation for more examples. iiiIiiIsJIPv4 Addresses can't be larger than 0xffffffffffffffffffffffffffffffff: %xi l sJIPv6 Addresses can't be larger than 0xffffffffffffffffffffffffffffffff: %xisonly IPv4 and IPv6 supportedt-is)first-last notation only allowed for IPv4s last address should be IPv4, toos(last address should be larger than firsts%s/%ss*the range %s is not on a network boundary.t/s"only one '/' allowed in IP Addresst.snetmask must be IPv4s"only one '-' allowed in IP Addresss can't parses!%s has invalid prefix length (%s)sUnsupported data type: %sN(tNoPrefixForSingleIptNonet WantPrefixLent isinstancetinttlongtipt ValueErrort _ipversiont _prefixlenRetstrtunicodetsplittlent parseAddresst _count1BitstIPt broadcasttfindt_netmaskToPrefixlent_prefixlenToNetmaskt_checkNetaddrWorksWithPrefixlentreprt TypeErrorttype(tselftdatat ipversiontmake_nettnetbitst prefixlentxRotlastt parsedVersiont lastversiontsizetnetmasktvers((s'/usr/lib/python2.7/site-packages/IPy.pyt__init__{s                   ,             " %cCs|jS(sReturn the first / base / network addess as an (long) integer. The same as IP[0]. >>> "%X" % IP('10.0.0.0/8').int() 'A000000' (Ro(R((s'/usr/lib/python2.7/site-packages/IPy.pyRmscCs|jS(sReturn the IP version of this Object. >>> IP('10.0.0.0/8').version() 4 >>> IP('::1').version() 6 (Rq(R((s'/usr/lib/python2.7/site-packages/IPy.pytversionscCs|jS(sWReturns Network Prefixlen. >>> IP('10.0.0.0/8').prefixlen() 8 (Rr(R((s'/usr/lib/python2.7/site-packages/IPy.pyRscCs |jS(sT Return the base (first) address of a network as an (long) integer. (Rm(R((s'/usr/lib/python2.7/site-packages/IPy.pytnetscCs|j|jdS(sl Return the broadcast (last) address of a network as an (long) integer. The same as IP[-1].i(RmRv(R((s'/usr/lib/python2.7/site-packages/IPy.pyRzscCs|jdkr|jdks<|jdkrQ|jdkrQ|jrQd}qQn|d kr~|j}|d kr~d}q~n|r|dkr|j}t|ttfs|j}ndt ||jS|d krd t |j |j d|jSd |jSnd Sd S(sPrints Prefixlen/Netmask. Not really. In fact it is our universal Netmask/Prefixlen printer. This is considered an internal function. want == 0 / None don't return anything 1.2.3.0 want == 1 /prefix 1.2.3.0/24 want == 2 /netmask 1.2.3.0/255.255.255.0 want == 3 -lastip 1.2.3.0-1.2.3.255 ii iiiiis/%sis-%ss/%dtN( RqRrRiRjRkRRlRmRntintToIpRoRv(RtwantR((s'/usr/lib/python2.7/site-packages/IPy.pyt _printPrefix%s$          %cCs|jdkrd}n$|jdkr0d}n td|jdkr`|dkr`d}nt|j}d|t|||j|S( sReturn a string representation as a binary value. >>> print(IP('127.0.0.1').strBin()) 01111111000000000000000000000001 ii iisonly IPv4 and IPv6 supportediRN(RqRpRkRjt _intToBinRoRvR(Rt wantprefixlentbitstret((s'/usr/lib/python2.7/site-packages/IPy.pytstrBinOs    c Cs|jd kr$|d kr$d}n|jdkr@|j|S|jd?dkrt|jd@d}d||j|}|Sg|jdjdD]}t|d ^q}dgd }x.t t |D]}t ||||>> IP('127.0.0.1').strCompressed() '127.0.0.1' >>> IP('2001:0658:022a:cafe:0200::1').strCompressed() '2001:658:22a:cafe:200::1' >>> IP('ffff:ffff:ffff:ffff:ffff:f:f:fffc/127').strCompressed() 'ffff:ffff:ffff:ffff:ffff:f:f:fffc/127' iii iIs::ffff:it:iiRRN(RkRjRqt strFullsizeRoRRRuRmtxrangeRvt_countFollowingZerostindextmaxt strNormaltinserttappendtjoin( RRtipv4ttextRthextetstfollowingzerostitcompressionpos((s'/usr/lib/python2.7/site-packages/IPy.pyt strCompressedcs0   4 +%  cCs|jd kr$|d kr$d}n|jdkrE|jd}nx|jdkrdjgg|jdjdD]}t|d^qvD]}t|d^q}n td||j |S( sReturn a string representation in the usual format. >>> print(IP('127.0.0.1').strNormal()) 127.0.0.1 >>> print(IP('2001:0658:022a:cafe:0200::1').strNormal()) 2001:658:22a:cafe:200:0:0:1 iiiiRiisonly IPv4 and IPv6 supportedN( RkRjRqRRRuRmthexRpR(RRRR((s'/usr/lib/python2.7/site-packages/IPy.pyRs  ] cCsJ|jdkr$|dkr$d}nt|j|jj|j|S(sReturn a string representation in the non-mangled format. >>> print(IP('127.0.0.1').strFullsize()) 127.0.0.1 >>> print(IP('2001:0658:022a:cafe:0200::1').strFullsize()) 2001:0658:022a:cafe:0200:0000:0000:0001 iN(RkRjRRoRqtlowerR(RR((s'/usr/lib/python2.7/site-packages/IPy.pyRs  cCsg|jdkr$|dkr$d}nt|j}|ddkrP|d }n|j|j|S(sReturn a string representation in hex format in lower case. >>> IP('127.0.0.1').strHex() '0x7f000001' >>> IP('2001:0658:022a:cafe:0200::1').strHex() '0x20010658022acafe0200000000000001' iitLN(RkRjRRoRR(RRR((s'/usr/lib/python2.7/site-packages/IPy.pytstrHexs   cCsa|jdkr$|dkr$d}nt|j}|ddkrP|d }n||j|S(sReturn a string representation in decimal format. >>> print(IP('127.0.0.1').strDec()) 2130706433 >>> print(IP('2001:0658:022a:cafe:0200::1').strDec()) 42540616829182469433547762482097946625 iiRN(RkRjRsRoR(RRR((s'/usr/lib/python2.7/site-packages/IPy.pytstrDecs   cCs|jdkrt}n$|jdkr0t}n td|j}x<tt|ddD]"}|| |kra||| SqaWdS(sReturn a description of the IP type ('PRIVATE', 'RESERVERD', etc). >>> print(IP('127.0.0.1').iptype()) PRIVATE >>> print(IP('192.168.1.1').iptype()) PRIVATE >>> print(IP('195.185.1.2').iptype()) PUBLIC >>> print(IP('::1').iptype()) LOOPBACK >>> print(IP('2001:0658:022a:cafe:0200::1').iptype()) ALLOCATED RIPE NCC The type information for IPv6 is out of sync with reality. iisonly IPv4 and IPv6 supportediitunknown(Rqt IPv4rangest IPv6rangesRpRRRv(RtiprangeRR((s'/usr/lib/python2.7/site-packages/IPy.pytiptypes    cCs]|jdkrd|j}n+|jdkr>d|j}n tdd|jd|>S(srReturn netmask as an integer. >>> "%X" % IP('195.185.0.0/16').netmask().int() 'FFFF0000' ii iisonly IPv4 and IPv6 supportedii(RqRrRp(Rtlocallen((s'/usr/lib/python2.7/site-packages/IPy.pyRs  cCso|jdkr8d|j}td|jd|>dS|jdkr_d|j}d|jStdd S( sReturn netmask as an string. Mostly useful for IPv6. >>> print(IP('195.185.0.0/16').strNetmask()) 255.255.0.0 >>> print(IP('2001:0658:022a:cafe::0/64').strNetmask()) /64 ii iiiis/%dsonly IPv4 and IPv6 supportedN(RqRrRRp(RR((s'/usr/lib/python2.7/site-packages/IPy.pyt strNetmask s    cCsR|jdkrd|j}n+|jdkr>d|j}n tdd|S(sReturn the length of a subnet. >>> print(IP('195.185.1.0/28').len()) 16 >>> print(IP('195.185.1.0/24').len()) 256 ii iisonly IPv4 and IPv6 supportedi(RqRrRp(RR((s'/usr/lib/python2.7/site-packages/IPy.pyRvs  cCstS(sAll IPy objects should evaluate to true in boolean context. Ordinarily they do, but if handling a default route expressed as 0.0.0.0/0, the __len__() of the object becomes 0, which is used as the boolean value of the object. (tTrue(R((s'/usr/lib/python2.7/site-packages/IPy.pyt __nonzero__0scCst|jS(sReturn the length of a subnet. Called to implement the built-in function len(). It breaks with IPv6 Networks. Anybody knows how to fix this.(RmRv(R((s'/usr/lib/python2.7/site-packages/IPy.pyt__len__9s cCst|ttfstn|dkrdt||jkr[|jt|}qtn||jkrtn|jt|S(sNCalled to implement evaluation of self[key]. >>> ip=IP('127.0.0.0/30') >>> for x in ip: ... print(repr(x)) ... IP('127.0.0.0') IP('127.0.0.1') IP('127.0.0.2') IP('127.0.0.3') >>> ip[2] IP('127.0.0.2') >>> ip[-1] IP('127.0.0.3') i(RlRmRnRtabsRvt IndexErrorRo(Rtkey((s'/usr/lib/python2.7/site-packages/IPy.pyt __getitem__Es    cCsTt|}|j|jkrL|j|j|j|jdkrLtStSdS(sCalled to implement membership test operators. Should return true if item is in self, false otherwise. Item can be other IP-objects, strings or ints. >>> IP('195.185.1.1').strHex() '0xc3b90101' >>> 0xC3B90101 in IP('195.185.1.0/24') True >>> '127.0.0.1' in IP('127.0.0.0/24') True >>> IP('127.0.0.0/24') in IP('127.0.0.0/25') False iN(RyRoRvRtFalse(Rtitem((s'/usr/lib/python2.7/site-packages/IPy.pyt __contains__es <cCsxt|}|j|jkr>|j|j|jkr>dS|j|jkrp|j|j|jkrpdSdSdS(sCheck if two IP address ranges overlap. Returns 0 if the two ranges don't overlap, 1 if the given range overlaps at the end and -1 if it does at the beginning. >>> IP('192.168.0.0/23').overlaps('192.168.1.0/24') 1 >>> IP('192.168.0.0/23').overlaps('192.168.1.255') 1 >>> IP('192.168.0.0/23').overlaps('192.168.2.0') 0 >>> IP('192.168.1.0/24').overlaps('192.168.0.0/23') -1 iiiN(RyRoRv(RR((s'/usr/lib/python2.7/site-packages/IPy.pytoverlaps|s  ..cCs |jS(sSDispatch to the prefered String Representation. Used to implement str(IP).(R(R((s'/usr/lib/python2.7/site-packages/IPy.pyt__str__scCsd|jdS(sPrint a representation of the Object. Used to implement repr(IP). Returns a string which evaluates to an identical Object (without the wantprefixlen stuff - see module docstring. >>> print(repr(IP('10.0.0.0/24'))) IP('10.0.0.0/24') s IPint('%s')i(R(R((s'/usr/lib/python2.7/site-packages/IPy.pyt__repr__s cCs|j|jkr&|j|jS|j|jkrL|j|jS|j|jkrbdS|j|jkrxdS|j|jkr|j|jkrdS|j|jkrdSdSndSdS(sCalled by comparison operations. Should return a negative integer if self < other, zero if self == other, a positive integer if self > other. Networks with different prefixlen are considered non-equal. Networks with the same prefixlen and differing addresses are considered non equal but are compared by their base address integer value to aid sorting of IP objects. The version of Objects is not put into consideration. >>> IP('10.0.0.0/24') > IP('10.0.0.0') 1 >>> IP('10.0.0.0/24') < IP('10.0.0.0') 0 >>> IP('10.0.0.0/24') < IP('12.0.0.0/24') 1 >>> IP('10.0.0.0/24') > IP('12.0.0.0/24') 0 iiiN(RrRRoRq(Rtother((s'/usr/lib/python2.7/site-packages/IPy.pyt__cmp__scCs|j|dkS(Ni(R(RR((s'/usr/lib/python2.7/site-packages/IPy.pyt__eq__scCs|j|dkS(Ni(R(RR((s'/usr/lib/python2.7/site-packages/IPy.pyt__lt__scCsWtd}|j}x(|dkr?||d@A}|d?}qW||jA}t|S(shCalled for the key object for dictionary operations, and by the built-in function hash(). Should return a 32-bit integer usable as a hash value for dictionary operations. The only required property is that objects which compare equal have the same hash value >>> IP('10.0.0.0/24').__hash__() -167772185 iiii (RmRoRr(RtthehashRo((s'/usr/lib/python2.7/site-packages/IPy.pyt__hash__s   N( t__name__t __module__t__doc__RRmRRRRzRRjRRRRRRRRRRvRRRRRRRRRRR(((s'/usr/lib/python2.7/site-packages/IPy.pyReus:    *  -     !        =  RycBsheZdZdZdZdZdZdZdZdZ dZ d Z d Z RS( s-Class for handling IP addresses and networks.cCsttj|d|jS(sReturn the base (first) address of a network as an IP object. The same as IP[0]. >>> IP('10.0.0.0/8').net() IP('10.0.0.0') R(RyReRRq(R((s'/usr/lib/python2.7/site-packages/IPy.pyRscCsttj|S(sReturn the broadcast (last) address of a network as an IP object. The same as IP[-1]. >>> IP('10.0.0.0/8').broadcast() IP('10.255.255.255') (RyReRz(R((s'/usr/lib/python2.7/site-packages/IPy.pyRzscCsttj|d|jS(siReturn netmask as an IP object. >>> IP('10.0.0.0/8').netmask() IP('255.0.0.0') R(RyReRRq(R((s'/usr/lib/python2.7/site-packages/IPy.pyRscCst|jdkrdS|jd?dkr*dS|jd@}|jdkrdd|dd|jf}nt|ddS( Nii iIis%s/%sRi(RqRjRoRrRy(RR((s'/usr/lib/python2.7/site-packages/IPy.pyt _getIPv4Map!s cCs|jdkr6g}|jdkrNx|D]}|j|jq.Wn|jdkrxtd|jdD]!}|j||jdqyWn|jdkrx|td|jdD]!}|j||jdqWn>x;td|jdD]!}|j||jdq W|S|jdkr|j}|dk rg|jSt|j dj }|dd kr|d }n|j ddkrt d nt |}|jd j|}td |j dd}d ||gStddS(sReturn a list with values forming the reverse lookup. >>> IP('213.221.113.87/32').reverseNames() ['87.113.221.213.in-addr.arpa.'] >>> IP('213.221.112.224/30').reverseNames() ['224.112.221.213.in-addr.arpa.', '225.112.221.213.in-addr.arpa.', '226.112.221.213.in-addr.arpa.', '227.112.221.213.in-addr.arpa.'] >>> IP('127.0.0.0/24').reverseNames() ['0.0.127.in-addr.arpa.'] >>> IP('127.0.0.0/23').reverseNames() ['0.0.127.in-addr.arpa.', '1.0.127.in-addr.arpa.'] >>> IP('127.0.0.0/16').reverseNames() ['0.127.in-addr.arpa.'] >>> IP('127.0.0.0/15').reverseNames() ['0.127.in-addr.arpa.', '1.127.in-addr.arpa.'] >>> IP('128.0.0.0/8').reverseNames() ['128.in-addr.arpa.'] >>> IP('128.0.0.0/7').reverseNames() ['128.in-addr.arpa.', '129.in-addr.arpa.'] >>> IP('::1:2').reverseNames() ['2.0.0.0.1.ip6.arpa.'] iiiiiiiitls3can't create IPv6 reverse names at sub nibble levelRhi s %s.ip6.arpa.sonly IPv4 and IPv6 supportedNiiiiii(RqRvRt reverseNameRRRjt reverseNamesRRoRRrtNotImplementedErrortlisttreverseRRmRp(RRRRRtstfirst_nibble_index((s'/usr/lib/python2.7/site-packages/IPy.pyR+s: ""      cCs<|jdkr|jd}|jd}|jtd|jd}|jddkrd|d|jdt|j|jddjddf}|dd kr|d }n|d7}nd }dj ||}d ||fS|jd kr,|j }|dk r)|j St |jd j}|dd kr_|d }n|jddkrd||jt |j|jdd jf}|dd kr|d }n|d7}nd }t|}|jdj |}td|jdd }d|||fStddS(sReturn the value for reverse lookup/PTR records as RFC 2317 look alike. RFC 2317 is an ugly hack which only works for sub-/24 e.g. not for /23. Do not use it. Better set up a zone for every address. See reverseName for a way to achieve that. >>> print(IP('195.185.1.1').reverseName()) 1.1.185.195.in-addr.arpa. >>> print(IP('195.185.1.0/28').reverseName()) 0-15.1.185.195.in-addr.arpa. >>> IP('::1:2').reverseName() '2.0.0.0.1.ip6.arpa.' iiRhis%s-%siiiRRs%s%s.in-addr.arpa.iii s%s%s.ip6.arpa.sonly IPv4 and IPv6 supportedN(RqRRuRRmRrRRoRvRRRjRRRRRp(RRtfirst_byte_indext nibblepartRR((s'/usr/lib/python2.7/site-packages/IPy.pyRcs@ F      8    cCs?dt|kr%td|ntd||fdtS(sTransform a single IP address into a network specification by applying the given netmask. Returns a new IP instance. >>> print(IP('127.0.0.1').make_net('255.0.0.0')) 127.0.0.0/8 Rgsinvalid netmask (%s)s%s/%sR(RsRpRyR(RR((s'/usr/lib/python2.7/site-packages/IPy.pyRs cCsttj||S(sACalled to implement evaluation of self[key]. >>> ip=IP('127.0.0.0/30') >>> for x in ip: ... print(str(x)) ... 127.0.0.0 127.0.0.1 127.0.0.2 127.0.0.3 >>> print(str(ip[2])) 127.0.0.2 >>> print(str(ip[-1])) 127.0.0.3 (RyReR(RR((s'/usr/lib/python2.7/site-packages/IPy.pyRscCsd|jdS(sePrint a representation of the Object. >>> IP('10.0.0.0/8') IP('10.0.0.0/8') sIP('%s')i(R(R((s'/usr/lib/python2.7/site-packages/IPy.pyRscCs|j|jkr'tdn|jdkrHtdn|j|jkrotdn||kr|j|St|j}|jd|_|SdS(s3Emulate numeric objects through network aggregations3Only networks with the same prefixlen can be added.is8Networks with a prefixlen longer than /1 can't be added.s4Only networks with the same IP version can be added.N(RRpRt__add__RyRmRr(RRR((s'/usr/lib/python2.7/site-packages/IPy.pyRs  ( RRRRRzRRRRRRRR(((s'/usr/lib/python2.7/site-packages/IPy.pyRys  8 5  c Csg}d}d}x|t|kr1||}|jdr{|dk r_td|nt|}|d7}qn|jd}|dkrtd|n|dkr |j|| |||d!dkr||7}n||d7}|t|kr.td|q.q|j|PqW|rd |dkr|dk r~|t|dk r~td |nt|dd}|d d |d ?d |d @g}n|dk rdt|}|dkrtd|n|| dg|||}nt|dkr9td|nd}d}x|D]}y0t|d }d|ko{d kn } Wntk rt} nX| rtd||fn|d >|}|d7}qLW|S(st Internal function used by parseAddress() to parse IPv6 address with ':'. >>> print(_parseAddressIPv6('::')) 0 >>> print(_parseAddressIPv6('::1')) 1 >>> print(_parseAddressIPv6('0:0:0:0:0:0:0:1')) 1 >>> print(_parseAddressIPv6('0:0:0::0:0:1')) 1 >>> print(_parseAddressIPv6('0:0:0:0:0:0:0:0')) 0 >>> print(_parseAddressIPv6('0:0:0::0:0:0')) 0 >>> print(_parseAddressIPv6('FEDC:BA98:7654:3210:FEDC:BA98:7654:3210')) 338770000845734292534325025077361652240 >>> print(_parseAddressIPv6('1080:0000:0000:0000:0008:0800:200C:417A')) 21932261930451111902915077091070067066 >>> print(_parseAddressIPv6('1080:0:0:0:8:800:200C:417A')) 21932261930451111902915077091070067066 >>> print(_parseAddressIPv6('1080:0::8:800:200C:417A')) 21932261930451111902915077091070067066 >>> print(_parseAddressIPv6('1080::8:800:200C:417A')) 21932261930451111902915077091070067066 >>> print(_parseAddressIPv6('FF01:0:0:0:0:0:0:43')) 338958331222012082418099330867817087043 >>> print(_parseAddressIPv6('FF01:0:0::0:0:43')) 338958331222012082418099330867817087043 >>> print(_parseAddressIPv6('FF01::43')) 338958331222012082418099330867817087043 >>> print(_parseAddressIPv6('0:0:0:0:0:0:13.1.68.3')) 218186755 >>> print(_parseAddressIPv6('::13.1.68.3')) 218186755 >>> print(_parseAddressIPv6('0:0:0:0:0:FFFF:129.144.52.38')) 281472855454758 >>> print(_parseAddressIPv6('::FFFF:129.144.52.38')) 281472855454758 >>> print(_parseAddressIPv6('1080:0:0:0:8:800:200C:417A')) 21932261930451111902915077091070067066 >>> print(_parseAddressIPv6('1080::8:800:200C:417A')) 21932261930451111902915077091070067066 >>> print(_parseAddressIPv6('::1:2:3:4:5:6')) 1208962713947218704138246 >>> print(_parseAddressIPv6('1:2:3:4:5:6::')) 5192455318486707404433266432802816 is::s,%r: Invalid IPv6 address: more than one '::'iRs%r: Invalid IPv6 addressiiRhs)%r: Invalid IPv6 address: '::' after IPv4s%04xiiis,%r: Invalid IPv6 address: '::' is not neededRs/%r: Invalid IPv6 address: should have 8 hextetss+%r: Invalid IPv6 address: invalid hexlet %rN( RjRvt startswithRpR{RRwRmR( tipstrtitemsRtfill_posRtpostvaluetdiffRterror((s'/usr/lib/python2.7/site-packages/IPy.pyt_parseAddressIPv6s`6        #'    !  cCs |jdrdt|dd}|dkrAtd|n|dkrW|dfS|dfSn|jd d krt|dfSt|d krt|ddfS|jd d kst|dkrt|d kr|jd }t|dkrtdn|dgdt|7}g|D]}t|^q9}x9|D]1}|dksv|dkrXtd|qXqXW|dd>|dd>|dd>|ddfSt|d}|dkrtdn|dkr|dfS|dfSdS(s Parse a string and return the corresponding IP address (as integer) and a guess of the IP version. Following address formats are recognized: >>> def testParseAddress(address): ... ip, version = parseAddress(address) ... print(("%s (IPv%s)" % (ip, version))) ... >>> testParseAddress('0x0123456789abcdef') # IPv4 if <= 0xffffffff else IPv6 81985529216486895 (IPv6) >>> testParseAddress('123.123.123.123') # IPv4 2071690107 (IPv4) >>> testParseAddress('123.123') # 0-padded IPv4 2071658496 (IPv4) >>> testParseAddress('1080:0000:0000:0000:0008:0800:200C:417A') 21932261930451111902915077091070067066 (IPv6) >>> testParseAddress('1080:0:0:0:8:800:200C:417A') 21932261930451111902915077091070067066 (IPv6) >>> testParseAddress('1080:0::8:800:200C:417A') 21932261930451111902915077091070067066 (IPv6) >>> testParseAddress('::1') 1 (IPv6) >>> testParseAddress('::') 0 (IPv6) >>> testParseAddress('0:0:0:0:0:FFFF:129.144.52.38') 281472855454758 (IPv6) >>> testParseAddress('::13.1.68.3') 218186755 (IPv6) >>> testParseAddress('::FFFF:129.144.52.38') 281472855454758 (IPv6) t0xiil s)%r: IP Address can't be bigger than 2^128IiiRii Rhis#IPv4 Address with more than 4 bytesRiis'%r: single byte must be 0 <= byte < 256iiiii s%IP Address can't be bigger than 2^128IN(RRnRpR{RRvRmRu(RRtbytesR((s'/usr/lib/python2.7/site-packages/IPy.pyRwIs6#    9 2   cCst|}|dkr+td|nd}|dkr|dkrbtdt|nx6tdD](}t|d@d|}|d ?}qoW|d }n|d krp|d krtd t|ntjdkrt|d}nt|dd !}|jd}xCtddD]2}|| |}|ddkr-d|}q-q-W|d}n td|S(s/Transform an integer string into an IP address.isIPs can't be negative: %dRiIs2IPv4 Addresses can't be larger than 0xffffffff: %siRhiiil sJIPv6 Addresses can't be larger than 0xffffffffffffffffffffffffffffffff: %siii ii!Rsonly IPv4 and IPv6 supported(RnRpRRRstsyst hexversiontzfill(RoRRRR((s'/usr/lib/python2.7/site-packages/IPy.pyRs2         cCs0|dkrdS|dkr dStddS(sReturn number of bits in address for a certain IP version. >>> _ipVersionToLen(4) 32 >>> _ipVersionToLen(6) 128 >>> _ipVersionToLen(5) Traceback (most recent call last): File "", line 1, in ? File "IPy.py", line 1076, in _ipVersionToLen raise ValueError("only IPv4 and IPv6 supported") ValueError: only IPv4 and IPv6 supported ii iisonly IPv4 and IPv6 supportedN(Rp(R((s'/usr/lib/python2.7/site-packages/IPy.pyt_ipVersionToLens   cCs@t|dkrdS|ddkr*dSdt|dSdS(sDReturn number of elements containing 0 at the beginning of the list.iiN(RvR(R((s'/usr/lib/python2.7/site-packages/IPy.pyRs t0000t0010t2t3t0100t4t0101t5t0110t6t0111t7t1000t8t1001t9t1010tat1011tbt1100tct1101tdtet1111tfcCs|dkrtdnt|j}d}|ddkrP|d }nx |dD]}|t|7}q[Wx0|ddkrt|dkr|d}qvW|S( s9Return the binary representation of an integer as string.isOnly positive values allowedRiRiRi(RpRRt _BitTableRv(tvalRRR((s'/usr/lib/python2.7/site-packages/IPy.pyRs  %cCs1d}x$|dkr,|d?}|d7}q W|S(s,Find the highest bit set to 1 in an integer.ii((tnumR((s'/usr/lib/python2.7/site-packages/IPy.pyRxs  cCspt|}|dkr+td|nd}x8|dkrk|d@dkrTPn|d?}|d7}q4W|S(s,Find the highest bit set to 0 in an integer.is Only positive Numbers please: %si(RnRp(RR((s'/usr/lib/python2.7/site-packages/IPy.pyt _count0Bitss   cCsit|}|dks$||kr(dS|dkrA|d}n t|}|||kradSdSdS(sJCheck the validity of a prefix Checks if the variant part of a prefix only has 0s, and the length is correct. >>> _checkPrefix(0x7f000000, 24, 4) 1 >>> _checkPrefix(0x7f000001, 24, 4) 0 >>> repr(_checkPrefix(0x7f000001, -1, 4)) 'None' >>> repr(_checkPrefix(0x7f000001, 33, 4)) 'None' iiN(RRjR(RoRRRtzbits((s'/usr/lib/python2.7/site-packages/IPy.pyt _checkPrefixs    cCst|}|}xD|d@dkrX|dkrX|d?}|d8}|dkrPqqWxM|dkr|d@dkrtdt|n|d?}|d8}q\WdS(s2Checks if a netmask is expressable as a prefixlen.iis+Netmask %s can't be expressed as an prefix.N(RnRpR(RtmasklenRR((s'/usr/lib/python2.7/site-packages/IPy.pyt _checkNetmask(s     cCs|t||@|kS(sBCheck if a base addess of a network is compatible with a prefixlen(R}(RRR((s'/usr/lib/python2.7/site-packages/IPy.pyR~<scCs-t|}t|}t||||S(snConvert an Integer representing a netmask to a prefixlen. E.g. 0xffffff00 (255.255.255.0) returns 24 (RRxR (RtnetlenR ((s'/usr/lib/python2.7/site-packages/IPy.pyR|As   cCsI|dkrdS|dkr+tdnd|d>dt||>S(sReturn a mask of n bits as a long integer. From 'IP address conversion functions with the builtin socket module' by Alex Martelli http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66517 isPrefixlen must be > 0ii(RpR(RR((s'/usr/lib/python2.7/site-packages/IPy.pyR}Ms   t__main__i(Rt __version__RttypesRRReRyRRwRRRRRRxRR R R~R|R}Rtdoctestttestmodtfailuretnbtesttexit(((s'/usr/lib/python2.7/site-packages/IPy.pyts       x L %