From dfcb1ddbc19b12c9392148769086b265eaa8ae7a Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 3 Aug 2014 08:38:55 +0200 Subject: [PATCH] monitor: Print data packet for unknown attributes --- monitor/nlmon.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/monitor/nlmon.c b/monitor/nlmon.c index c4cace86..9c915c98 100644 --- a/monitor/nlmon.c +++ b/monitor/nlmon.c @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -59,6 +60,51 @@ static void nlmon_req_free(void *data) l_free(req); } +#define print_indent(indent, fmt, args...) \ + printf("%*c" fmt "\n", (indent), ' ', ## args) + +#define print_text(fmt, args...) \ + print_indent(8, fmt, ## args) + +static void print_hexdump(const unsigned char *buf, uint16_t len) +{ + static const char hexdigits[] = "0123456789abcdef"; + char str[68]; + uint16_t i; + + if (!len) + return; + + for (i = 0; i < len; i++) { + str[((i % 16) * 3) + 0] = hexdigits[buf[i] >> 4]; + str[((i % 16) * 3) + 1] = hexdigits[buf[i] & 0xf]; + str[((i % 16) * 3) + 2] = ' '; + str[(i % 16) + 49] = isprint(buf[i]) ? buf[i] : '.'; + + if ((i + 1) % 16 == 0) { + str[47] = ' '; + str[48] = ' '; + str[65] = '\0'; + print_text("%s", str); + str[0] = ' '; + } + } + + if (i % 16 > 0) { + uint16_t j; + for (j = (i % 16); j < 16; j++) { + str[(j * 3) + 0] = ' '; + str[(j * 3) + 1] = ' '; + str[(j * 3) + 2] = ' '; + str[j + 49] = ' '; + } + str[47] = ' '; + str[48] = ' '; + str[65] = '\0'; + print_text("%s", str); + } +} + enum attr_type { ATTR_UNSPEC, ATTR_FLAG, @@ -533,6 +579,7 @@ static void print_attributes(int indent, const void *buf, uint32_t len) case ATTR_UNSPEC: printf("%*c%s: len %u\n", indent, ' ', str, NLA_PAYLOAD(nla)); + print_hexdump(NLA_DATA(nla), NLA_PAYLOAD(nla)); break; case ATTR_FLAG: printf("%*c%s: true\n", indent, ' ', str); @@ -574,12 +621,14 @@ static void print_attributes(int indent, const void *buf, uint32_t len) case ATTR_ADDRESS: printf("%*c%s: len %u\n", indent, ' ', str, NLA_PAYLOAD(nla)); + print_hexdump(NLA_DATA(nla), NLA_PAYLOAD(nla)); if (NLA_PAYLOAD(nla) != 6) printf("malformed packet\n"); break; case ATTR_BINARY: printf("%*c%s: len %u\n", indent, ' ', str, NLA_PAYLOAD(nla)); + print_hexdump(NLA_DATA(nla), NLA_PAYLOAD(nla)); break; }