IPprefix Module

This Module (ipp) provides methods to store and use IPv4 and Pv6 addresses.

To do anything with IPprefix objects using pypy-libtrace, you must import pp.

(This is a change from python-libtrace, which automatically imported ipp).

Class IPprefix

Represents an IP adress or prefix.

Instance Variables
version 4 for IPv4, 6 for IPv6
addr 4- or 16-byte pypy CFFI array containing address; the array bytes contain the actual bytes of the address
length Number of bits in prefix. If length was not specified the IPprefix represents just an IP address, its length (by default) is 32 (IPv4) or 128 (IPv6)

Class Methods
from_s() ipp.from_s(aString)-> aNewIPprefix

Parses str to find version and address, e.g.
   p = ipp.from_s('192.168.1.1')
   p = ipp.from_s('fe80::20d:60ff:fe38:18b/64')
str()
str(IPprefix) -> aString

Returns a string containing a printable version of the IPprefix's address.
If the IPprefix's width is not  32 (IPv4) or 128 (IPv6), /width will be appended to the result string.

Instance Methods
<, <=, ==, >=, >, !=
IPprefix op OtherIPprefix -> True or False

'Rich compare' operators: comparisions use the versions, addresses and lengths of two IPprefixes.  These can be used to sort IPprefixes into ascending or descending order, IPprefixes with the same addr compare based on their lengths.
Note: IPprefixes can be used as dictionary keys, but a dictionary must contain only IPv4 or only IPv6 keys
IPprefix(v, a)
IPprefix(version,address) -> aNewIPprefix

Returns an IPprefix conatining an IP address, e.g. 192.168.0.3
   version  = 4 for IPv4, 6 for IPv6
   address = 4- or 16-byte array containing
                    actual address
   length defaults to 32 (IPv4) or 128 (IPv6)
IPprefix(v, a, len) IPprefix(version, address, length) -> aNewIPprefix

Returns an IPprefix containing a network prefix, e.g. 192.168.0.0/16.
   version, address - as above
   length = number of bits in prefix
version IPprefix.version -> anIinteger
addr IPprefix.addr -> aByteArray
length IPprefix.length-> anIinteger

Read values of the instance variables.
length= IPprefix.length = anInteger -> anIinteger

Sets value for an IPprefix width.

Instance Methods for prefix testing
is_prefix() IPprefix .is_prefix(OtherIPprefix) -> True or False

Returns true if this IPprefix is a prefix of OtherIPprefix, i.e. their first IPprefix.length bits are the same.
equal() IPprefix.equal(OtherIPprefix) -> True or False

Two IPprefixes are equal if they have the same version and addr.
width IPprefix.width -> anInteger

Returns IPprefix.length - 1
has_bit_set() IPprefix.has_bit_set(Integer) -> True or False

Returns true if the bit specified by Integer is 1.
For example, if Integer is 0, has_bit_set is true if the leftmost bit of IPprefix is 1.
first_bit_different() IPprefix.first_bit_different(OtherIPprefix) -> anInteger

Returns an Integer, the (0-origin) bit position where the two IPprefixes are different.
complement IPprefix.complement -> OtherIPprefix

Returns the complement of an IPprefix, i.e. one having the same length, but all bits the ones-complement of those in IPprefix.
is_rfc1918 IPprefix.is_rfc1918 -> True or False

Returns True if the IPprefix's address is in the IPv4 range specified in RFC 1918, i.e. in one of the Private prefixes below.

Global Constants
'Private'  prefixes
See RFC 1918 for more details

ipp.rfc1918s16 192.168.0.0/16

ipp.rfc1918s12 172.16.0.0/12

ipp.rfc1918s8 10.0.0.0/8

Nevil Brownlee
Fri,  5 Jan 2016, (NZDT)