def print_hdr(ofd, msg): """https://github.com/thom311/libnl/blob/libnl3_2_25/lib/msg.c#L793. Positional arguments: ofd -- function to call with arguments similar to `logging.debug`. msg -- message to print (nl_msg class instance). """ nlh = nlmsg_hdr(msg) buf = bytearray() ofd(' .nlmsg_len = %d', nlh.nlmsg_len) ops = nl_cache_ops_associate_safe(msg.nm_protocol, nlh.nlmsg_type) if ops: mt = nl_msgtype_lookup(ops, nlh.nlmsg_type) if not mt: raise BUG buf.extend('{0}::{1}'.format(ops.co_name, mt.mt_name).encode('ascii')) else: nl_nlmsgtype2str(nlh.nlmsg_type, buf, 128) ofd(' .type = %d <%s>', nlh.nlmsg_type, buf.decode('ascii')) ofd(' .flags = %d <%s>', nlh.nlmsg_flags, nl_nlmsg_flags2str(nlh.nlmsg_flags, buf, 128).decode('ascii')) ofd(' .seq = %d', nlh.nlmsg_seq) ofd(' .port = %d', nlh.nlmsg_pid)
def callback(_, msg_): nlh = nlmsg_hdr(msg_) assert 20 == nlh.nlmsg_len assert 16 == nlh.nlmsg_type assert 5 == nlh.nlmsg_flags ops = nl_cache_ops_associate_safe(NETLINK_GENERIC, nlh.nlmsg_type) assert 'genl/family' == ops.co_name assert 4 == ops.co_hdrsize assert 16 == ops.co_protocol assert 0 == ops.co_hash_size assert 0 == ops.co_flags assert 'genl/family' == ops.co_obj_ops.oo_name assert 80 == ops.co_obj_ops.oo_size assert 1 == ops.co_obj_ops.oo_id_attrs assert 0 == nlmsg_attrlen(nlh, ops.co_hdrsize) mt = nl_msgtype_lookup(ops, nlh.nlmsg_type) assert 16 == mt.mt_id assert 0 == mt.mt_act assert 'nlctrl' == mt.mt_name called.append(True) return NL_STOP