From cda5a477c8138226d184a176256d559971b4f7ed Mon Sep 17 00:00:00 2001
From: Guus Sliepen <guus@tinc-vpn.org>
Date: Thu, 10 Dec 2015 16:45:05 +0100
Subject: [PATCH] Use static buffers for recvmmsg(), initialize them only as
 needed.

As suggested by Michael Tokarev.
---
 src/net_packet.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/net_packet.c b/src/net_packet.c
index e7161445..f6c19b7a 100644
--- a/src/net_packet.c
+++ b/src/net_packet.c
@@ -1504,12 +1504,13 @@ void handle_incoming_vpn_data(void *data, int flags) {
 
 #ifdef HAVE_RECVMMSG
 #define MAX_MSG 64
-	vpn_packet_t pkt[MAX_MSG];
-	sockaddr_t addr[MAX_MSG];
-	struct mmsghdr msg[MAX_MSG];
-	struct iovec iov[MAX_MSG];
+	static int num = MAX_MSG;
+	static vpn_packet_t pkt[MAX_MSG];
+	static sockaddr_t addr[MAX_MSG];
+	static struct mmsghdr msg[MAX_MSG];
+	static struct iovec iov[MAX_MSG];
 
-	for(int i = 0; i < MAX_MSG; i++) {
+	for(int i = 0; i < num; i++) {
 		pkt[i].offset = 0;
 
 		iov[i] = (struct iovec){
@@ -1525,7 +1526,7 @@ void handle_incoming_vpn_data(void *data, int flags) {
 		};
 	}
 
-	int num = recvmmsg(ls->udp.fd, msg, MAX_MSG, MSG_DONTWAIT, NULL);
+	num = recvmmsg(ls->udp.fd, msg, MAX_MSG, MSG_DONTWAIT, NULL);
 
 	if(num < 0) {
 		if(!sockwouldblock(sockerrno))
-- 
2.39.5