diff options
author | Philipp Tölke <toelke@in.tum.de> | 2010-07-20 05:45:23 +0000 |
---|---|---|
committer | Philipp Tölke <toelke@in.tum.de> | 2010-07-20 05:45:23 +0000 |
commit | aea5d092050795eb39acd379283774360d352f3b (patch) | |
tree | e73368927a6ba2b581fce82f4bb95c1c68a989d7 /src/vpn | |
parent | 6f26625f6a432f75ff339adf7b3330d5f32855ba (diff) | |
download | gnunet-aea5d092050795eb39acd379283774360d352f3b.tar.gz gnunet-aea5d092050795eb39acd379283774360d352f3b.zip |
Shuttle data back and forth
Diffstat (limited to 'src/vpn')
-rw-r--r-- | src/vpn/gnunet-vpn-helper.c | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/src/vpn/gnunet-vpn-helper.c b/src/vpn/gnunet-vpn-helper.c index 66af3ca51..a5a569b9a 100644 --- a/src/vpn/gnunet-vpn-helper.c +++ b/src/vpn/gnunet-vpn-helper.c | |||
@@ -2,6 +2,8 @@ | |||
2 | #include <arpa/inet.h> | 2 | #include <arpa/inet.h> |
3 | #include <linux/if.h> | 3 | #include <linux/if.h> |
4 | 4 | ||
5 | #include <fcntl.h> | ||
6 | |||
5 | #include <sys/types.h> | 7 | #include <sys/types.h> |
6 | #include <sys/socket.h> | 8 | #include <sys/socket.h> |
7 | #include <sys/ioctl.h> | 9 | #include <sys/ioctl.h> |
@@ -58,6 +60,34 @@ static void set_address(char* dev, char* address, unsigned long prefix_len) { /* | |||
58 | /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr); | 60 | /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr); |
59 | } /* }}} */ | 61 | } /* }}} */ |
60 | 62 | ||
63 | void setnonblocking(int fd) { | ||
64 | int opts; | ||
65 | |||
66 | opts = fcntl(fd,F_GETFL); | ||
67 | if (opts < 0) { | ||
68 | perror("fcntl(F_GETFL)"); | ||
69 | } | ||
70 | opts = (opts | O_NONBLOCK); | ||
71 | if (fcntl(fd,F_SETFL,opts) < 0) { | ||
72 | perror("fcntl(F_SETFL)"); | ||
73 | } | ||
74 | return; | ||
75 | } | ||
76 | |||
77 | static int copy (int in, int out) { | ||
78 | unsigned char buf[65600]; // 64k + 64; | ||
79 | int r = read(in, buf, 65600); | ||
80 | int w = 0; | ||
81 | if (r < 0) return r; | ||
82 | while (w < r) { | ||
83 | int t = write(out, buf + w, r - w); | ||
84 | if (t > 0) w += t; | ||
85 | if (t < 0) return t; | ||
86 | } | ||
87 | return 0; | ||
88 | } | ||
89 | |||
90 | |||
61 | int main(int argc, char** argv) { | 91 | int main(int argc, char** argv) { |
62 | char dev[IFNAMSIZ]; | 92 | char dev[IFNAMSIZ]; |
63 | memset(dev, 0, IFNAMSIZ); | 93 | memset(dev, 0, IFNAMSIZ); |
@@ -75,8 +105,32 @@ int main(int argc, char** argv) { | |||
75 | if (setresuid (uid, uid, uid) != 0 ) | 105 | if (setresuid (uid, uid, uid) != 0 ) |
76 | fprintf (stderr, "Failed to setresuid: %m\n"); | 106 | fprintf (stderr, "Failed to setresuid: %m\n"); |
77 | 107 | ||
78 | // Wait | 108 | setnonblocking(0); |
79 | read(0, dev, 10); | 109 | setnonblocking(1); |
110 | setnonblocking(fd_tun); | ||
111 | |||
112 | fd_set fds_w; | ||
113 | fd_set fds_r; | ||
114 | for(;;) { | ||
115 | FD_ZERO(&fds_w); | ||
116 | FD_ZERO(&fds_r); | ||
117 | |||
118 | FD_SET(0, &fds_r); | ||
119 | FD_SET(fd_tun, &fds_r); | ||
120 | |||
121 | FD_SET(1, &fds_w); | ||
122 | FD_SET(fd_tun, &fds_w); | ||
123 | |||
124 | int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0); | ||
125 | |||
126 | if(r > 0) { | ||
127 | if (FD_ISSET(0, &fds_r) && FD_ISSET(fd_tun, &fds_w)) { | ||
128 | copy(0, fd_tun); | ||
129 | } else if (FD_ISSET(1, &fds_w) && FD_ISSET(fd_tun, &fds_r)) { | ||
130 | copy(fd_tun, 1); | ||
131 | } | ||
132 | } | ||
133 | } | ||
80 | 134 | ||
81 | return 0; | 135 | return 0; |
82 | } | 136 | } |