a
´i< ã @ sˆ d Z dZddlZdZdZG dd„ deƒZG dd „ d eƒZd
d„ Zd=d
d„Z dd„ Z
dd„ Zdd„ Zdd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ ZG d#d$„ d$ƒZdaejG d%d&„ d&eƒƒZejG d'd(„ d(eƒƒZG d)d*„ d*ƒZG d+d,„ d,eeƒZG d-d.„ d.eƒZG d/d0„ d0eeƒZG d1d2„ d2ƒZee_G d3d4„ d4ƒZG d5d6„ d6eeƒZ G d7d8„ d8e ƒZ!G d9d:„ d:eeƒZ"G d;d<„ d<ƒZ#e#e _dS )>z•A fast, lightweight IPv4/IPv6 manipulation library in Python.
This library is used to create/poke/manipulate IPv4 and IPv6 addresses
and networks.
z1.0é Né é€ c @ s e Zd ZdZdS )ÚAddressValueErrorz%A Value Error related to the address.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r
r
ú!/usr/lib64/python3.9/ipaddress.pyr s r c @ s e Zd ZdZdS )ÚNetmaskValueErrorz%A Value Error related to the netmask.Nr r
r
r
r r s r c C sV z
t | ƒW S ttfy Y n0 z
t| ƒW S ttfyB Y n0 t| ›d�ƒ‚dS )a® Take 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 addressN)ÚIPv4Addressr r ÚIPv6AddressÚ
ValueError©Úaddressr
r
r Ú
ip_address s
r Tc C sZ zt | |ƒW S ttfy" Y n0 zt| |ƒW S ttfyF Y n0 t| ›d�ƒ‚dS )aÐ Take 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 networkN)ÚIPv4Networkr r ÚIPv6Networkr )r Ústrictr
r
r Ú
ip_network8 s r c C sV z
t | ƒW S ttfy Y n0 z
t| ƒW S ttfyB Y n0 t| ›d�ƒ‚dS )ag Take 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 interfaceN)Ú
IPv4Interfacer r Ú
IPv6Interfacer r r
r
r Úip_interfaceU s
r c C s. z| dd¡W S ty( tdƒ‚Y n0 dS )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.
é Úbigz&Address negative or too large for IPv4N©Úto_bytesÚ
OverflowErrorr r r
r
r Úv4_int_to_packedw s r c C s. z| dd¡W S ty( tdƒ‚Y n0 dS )zõRepresent 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.
é r z&Address negative or too large for IPv6Nr r r
r
r Úv6_int_to_packed‹ s
r! c C s, t | ƒ d¡}t|ƒdkr(td| ›�ƒ‚|S )zAHelper to split the netmask and raise AddressValueError if neededú/é zOnly one '/' permitted in )ÚstrÚsplitÚlenr )r Úaddrr
r
r Ú_split_optional_netmask› s r( c c sN t | ƒ}t|ƒ }}|D ]&}|j|jd kr:||fV |}|}q||fV dS )zÏFind a sequence of sorted deduplicated IPv#Address.
Args:
addresses: a list of IPv#Address objects.
Yields:
A tuple containing the first and last IP addresses in the sequence.
é N)ÚiterÚnextÚ_ip)Ú addressesÚitÚfirstÚlastÚipr
r
r Ú_find_address_range£ s
r2 c C s$ | dkr|S t || | d @ ¡ ƒS )zåCount the number of zero bits on the right hand side.
Args:
number: an integer.
bits: maximum number of bits to count.
Returns:
The number of zero bits on the right hand side of the number.
r r) )ÚminÚ
bit_length)ÚnumberÚbitsr
r
r Ú_count_righthand_zero_bits· s r7 c c sâ t | tƒrt |tƒstdƒ‚| j|jkr8td| |f ƒ‚| |krHtdƒ‚| jdkrXt}n| jdkrht}ntdƒ‚| j}| j}|j}||krÞt t
||ƒ|| d ¡ d ƒ}|||| fƒ}|V |d|> 7 }|d |jkr‚qÞq‚dS ) a¤ Summarize a network range given the first and last IP addresses.
Example:
>>> list(summarize_address_range(IPv4Address('192.0.2.0'),
... IPv4Address('192.0.2.130')))
... #doctest: +NORMALIZE_WHITESPACE
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'),
IPv4Network('192.0.2.130/32')]
Args:
first: the first IPv4Address or IPv6Address in the range.
last: the last IPv4Address or IPv6Address in the range.
Returns:
An iterator of the summarized IPv(4|6) network objects.
Raise:
TypeError:
If the first and last objects are not IP addresses.
If the first and last objects are not the same version.
ValueError:
If the last object is not greater than the first.
If the version of the first address is not 4 or 6.
z1first and last must be IP addresses, not networksú%%s and %s are not of the same versionz*last IP address must be greater than firstr é zunknown IP versionr) N)
Ú
isinstanceÚ_BaseAddressÚ TypeErrorÚversionr r r Ú_max_prefixlenr, r3 r7 r4 Ú _ALL_ONES)r/ r0 r1 Úip_bitsÚ first_intÚlast_intÚnbitsÚnetr
r
r Úsummarize_address_rangeÇ s6
ÿÿ
ÿrE c c s� t | ƒ}i }|rV| ¡ }| ¡ }| |¡}|du r<|||<