U .Ѫ_ @sDdZddlZddlmZmZmZmZmZm Z m Z m Z m Z m Z ddlmZmZddlmZmZmZmZmZmZGdddeZGdd d eZGd d d eZdUd dZGdddeeZGdddeeZ ddZ!ddZ"ddZ#ddZ$ddZ%ddZ&dVd d!Z'd"d#Z(d$d%Z)d&d'Z*d(d)Z+ed*Z,ed+ed,ed-ed.ed/ed0e d1d2fZ-ed3Z.ed4Z/ed5Z0ed6ed7ed8ed9ed:ed;e dd?fe,e0fZ1ed@Z2edAedBfZ3edCZ4edDZ5edEedFedGedHedIedJedKedLedMedNedOedPedQedRedSedTfZ6dS)Wz9Routines for IPv4 and IPv6 addresses, subnets and ranges.N) AddrFormatErrorAddrConversionErrornum_bits DictDotLookupNOHOSTN INET_PTONPZEROFILLZ)ipv4ipv6) _sys_maxint _iter_next _iter_range_is_str _int_type _str_typec@seZdZdZdZddZddZedded d Zd d Z d dZ ddZ ddZ ddZ ddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zed-d.Zed/d0Zd1S)2BaseIPzi An abstract base class for common operations shared between various IP related subclasses. )_value_module __weakref__cCsd|_d|_dS)z Constructor.N)rrselfr:/Users/cjl/code/aws-vpc-ipam/lambda/netaddr/ip/__init__.py__init__szBaseIP.__init__cCsNt|tstdt|d|kr2|jjksDntd|jj||_dS)Nint argument expected, not %srz&value out of bounds for an %s address!) isinstancer TypeErrortypermax_intr family_namerrvaluerrr _set_value s zBaseIP._set_valuecCs|jSNrrrrr(zBaseIP.z?a positive integer representing the value of IP address/subnet.doccCstSzP :return: a key tuple that uniquely identifies this IP address. NotImplementedrrrrkey+sz BaseIP.keycCstS)zg :return: A key tuple used to compare and sort this `IPAddress` correctly. r-rrrrsort_key1szBaseIP.sort_keycCs t|S)zM :return: A hash value uniquely indentifying this IP object. )hashr/rrrr__hash__8szBaseIP.__hash__c Cs4z||kWSttfk r.tYSXdS)z :param other: an `IPAddress` or `IPNetwork` object. :return: ``True`` if this `IPAddress` or `IPNetwork` object is equivalent to ``other``, ``False`` otherwise. Nr/AttributeErrorrr.rotherrrr__eq__>sz BaseIP.__eq__c Cs4z||kWSttfk r.tYSXdS)z :param other: an `IPAddress` or `IPNetwork` object. :return: ``True`` if this `IPAddress` or `IPNetwork` object is not equivalent to ``other``, ``False`` otherwise. Nr3r5rrr__ne__Jsz BaseIP.__ne__c Cs4z||kWSttfk r.tYSXdS)z :param other: an `IPAddress` or `IPNetwork` object. :return: ``True`` if this `IPAddress` or `IPNetwork` object is less than ``other``, ``False`` otherwise. Nr0r4rr.r5rrr__lt__Vsz BaseIP.__lt__c Cs4z||kWSttfk r.tYSXdS)z :param other: an `IPAddress` or `IPNetwork` object. :return: ``True`` if this `IPAddress` or `IPNetwork` object is less than or equal to ``other``, ``False`` otherwise. Nr9r5rrr__le__bsz BaseIP.__le__c Cs4z||kWSttfk r.tYSXdS)z :param other: an `IPAddress` or `IPNetwork` object. :return: ``True`` if this `IPAddress` or `IPNetwork` object is greater than ``other``, ``False`` otherwise. Nr9r5rrr__gt__nsz BaseIP.__gt__c Cs4z||kWSttfk r.tYSXdS)z :param other: an `IPAddress` or `IPNetwork` object. :return: ``True`` if this `IPAddress` or `IPNetwork` object is greater than or equal to ``other``, ``False`` otherwise. Nr9r5rrr__ge__zsz BaseIP.__ge__cCs | S)z<:return: ``True`` if this IP is unicast, ``False`` otherwise) is_multicastrrrr is_unicastszBaseIP.is_unicastcCs(|jtkr|tkS|jtkr$|tkSdS)z>:return: ``True`` if this IP is multicast, ``False`` otherwiseN)r_ipv4IPV4_MULTICAST_ipv6IPV6_MULTICASTrrrrr>s  zBaseIP.is_multicastcCs,|jjdkr|tkS|jjdkr(|tkSdS)z :return: ``True`` if this IP is loopback address (not for network transmission), ``False`` otherwise. References: RFC 3330 and 4291. N)rversion IPV4_LOOPBACK IPV6_LOOPBACKrrrr is_loopbacks  zBaseIP.is_loopbackcCsZ|jjdkr&tD]}||krdSqn$|jjdkrJtD]}||kr6dSq6|rVdSdS)z :return: ``True`` if this IP is for internal/private use only (i.e. non-public), ``False`` otherwise. Reference: RFCs 1918, 3330, 4193, 3879 and 2365. rDTrEF)rrF IPV4_PRIVATE IPV6_PRIVATE is_link_localrcidrrrr is_privates   zBaseIP.is_privatecCs,|jjdkr|tkS|jjdkr(|tkSdS)z :return: ``True`` if this IP is link-local address ``False`` otherwise. Reference: RFCs 3927 and 4291. rDrEN)rrFIPV4_LINK_LOCALIPV6_LINK_LOCALrrrrrLs  zBaseIP.is_link_localcCsN|jjdkr&tD]}||krdSqn$|jjdkrJtD]}||kr6dSq6dS)z :return: ``True`` if this IP is in IANA reserved range, ``False`` otherwise. Reference: RFCs 3330 and 3171. rDTrEF)rrF IPV4_RESERVED IPV6_RESERVEDrMrrr is_reserveds   zBaseIP.is_reservedcCs|jjdko|jd?dkS)zp :return: ``True`` if this IP is IPv4-compatible IPv6 address, ``False`` otherwise. rE irrFrrrrris_ipv4_mappedszBaseIP.is_ipv4_mappedcCs|jjdko|jd?dkS)zl :return: ``True`` if this IP is IPv4-mapped IPv6 address, ``False`` otherwise. rErUrrVrrrris_ipv4_compatszBaseIP.is_ipv4_compatcCsddlm}t||S)z~ A record dict containing IANA registration details for this IP address if available, None otherwise. r)query)Znetaddr.ip.ianarYr)rrYrrrinfos z BaseIP.infocCs|jjS)z6the IP protocol version represented by this IP object.)rrFrrrrrFszBaseIP.versionN)__name__ __module__ __qualname____doc__ __slots__rr%propertyr$r/r0r2r7r8r:r;r<r=r?r>rIrOrLrTrWrXrZrFrrrrrs8          rcsTeZdZdZdZdLfdd ZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ e ZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*ZdMd+d,Zed-d.Zed/d0Zed1d2Zed3d4Zd5d6ZdNd8d9Z dOd:d;Z!dd?Z#d@dAZ$dBdCZ%dDdEZ&dFdGZ'e'Z(dHdIZ)dJdKZ*Z+S)P IPAddressz An individual IPv4 or IPv6 address without a net mask or subnet prefix. To support these and other network based operations, see `IPNetwork`. rNrcs tt|t|trH|dk r4||jjkr4td|j|_|j|_n|dk r||dkr`t |_n|dkrpt |_n td|t |rd|krtd|j j |jdkrt|tr6zhdt|krt jkrnnt||_t |_n4t jt|krt jkrnnt||_t |_Wntk r2YnXnBt t fD]8}z||||_WnYq>YnX||_qxq>|jdkrtd |nt |rz|j|||_Wn*tk rtd ||jjfYnXn>dt|kr|jjkrnn t||_ntd |fdS) a Constructor. :param addr: an IPv4 or IPv6 address which may be represented in an accepted string format, as an unsigned integer or as another IPAddress object (copy construction). :param version: (optional) optimizes version detection if specified and distinguishes between IPv4 and IPv6 for addresses with an equivalent integer value. :param flags: (optional) decides which rules are applied to the interpretation of the addr value. Supported constants are INET_PTON and ZEROFILL. See the netaddr.core docs for further details. Nz1cannot switch IP versions using copy constructor!rDrE%r is an invalid IP version!/zQ%s() does not support netmasks or subnet prefixes! See documentation for details.rz+failed to detect a valid IP address from %rzbase address %r is not IPv%dzbad address format: %r)superrarrrrrF ValueErrorrr@rBr __class__r[rintr!Z str_to_intr)raddrrFflagsmodulerfrrrs`      $        $ zIPAddress.__init__cCs|j|jjfS)z1:returns: Pickled state of an `IPAddress` object.)rrrFrrrr __getstate__AszIPAddress.__getstate__cCsB|\}}||_|dkrt|_n |dkr.t|_ntdt|dS)zT :param state: data used to unpickle a pickled `IPAddress` object. rDrEz&unpickling failed for object state: %sN)rr@rrBrestr)rstater$rFrrr __setstate__EszIPAddress.__setstate__cCs|s|jjS|jdkrdS|j}d}|dkrP|d@dkr>qP|d7}|dL}q(|jj|}d|krt|jjksntd||S)z @return: If this IP is a valid netmask, the number of non-zero bits are returned, otherwise it returns the width in bits for the IP address version. rz+Unexpected mask length %d for address type!) is_netmaskrwidthrre)rZi_valnumbitsZ mask_lengthrrr netmask_bitsVs"    zIPAddress.netmask_bitscCs|jd}||d@dkS)zV :return: ``True`` if this IP address host mask, ``False`` otherwise. rprr'rint_valrrr is_hostmaskus zIPAddress.is_hostmaskcCs"|j|jjAd}||d@dkS)zY :return: ``True`` if this IP address network mask, ``False`` otherwise. rpr)rrr!rurrrrq|szIPAddress.is_netmaskcCs@t|j|}d|kr&|jjkr4nn ||_|StddS)z Increases the numerical value of this IPAddress by num. An IndexError is raised if result exceeds maximum IP address value or is less than zero. :param num: size of IP address increment. r)result outside valid IP address boundary!Nrgrrr! IndexErrorrnum new_valuerrr__iadd__s zIPAddress.__iadd__cCs@t|j|}d|kr&|jjkr4nn ||_|StddS)z Decreases the numerical value of this IPAddress by num. An IndexError is raised if result is less than zero or exceeds maximum IP address value. :param num: size of IP address decrement. rrxNryr{rrr__isub__s zIPAddress.__isub__cCsFt|j|}d|kr&|jjkr:nn|||jjStddS)a Add the numerical value of this IP address to num and provide the result as a new IPAddress object. :param num: size of IP address increase. :return: a new IPAddress object with its numerical value increased by num. rrxNrgrrr!rfrFrzr{rrr__add__s zIPAddress.__add__cCsFt|j|}d|kr&|jjkr:nn|||jjStddS)a Subtract the numerical value of this IP address from num providing the result as a new IPAddress object. :param num: size of IP address decrease. :return: a new IPAddress object with its numerical value decreased by num. rrxNrr{rrr__sub__s zIPAddress.__sub__cCsFt||j}d|kr&|jjkr:nn|||jjStddS)a Subtract num (lvalue) from the numerical value of this IP address (rvalue) providing the result as a new IPAddress object. :param num: size of IP address decrease. :return: a new IPAddress object with its numerical value decreased by num. rrxNrr{rrr__rsub__s zIPAddress.__rsub__cCs|jj|jfSr,rVrrrrr/sz IPAddress.keycCs|jj|j|jjfS)zI:return: A key tuple used to compare and sort this `IPAddress` correctly.)rrFrrrrrrrr0szIPAddress.sort_keycCs|jSz<:return: the value of this IP address as an unsigned integerr'rrrr__int__szIPAddress.__int__cCs|jSrr'rrrr__long__szIPAddress.__long__cCs|jdkrdSd|jS)z;:return: an octal string representation of this IP address.r0z0%or'rrrr__oct__s zIPAddress.__oct__cCs d|jS)z@:return: a hexadecimal string representation of this IP address.z0x%xr'rrrr__hex__szIPAddress.__hex__cCs|jS)zx :return: return the integer value of this IP address when called by hex(), oct() or bin(). r'rrrr __index__szIPAddress.__index__cCs|j|jjddS)zt :return: a bytes object equivalent to this IP address. In network byte order, big-endian. big)rto_bytesrrrrrrr __bytes__szIPAddress.__bytes__cCs|j|j|S)z :param word_sep: (optional) the separator to insert between words. Default: None - use default separator for address type. :return: the value of this IP address as a binary digit string.)rZ int_to_bitsr)rZword_seprrrbitsszIPAddress.bitscCs|j|jS)z7The value of this IP address as a packed binary string.)rZ int_to_packedrrrrrpackedszIPAddress.packedcCs|j|jS)zx A list of unsigned integer words (octets for IPv4, hextets for IPv6) found in this IP address. )rZ int_to_wordsrrrrrwords szIPAddress.wordscCs|j|jS)z The value of this IP adddress in standard Python binary representational form (0bxxx). A back port of the format provided by the builtin bin() function found in Python 2.6.x and higher. )rZ int_to_binrrrrrbinsz IPAddress.bincCs|j|jS)z1The reverse DNS lookup record for this IP address)rZ int_to_arparrrrr reverse_dnsszIPAddress.reverse_dnscCsd}|j}|jjdkr$||jd}np|jjdkrd|jkrHtjkrZnn||jd}n:tj|jkrrdkrnn||jdd}n td||S)z Raises an `AddrConversionError` if IPv6 address cannot be converted to IPv4. :return: A numerically equivalent version 4 `IPAddress` object. NrDrEr2IPv6 address %s unsuitable for conversion to IPv4!)rfrrFrr@r!r)ripklassrrrr !s  zIPAddress.ipv4FcCsd}|j}|jjdkrT|rFd|jkr0dkrFnn||jdd}q||jd}n,|jjdkr||jd}|s|d|jd}|S)a .. note:: The IPv4-mapped IPv6 address format is now considered deprecated. See RFC 4291 or later for details. :param ipv4_compatible: If ``True`` returns an IPv4-mapped address (::ffff:x.x.x.x), an IPv4-compatible (::x.x.x.x) address otherwise. Default: False (IPv4-mapped). :return: A numerically equivalent version 6 `IPAddress` object. NrErrrD)rfrrFrrZipv4_compatiblerrrrrr 7s     zIPAddress.ipv6cCs,|dk rt|dstd|jj|j|dS)z Only relevant for IPv6 addresses. Has no effect for IPv4. :param dialect: An ipv6_* dialect class. :return: an alternate string representation for this IP address. NZword_fmtz-custom dialects should subclass ipv6_verbose!)dialect)hasattrrr int_to_strr)rrrrrformatTs  zIPAddress.formatcCs||jt|B|jjS)z :param other: An `IPAddress` object (or other int-like object). :return: bitwise OR (x | y) between the integer value of this IP address and ``other``. rfrrgrrFr5rrr__or__bszIPAddress.__or__cCs||jt|@|jjS)z :param other: An `IPAddress` object (or other int-like object). :return: bitwise AND (x & y) between the integer value of this IP address and ``other``. rr5rrr__and__kszIPAddress.__and__cCs||jt|A|jjS)z :param other: An `IPAddress` object (or other int-like object). :return: bitwise exclusive OR (x ^ y) between the integer value of this IP address and ``other``. rr5rrr__xor__tszIPAddress.__xor__cCs||j|>|jjS)z :param numbits: size of bitwise shift. :return: an `IPAddress` object based on this one with its integer value left shifted by ``numbits``. rfrrrFrrsrrr __lshift__}szIPAddress.__lshift__cCs||j|?|jjS)z :param numbits: size of bitwise shift. :return: an `IPAddress` object based on this one with its integer value right shifted by ``numbits``. rrrrr __rshift__szIPAddress.__rshift__cCs t|jS)zi:return: ``True`` if the numerical value of this IP address is not zero, ``False`` otherwise.)boolrrrrr __nonzero__szIPAddress.__nonzero__cCs|j|jS)z,:return: IP address in presentational format)rrrrrrr__str__szIPAddress.__str__cCsd|jj|fS8:return: Python statement to create an equivalent objectz%s('%s')rfr[rrrr__repr__szIPAddress.__repr__)Nr)N)F)N),r[r\r]r^r_rrlrortrwrqr~rr__radd__rrr/r0rrrrrrrr`rrrrr r rrrrrrr__bool__rr __classcell__rrrkrrasVQ             rac@sLeZdZdZdZddZeddZddZd d Z d d Z d dZ e Z dS) IPListMixinzu A mixin class providing shared list-like functionality to classes representing groups of IP addresses. rcCs*t|j|jj}t|j|jj}t||S)z :return: An iterator providing access to all `IPAddress` objects within range represented by this ranged IP object. )rafirstrrFlast iter_iprange)rstart_ipend_iprrr__iter__szIPListMixin.__iter__cCst|j|jdS)zP The total number of IP addresses within this ranged IP object. rp)rgrrrrrrsizeszIPListMixin.sizecCs|j}|tkrtdt|S)z :return: the number of IP addresses in this ranged IP object. Raises an `IndexError` if size > system max int (a Python 2.x limitation). Use the .size property for subnets of any size. zVrange contains more than %d (sys.maxint) IP addresses! Use the .size property instead.)rrrz)rrrrr__len__s zIPListMixin.__len__cCsBd}t|dr|jjdkr"td||j\}}}||dksH||kr`tt|j|jjg}n8t|j||jj}t|j|||jj}t |||}nzt |}|j |krdkrnnt|j |d|jj}n>d|kr|jdkrnnt|j||jj}nt dWn"t k r<td|YnX|S) z :return: The IP address(es) in this `IPNetwork` object referenced by index or slice. As slicing can produce large sequences of objects an iterator is returned instead of the more usual `list`. NindicesrEzIPv6 slices are not supported!rrpz'index out range for address range size!zunsupported index type %r!)rrrFrrriterrarrrgrrzre)rindexitemstartstopsteprrrrr __getitem__s(    zIPListMixin.__getitem__cCsdt|trX|jj|jjkrdSt|tr@|j|jko>|j|jkS|j|jkoV|j|jkSt||kS) :param other: an `IPAddress` or ranged IP object. :return: ``True`` if other falls within the boundary of this one, ``False`` otherwise. F)rrrrFrarrrr5rrr __contains__s  zIPListMixin.__contains__cCsdS)z Ranged IP objects always represent a sequence of at least one IP address and are therefore always True in the boolean context. TrrrrrrszIPListMixin.__nonzero__N) r[r\r]r^r_rr`rrrrrrrrrrrs   %rFc Cst|tr|t|dkr$td|j|\}}d|krB|jksRntd|jd|krh|jksxntd|jnpt|tr|rt|}d|kr| dd\}}n|}d}zt ||j t d}WnJtk r|j d krt |} t | |j t d}n td |YnX|j}z t|}Wntk rN|dkrJ|j}Ynbtk rt ||j t d} | r|j| j}n$| r|j| j}n td |YnXd|kr|jksntd |jntd t||t@r|j|} || @}||fS)Nzinvalid %s tuple!rz#invalid address value for %s tuple!zinvalid prefix for %s tuple!rcrprirDzinvalid IPNetwork address %s!z!addr %r is not a valid IPNetwork!zinvalid prefix for %s address!zunexpected type %s for addr arg)rtuplelenrr"r!rrrcidr_abbrev_to_verbosesplitrarFrr@Zexpand_partial_addressrrgrrerqZnetmask_to_prefixrwZhostmask_to_prefixr rZprefix_to_netmask) rjrhimplicit_prefixrir$ prefixlenZval1Zval2rZ expanded_addrmasknetmaskrrrparse_ip_networksd              rcsPeZdZdZdZdDfdd Zdd Zd d Zd d Ze ddeddZ e ddZ e ddZ e ddZ e ddZe ddZe ddZejddZe dd Ze d!d"Ze d#d$Ze d%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2ZdEd3d4ZdFd6d7ZdGd8d9ZdHd:d;ZdIdd?Z!d@dAZ"dBdCZ#Z$S)J IPNetworka An IPv4 or IPv6 network or subnet. A combination of an IP address and a network mask. Accepts CIDR and several related variants : a) Standard CIDR:: x.x.x.x/y -> 192.0.2.0/24 x::/y -> fe80::/10 b) Hybrid CIDR format (netmask address instead of prefix), where 'y' address represent a valid netmask:: x.x.x.x/y.y.y.y -> 192.0.2.0/255.255.255.0 x::/y:: -> fe80::/ffc0:: c) ACL hybrid CIDR format (hostmask address instead of prefix like Cisco's ACL bitmasks), where 'y' address represent a valid netmask:: x.x.x.x/y.y.y.y -> 192.0.2.0/0.0.0.255 x::/y:: -> fe80::/3f:ffff:ffff:ffff:ffff:ffff:ffff:ffff d) Abbreviated CIDR format (as of netaddr 0.7.x this requires the optional constructor argument ``implicit_prefix=True``):: x -> 192 x/y -> 10/8 x.x/y -> 192.168/16 x.x.x/y -> 192.168.0/24 which are equivalent to:: x.0.0.0/y -> 192.0.0.0/24 x.0.0.0/y -> 10.0.0.0/8 x.x.0.0/y -> 192.168.0.0/16 x.x.x.0/y -> 192.168.0.0/24 .. warning:: The next release (0.9.0) will contain a backwards incompatible change connected to handling of RFC 6164 IPv6 addresses (/127 and /128 subnets). When iterating ``IPNetwork`` and ``IPNetwork.iter_hosts()`` the first addresses in the networks will no longer be excluded and ``broadcast`` will be ``None``.  _prefixlenFNrc s>tt|d\}}}t|dr6|j}|j}|j}nt|drT|j}|j}|j}n|dkrvtt |||d\}}t }n|dkrtt |||d\}}t }n|dk rt d|zt }t||||\}}Wn`t k r&zt }t||||\}}Wnt k rYnX|dkr"t d |fYnX||_||_||_dS) a Constructor. :param addr: an IPv4 or IPv6 address with optional CIDR prefix, netmask or hostmask. May be an IP address in presentation (string) format, an tuple containing and integer address and a network prefix, or another IPAddress/IPNetwork object (copy construction). :param implicit_prefix: (optional) if True, the constructor uses classful IPv4 rules to select a default prefix when one is not provided. If False it uses the length of the IP address version. (default: False) :param version: (optional) optimizes version detection if specified and distinguishes between IPv4 and IPv6 for addresses with an equivalent integer value. :param flags: (optional) decides which rules are applied to the interpretation of the addr value. Currently only supports the NOHOST option. See the netaddr.core docs for further details. )NNNrrrD)rrirENrbzinvalid IPNetwork %s) rdrrrrrrrrrr@rBrer)rrhrrFrir$rrjrkrrr{sX         zIPNetwork.__init__cCs|j|j|jjfS)z0:return: Pickled state of an `IPNetwork` object.)rrrrFrrrrrlszIPNetwork.__getstate__cCst|\}}}||_|dkr t|_n|dkr0t|_ntd|fd|krV|jjkrbnn||_ntd|fdS)zT :param state: data used to unpickle a pickled `IPNetwork` object. rDrEz%unpickling failed for object state %srN)rr@rrBrerrr)rrnr$rrFrrrros zIPNetwork.__setstate__cCsNt|tstdt|d|kr2|jjksDntd|jj||_dS)Nrrz!invalid prefix for an %s address!) rrrr rrrrr"rr#rrr_set_prefixlens zIPNetwork._set_prefixlencCs|jSr&rrrrrr(r)zIPNetwork.zCsize of the bitmask used to separate the network from the host bitsr*cCst|j|jjS)z The IP address of this `IPNetwork` object. This is may or may not be the same as the network IP address which varies according to the value of the CIDR subnet prefix. )rarrrFrrrrrsz IPNetwork.ipcCst|j|j@|jjS)z/The network address of this `IPNetwork` object.)rar _netmask_intrrFrrrrnetworkszIPNetwork.networkcCs<|jjdkr"|jj|jdkr"dSt|j|jB|jjSdS)aFThe broadcast address of this `IPNetwork` object. .. warning:: The next release (0.9.0) will contain a backwards incompatible change connected to handling of RFC 6164 IPv6 addresses (/127 and /128 subnets). ``broadcast`` will be ``None`` when dealing with those networks. rDrpN)rrFrrrrar _hostmask_intrrrr broadcasts zIPNetwork.broadcastcCs|j|jj|jA@S)ze The integer value of first IP address found within this `IPNetwork` object. )rrr!rrrrrrszIPNetwork.firstcCs d|jj|j>d}|j|BS)zd The integer value of last IP address found within this `IPNetwork` object. rp)rrrrrrhostmaskrrrr szIPNetwork.lastcCs|jj|jA}t||jjS)z+The subnet mask of this `IPNetwork` object.)rr!rrarF)rrrrrrszIPNetwork.netmaskcCsJt|}|j|jkr$td||f|sd}t||jjS)z)The host mask of this `IPNetwork` object.rp)rrrrrarFrrrrr+szIPNetwork.hostmaskcCsd|jj|j>dS)z,Same as self.hostmask, but in integer formatrp)rrrrrrrrr1szIPNetwork._hostmask_intcCst|j|j@|jf|jjdS)z The true CIDR address for this `IPNetwork` object which omits any host bits to the right of the CIDR subnet prefix. rF)rrrrrrFrrrrrN6szIPNetwork.cidrcCsLt|j|j|}||jd|jjkr2td|dkrBtd||_|S)aP Increases the value of this `IPNetwork` object by the current size multiplied by ``num``. An `IndexError` is raised if result exceeds maximum IP address value or is less than zero. :param num: (optional) number of `IPNetwork` blocks to increment this IPNetwork's value by. rpz#increment exceeds address boundary!rzincrement is less than zero!)rgrrrr!rzrr{rrrr~@s zIPNetwork.__iadd__cCsLt|j|j|}|dkr$td||jd|jjkrBtd||_|S)aP Decreases the value of this `IPNetwork` object by the current size multiplied by ``num``. An `IndexError` is raised if result is less than zero or exceeds maximum IP address value. :param num: (optional) number of `IPNetwork` blocks to decrement this IPNetwork's value by. rzdecrement is less than zero!rpz#decrement exceeds address boundary!)rgrrrzrr!rr{rrrrUs zIPNetwork.__isub__cCst|tr|jj|jjkrdS|jj|j}|j|?}t|trd||>|jjkob|d|>|j jkS|j|?}t|t r||kSt|t r||ko|j|jkSt ||kS)rFrp) rrrrFrrrrIPRange_start_endrar)rr6 shiftwidthZself_netZ other_netrrrrjs      zIPNetwork.__contains__cCs|jj|j|jfS)zR :return: A key tuple used to uniquely identify this `IPNetwork`. rrFrrrrrrr/sz IPNetwork.keycCs8|jd}|j|jj|jA@}|j|}|jj|||fS)z[ :return: A key tuple used to compare and sort this `IPNetwork` correctly. rp)rrrr!rrF)rZ net_size_bitsrZ host_bitsrrrr0s  zIPNetwork.sort_keycCsd}|j}|jjdkr,|d|j|jf}n|jjdkrd|jkrPtjkrxnn$t|j}|d||jdf}nPtj|jkrdkrnn(t|jd}|d||jdf}n t d ||S) z :return: A numerically equivalent version 4 `IPNetwork` object. Raises an `AddrConversionError` if IPv6 address cannot be converted to IPv4. NrD%s/%drEr`rrr) rfrrFrrrr@r!rr)rrrrhrrrr s   zIPNetwork.ipv4cCsd}|j}|jjdkrd|rNd|jkr0dkrNnn||jd|jfdd}q||j|jfdd}nF|jjdkr|r||j|jdfdd}n|d|j|jdfdd}|S)a .. note:: the IPv4-mapped IPv6 address format is now considered deprecated. See RFC 4291 or later for details. :param ipv4_compatible: If ``True`` returns an IPv4-mapped address (::ffff:x.x.x.x), an IPv4-compatible (::x.x.x.x) address otherwise. Default: False (IPv4-mapped). :return: A numerically equivalent version 6 `IPNetwork` object. NrErrrrDr)rfrrFrrrrrrr s,     zIPNetwork.ipv6rpcCs(|d|j|jf|jj}||8}|S)z :param step: the number of IP subnets between this `IPNetwork` object and the expected subnet. Default: 1 (the previous IP subnet). :return: The adjacent subnet preceding this `IPNetwork` object. rrfrrrrFrrZip_copyrrrpreviouss zIPNetwork.previouscCs(|d|j|jf|jj}||7}|S)z :param step: the number of IP subnets between this `IPNetwork` object and the expected subnet. Default: 1 (the next IP subnet). :return: The adjacent subnet succeeding this `IPNetwork` object. rrrrrrnexts zIPNetwork.nextcCsjd|kr|jjks.ntd||jjfg}|j}||_|j|jkrf||j|jd7_q>|S)ab Provides a list of supernets for this `IPNetwork` object between the size of the current prefix and (if specified) an endpoint prefix. :param prefixlen: (optional) a CIDR prefix for the maximum supernet. Default: 0 - returns all possible supernets. :return: a tuple of supernet `IPNetwork` objects. r"CIDR prefix /%d invalid for IPv%d!rp)rrrrerFrNrappend)rr supernetssupernetrrrrs    zIPNetwork.supernetc csd|jkr|jjks0ntd||jjf|j|ks>dS|jj}d||jd||}|dkrl|}d|kr|ksntd|j|j}d}||kr|d||f|jj}|j|j |7_||_|d7}|VqdS)a A generator that divides up this IPNetwork's subnet into smaller subnets based on a specified CIDR prefix. :param prefixlen: a CIDR prefix indicating size of subnets to be returned. :param count: (optional) number of consecutive IP subnets to be returned. :return: an iterator containing IPNetwork subnet objects. rrNrrpz,count outside of current IP subnet boundary!r) rrrrrerFrrrfr$r) rrcountfmtrrZ max_subnetsZ base_subnetisubnetrrrrs,   zIPNetwork.subnetcCstg}|jjdkrn|jdkrJtt|jd|jjt|jd|jj}qtt|j|jjt|j|jj}n0|jdkrtt|jd|jjt|j|jj}|S)a] A generator that provides all the IP addresses that can be assigned to hosts within the range of this IP object's subnet. - for IPv4, the network and broadcast addresses are always excluded. for subnets that contains less than 4 IP addresses /31 and /32 report in a manner per RFC 3021 - for IPv6, only the unspecified address '::' or Subnet-Router anycast address (first address in the network) is excluded. .. warning:: The next release (0.9.0) will contain a backwards incompatible change connected to handling of RFC 6164 IPv6 addresses (/127 and /128 subnets). When iterating ``IPNetwork`` and ``IPNetwork.iter_hosts()`` the first addresses in the networks will no longer be excluded. :return: an IPAddress iterator rDrpr)rrrFrrrarr)rZit_hostsrrr iter_hosts&s"   zIPNetwork.iter_hostscCs|j|j}d||jfS)z&:return: this IPNetwork in CIDR format%s/%s)rrrr)rrhrrrrRszIPNetwork.__str__cCsd|jj|fSrrrrrrrWszIPNetwork.__repr__)FNr)F)rp)rp)r)NN)%r[r\r]r^r_rrlrorr`rrrrrrrsetterrrrrNr~rrr/r0r r rrrrrrrrrrrkrrIsZ/D               ),rc@sveZdZdZdZdddZddZdd Zd d Ze d d Z e ddZ ddZ ddZ ddZddZddZdS)rz An arbitrary IPv4 or IPv6 address range. Formed from a lower and upper bound IP address. The upper bound IP cannot be numerically smaller than the lower bound and the IP version of both must match. rrrcCsLt||d|_|jj|_t||jj|d|_t|jt|jkrHtddS)a Constructor. :param start: an IPv4 or IPv6 address that forms the lower boundary of this IP range. :param end: an IPv4 or IPv6 address that forms the upper boundary of this IP range. :param flags: (optional) decides which rules are applied to the interpretation of the start and end values. Supported constants are INET_PTON and ZEROFILL. See the netaddr.core docs for further details. rz(lower bound IP greater than upper bound!N)rarrrFrrgr)rrendrirrrrgs  zIPRange.__init__cCs|jj|jj|jjfS)z.:return: Pickled state of an `IPRange` object.)rr$rrrFrrrrrl}szIPRange.__getstate__cCs0|\}}}t|||_|jj|_t|||_dS)zQ :param state: data used to unpickle a pickled `IPRange` object. N)rarrr)rrnrrrFrrrros   zIPRange.__setstate__cCst|tr|jj|jjkrdSt|trD|jj|jkoB|jj|jkSt|trn|jj|jjkol|jj|jjkSt|t r|jj |j }|j|?|>}|d|>}|jj|ko|jj|kSt||kS)NFrp) rrrrFrarrrrrrrr)rr6rZ other_startZother_next_startrrrrs&        zIPRange.__contains__cCs t|jS)z?The integer value of first IP address in this `IPRange` object.)rgrrrrrrsz IPRange.firstcCs t|jS)z>The integer value of last IP address in this `IPRange` object.)rgrrrrrrsz IPRange.lastcCs|jj|j|jfS)zP :return: A key tuple used to uniquely identify this `IPRange`. rrrrrr/sz IPRange.keycCs$|jjt|j}|jj|jj|fS)zY :return: A key tuple used to compare and sort this `IPRange` correctly. )rrrrrrFrr)rZskeyrrrr0szIPRange.sort_keycCst|j|jS)zx The list of CIDR addresses found within the lower and upper bound addresses of this `IPRange`. )iprange_to_cidrsrrrrrrcidrssz IPRange.cidrscCsd|j|jfS)z<:return: this `IPRange` in a common representational format.z%s-%srrrrrrszIPRange.__str__cCsd|jj|j|jfS)rz%s('%s', '%s'))rfr[rrrrrrrszIPRange.__repr__N)r)r[r\r]r^r_rrlrorr`rrr/r0rrrrrrrr\s    rcgs"t|D]}|D] }|VqqdS)z :param args: A list of IP addresses and subnets passed in as arguments. :return: A generator that flattens out IP subnets, yielding unique individual IP addresses (no duplicates). N) cidr_merge)argsrNrrrriter_unique_ipss rc Csldd}t|r$d|ks |dkr$|Szt|}d|||fWStk rJd|kr|dd\}}z,dt|krd ksntd |fWqtk r|YYSXn|}d }|d }t|d kr|YStd t|D]}|dq|d kr4z||d}Wntk r2|YYSXdd ||fYStt fk rf|YSXd S)a A function that converts abbreviated IPv4 CIDRs to their more verbose equivalent. :param abbrev_cidr: an abbreviated CIDR. Uses the old-style classful IP address rules to decide on a default subnet prefix if one is not explicitly provided. Only supports IPv4 addresses. Examples :: 10 - 10.0.0.0/8 10/16 - 10.0.0.0/16 128 - 128.0.0.0/16 128/8 - 128.0.0.0/8 192.168 - 192.168.0.0/16 :return: A verbose CIDR from an abbreviated CIDR or old-style classful network address. The original value if it was not recognised as a supported abbreviation. cSst|}d|krdks*ntd|d|kr>dkrFnndSd|krZdkrbnndSd |krvd kr~nnd Sd |krd krnndSdS)NrzInvalid octet: %r!rrDrU)rgrz)octetrrrclassful_prefixs z/cidr_abbrev_to_verbose..classful_prefix:z %s.0.0.0/%srcrprrUz.prefixlen in address %r out of range for IPv4!N.rDrr) rrgrerrrangerjoinrrz)Z abbrev_cidrrrZ part_addrprefixtokensrrrrs@     rc Cst|dstdg}|D]8}t|ttfr2|}nt|}||j|j|j|fq| t |d}|dkr||d||ddkr||dd||ddkr||d||dt ||dd||df||d<||=|d8}qhg}|D]|}t |dkrB|d}t|tr6| | n ||n8|d}t|d|d} t|d|d} | t| | q|S) a A function that accepts an iterable sequence of IP addresses and subnets merging them into the smallest possible list of CIDRs. It merges adjacent subnets where possible, those contained within others and also removes any duplicates. :param ip_addrs: an iterable sequence of IP addresses, subnets or ranges. :return: a summarized list of `IPNetwork` objects. rz#A sequence or iterator is expected!rprrrDr)rrerrrrrFrrsortrminextendrrar) ip_addrsZrangesrZnetrmergedZ range_tupleoriginalrFZ range_startZ range_stoprrrr)s6  <<   rcCst||\}}}||S)a Removes an exclude IP address or subnet from target IP subnet. :param target: the target IP address or subnet to be divided up. :param exclude: the IP address or subnet to be removed from target. :return: list of `IPNetwork` objects remaining after exclusion. )cidr_partition)targetexcludeleft_rightrrr cidr_exclude\s rc Cs"t|}t|}|j|jkr*gg|jgfS|j|jkrD|jgggfS|j|jkr\g|ggfSg}g}|jd}|jj}|j}|j}|}|d||} |j|kr |j| kr|t||f|d| } n|t| |f|d|} |d7}||krq | }| d||} q||g|dddfS)ac Partitions a target IP subnet on an exclude IP address. :param target: the target IP address or subnet to be divided up. :param exclude: the IP address or subnet to partition on :return: list of `IPNetwork` objects before, the partition and after, sorted. Adding the three lists returns the equivalent of the original subnet. rprrN) rrrrNrrrrrFr) rrrrZ new_prefixlenZtarget_module_widthZ target_firstrFZi_lowerZi_uppermatchedrrrrjs8        rc Cst|}ztt|}tt|}Wntk r@tdYnX||krT|}|}n|}|}|D]$}t|}||krx|}||kr`|}q`|j|jkrtd|j}|j} |j } |j j } | dkr|| kr| d8} |d| | > M}qt|| f|jdS)aO Function that accepts a sequence of IP addresses and subnets returning a single `IPNetwork` subnet that is large enough to span the lower and upper bound IP addresses with a possible overlap on either end. :param ip_addrs: sequence of IP addresses and subnets. :return: a single spanning `IPNetwork` subnet. z-IP sequence must contain at least 2 elements!z.IP sequence cannot contain both IPv4 and IPv6!rrpr) rrr StopIterationrerFrrrrrrr) r Z ip_addrs_iterZ network_aZ network_bZ min_networkZ max_networkrrZipnumrZ lowest_ipnumrrrrr spanning_cidrs6   rrpccst|}t|}|j|jkr$td|j}t|}|dkrBtdt|}t|}d}|dkrbd}||}||7}|r||ksqn ||ksqt||VqjdS)a A generator that produces IPAddress objects between an arbitrary start and stop IP address with intervals of step between them. Sequences produce are inclusive of boundary IPs. :param start: start IP address. :param end: end IP address. :param step: (optional) size of step between IP addresses. Default: 1 :return: an iterator of one or more `IPAddress` objects. z(start and stop IP versions do not match!rzstep argument cannot be zeroFTN)rarFrrgre)rrrrFrZ negative_steprrrrrs* rcCsg}t|}t|}|j|jg}t||g}|jj}|j|dkrrt|dd|f|jd}t||d}|}|j|dkrt|dd|f|jd}|t||d7}n | ||S)a` A function that accepts an arbitrary start and end IP address or subnet and returns a list of CIDR subnets that fit exactly between the boundaries of the two with no overlap. :param start: the start IP address or subnet. :param end: the end IP address or subnet. :return: a list of one or more IP addresses and subnets. rrprr) rrrrrrrrFrpopr)rrZ cidr_listZiprangeZ cidr_spanrrrrrrrs    rcCsdd}t|dstd|ft|}tdd|DD](}||krH|}q6|dk r6|j|kr6q`q6|S)ab Matches an IP address or subnet against a given sequence of IP addresses and subnets. :param ip: a single IP address or subnet. :param cidrs: a sequence of IP addresses and/or subnets. :return: the smallest (most specific) matching IPAddress or IPNetwork object from the provided sequence, None if there was no match. Nr,IP address/subnet sequence expected, not %r!cSsg|] }t|qSrr.0rNrrr :sz*smallest_matching_cidr..)rrrasortedrrrmatchrNrrrsmallest_matching_cidr's  r$cCsPd}t|dstd|ft|}tdd|DD]}||kr6|}qLq6|S)ab Matches an IP address or subnet against a given sequence of IP addresses and subnets. :param ip: a single IP address or subnet. :param cidrs: a sequence of IP addresses and/or subnets. :return: the largest (least specific) matching IPAddress or IPNetwork object from the provided sequence, None if there was no match. NrrcSsg|] }t|qSrrrrrrr Wsz)largest_matching_cidr..)rrrar!r"rrrlargest_matching_cidrDs  r%cCsjg}t|dstd|ft|}tdd|DD].}||krN||q6|r6|j|dkr6qfq6|S)aM Matches an IP address or subnet against a given sequence of IP addresses and subnets. :param ip: a single IP address. :param cidrs: a sequence of IP addresses and/or subnets. :return: all matching IPAddress and/or IPNetwork objects from the provided sequence, an empty list if there was no match. rrcSsg|] }t|qSrrrrrrr rsz&all_matching_cidrs..r)rrrar!rr)rrZmatchesrNrrrall_matching_cidrs_s   r&z 127.0.0.0/8z 10.0.0.0/8z 100.64.0.0/10z 172.16.0.0/12z 192.0.0.0/24z192.168.0.0/16z 198.18.0.0/15z 239.0.0.0z239.255.255.255z169.254.0.0/16z 224.0.0.0/4z192.88.99.0/24z 0.0.0.0/8z 192.0.2.0/24z 240.0.0.0/4z198.51.100.0/24z203.0.113.0/24z233.252.0.0/24z 234.0.0.0z238.255.255.255z 225.0.0.0z231.255.255.255z::1zfc00::/7z fec0::/10z fe80::/10zff00::/8z ff00::/12z::/8z0100::/8z0200::/7z0400::/6z0800::/5z1000::/4z4000::/3z6000::/3z8000::/3zA000::/3zC000::/3zE000::/4zF000::/5zF800::/6zFE00::/9)Fr)rp)7r^sys_sys netaddr.corerrrrrrrr r r Znetaddr.strategyr r@r rBnetaddr.compatrrrrrrobjectrrarrrrrrrrrrrrr$r%r&rGrJrPrAZ IPV4_6TO4rRrHrKrQrCrSrrrrs0 U<e Dm T3<. /$