[patch libnl] use MSG_TRUNC flag to get recv message size at once

Jiri Pirko jpirko at redhat.com
Mon May 28 08:05:27 EDT 2012


prevent multiple calls of recvmsg.

Signed-off-by: Jiri Pirko <jpirko at redhat.com>
---
 lib/nl.c |   17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

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 */
-- 
1.7.10.2




More information about the libnl mailing list