Adding nla_put() to the capi using a typemap on the input
parameter which needs to be either a str or bytearray.
Otherwise a SWIG exception with be thrown.
Signed-off-by: Arend van Spriel <aspriel@gmail.com>
The define was left enabled during development on netlink (and genl)
python swig api. It is a bit annoying in production release so disable
the define.
Signed-off-by: Arend van Spriel <aspriel@gmail.com>
The message receive callback handler in the netlink api processes
the result object from the python callback. It used PyArg_ParseTuple()
to get the value, but this does not work as intended (see ref [1]).
Instead check the type and convert it accordingly.
refs:
[1] http://stackoverflow.com/questions/13636711/what-is-the-proper-usage-of-pyarg-parsetuple
Reported-by: Teto <mattator@gmail.com>
Signed-off-by: Arend van Spriel <arend@broadcom.com>
Signed-off-by: Thomas Graf <tgraf@suug.ch>
Copied the typedefs when adding callback support, but they serve
no actual use in the swig input file.
Signed-off-by: Arend van Spriel <arend@broadcom.com>
Signed-off-by: Thomas Graf <tgraf@suug.ch>
The callback functionality only worked for regular Python
functions. With this patch it also allows the callback
to be a class method (bounded or unbounded) as show in
example below.
class test_class(object):
def my_callback(self, msg, arg):
print('handling %s' % str(msg))
s = netlink.core.Socket()
testobj = test_class()
netlink.capi.py_nl_cb_set(cb, netlink.capi.NL_CB_VALID,
netlink.capi.NL_CB_CUSTOM,
test_class.my_callback, testobj)
netlink.capi.py_nl_cb_err(cb, netlink.capi.NL_CB_CUSTOM,
test_class.my_callback, s)
Signed-off-by: Arend van Spriel <arend@broadcom.com>
Signed-off-by: Thomas Graf <tgraf@suug.ch>
The initial commit adding netlink callback handling also introduced
memory leak issue. The python callback info was stored in an allocated
structure, but that was never freed.
Only exposing nl_cb_alloc() as is. nl_cb_get() is removed as it is
not very useful to use reference counting mechanism. Python uses
that itself internally. To deal properly with Python callback info
the function nl_cb_put() and nl_cb_clone() have a custom wrapper
taking care of Python reference counting.
This commit also adds a Callback python class using the netlink
callback functions.
Signed-off-by: Arend van Spriel <arend@broadcom.com>
Signed-off-by: Thomas Graf <tgraf@suug.ch>
added support functions to access the netlink attributes and use
custom callback handlers. Most is wrapped as is, but there are
a couple of special cases handled.
1) void *nla_data(struct nlattr *);
The return value is changed to a Python byte array so it includes
the lenght of the data stream.
2) int nla_parse_nested(...);
This returns a tuple (err, dict). 'err' is the error code and 'dict'
is a dictionary with attribute identifier as key and value represents
a struct nlattr object.
3) macro nla_for_each_nested()
Provide nla_get_nested() which returns a Python list of struct nlattr
objects that is iterable.
4) allocate struct nla_policy array
Provide nla_policy_array() function that allocates consecutive space
in memory for struct nla_policy array entries. Each entry is put in
a Python list so the entry fields can be modified in Python. This
array object can be passed to the nla_parse_nested() function.
Signed-off-by: Arend van Spriel <arend@broadcom.com>
Signed-off-by: Thomas Graf <tgraf@suug.ch>