]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blobdiff - lib/libnetlink.c
libnetlink: change rtnl_send() to take void *
[lisovros/iproute2_canprio.git] / lib / libnetlink.c
index cfeb8941cc07843cf5eef2cd4c6b0a812bee3eb7..7c29985c541ad8d4d216644af42c398feda52fb5 100644 (file)
@@ -107,12 +107,12 @@ int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
        return send(rth->fd, (void*)&req, sizeof(req), 0);
 }
 
-int rtnl_send(struct rtnl_handle *rth, const char *buf, int len)
+int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
 {
        return send(rth->fd, buf, len, 0);
 }
 
-int rtnl_send_check(struct rtnl_handle *rth, const char *buf, int len)
+int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
 {
        struct nlmsghdr *h;
        int status;
@@ -148,7 +148,7 @@ int rtnl_send_check(struct rtnl_handle *rth, const char *buf, int len)
 int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
 {
        struct nlmsghdr nlh;
-       struct sockaddr_nl nladdr;
+       struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
        struct iovec iov[2] = {
                { .iov_base = &nlh, .iov_len = sizeof(nlh) },
                { .iov_base = req, .iov_len = len }
@@ -160,9 +160,6 @@ int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
                .msg_iovlen = 2,
        };
 
-       memset(&nladdr, 0, sizeof(nladdr));
-       nladdr.nl_family = AF_NETLINK;
-
        nlh.nlmsg_len = NLMSG_LENGTH(len);
        nlh.nlmsg_type = type;
        nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
@@ -189,6 +186,8 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth,
        while (1) {
                int status;
                const struct rtnl_dump_filter_arg *a;
+               int found_done = 0;
+               int msglen = 0;
 
                iov.iov_len = sizeof(buf);
                status = recvmsg(rth->fd, &msg, 0);
@@ -208,8 +207,9 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth,
 
                for (a = arg; a->filter; a++) {
                        struct nlmsghdr *h = (struct nlmsghdr*)buf;
+                       msglen = status;
 
-                       while (NLMSG_OK(h, status)) {
+                       while (NLMSG_OK(h, msglen)) {
                                int err;
 
                                if (nladdr.nl_pid != 0 ||
@@ -224,8 +224,10 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth,
                                        goto skip_it;
                                }
 
-                               if (h->nlmsg_type == NLMSG_DONE)
-                                       return 0;
+                               if (h->nlmsg_type == NLMSG_DONE) {
+                                       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))) {
@@ -242,15 +244,19 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth,
                                        return err;
 
 skip_it:
-                               h = NLMSG_NEXT(h, status);
+                               h = NLMSG_NEXT(h, msglen);
                        }
-               } while (0);
+               }
+
+               if (found_done)
+                       return 0;
+
                if (msg.msg_flags & MSG_TRUNC) {
                        fprintf(stderr, "Message truncated\n");
                        continue;
                }
-               if (status) {
-                       fprintf(stderr, "!!!Remnant of size %d\n", status);
+               if (msglen) {
+                       fprintf(stderr, "!!!Remnant of size %d\n", msglen);
                        exit(1);
                }
        }
@@ -484,7 +490,7 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
        nladdr.nl_groups = 0;
 
        while (1) {
-               int err, len, type;
+               int err, len;
                int l;
 
                status = fread(&buf, 1, sizeof(*h), rtnl);
@@ -499,7 +505,6 @@ int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
                        return 0;
 
                len = h->nlmsg_len;
-               type= h->nlmsg_type;
                l = len - sizeof(*h);
 
                if (l<0 || len>sizeof(buf)) {