Modified do_get_nl_link to potentially process multiple netlink dump replies.

Prior to this commit, do_get_nl_link assumed there would only be a single
reply for a dump request. On my system with multiple can interfaces, this
would cause only half of them to be listed in the dump reply. This change
checks for NLMSG_DONE dump terminator and stops receiving messages once
received.

Signed-off-by: Andrew Beard <abeard@ovro.caltech.edu>
[mkl: fix indention, move variable init out of loop]
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
pull/106/head^2
Andrew Beard 2012-08-03 11:10:27 -07:00 committed by Marc Kleine-Budde
parent 5b990a4771
commit 5ba2310d85
1 changed files with 123 additions and 118 deletions

View File

@ -323,6 +323,7 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
char nlbuf[1024 * 8];
int ret = -1;
int done = 0;
struct iovec iov = {
.iov_base = (void *)nlbuf,
@ -349,16 +350,13 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
return ret;
}
if ((msglen = recvmsg(fd, &msg, 0)) <= 0) {
perror("Receive error");
return ret;
}
while (!done && (msglen = recvmsg(fd, &msg, 0)) > 0) {
size_t u_msglen = (size_t) msglen;
/* Check to see if the buffers in msg get truncated */
if (msg.msg_namelen != sizeof(peer) ||
(msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
fprintf(stderr, "Uhoh... truncated message.\n");
return ret;
return -1;
}
for (nl_msg = (struct nlmsghdr *)nlbuf;
@ -366,6 +364,11 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
nl_msg = NLMSG_NEXT(nl_msg, u_msglen)) {
int type = nl_msg->nlmsg_type;
int len;
if (type == NLMSG_DONE) {
done++;
continue;
}
if (type != RTM_NEWLINK)
continue;
@ -484,6 +487,8 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
fprintf(stderr, "unknown acquire mode\n");
}
}
}
return ret;
}