From: Jiri Pirko Date: Mon, 28 May 2012 12:05:27 +0000 (+0200) Subject: use MSG_TRUNC flag to get recv message size at once X-Git-Tag: libnl3_2_10~8 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d733f8a0a65a817d16ebaa1a8f5ad1330682b489;p=libnl use MSG_TRUNC flag to get recv message size at once prevent multiple calls of recvmsg. Signed-off-by: Jiri Pirko --- diff --git a/lib/nl.c b/lib/nl.c index 64e1382..b572a1a 100644 --- a/lib/nl.c +++ b/lib/nl.c @@ -435,7 +435,7 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, memset(nla, 0, sizeof(*nla)); if (sk->s_flags & NL_MSG_PEEK) - flags |= MSG_PEEK; + flags |= MSG_PEEK | MSG_TRUNC; if (page_size == 0) page_size = getpagesize(); @@ -466,16 +466,17 @@ retry: } } - if (iov.iov_len < n || - msg.msg_flags & MSG_TRUNC) { + if (msg.msg_flags & MSG_CTRUNC) { + msg.msg_controllen *= 2; + msg.msg_control = realloc(msg.msg_control, msg.msg_controllen); + goto retry; + } else if (iov.iov_len < n || msg.msg_flags & MSG_TRUNC) { /* Provided buffer is not long enough, enlarge it + * to size of n (which should be total length of the message) * and try again. */ - iov.iov_len *= 2; + iov.iov_len = n; iov.iov_base = *buf = realloc(*buf, iov.iov_len); - goto retry; - } else if (msg.msg_flags & MSG_CTRUNC) { - msg.msg_controllen *= 2; - msg.msg_control = realloc(msg.msg_control, msg.msg_controllen); + flags = 0; goto retry; } else if (flags != 0) { /* Buffer is big enough, do the actual reading */