chiark / gitweb /
Ooops. Stupid bugs.
[tripe] / tun-unet.c
index 57e1397d6061feab98c51b603f90188a5038b214..8d0225a366c776621a736f0583208ebf38548a72 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: tun-unet.c,v 1.2 2001/02/04 17:10:58 mdw Exp $
+ * $Id: tun-unet.c,v 1.6 2004/04/08 01:36:17 mdw Exp $
  *
  * Tunnel interface based on Linux Usernet
  *
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: tun-unet.c,v $
- * Revision 1.2  2001/02/04 17:10:58  mdw
- * Make file descriptors be nonblocking and close-on-exec.
- *
- * Revision 1.1  2001/02/03 20:26:37  mdw
- * Initial checkin.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "tripe.h"
 
 #include <sys/ioctl.h>
+#include <net/if.h>
 #include <unet.h>
 
 /*----- Main code ---------------------------------------------------------*/
 
+#if TUN_TYPE != TUN_UNET
+#  error "Tunnel type mismatch: fix the Makefile"
+#endif
+
 /* --- @t_read@ --- *
  *
  * Arguments:  @int fd@ = file descriptor to read
@@ -57,7 +51,7 @@
  * Use:                Reads data from the tunnel.
  */
 
-void t_read(int fd, unsigned mode, void *v)
+static void t_read(int fd, unsigned mode, void *v)
 {
   tunnel *t = v;
   ssize_t n;
@@ -104,12 +98,19 @@ void tun_init(void)
 int tun_create(tunnel *t, peer *p)
 {
   int fd;
+  int f;
 
   if ((fd = open("/dev/unet", O_RDWR)) < 0) {
     a_warn("open `/dev/unet' failed: %s", strerror(errno));
     return (-1);
   }
   fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
+  if ((f = ioctl(fd, UNIOCGIFFLAGS)) < 0 ||
+      ioctl(fd, UNIOCSIFFLAGS, f | IFF_POINTOPOINT)) {
+    a_warn("couldn't set point-to-point flag: %s", strerror(errno));
+    close(fd);
+    return (-1);
+  }
   t->p = p;
   sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
   sel_addfile(&t->f);