[PATCH 4/4] Report errors when a packet cannot be delivered to the tun device. Wrong-size writes count as errors.

Richard Kettlewell rjk at terraraq.org.uk
Wed Jul 6 21:19:27 BST 2011


From: Richard Kettlewell <rjk at greenend.org.uk>

The error reporting is rate limited to ensure that the logs are not
flooded.

Signed-off-by: Richard Kettlewell <rjk at greenend.org.uk>
---
 tun.c |   26 +++++++++++++++++++++++---
 1 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/tun.c b/tun.c
index 8728676..d913f06 100644
--- a/tun.c
+++ b/tun.c
@@ -3,8 +3,10 @@
 #include "netlink.h"
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <time.h>
 #include <sys/ioctl.h>
 #include <sys/utsname.h>
 #include <sys/socket.h>
@@ -134,11 +136,29 @@ static void tun_afterpoll(void *sst, struct pollfd *fds, int nfds)
 static void tun_deliver_to_kernel(void *sst, struct buffer_if *buf)
 {
     struct tun *st=sst;
+    ssize_t rc;
 
     BUF_ASSERT_USED(buf);
-    /* No error checking, because we'd just throw the packet away
-       anyway if it didn't work. */
-    write(st->fd,buf->start,buf->size);
+    
+    /* Log errors, so we can tell what's going on, but only once a
+       minute, so we don't flood the logs.  Short writes count as
+       errors. */
+    rc = write(st->fd,buf->start,buf->size);
+    if(rc != buf->size) {
+	static time_t last_report;
+	time_t now;
+	time(&now);
+	if(now >= last_report + 60) {
+	    if(rc < 0)
+		Message(M_WARNING,
+			"failed to deliver packet to tun device: %s\n",
+			strerror(errno));
+	    else
+		Message(M_WARNING,
+			"truncated packet delivered to tun device\n");
+	    last_report = now;
+	}
+    }
     BUF_FREE(buf);
 }
 
-- 
1.6.4.2




More information about the sgo-software-discuss mailing list