All the netlink providers are derived from the socket class, so they provide normal socket API, including getsockopt(), setsockopt(), they can be used in poll/select I/O loops etc.
Generic marshalling class
Parse string data.
At this moment all transport, except of the native Netlink is deprecated in this library, so we should not support any defragmentation on that level
Generic netlink socket
Bind the socket to given multicast groups, using given pid.
Correctly close the socket and free all resources.
Get parsed messages list. If msg_seq is given, return only messages with that msg[‘header’][‘sequence_number’], saving all other messages into self.backlog.
The routine is thread-safe.
The bufsize parameter can be:
the network data
0: bufsize will be calculated from SO_RCVBUF sockopt
int >= 0: just a bufsize
Return policy for a given message type or for all message types. Policy parameter can be either int, or a list of ints. Always return dictionary.
Construct a message from a dictionary and send it to the socket. Parameters:
Example:
s = IPRSocket()
s.bind()
s.put({'index': 1}, RTM_GETLINK)
s.get()
s.close()
Please notice, that the return value of s.get() can be not the result of s.put(), but any broadcast message. To fix that, use msg_seq – the response must contain the same msg[‘header’][‘sequence_number’] value.
Register a callback to run on a message arrival.
Callback is the function that will be called with the message as the first argument. Predicate is the optional callable object, that returns True or False. Upon True, the callback will be called. Upon False it will not. Args is a list or tuple of arguments.
Simplest example, assume ipr is the IPRoute() instance:
# create a simplest callback that will print messages
def cb(env, msg):
print(msg)
# register callback for any message:
ipr.register_callback(cb)
More complex example, with filtering:
# Set object's attribute after the message key
def cb(env, msg, obj):
obj.some_attr = msg["some key"]
# Register the callback only for the loopback device, index 1:
ipr.register_callback(cb,
lambda e, x: x.get('index', None) == 1,
(self, ))
Please note: you do not need to register the default 0 queue to invoke callbacks on broadcast messages. Callbacks are iterated before messages get enqueued.
Register netlink encoding/decoding policy. Can be specified in two ways: nlsocket.register_policy(MSG_ID, msg_class) to register one particular rule, or nlsocket.register_policy({MSG_ID1: msg_class}) to register several rules at once. E.g.:
policy = {RTM_NEWLINK: ifinfmsg,
RTM_DELLINK: ifinfmsg,
RTM_NEWADDR: ifaddrmsg,
RTM_DELADDR: ifaddrmsg}
nlsocket.register_policy(policy)
One can call register_policy() as many times, as one want to – it will just extend the current policy scheme, not replace it.
Remove the first reference to the function from the callback register
Unregister policy. Policy can be:
In the last case the routine will ignore dict values, it is implemented so just to make it compatible with get_policy_map() return value.