From: Stephen Hemminger Date: Tue, 13 Dec 2016 18:40:49 +0000 (-0800) Subject: libnetlink: break up dump function X-Git-Tag: v4.10.0~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=892a25e286fbe6d43e658d74d35d868d7a4fc3bc;p=thirdparty%2Fiproute2.git libnetlink: break up dump function Indentation is deep here. Signed-off-by: Stephen Hemminger --- diff --git a/lib/libnetlink.c b/lib/libnetlink.c index 9d7e89aeb..9303b6686 100644 --- a/lib/libnetlink.c +++ b/lib/libnetlink.c @@ -261,6 +261,48 @@ int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n) return sendmsg(rth->fd, &msg, 0); } +static int rtnl_dump_done(const struct rtnl_handle *rth, + struct nlmsghdr *h) +{ + int len = *(int *)NLMSG_DATA(h); + + if (rth->proto == NETLINK_SOCK_DIAG) { + if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) { + fprintf(stderr, "DONE truncated\n"); + return -1; + } + + + if (len < 0) { + errno = -len; + if (errno == ENOENT || errno == EOPNOTSUPP) + return -1; + perror("RTNETLINK answers"); + return len; + } + } + return 0; +} + +static void rtnl_dump_error(const struct rtnl_handle *rth, + struct nlmsghdr *h) +{ + + if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { + fprintf(stderr, "ERROR truncated\n"); + } else { + const struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); + + errno = -err->error; + if (rth->proto == NETLINK_SOCK_DIAG && + (errno == ENOENT || + errno == EOPNOTSUPP)) + return; + + perror("RTNETLINK answers"); + } +} + int rtnl_dump_filter_l(struct rtnl_handle *rth, const struct rtnl_dump_filter_arg *arg) { @@ -320,40 +362,16 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth, dump_intr = 1; if (h->nlmsg_type == NLMSG_DONE) { - if (rth->proto == NETLINK_SOCK_DIAG) { - if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) { - fprintf(stderr, "DONE truncated\n"); - return -1; - } else { - int len = *(int *)NLMSG_DATA(h); - if (len < 0) { - errno = -len; - if (errno == ENOENT || - errno == EOPNOTSUPP) - return -1; - perror("RTNETLINK answers"); - return len; - } - } - } + err = rtnl_dump_done(rth, h); + if (err < 0) + return -1; + found_done = 1; break; /* process next filter */ } + if (h->nlmsg_type == NLMSG_ERROR) { - struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); - - if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { - fprintf(stderr, - "ERROR truncated\n"); - } else { - errno = -err->error; - if (rth->proto == NETLINK_SOCK_DIAG && - (errno == ENOENT || - errno == EOPNOTSUPP)) - return -1; - - perror("RTNETLINK answers"); - } + rtnl_dump_error(rth, h); return -1; }