[pisa-src] r2265 - trunk/libpisa/sysdep_tun.c

  • From: Mircea Gherzan <mircea.gherzan@xxxxxxxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Wed, 14 Apr 2010 16:38:45 +0200

Author: gherzan
Date: Wed Apr 14 16:38:45 2010
New Revision: 2265

Log:
libpisa: add tunnel-opening code for Mac OS X

Modified:
   trunk/libpisa/sysdep_tun.c

Modified: trunk/libpisa/sysdep_tun.c
==============================================================================
--- trunk/libpisa/sysdep_tun.c  Tue Apr 13 10:32:35 2010        (r2264)
+++ trunk/libpisa/sysdep_tun.c  Wed Apr 14 16:38:45 2010        (r2265)
@@ -16,6 +16,7 @@
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdio.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/types.h>
@@ -24,8 +25,16 @@
 #include "util.h"
 
 #ifdef __linux__
+# include <linux/if_tun.h>
+#elif defined(__APPLE__)
+/* TODO: add the OS X header for tunneling */
+#else /* FreeBSD */
+# include <net/tun.h>
+#endif
 
-#include <linux/if_tun.h>
+
+/* systems like Linux and FreeBSD use a common tunneling interface */
+#ifdef IFF_TUN
 
 /* see the header */
 int pisa_tunnel_open_tundev(char *devicename, size_t len)
@@ -50,4 +59,23 @@
     return fd_tun;
 }
 
-#endif /* __linux__ */
+#else
+
+int pisa_tunnel_open_tundev(char *devicename, size_t len)
+{
+    int i, fd;
+    char name[32];
+
+    for (i = 0; i < 255; i++) {
+        snprintf(name, sizeof(name), "/dev/tun%d", i);
+        if ((fd = open(name, O_RDWR)) > 0) {
+            snprintf(devicename, len, "tun%d", i);
+            return fd;
+        }
+    }
+
+    pisa_die("Could not open tunnel device.", EXIT_FAILURE);
+    return -1;
+}
+
+#endif /* IFF_TUN */

Other related posts: