noc.core.ip

Module Contents

noc.core.ip.B16 = 65535
noc.core.ip.B32 = 4294967295
class noc.core.ip.IP(prefix)

Bases: object

Base class for IP prefix

afi
special_addresses

Set of ‘special’ addresses for prefix. ‘special’ addresses, like network or broadcast usually can’t be allocated or used :return: Set of specoal addresses

__repr__(self)

Returns string representation of prefix

__str__(self)

Returns string containing prefix

__len__(self)

Returns mask length (in bits)

__cmp__(self, other)

Compare prefix with other

Parameters:other (IP) – IP instance to be compared
Return type:int

Returns:

  • 0 – if prefix equals to other
  • <0 – if prefix less than other
  • >0 – if prefix greater than other
__le__(self, other)

<= operator

__ge__(self, other)

>= operator

__contains__(self, other)

“other in self”

classmethod get_afi(cls, prefix)
classmethod prefix(cls, prefix)

Convert string to prefix instance.

Parameters:prefix (str) – String containing IPv4/IPv6 prefix
Returns:IPv6 or IPv6 instance
Return type:IPv4 or IPv6 instance
iter_address(self, count=None, until=None, filter=None)

Return generator of continuing addresses beginning from prefix instance.

Parameters:
  • count (Integer, Optional) – Stop after yielding count addresses
  • until (IP instance, Optional) – Stop when reaching until address
  • filter (Callable accepting IP instance and returning boolean, Optional.) – Filter addresses through filter callable
Returns:

Generator of continuing addresses

Return type:

Generator of IP instances

iter_cover(self, mask)

Generate prefixes of size _mask_ covering basic prefix

iter_free(self, prefixes)

Return generator of free prefixes.

Parameters:prefixes (list) – List of occupied prefixes
Returns:Generator of free prefixes
Return type:Generator of IP instances
area_spot(self, addresses, dist, sep=False, exclude_special=True)

Returns a list of addresses, laying inside prefix and containing area around given addresses

Parameters:
  • addresses (list) – Used addresses
  • dist (int) – Distance to spot araund used addresses
  • sep (bool) – Insert None into the gaps if sep is True
  • exclude_special (bool) – Exclude Broadcast & network addresses
Returns:

List containing area spot

Return type:

list

rebase(self, base, new_base)

Rebase to a new base prefix :param base: :param new_base: :return:

static expand(addr)

Expand and normalize address for reliable key lookup :param addr: :return:

class noc.core.ip.IPv4(prefix, netmask=None)

Bases: noc.core.ip.IP

IPv4 Prefix. Internally stored as unsigned 32-bit integer and mask

afi = 4
size

Get size of prefix (number of addresses to hold)

Returns:Size of prefix
Return type:integer
first

Returns new IPv4 instance with first address of the block (network) :return: First address :rtype:

last

Returns new IPv4 instance with last address of the block (broadcast) :return:

netmask

Returns new IPv4 instance holding netmask :return:

wildcard

Returns new IPv4 instance with Cisco-style wildcard :return:

normalized

Returns new IPv4 instance in normalized minimal possible form :return:

special_addresses
classmethod netmask_to_len(cls, netmask)

Returns netmask mask length

_get_parts(self)

get list of 4 integers (IPv4 octets)

Returns:List of 4 integers
Return type:List of 4 integers
classmethod _to_prefix(cls, s, mask)

Convert integer and mask into new IPv4 instance

Parameters:
  • s (integer) – integer representation of address (unsigned 32-bit integer)
  • mask (integer) – mask length (0 .. 32)
__hash__(self)

Hashing

__eq__(self, other)

== operator

__ne__(self, other)

!= operator

__lt__(self, other)

< operator

__gt__(self, other)

> operator

__add__(self, n)
  • operator.
Parameters:n (integer) – distance
Returns:IPv4 instance
Return type:IPv4 instance
__sub__(self, n)
  • operator.

If argument is integer - returns new IPv4 instance If IPv4 instance - returns a distance between addresses

Parameters:n (IPv4 instance or integer) –
Returns:Distance or new instance
Return type:integer of IPv4 instance
iter_bits(self)

Get generator returning up to mask bits of prefix

Returns:Generator of bits
Return type:Generator
classmethod from_bits(cls, bits)

Create new IPv4 instance from list of bits

Parameters:bits – List of 0 or 1
Returns:New IPv4 instance
Return type:IPv4 instance
contains(self, other)

Check if other contained in prefix. Returns bool :param other: :return:

set_mask(self, mask=32)

Returns new IPv4 instance with new mask value. If mask not set, returns with /32 :param mask: :return:

classmethod range_to_prefixes(cls, first, last)

Convert IPv4 address range to minimal list of covering prefixes

>>> IPv4.range_to_prefixes('192.168.0.2', '192.168.0.2')
[<IPv4 192.168.0.2/32>]
>>> IPv4.range_to_prefixes('192.168.0.2', '192.168.0.16')
[<IPv4 192.168.0.2/31>, <IPv4 192.168.0.4/30>, <IPv4 192.168.0.8/29>, <IPv4 192.168.0.16/32>]
>>> IPv4.range_to_prefixes('0.0.0.0', '255.255.255.255')
[<IPv4 0.0.0.0/0>]
Parameters:
  • cls
  • first
  • last
Returns:

static expand(addr)

IPv6.expand compatibility stub :param addr: :return:

class noc.core.ip.IPv6(prefix, netmask=None)

Bases: noc.core.ip.IP

IPv6 prefix Internally stored as four 32-bit integers

afi = 6
first

Returns new IPv6 instance with first address of prefix :return:

last

Returns new IPv6 instance with last address of prefix :return:

normalized

Returns new IPv6 instance in normalized minimal possible form :return:

digits

Returns 32 hexadecimal digits :return:

static __split_parts(address)

Convert prefix to a list of 8 integers :return:

static mask_to_bits(mask)
_get_parts(self)

Convert prefix to a list of 8 integers :return:

_get_masks(self)

Return 4 integers, containing bit mask :return:

classmethod _to_prefix(cls, d0, d1, d2, d3, mask)

Convert four 32-bit integers and mask to a new IPv6 instance :param d0: :param d1: :param d2: :param d3: :param mask: :return:

__hash__(self)

hash(..) :return:

__eq__(self, other)

== operator :param other: :return:

__ne__(self, other)

!= operator :param other: :return:

__lt__(self, other)

< operator :param other: :return:

__gt__(self, other)

> operator :param other: :return:

__add__(self, n)
  • operator. Second argument is an integer.

Returns new IPv6 instance :param n: :return:

__sub__(self, n)
  • operator. Second argument is an integer or IPv6 instance

Return new IPv6 instance, if second argument is integer, or distance betweed two prefixes :param n: :return:

iter_bits(self)

Generator returning mask bits of prefix :return:

classmethod from_bits(cls, bits)

Convert a list of bits to a new IPv6 prefix instance :param bits: :return:

contains(self, other)

Check if other contained within prefix. Returns bool :param other: :return:

set_mask(self, mask=128)

Returns new IPv4 instance with new mask value. If mask not set, returns with /128 :param mask: :return:

ptr(self, origin_len)

Returns PTR value for IPv6 reverse zone :param origin_len: :return:

static expand(addr)

Expand :: with appropriate amount of :0: :param addr: :return: Expanded address as string

class noc.core.ip.PrefixDB(key=None)

Bases: object

Generalized binary-tree prefix lookup database

__getitem__(self, prefix)

Get key by prefix :param prefix: :return:

__setitem__(self, prefix, key)

Put prefix with key :param prefix: :param key: :return:

iter_free(self, root)

Generator returning free blocks :param root: :return: