corrected the test-case, the test-packet needs to use the GNUNET_MessageHeader
[oweals/gnunet.git] / src / vpn / gnunet-helper-vpn.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file vpn/gnunet-daemon-vpn.c
23  * @brief
24  * @author Philipp Tölke
25  */
26 #define _GNU_SOURCE
27 #include <arpa/inet.h>
28 #include <linux/if.h>
29
30 #include <fcntl.h>
31
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35
36 #include <string.h>
37
38 #include <signal.h>
39
40 #include <stdio.h>
41 #include <unistd.h>
42
43 #include "gnunet-vpn-tun.h"
44 #include "gnunet_common.h"
45 #include "gnunet_protocols.h"
46 #include "gnunet-vpn-helper-p.h"
47
48 #ifndef _LINUX_IN6_H
49 // This is in linux/include/net/ipv6.h.
50
51 #define MAX_SIZE (65535 - sizeof(struct GNUNET_MessageHeader))
52
53 struct in6_ifreq {
54     struct in6_addr ifr6_addr;
55     __u32 ifr6_prefixlen;
56     unsigned int ifr6_ifindex;
57 };
58
59 #endif
60
61 int running = 1;
62
63 void term(int sig) {
64         fprintf(stderr, "Got SIGTERM...\n");
65         if (sig == SIGTERM)
66                 running = 0;
67 }
68
69 static void set_address(char* dev, char* address, unsigned long prefix_len) { /* {{{ */
70         int fd = socket(AF_INET6, SOCK_DGRAM, 0);
71
72         struct ifreq ifr;
73         struct in6_ifreq ifr6;
74
75         struct sockaddr_in6 sa6;
76         memset(&sa6, 0, sizeof(struct sockaddr_in6));
77
78         sa6.sin6_family = AF_INET6;
79
80         /* FIXME */ inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr);
81
82         memcpy((char *) &ifr6.ifr6_addr, (char *) &sa6.sin6_addr, sizeof(struct in6_addr));
83
84         strncpy(ifr.ifr_name, dev, IFNAMSIZ);
85
86         if (ioctl(fd, SIOGIFINDEX, &ifr) < 0) {
87                 perror("SIOGIFINDEX");
88         }
89
90         ifr6.ifr6_ifindex = ifr.ifr_ifindex;
91         ifr6.ifr6_prefixlen = prefix_len;
92
93         if (ioctl(fd, SIOCSIFADDR, &ifr6) < 0) {
94                 perror("SIOCSIFADDR");
95         }
96
97         /* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
98         ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
99         /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
100 } /* }}} */
101
102 void setnonblocking(int fd) {/*{{{*/
103         int opts;
104
105         opts = fcntl(fd,F_GETFL);
106         if (opts < 0) {
107                         perror("fcntl(F_GETFL)");
108         }
109         opts = (opts | O_NONBLOCK);
110         if (fcntl(fd,F_SETFL,opts) < 0) {
111                         perror("fcntl(F_SETFL)");
112         }
113         return;
114 }/*}}}*/
115
116 int main(int argc, char** argv) {
117         unsigned char buf[MAX_SIZE];
118
119         char dev[IFNAMSIZ];
120         memset(dev, 0, IFNAMSIZ);
121
122         signal(SIGTERM, &term);
123
124         int fd_tun = init_tun(dev);
125
126         if (fd_tun < 0) {
127                 fprintf(stderr, "Could not initialize tun-interface: %m\n");
128                 exit(1);
129         }
130
131         fprintf(stderr, "Initialized the interface %s as %d.\n", dev, fd_tun);
132
133         // TODO: get this out of argv
134         char address[] = "1234::1";
135         unsigned long prefix_len = 16;
136
137         set_address(dev, address, prefix_len);
138
139         uid_t uid = getuid ();
140         if (setresuid (uid, uid, uid) != 0 )
141                 fprintf (stderr, "Failed to setresuid: %m\n");
142
143         setnonblocking(0);
144         setnonblocking(1);
145         setnonblocking(fd_tun);
146
147         fd_set fds_w;
148         fd_set fds_r;
149
150         int rea = 1;
151         int wri = 1;
152
153         int write_fd_possible = 0;
154         int write_stdout_possible = 0;
155 outer:
156         while((rea == 1 || wri == 1) && running == 1) {
157                 FD_ZERO(&fds_w);
158                 FD_ZERO(&fds_r);
159
160                 if (rea) {
161                         FD_SET(fd_tun, &fds_r);
162                         if (!write_stdout_possible)
163                                 FD_SET(1, &fds_w);
164                 }
165
166                 if (wri) {
167                         FD_SET(0, &fds_r);
168                         if (!write_fd_possible)
169                                 FD_SET(fd_tun, &fds_w);
170                 }
171
172                 int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0);
173
174                 if(r > 0) {
175                         if (FD_ISSET(fd_tun, &fds_w)) write_fd_possible = 1;
176                         if (FD_ISSET(1, &fds_w)) write_stdout_possible = 1;
177
178                         if (FD_ISSET(0, &fds_r) && write_fd_possible) {
179                                 write_fd_possible = 0;
180                                 struct suid_packet *pkt = (struct suid_packet*) buf;
181                                 r = read(0, buf, sizeof(struct GNUNET_MessageHeader));
182                                 if (r <= 0) {
183                                         fprintf(stderr, "read-error: %m\n");
184                                         shutdown(fd_tun, SHUT_WR);
185                                         shutdown(0, SHUT_RD);
186                                         wri=0;
187                                         goto outer;
188                                 }
189                                 while (r < ntohs(pkt->hdr.size)) {
190                                         int t = read(0, buf + r, ntohs(pkt->hdr.size) - r);
191                                         if (r < 0) {
192                                                 fprintf(stderr, "read-error: %m\n");
193                                                 shutdown(fd_tun, SHUT_WR);
194                                                 shutdown(0, SHUT_RD);
195                                                 wri=0;
196                                                 goto outer;
197                                         }
198                                         r += t;
199                                 }
200                                 r = 0;
201                                 while (r < ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader)) {
202                                         int t = write(fd_tun, pkt->data, ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader) - r);
203                                         if (t < 0) {
204                                                 fprintf(stderr, "write-error 3: %m\n");
205                                                 shutdown(fd_tun, SHUT_WR);
206                                                 shutdown(0, SHUT_RD);
207                                                 wri = 0;
208                                                 goto outer;
209                                         }
210                                         r += t;
211                                 }
212                         } else if (write_stdout_possible && FD_ISSET(fd_tun, &fds_r)) {
213                                 write_stdout_possible = 0;
214                                 r = read(fd_tun, buf, MAX_SIZE);
215                                 if (r <= 0) {
216                                         fprintf(stderr, "read-error: %m\n");
217                                         shutdown(fd_tun, SHUT_RD);
218                                         shutdown(1, SHUT_WR);
219                                         rea = 0;
220                                         goto outer;
221                                 }
222                                 struct GNUNET_MessageHeader hdr = { .size = htons(r + sizeof(struct GNUNET_MessageHeader)), .type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER) };
223                                 r = 0;
224                                 while(r < sizeof(struct GNUNET_MessageHeader)) {
225                                         int t = write(1, &hdr, sizeof(struct GNUNET_MessageHeader) - r);
226                                         if (t < 0) {
227                                                 fprintf(stderr, "write-error 2: %m\n");
228                                                 shutdown(fd_tun, SHUT_RD);
229                                                 shutdown(1, SHUT_WR);
230                                                 rea = 0;
231                                                 goto outer;
232                                         }
233                                         r += t;
234                                 }
235                                 while(r < ntohs(hdr.size)) {
236                                         int t = write(1, buf, ntohs(hdr.size) - r);
237                                         if (t < 0) {
238                                                 fprintf(stderr, "write-error 1: %m, written %d/%d\n", r, ntohs(hdr.size));
239                                                 shutdown(fd_tun, SHUT_RD);
240                                                 shutdown(1, SHUT_WR);
241                                                 rea = 0;
242                                                 goto outer;
243                                         }
244                                         r += t;
245                                 }
246                         }
247                 }
248         }
249         fprintf(stderr, "Quitting!\n");
250
251         return 0;
252 }