def dump_attrs(ofd, attrs, attrlen, prefix=0): """https://github.com/thom311/libnl/blob/libnl3_2_25/lib/msg.c#L869. Positional arguments: ofd -- function to call with arguments similar to `logging.debug`. attrs -- nlattr class instance. attrlen -- length of payload (integer). Keyword arguments: prefix -- additional number of whitespace pairs to prefix each log statement with. """ prefix_whitespaces = ' ' * prefix rem = c_int() for nla in nla_for_each_attr(attrs, attrlen, rem): alen = nla_len(nla) if nla.nla_type == 0: ofd('%s [ATTR PADDING] %d octets', prefix_whitespaces, alen) else: is_nested = ' NESTED' if nla_is_nested(nla) else '' ofd('%s [ATTR %02d%s] %d octets', prefix_whitespaces, nla.nla_type, is_nested, alen) if nla_is_nested(nla): dump_attrs(ofd, nla_data(nla), alen, prefix + 1) else: dump_attr(ofd, nla, prefix) padlen = nla_padlen(alen) if padlen > 0: ofd('%s [PADDING] %d octets', prefix_whitespaces, padlen) dump_hex(ofd, bytearray_ptr(nla_data(nla), alen), padlen, prefix) if rem.value: ofd('%s [LEFTOVER] %d octets', prefix_whitespaces, rem)
def nlmsg_for_each_attr(nlh, hdrlen, rem): """Iterate over a stream of attributes in a message. https://github.com/thom311/libnl/blob/libnl3_2_25/include/netlink/msg.h#L123 Positional arguments: nlh -- Netlink message header (nlmsghdr class instance). hdrlen -- length of family header (integer). rem -- initialized to len, holds bytes currently remaining in stream (c_int). Returns: Generator yielding nl_attr instances. """ return nla_for_each_attr(nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen), rem)