From 31118c770f1bc822226b8d3f70bad1904552745c Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Mon, 19 Apr 2010 18:14:45 +0200 Subject: add mnl_nlmsg_fprintf() function for debugging purposes This function is still quite preliminary, comments welcome! Signed-off-by: Pablo Neira Ayuso --- examples/rtnl-link-set.c | 2 +- include/libmnl/libmnl.h | 3 ++- src/nlmsg.c | 28 +++++++++++++++++----------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/examples/rtnl-link-set.c b/examples/rtnl-link-set.c index 0a63527..9be6635 100644 --- a/examples/rtnl-link-set.c +++ b/examples/rtnl-link-set.c @@ -52,7 +52,7 @@ int main(int argc, char *argv[]) } portid = mnl_socket_get_portid(nl); - mnl_nlmsg_print(nlh); + mnl_nlmsg_fprintf(stdout, nlh); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { perror("mnl_socket_send"); diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h index 259ddc2..cec6304 100644 --- a/include/libmnl/libmnl.h +++ b/include/libmnl/libmnl.h @@ -1,6 +1,7 @@ #ifndef _LIBMNL_H_ #define _LIBMNL_H_ +#include #include #include /* for sa_family_t */ #include @@ -57,7 +58,7 @@ extern void *mnl_nlmsg_get_payload_offset(const struct nlmsghdr *nlh, int offset extern void *mnl_nlmsg_get_payload_tail(const struct nlmsghdr *nlh); /* Netlink dump message */ -extern void mnl_nlmsg_print(const struct nlmsghdr *nlh); +extern void mnl_nlmsg_fprintf(FILE *fd, const struct nlmsghdr *nlh); /* * generic netlink attributes API diff --git a/src/nlmsg.c b/src/nlmsg.c index de41be6..54c11c0 100644 --- a/src/nlmsg.c +++ b/src/nlmsg.c @@ -220,27 +220,33 @@ int mnl_nlmsg_portid_ok(const struct nlmsghdr *nlh, unsigned int portid) return nlh->nlmsg_pid ? nlh->nlmsg_pid == portid : 1; } -/* XXX: rework this, please */ -void mnl_nlmsg_print(const struct nlmsghdr *nlh) +/** + * mnl_nlmsg_fprintf - print netlink message to file + * @nlh: pointer to netlink message that we want to print + * + * This function prints the netlink header to a file. This function may be + * useful for debugging purposes. + */ +void mnl_nlmsg_fprintf(FILE *fd, const struct nlmsghdr *nlh) { - int i; + size_t i; - printf("========= netlink header ==========\n"); - printf("length(32 bits)=%.08u\n", nlh->nlmsg_len); - printf("type(16 bits)=%.04u flags(16 bits)=%.04x\n", + fprintf(fd, "========= netlink header ==========\n"); + fprintf(fd, "length(32 bits)=%.08u\n", nlh->nlmsg_len); + fprintf(fd, "type(16 bits)=%.04u flags(16 bits)=%.04x\n", nlh->nlmsg_type, nlh->nlmsg_flags); - printf("sequence number(32 bits)=%.08x\n", nlh->nlmsg_seq); - printf("port ID(32 bits)=%.08u\n", nlh->nlmsg_pid); - printf("===================================\n"); + fprintf(fd, "sequence number(32 bits)=%.08x\n", nlh->nlmsg_seq); + fprintf(fd, "port ID(32 bits)=%.08u\n", nlh->nlmsg_pid); + fprintf(fd, "===================================\n"); for (i=sizeof(struct nlmsghdr); inlmsg_len; i+=4) { char *b = (char *) nlh; - printf("(%.3d) %.2x %.2x %.2x %.2x | ", i, + fprintf(fd, "(%.3d) %.2x %.2x %.2x %.2x | ", i, 0xff & b[i], 0xff & b[i+1], 0xff & b[i+2], 0xff & b[i+3]); - printf("%c %c %c %c\n", + fprintf(fd, "%c %c %c %c\n", isalnum(b[i]) ? b[i] : 0, isalnum(b[i+1]) ? b[i+1] : 0, isalnum(b[i+2]) ? b[i+2] : 0, -- cgit v1.2.3