
-=Mc           @   s2  d  Z  d Z d d l Z d d l Z i	 d d 6d d 6d d 6d d	 6d d
 6d d 6d d 6d d 6d d 6Z iP d d 6d d d 6d d d 6d d d d
 6d d d d
 d 6d d d d 6d d 6d d 6d d 6d d 6d d  6d d! 6d" d# 6d$ d% 6d& d' 6d( d) d d 6d* d+ 6d, d- 6d. d/ 6d0 d1 6d0 d2 6d0 d3 6d, d4 6d5 d6 6d, d7 6d8 d9 6d0 d: 6d0 d; 6d. d< 6d0 d= 6d0 d> 6d0 d? 6d0 d@ 6d0 dA 6d dB 6d0 dC 6dD dE 6d, dF 6d0 dG 6d. dH 6d0 dI 6d0 dJ 6d0 dK 6d, dL 6d, dM 6d, dN 6dO dP 6d0 dQ 6d, dR 6d. dS 6d. dT 6d. dU 6d8 dV 6d0 dW 6dD dX 6d dY 6d dZ 6d d[ 6d d\ 6d d] 6d d^ 6d d_ 6d d` 6d da 6d db 6d dc 6dd de 6d df 6dg dh 6d di 6dj dk 6dl dm 6dn do 6dp dq 6dr ds 6dt du 6dv dw 6dx dy 6dz d{ 6d| d} 6Z d~ f  d     YZ d e f d     YZ d   Z d   Z	 d   Z
 d   Z d   Z i d d 6d! d
 6d d 6d[ d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6da d 6d d 6Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e d k r.d d l Z e j   \ Z Z e r+d d l Z e j d  n  n  d S(   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
s   0.75iNt   PUBLICt   0t   PRIVATEt   00000000t   00001010t   01111111t   1t   1010100111111110t   101011000001t   1100000010101000t   RESERVEDt   111i`   t   UNSPECIFIEDi   t   LOOPBACKi   t   IPV4MAPiP   i   t
   WKP46TRANSt    00000000011001001111111110011011i@   t
   UNASSIGNEDt   00000001t   0000001t   0000010t   0000011t   00001t   0001s   GLOBAL-UNICASTt   001t   SPECIALPURPOSEt   00100000000000010000000t   TEREDOt    00100000000000010000000000000000t   BMWGt    00100000000000010000000000000010t   ORCHIDt   0010000000000001000000000001s   ALLOCATED APNICt   00100000000000010000001s   ALLOCATED ARINt   00100000000000010000010s   ALLOCATED RIPE NCCt   00100000000000010000011t   00100000000000010000100t   00100000000000010000101t   00100000000000010000110t   DOCUMENTATIONt    00100000000000010000110110111000t   00100000000000010000111s   ALLOCATED LACNICt   00100000000000010001001t   00100000000000010001010t   00100000000000010001011t   00100000000000010001100t   00100000000000010001101t   0010000000000001000111t   00100000000000010010t   001000000000000100110t   0010000000000001001110t   0010000000000001001111t   00100000000000010100000s   ALLOCATED AFRINICt   00100000000000010100001t   00100000000000010100010t   00100000000000010100011t   00100000000000010100100t   00100000000000010100101t   00100000000000010100110t   00100000000000010101t   0010000000000001100t   00100000000000011010t   00100000000000011011t   6TO4t   0010000000000010t   001000000000001100t   001001000000t   001001100000t   00100110000100000000000t   00100110001000000000000t   001010000000t   001010100000t   001011000000t   00101101t   0010111t   0011t   010t   011t   100t   101t   110t   1110t   11110t   111110t   ULAt   1111110t	   111111100t	   LINKLOCALt
   1111111010t
   1111111011t	   MULTICASTt   11111111s   NODE-LOCAL MULTICASTt   1111111100000001s   LINK-LOCAL MULTICASTt   1111111100000010s   ADMIN-LOCAL MULTICASTt   1111111100000100s   SITE-LOCAL MULTICASTt   1111111100000101s   ORG-LOCAL MULTICASTt   1111111100001000s   GLOBAL MULTICASTt   1111111100001110s   RESERVED MULTICASTt   1111111100001111s   PREFIX-BASED MULTICASTt   111111110011s   RP-EMBEDDED MULTICASTt   111111110111t   IPintc           B   s"  e  Z d  Z d d d  Z d   Z d   Z d   Z d   Z d   Z d   Z	 d d	  Z d d
  Z d d  Z d d  Z d d  Z d d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s   Handling of IP addresses returning integers.

    Use class IP instead because some features are not implemented for
    IPint.i    c         C   sW  d |  _  d |  _ d } d } t | t t f  rt |  |  _ | d k ro |  j d k  rf d } qo d } n  | d k r |  j d k r t d |  j   n  d	 } nF | d k r |  j d
 k r t d |  j   n  d } n t d   | |  _ | |  _	 nOt | t
  r:| j |  _ | j	 |  _	 | j |  _ nt | t t f  r=| j d  } t |  d k rR| \ } } t |  \ |  _ }	 |	 d k rt d   n  t |  \ } }
 |
 d k rt d   n  | |  j k  rt d   n  | |  j } t |  } t d | d	 | f  j   j   | k rFt d |   qFn t |  d k r| j d  } t |  d k r| d } d } qFt |  d k rt d   qF| \ } } | j d  d k rFt |  \ } } | d k rt d   n  t |  } qFn- t |  d k r:t d   n t d   t |  \ |  _ }	 | d k rp|	 } n  | d k r| d k rd	 | } q| d k rd | } qt d   n  | |  _ t |  |  _	 | r|  j t |  j	 |  j  @|  _ n  t |  j |  j	 |  j  sSt d t |   |  j	 f   qSn t d t |    d S(   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.
        i   i    iI       i   i   I    sJ   IPv4 Addresses can't be larger than 0xffffffffffffffffffffffffffffffff: %xi    l	    sJ   IPv6 Addresses can't be larger than 0xffffffffffffffffffffffffffffffff: %xi   s   only IPv4 and IPv6 supportedt   -i   s)   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   .s   netmask must be IPv4s"   only one '-' allowed in IP Addresss   can't parses!   %s has invalid prefix length (%s)s   Unsupported data type: %sN(   t   NoPrefixForSingleIpt   Nonet   WantPrefixLent
   isinstancet   intt   longt   ipt
   ValueErrort
   _ipversiont
   _prefixlenRe   t   strt   unicodet   splitt   lent   parseAddresst   _count1Bitst   IPt	   broadcastt   findt   _netmaskToPrefixlent   _prefixlenToNetmaskt   _checkNetaddrWorksWithPrefixlent   reprt	   TypeErrort   type(   t   selft   datat	   ipversiont   make_nett   netbitst	   prefixlent   xRo   t   lastt   parsedVersiont   lastversiont   sizet   netmaskt   vers(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __init__{   s    							,
			"	%c         C   s   |  j  S(   s   Return 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.pyRm      s    c         C   s   |  j  S(   s   Return 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.pyt   version  s    c         C   s   |  j  S(   sW   Returns Network Prefixlen.

        >>> IP('10.0.0.0/8').prefixlen()
        8
        (   Rr   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    c         C   s
   |  j    S(   sT   
        Return the base (first) address of a network as an (long) integer.
        (   Rm   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   net  s    c         C   s   |  j    |  j   d S(   sl   
        Return the broadcast (last) address of a network as an (long) integer.

        The same as IP[-1].i   (   Rm   Rv   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyRz     s    c         C   s  |  j  d k r |  j d k s< |  j  d k rQ |  j d k rQ |  j rQ d } qQ n  | d k r~ |  j } | d k r~ d } q~ n  | r| d k r |  j   } t | t t f  s | j   } n  d t	 | |  j   S| d	 k rd
 t	 |  j
 |  j   d |  j   Sd |  j Sn d Sd S(   s  Prints 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
        i   i    i   i   i    i   i   s   /%si   s   -%ss   /%dt    N(   Rq   Rr   Ri   Rj   Rk   R   Rl   Rm   Rn   t   intToIpRo   Rv   (   R   t   wantR   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   _printPrefix%  s$    		%c         C   s   |  j  d k r d } n$ |  j  d k r0 d } n t d   |  j d k r` | d k r` d } n  t |  j  } d | t |  | |  j |  S(	   s   Return a string representation as a binary value.

        >>> print(IP('127.0.0.1').strBin())
        01111111000000000000000000000001
        i   i    i   i   s   only IPv4 and IPv6 supportedi    R   N(   Rq   Rp   Rk   Rj   t	   _intToBinRo   Rv   R   (   R   t   wantprefixlent   bitst   ret(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   strBinO  s    			c   	      C   s  |  j  d k r$ | d k r$ d } n  |  j d k r@ |  j |  S|  j d ?d k r t |  j d @d  } d | |  j |  } | Sg  |  j d  j d  D] } t | d	  ^ q } d g d
 } x. t	 t
 |   D] } t | |  | | <q W| j t |   } t |  d k rg  |  j d  j d  D] } | ^ q6} x- | t
 |  k  rw| | d k rw| | =qKW| j | d  | d t
 |  k r| j d  n  | d k rd g | } n  d j |  |  j |  S|  j d  |  j |  Sd S(   sk  Return a string representation in compressed format using '::' Notation.

        >>> 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'
        i   i   i    i  I    s   ::ffff:i    t   :i   i   R   R   N(   Rk   Rj   Rq   t   strFullsizeRo   R   R   Ru   Rm   t   xrangeRv   t   _countFollowingZerost   indext   maxt	   strNormalt   insertt   appendt   join(	   R   R   t   ipv4t   textR   t   hextetst   followingzerost   it   compressionpos(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   strCompressedc  s0    	4+%c         C   s   |  j  d	 k r$ | d	 k r$ d } n  |  j d k rE |  j d  } nx |  j d k r d j g  g  |  j d  j d  D] } t | d  ^ qv D] } t |  d ^ q  } n t d   | |  j	 |  S(
   s   Return 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
        i   i   i    i   R   i   i   s   only IPv4 and IPv6 supportedN(
   Rk   Rj   Rq   R   R   Ru   Rm   t   hexRp   R   (   R   R   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    		]c         C   sJ   |  j  d k r$ | d k r$ d } n  t |  j |  j  j   |  j |  S(   s   Return 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
        i   N(   Rk   Rj   R   Ro   Rq   t   lowerR   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    		c         C   sg   |  j  d k r$ | d k r$ d } n  t |  j  } | d d k rP | d  } n  | j   |  j |  S(   s   Return 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'
        i    it   LN(   Rk   Rj   R   Ro   R   R   (   R   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   strHex  s    		c         C   sa   |  j  d k r$ | d k r$ d } n  t |  j  } | d d k rP | d  } n  | |  j |  S(   s   Return a string representation in decimal format.

        >>> print(IP('127.0.0.1').strDec())
        2130706433
        >>> print(IP('2001:0658:022a:cafe:0200::1').strDec())
        42540616829182469433547762482097946625
        i    iR   N(   Rk   Rj   Rs   Ro   R   (   R   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   strDec  s    		c         C   s   |  j  d k r t } n$ |  j  d k r0 t } n t d   |  j   } x< t t |  d d  D]" } | |  | k ra | | |  Sqa Wd S(   s  Return 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.
        i   i   s   only IPv4 and IPv6 supportedi    it   unknown(   Rq   t
   IPv4rangest
   IPv6rangesRp   R   R   Rv   (   R   t   iprangeR   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   iptype  s    		c         C   s]   |  j  d k r d |  j } n+ |  j  d k r> d |  j } n t d   d |  j d | >S(   sr   Return netmask as an integer.

        >>> "%X" % IP('195.185.0.0/16').netmask().int()
        'FFFF0000'
        i   i    i   i   s   only IPv4 and IPv6 supportedi   i   (   Rq   Rr   Rp   (   R   t   locallen(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    c         C   so   |  j  d k r8 d |  j } t d |  j d | >d  S|  j  d k r_ d |  j } d |  j St d   d	 S(
   s   Return 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
        i   i    i   i   i   i   s   /%ds   only IPv4 and IPv6 supportedN(   Rq   Rr   R   Rp   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt
   strNetmask
  s    
c         C   sR   |  j  d k r d |  j } n+ |  j  d k r> d |  j } n t d   d | S(   s   Return the length of a subnet.

        >>> print(IP('195.185.1.0/28').len())
        16
        >>> print(IP('195.185.1.0/24').len())
        256
        i   i    i   i   s   only IPv4 and IPv6 supportedi   (   Rq   Rr   Rp   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyRv     s    	c         C   s   t  S(   s  All 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.
        (   t   True(   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __nonzero__0  s    c         C   s   t  |  j    S(   s   Return the length of a subnet.

        Called to implement the built-in function len().
        It breaks with IPv6 Networks. Anybody knows how to fix this.(   Rm   Rv   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __len__9  s    	c         C   s   t  | t t f  s t  n  | d k  rd t |  |  j   k r[ |  j   t |  } q t  n | |  j   k r t  n  |  j t |  S(   sN  Called 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    (   Rl   Rm   Rn   R   t   absRv   t
   IndexErrorRo   (   R   t   key(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __getitem__E  s    			c         C   sT   t  |  } | j |  j k rL | j |  j |  j   | j   d k  rL t St Sd S(   s  Called 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
        i   N(   Ry   Ro   Rv   R   t   False(   R   t   item(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __contains__e  s    <c         C   sx   t  |  } | j |  j k r> | j |  j |  j   k  r> d S|  j | j k rp |  j | j | j   k  rp d Sd Sd S(   s  Check 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
        i   ii    N(   Ry   Ro   Rv   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   overlaps|  s    ..c         C   s
   |  j    S(   sS   Dispatch to the prefered String Representation.

        Used to implement str(IP).(   R   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __str__  s    c         C   s   d |  j  d  S(   s  Print 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    c         C   s   |  j  | j   k  r& | j   |  j  S|  j  | j   k rL | j   |  j  S|  j | j k  rb d S|  j | j k rx d S|  j | j k r |  j | j k  r d S|  j | j k r d Sd Sn d Sd S(   s  Called 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

        ii   i    N(   Rr   R   Ro   Rq   (   R   t   other(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __cmp__  s    c         C   s   |  j  |  d k S(   Ni    (   R   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __eq__  s    c         C   s   |  j  |  d k  S(   Ni    (   R   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __lt__  s    c         C   sW   t  d  } |  j } x( | d k r? | | d @A} | d ?} q W| |  j A} t  |  S(   sh  Called 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
        ii    ii    (   Rm   Ro   Rr   (   R   t   thehashRo   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   __hash__  s    	N(    t   __name__t
   __module__t   __doc__R   Rm   R   R   R   Rz   R   Rj   R   R   R   R   R   R   R   R   R   Rv   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyRe   u   s:   	
	
				*-	!							 					=		Ry   c           B   sh   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z RS(   s-   Class for handling IP addresses and networks.c         C   s   t  t j |   d |  j S(   s   Return 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   (   Ry   Re   R   Rq   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    c         C   s   t  t j |    S(   s   Return 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')
        (   Ry   Re   Rz   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyRz     s    c         C   s   t  t j |   d |  j S(   si   Return netmask as an IP object.

        >>> IP('10.0.0.0/8').netmask()
        IP('255.0.0.0')
         R   (   Ry   Re   R   Rq   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    c         C   st   |  j  d k r d  S|  j d ?d k r* d  S|  j d @} |  j d k rd d | d d |  j f } n  t | d d S(	   Ni   i    i  I    i   s   %s/%sR   i   (   Rq   Rj   Ro   Rr   Ry   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   _getIPv4Map!  s    c         C   s  |  j  d k r6g  } |  j   d k  rN x|  D] } | j | j    q. Wn |  j   d k  r x t d |  j   d  D]! } | j |  | j   d  qy Wn |  j   d k  r x| t d |  j   d  D]! } | j |  | j   d  q Wn> x; t d |  j   d  D]! } | j |  | j   d  qW| S|  j  d k r|  j   } | d k	 rg| j   St |  j	  d j
   } | d d	 k r| d  } n  |  j d d k rt d
   n  t |  } | j   d j |  } t d |  j d  d } d | | g St d   d S(   s  Return 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.']
        i   i   i   i   i    i   i   it   ls3   can't create IPv6 reverse names at sub nibble levelRh   i    s   %s.ip6.arpa.s   only IPv4 and IPv6 supportedNi   i   i   i   i   i   (   Rq   Rv   R   t   reverseNameR   R   Rj   t   reverseNamesR   Ro   R   Rr   t   NotImplementedErrort   listt   reverseR   Rm   Rp   (   R   R   R   R   R   t   st   first_nibble_index(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR   +  s:    ""

c         C   s<  |  j  d k r |  j d  } | j d  } | j   t d |  j d  } |  j d d k r d | d |  j d t |  j |  j   d d  j d  d f } | d d	 k r | d  } n  | d 7} n d
 } d j	 | |  } d | | f S|  j  d k r,|  j
   } | d k	 r)| j   St |  j  d j   } | d d	 k r_| d  } n  |  j d d k rd | |  j t |  j |  j   d  d j   f } | d d	 k r| d  } n  | d 7} n d
 } t |  } | j   d j	 |  } t d |  j d  d } d | | | f St d   d S(   s  Return 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.'
        i   i    Rh   i   s   %s-%si   i   iR   R   s   %s%s.in-addr.arpa.i   i   i    s   %s%s.ip6.arpa.s   only IPv4 and IPv6 supportedN(   Rq   R   Ru   R   Rm   Rr   R   Ro   Rv   R   R   Rj   R   R   R   R   Rp   (   R   R   t   first_byte_indext
   nibblepartR   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR   c  s@    
F
8
c         C   s?   d t  |  k r% t d |   n  t d |  | f d t S(   s   Transform 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
        Rg   s   invalid netmask (%s)s   %s/%sR   (   Rs   Rp   Ry   R   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    	c         C   s   t  t j |  |   S(   sA  Called 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
        (   Ry   Re   R   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    c         C   s   d |  j  d  S(   se   Print a representation of the Object.

        >>> IP('10.0.0.0/8')
        IP('10.0.0.0/8')
        s   IP('%s')i   (   R   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    c         C   s   |  j    | j    k r' t d   n  |  j    d k  rH t d   n  |  j   | j   k ro t d   n  |  | k r | j |   St |  j    } |  j    d | _ | Sd S(   s3   Emulate numeric objects through network aggregations3   Only networks with the same prefixlen can be added.i   s8   Networks with a prefixlen longer than /1 can't be added.s4   Only networks with the same IP version can be added.N(   R   Rp   R   t   __add__Ry   Rm   Rr   (   R   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    (   R   R   R   R   Rz   R   R   R   R   R   R   R   R   (    (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyRy     s   	
	
		
	8	5				c   
      C   s  g  } d } d } x| t |   k  r1|  | } | j d  r{ | d k	 r_ t d |    n  t |  } | d 7} q n  | j d  } | d k r t d |    n  | d k r | j | |   | | | d !d k r | | 7} n | | d 7} | t |   k r.t d |    q.q | j |  Pq W| rd	 | d k r| d k	 r~| t |  d k r~t d
 |    n  t | d  d } | d  d | d ?d | d @g } n  | d k	 rd t |  } | d k rt d |    n  | |  d g | | | } n  t |  d k r9t d |    n  d } d } x | D] } y0 t | d  } d | k o{d k n }	 Wn t k
 rt }	 n X|	 rt d |  | f   n  | d >| } | d 7} qLW| 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
    i    s   ::s,   %r: Invalid IPv6 address: more than one '::'i   R   s   %r: Invalid IPv6 addressii   Rh   s)   %r: Invalid IPv6 address: '::' after IPv4s   %04xi   i  i   s,   %r: Invalid IPv6 address: '::' is not neededR   s/   %r: Invalid IPv6 address: should have 8 hextetss+   %r: Invalid IPv6 address: invalid hexlet %rN(	   Rj   Rv   t
   startswithRp   R{   R   Rw   Rm   R   (
   t   ipstrt   itemsR   t   fill_posR   t   post   valuet   diffR   t   error(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   _parseAddressIPv6  s`    6

#' !
c         C   s  |  j  d  rd t |  d d  } | d k rA t d |    n  | d k  rW | d f S| d f Sn  |  j d	  d
 k r t |   d f St |   d k r t |  d  d f S|  j d  d
 k s t |   d k  rt |   d k  r|  j d  } t |  d k rt d   n  | d g d t |  7} g  | D] } t |  ^ q9} x9 | D]1 } | d k sv| d k  rXt d |    qXqXW| d d >| d d >| d d >| d d f St |  d  } | d k rt d   n  | d k r| d f S| d f Sd S(   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)
    t   0xi   i   l	    s)   %r: IP Address can't be bigger than 2^128I       i   i   R   ii    Rh   i   s#   IPv4 Address with more than 4 bytesR   i   i    s'   %r: single byte must be 0 <= byte < 256i   i   i   i   i
   s%   IP Address can't be bigger than 2^128I    N(   R   Rn   Rp   R{   R   Rv   Rm   Ru   (   R   R   t   bytesR   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyRw   I  s6    #
92
c         C   s  t  |   }  |  d k  r+ t d |    n  d } | d k r |  d k rb t d t |     n  x6 t d  D]( } t |  d @ d | } |  d	 ?}  qo W| d
  } n | d k rp|  d k r t d t |     n  t j d k r t |   d } n t |   d d
 !} | j d  } xC t d d  D]2 } | | | } | d d k r-d | } q-q-W| d } n t d   | S(   s/   Transform an integer string into an IP address.i    s   IPs can't be negative: %dR   i   I    s2   IPv4 Addresses can't be larger than 0xffffffff: %si   Rh   i   ii   l	    sJ   IPv6 Addresses can't be larger than 0xffffffffffffffffffffffffffffffff: %si   i   i    i   i!   R   s   only IPv4 and IPv6 supported(   Rn   Rp   R   R   Rs   t   syst
   hexversiont   zfill(   Ro   R   R   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s2    c         C   s0   |  d k r d S|  d k r  d St  d   d S(   s  Return number of bits in address for a certain IP version.

    >>> _ipVersionToLen(4)
    32
    >>> _ipVersionToLen(6)
    128
    >>> _ipVersionToLen(5)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "IPy.py", line 1076, in _ipVersionToLen
        raise ValueError("only IPv4 and IPv6 supported")
    ValueError: only IPv4 and IPv6 supported
    i   i    i   i   s   only IPv4 and IPv6 supportedN(   Rp   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   _ipVersionToLen  s
    c         C   s@   t  |   d k r d S|  d d k r* d Sd t |  d  Sd S(   sD   Return number of elements containing 0 at the beginning of the list.i    i   N(   Rv   R   (   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s
    t   0000t   0010t   2t   3t   0100t   4t   0101t   5t   0110t   6t   0111t   7t   1000t   8t   1001t   9t   1010t   at   1011t   bt   1100t   ct   1101t   dt   et   1111t   fc         C   s   |  d k  r t  d   n  t |   j   } d } | d d k rP | d  } n  x  | d D] } | t | 7} q[ Wx0 | d d k r t |  d k r | d } qv W| S(	   s9   Return the binary representation of an integer as string.i    s   Only positive values allowedR   iR   i   R   i   (   Rp   R   R   t	   _BitTableRv   (   t   valR   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR     s    %c         C   s1   d } x$ |  d k r, |  d ?}  | d 7} q	 W| S(   s,   Find the highest bit set to 1 in an integer.i    i   (    (   t   numR   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyRx     s
    
c         C   sp   t  |   }  |  d k  r+ t d |    n  d } x8 |  d k rk |  d @d k rT Pn  |  d ?}  | d 7} q4 W| S(   s,   Find the highest bit set to 0 in an integer.i    s    Only positive Numbers please: %si   (   Rn   Rp   (   R  R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   _count0Bits  s    
c         C   si   t  |  } | d k  s$ | | k r( d S|  d k rA | d } n t |   } | | | k  ra d Sd Sd S(   sJ  Check 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'
    i    i   N(   R   Rj   R  (   Ro   R   R   R   t   zbits(    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   _checkPrefix  s    c         C   s   t  |   } | } xD | d @d k rX | d k rX | d ?} | d 8} | d k r Pq q WxM | d k r | d @d k r t d t |     n  | d ?} | d 8} q\ Wd S(   s2   Checks if a netmask is expressable as a prefixlen.i   i    s+   Netmask %s can't be expressed as an prefix.N(   Rn   Rp   R   (   R   t   masklenR  R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   _checkNetmask(  s    


c         C   s   |  t  | |  @|  k S(   sB   Check if a base addess of a network is compatible with a prefixlen(   R}   (   R   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR~   <  s    c         C   s-   t  |   } t |   } t |  |  | | S(   sn   Convert an Integer representing a netmask to a prefixlen.

    E.g. 0xffffff00 (255.255.255.0) returns 24
    (   R  Rx   R  (   R   t   netlenR
  (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR|   A  s    c         C   sI   |  d k r d S|  d k  r+ t  d   n  d |  d >d t |  |  >S(   s   Return 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
    i    s   Prefixlen must be > 0i   i   (   Rp   R   (   R   R   (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyR}   M  s
    t   __main__i   (   R   t   __version__R   t   typesR   R   Re   Ry   R   Rw   R   R   R   R  R   Rx   R  R	  R  R~   R|   R}   R   t   doctestt   testmodt   failuret   nbtestt   exit(    (    (    s'   /usr/lib/python2.7/site-packages/IPy.pyt   <module>   s   

  	x	L	%		
				 				