gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r12259 - gnunet/src/vpn


From: gnunet
Subject: [GNUnet-SVN] r12259 - gnunet/src/vpn
Date: Tue, 20 Jul 2010 07:45:23 +0200

Author: toelke
Date: 2010-07-20 07:45:23 +0200 (Tue, 20 Jul 2010)
New Revision: 12259

Modified:
   gnunet/src/vpn/gnunet-vpn-helper.c
Log:
Shuttle data back and forth

Modified: gnunet/src/vpn/gnunet-vpn-helper.c
===================================================================
--- gnunet/src/vpn/gnunet-vpn-helper.c  2010-07-20 05:45:21 UTC (rev 12258)
+++ gnunet/src/vpn/gnunet-vpn-helper.c  2010-07-20 05:45:23 UTC (rev 12259)
@@ -2,6 +2,8 @@
 #include <arpa/inet.h>
 #include <linux/if.h>
 
+#include <fcntl.h>
+
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
@@ -58,6 +60,34 @@
        /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
 } /* }}} */
 
+void setnonblocking(int fd) {
+       int opts;
+
+       opts = fcntl(fd,F_GETFL);
+       if (opts < 0) {
+                       perror("fcntl(F_GETFL)");
+       }
+       opts = (opts | O_NONBLOCK);
+       if (fcntl(fd,F_SETFL,opts) < 0) {
+                       perror("fcntl(F_SETFL)");
+       }
+       return;
+}
+
+static int copy (int in, int out) {
+       unsigned char buf[65600]; // 64k + 64;
+       int r = read(in, buf, 65600);
+       int w = 0;
+       if (r < 0) return r;
+       while (w < r) {
+               int t = write(out, buf + w, r - w);
+               if (t > 0) w += t;
+               if (t < 0) return t;
+       }
+       return 0;
+}
+
+
 int main(int argc, char** argv) {
        char dev[IFNAMSIZ];
        memset(dev, 0, IFNAMSIZ);
@@ -75,8 +105,32 @@
        if (setresuid (uid, uid, uid) != 0 )
                fprintf (stderr, "Failed to setresuid: %m\n");
 
-       // Wait
-       read(0, dev, 10);
+       setnonblocking(0);
+       setnonblocking(1);
+       setnonblocking(fd_tun);
 
+       fd_set fds_w;
+       fd_set fds_r;
+       for(;;) {
+               FD_ZERO(&fds_w);
+               FD_ZERO(&fds_r);
+
+               FD_SET(0, &fds_r);
+               FD_SET(fd_tun, &fds_r);
+
+               FD_SET(1, &fds_w);
+               FD_SET(fd_tun, &fds_w);
+
+               int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0);
+
+               if(r > 0) {
+                       if (FD_ISSET(0, &fds_r) && FD_ISSET(fd_tun, &fds_w)) {
+                               copy(0, fd_tun);
+                       } else if (FD_ISSET(1, &fds_w) && FD_ISSET(fd_tun, 
&fds_r)) {
+                               copy(fd_tun, 1);
+                       }
+               }
+       }
+
        return 0;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]