]> granicus.if.org Git - libnl/commitdiff
msg: Pretty print error message header even if incomplete
authorThomas Graf <tgraf@suug.ch>
Wed, 13 Mar 2013 16:44:47 +0000 (17:44 +0100)
committerThomas Graf <tgraf@suug.ch>
Thu, 14 Mar 2013 11:46:14 +0000 (12:46 +0100)
Signed-off-by: Thomas Graf <tgraf@suug.ch>
lib/msg.c

index 043efe904c3d092e346634e6ff16950ff0a0c435..b07de630733927c89919fdf9230cdc219e718ccc 100644 (file)
--- a/lib/msg.c
+++ b/lib/msg.c
@@ -863,6 +863,27 @@ static void dump_attrs(FILE *ofd, struct nlattr *attrs, int attrlen,
        }
 }
 
+static void dump_error_msg(struct nl_msg *msg, FILE *ofd)
+{
+       struct nlmsghdr *hdr = nlmsg_hdr(msg);
+       struct nlmsgerr *err = nlmsg_data(hdr);
+
+       fprintf(ofd, "  [ERRORMSG] %zu octets\n", sizeof(*err));
+
+       if (nlmsg_len(hdr) >= sizeof(*err)) {
+               char buf[256];
+               struct nl_msg *errmsg;
+
+               fprintf(ofd, "    .error = %d \"%s\"\n", err->error,
+                       strerror_r(-err->error, buf, sizeof(buf)));
+               fprintf(ofd, "  [ORIGINAL MESSAGE] %zu octets\n", sizeof(*hdr));
+
+               errmsg = nlmsg_inherit(&err->msg);
+               print_hdr(ofd, errmsg);
+               nlmsg_free(errmsg);
+       }
+}
+
 /**
  * Dump message in human readable format to file descriptor
  * @arg msg            Message to print
@@ -879,21 +900,9 @@ void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
        fprintf(ofd, "  [HEADER] %zu octets\n", sizeof(struct nlmsghdr));
        print_hdr(ofd, msg);
 
-       if (hdr->nlmsg_type == NLMSG_ERROR &&
-           hdr->nlmsg_len >= nlmsg_msg_size(sizeof(struct nlmsgerr))) {
-               struct nl_msg *errmsg;
-               struct nlmsgerr *err = nlmsg_data(hdr);
-               char buf[256];
-
-               fprintf(ofd, "  [ERRORMSG] %zu octets\n", sizeof(*err));
-               fprintf(ofd, "    .error = %d \"%s\"\n", err->error,
-                       strerror_r(-err->error, buf, sizeof(buf)));
-               fprintf(ofd, "  [ORIGINAL MESSAGE] %zu octets\n", sizeof(*hdr));
-
-               errmsg = nlmsg_inherit(&err->msg);
-               print_hdr(ofd, errmsg);
-               nlmsg_free(errmsg);
-       } else if (nlmsg_len(hdr) > 0) {
+       if (hdr->nlmsg_type == NLMSG_ERROR)
+               dump_error_msg(msg, ofd);
+       else if (nlmsg_len(hdr) > 0) {
                struct nl_cache_ops *ops;
                int payloadlen = nlmsg_len(hdr);
                int attrlen = 0;