pymnl.attributes Module

Netlink attributes can have the following types:

Name Type
TYPE_UNSPEC Unspecified type
TYPE_U8 8-bit integer
TYPE_U16 16-bit integer
TYPE_U32 32-bit integer
TYPE_U64 64-bit integer
TYPE_STRING character string
TYPE_FLAG flag
TYPE_MSECS micro seconds (64bit)
TYPE_NESTED nested attributes
TYPE_NESTED_COMPAT  
TYPE_NUL_STRING null-terminated character string
TYPE_BINARY  
pymnl.attributes.NLA_F_NESTED

Signifies whether the attribute has nested attributes.

pymnl.attributes.NLA_F_NET_BYTEORDER

Signifies whether the attribute is in network byte order.

pymnl.attributes.NLA_ALIGNTO

The size to which netlink attributes should be aligned.

pymnl.attributes.NLA_ALIGN(x)

A function that returns the size of x aligned to the size of NLA_ALIGNTO.

pymnl.attributes.ATTR_HDRLEN

The aligned size of an attribute header.

class pymnl.attributes.Attr(type=None, value=None, packed_data=None)

Netlink Length-Type-Value (LTV) attribute:

|<-- 2 bytes -->|<-- 2 bytes -->|<-- variable -->|
-------------------------------------------------
|     length    |      type     |      value     |
-------------------------------------------------
|<--------- header ------------>|<-- payload --->|

The payload of the Netlink message contains sequences of attributes that are expressed in LTV format.

Attr stores the value in packed format, internally. The value is immediately packed when an Attr is created via non-packed data. It will be unpacked, as needed, when called for through the get_*() methods.

__init__(type=None, value=None, packed_data=None)

Create a new Attr object.

type - attribute's type (see NLA_* constants in linux/netlink.h)

value - string or number representing the payload

packed_data - This is the binary string containing the
attribute data. This could be the output from another object's get_binary() method or data directly from Netlink.
__len__()

Get the length of the packed attribute (in bytes).

__weakref__

list of weak references to the object (if defined)

get_binary()

Return a packed struct to include in message payload.

Adds null-bytes to end to pad attribute's length to a multiple of NLA_ALIGNTO.

get_data()

Return the non-header data string, a.k.a. the attribute's payload (not it's Payload).

get_str()

Return value as a string.

Raises TypeError if the data length is zero. Also raises TypeError if type is TYPE_NUL_STRING and is not null-terminated. A string should have a non-zero length and a null-terminated string should have a null termination or something went wrong.

get_str_stripped()

Return value as a string, without zero terminator.

get_type()

Get the attribute's type.

get_u16()

Return value as a two byte integer.

Raises TypeError if the data length does not match the length expected for the type.

get_u32()

Return value as a four byte integer.

Raises TypeError if the data length does not match the length expected for the type.

get_u64()

Return value as an eight byte integer.

Raises TypeError if the data length does not match the length expected for the type.

get_u8()

Return value as a one byte integer.

Raises TypeError if the data length does not match the length expected for the type.

get_value_len()

Return the length of the data payload.

is_nested()

Return True if the nested flag is set, else return False.

classmethod new_str(type, value)

Return a new Attr object with a non-zero-terminated string.

classmethod new_strz(type, value)

Return a new Attr object with a zero-terminated string.

This method will add the null termination. Pass this method a non-zero-terminated string.

classmethod new_u16(type, value)

Return a new two byte long Attr object.

classmethod new_u32(type, value)

Return a new four byte long Attr object.

classmethod new_u64(type, value)

Return a new eight byte long Attr object.

classmethod new_u8(type, value)

Return a new one byte long Attr object.

set(type, value)

Set the attribute type and value.

type - attribute's type (see NLA_* constants in linux/netlink.h)

value - string representing the payload

toggle_nested()

Toggle the nested flag on and off for the Attr object.

type_valid()

Return False if the type is not known, otherwise return True.

class pymnl.attributes.AttrParser(data_obj=None, offset=0)

Base class for attribute parsers.

This class provides the most basic parsing capability. In most cases, you should use a subclass with callback methods or even replace the parse() method.

However, AttrParser will handle simple attribute data. And return a list of the attributes found.

__init__(data_obj=None, offset=0)

Parse a string for netlink attributes.

data_obj - An optional object with attributes. The data
object can be passed here and will be immediately parsed. Or the object can be sent to the parse() method after initialization. See parse() for more details.

offset - offset into data at which to start

__weakref__

list of weak references to the object (if defined)

get_attrs()

Return list of attributes parsed from data string.

If the data string is passed on object creation, and no callback methods have been assigned, this method can be used to get the list of attributes.

parse(data_obj, offset=0)

Returns a (possibly empty) list of Attr processed from the binary string.

data_obj - An object containing attributes and providing the
get_binary() method. See Message and Payload for examples of get_binary().

offset - offset into data at which to start

parse_nested(data_obj)

Returns a (possibly empty) list of Attr processed from the binary string.

data_obj - An object containing nested attributes and providing
the get_data() method. get_data() must return the non-header binary string to be parsed here. See Payload and Attr for examples of get_data().
parse_string(data, offset=0)

Process the attributes.

data - raw data to parse

offset - offset into data at which to start

This method takes care of the low-level detail of finding attributes in a data string and returns individual attributes one at a time (parse_string() is a generator).

Subclasses can override parse() and still use parse_string() to retrieve individual attributes from the data.