def nlmsg_attrlen(nlh, hdrlen): """Length of attributes data. https://github.com/thom311/libnl/blob/libnl3_2_25/lib/msg.c#L154 nlh -- Netlink message header (nlmsghdr class instance). hdrlen -- length of family specific header (integer). Returns: Integer. """ return max(nlmsg_len(nlh) - libnl.linux_private.netlink.NLMSG_ALIGN(hdrlen), 0)
def dump_error_msg(msg, ofd=_LOGGER.debug): """https://github.com/thom311/libnl/blob/libnl3_2_25/lib/msg.c#L908. Positional arguments: msg -- message to print (nl_msg class instance). Keyword arguments: ofd -- function to call with arguments similar to `logging.debug`. """ hdr = nlmsg_hdr(msg) err = libnl.linux_private.netlink.nlmsgerr(nlmsg_data(hdr)) ofd(' [ERRORMSG] %d octets', err.SIZEOF) if nlmsg_len(hdr) >= err.SIZEOF: ofd(' .error = %d "%s"', err.error, os.strerror(-err.error)) ofd(' [ORIGINAL MESSAGE] %d octets', hdr.SIZEOF) errmsg = nlmsg_inherit(err.msg) print_hdr(ofd, errmsg)
def nl_msg_dump(msg, ofd=_LOGGER.debug): """Dump message in human readable format to callable. https://github.com/thom311/libnl/blob/libnl3_2_25/lib/msg.c#L970 Positional arguments: msg -- message to print (nl_msg class instance). Keyword arguments: ofd -- function to call with arguments similar to `logging.debug`. """ hdr = nlmsg_hdr(msg) ofd('-------------------------- BEGIN NETLINK MESSAGE ---------------------------') ofd(' [NETLINK HEADER] %d octets', hdr.SIZEOF) print_hdr(ofd, msg) if hdr.nlmsg_type == libnl.linux_private.netlink.NLMSG_ERROR: dump_error_msg(msg, ofd) elif nlmsg_len(hdr) > 0: print_msg(msg, ofd, hdr) ofd('--------------------------- END NETLINK MESSAGE ---------------------------')
def print_msg(msg, ofd, hdr): """https://github.com/thom311/libnl/blob/libnl3_2_25/lib/msg.c#L929. Positional arguments: msg -- Netlink message (nl_msg class instance). ofd -- function to call with arguments similar to `logging.debug`. hdr -- Netlink message header (nlmsghdr class instance). """ payloadlen = c_int(nlmsg_len(hdr)) attrlen = 0 data = nlmsg_data(hdr) ops = nl_cache_ops_associate_safe(msg.nm_protocol, hdr.nlmsg_type) if ops: attrlen = nlmsg_attrlen(hdr, ops.co_hdrsize) payloadlen.value -= attrlen if msg.nm_protocol == libnl.linux_private.netlink.NETLINK_GENERIC: data = print_genl_msg(msg, ofd, hdr, ops, payloadlen) if payloadlen.value: ofd(' [PAYLOAD] %d octets', payloadlen.value) dump_hex(ofd, data, payloadlen.value, 0) if attrlen: attrs = nlmsg_attrdata(hdr, ops.co_hdrsize) attrlen = nlmsg_attrlen(hdr, ops.co_hdrsize) dump_attrs(ofd, attrs, attrlen, 0)