restart the helper when something goes wrong
[oweals/gnunet.git] / src / vpn / gnunet-vpn-helper.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-helper-p.h"
44 #include "gnunet-vpn-tun.h"
45
46 #ifndef _LINUX_IN6_H
47 // This is in linux/include/net/ipv6.h.
48
49 struct in6_ifreq {
50     struct in6_addr ifr6_addr;
51     __u32 ifr6_prefixlen;
52     unsigned int ifr6_ifindex;
53 };
54
55 #endif
56
57 int running = 1;
58
59 void term(int sig) {
60         fprintf(stderr, "Got SIGTERM...\n");
61         if (sig == SIGTERM)
62                 running = 0;
63 }
64
65 static void set_address(char* dev, char* address, unsigned long prefix_len) { /* {{{ */
66         int fd = socket(AF_INET6, SOCK_DGRAM, 0);
67
68         struct ifreq ifr;
69         struct in6_ifreq ifr6;
70
71         struct sockaddr_in6 sa6;
72         memset(&sa6, 0, sizeof(struct sockaddr_in6));
73
74         sa6.sin6_family = AF_INET6;
75
76         /* FIXME */ inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr);
77
78         memcpy((char *) &ifr6.ifr6_addr, (char *) &sa6.sin6_addr, sizeof(struct in6_addr));
79
80         strncpy(ifr.ifr_name, dev, IFNAMSIZ);
81
82         if (ioctl(fd, SIOGIFINDEX, &ifr) < 0) {
83                 perror("SIOGIFINDEX");
84         }
85
86         ifr6.ifr6_ifindex = ifr.ifr_ifindex;
87         ifr6.ifr6_prefixlen = prefix_len;
88
89         if (ioctl(fd, SIOCSIFADDR, &ifr6) < 0) {
90                 perror("SIOCSIFADDR");
91         }
92
93         /* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
94         ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
95         /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
96 } /* }}} */
97
98 void setnonblocking(int fd) {/*{{{*/
99         int opts;
100
101         opts = fcntl(fd,F_GETFL);
102         if (opts < 0) {
103                         perror("fcntl(F_GETFL)");
104         }
105         opts = (opts | O_NONBLOCK);
106         if (fcntl(fd,F_SETFL,opts) < 0) {
107                         perror("fcntl(F_SETFL)");
108         }
109         return;
110 }/*}}}*/
111
112 int main(int argc, char** argv) {
113         unsigned char buf[65600]; // 64k + 64;
114
115         char dev[IFNAMSIZ];
116         memset(dev, 0, IFNAMSIZ);
117
118         signal(SIGTERM, &term);
119
120         int fd_tun = init_tun(dev);
121         fprintf(stderr, "Initialized the interface %s as %d.\n", dev, fd_tun);
122
123         // TODO: get this out of argv
124         char address[] = "1234::1";
125         unsigned long prefix_len = 16;
126
127         set_address(dev, address, prefix_len);
128
129         uid_t uid = getuid ();
130         if (setresuid (uid, uid, uid) != 0 )
131                 fprintf (stderr, "Failed to setresuid: %m\n");
132
133         setnonblocking(0);
134         setnonblocking(1);
135         setnonblocking(fd_tun);
136
137         fd_set fds_w;
138         fd_set fds_r;
139
140         int rea = 1;
141         int wri = 1;
142
143         int write_fd_possible = 0;
144         int write_stdout_possible = 0;
145 outer:
146         while((rea == 1 || wri == 1) && running == 1) {
147                 FD_ZERO(&fds_w);
148                 FD_ZERO(&fds_r);
149
150                 if (rea) {
151                         FD_SET(fd_tun, &fds_r);
152                         if (!write_stdout_possible)
153                                 FD_SET(1, &fds_w);
154                 }
155
156                 if (wri) {
157                         FD_SET(0, &fds_r);
158                         if (!write_fd_possible)
159                                 FD_SET(fd_tun, &fds_w);
160                 }
161
162                 int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0);
163
164                 if(r > 0) {
165                         if (FD_ISSET(fd_tun, &fds_w)) write_fd_possible = 1;
166                         if (FD_ISSET(1, &fds_w)) write_stdout_possible = 1;
167
168                         if (FD_ISSET(0, &fds_r) && write_fd_possible) {
169                                 write_fd_possible = 0;
170                                 struct suid_packet *pkt = (struct suid_packet*) buf;
171                                 r = read(0, buf, sizeof(struct suid_packet_header));
172                                 if (r <= 0) {
173                                         fprintf(stderr, "read-error: %m\n");
174                                         shutdown(fd_tun, SHUT_WR);
175                                         shutdown(0, SHUT_RD);
176                                         wri=0;
177                                         goto outer;
178                                 }
179                                 while (r < ntohl(pkt->hdr.size)) {
180                                         int t = read(0, buf + r, ntohl(pkt->hdr.size) - r);
181                                         if (r < 0) {
182                                                 fprintf(stderr, "read-error: %m\n");
183                                                 shutdown(fd_tun, SHUT_WR);
184                                                 shutdown(0, SHUT_RD);
185                                                 wri=0;
186                                                 goto outer;
187                                         }
188                                         r += t;
189                                 }
190                                 r = 0;
191                                 while (r < ntohl(pkt->hdr.size) - sizeof(struct suid_packet_header)) {
192                                         int t = write(fd_tun, pkt->data, ntohl(pkt->hdr.size) - sizeof(struct suid_packet_header) - r);
193                                         if (t < 0) {
194                                                 fprintf(stderr, "write-error 3: %m\n");
195                                                 shutdown(fd_tun, SHUT_WR);
196                                                 shutdown(0, SHUT_RD);
197                                                 wri = 0;
198                                                 goto outer;
199                                         }
200                                         r += t;
201                                 }
202                         } else if (write_stdout_possible && FD_ISSET(fd_tun, &fds_r)) {
203                                 write_stdout_possible = 0;
204                                 r = read(fd_tun, buf, 65600);
205                                 if (r <= 0) {
206                                         fprintf(stderr, "read-error: %m\n");
207                                         shutdown(fd_tun, SHUT_RD);
208                                         shutdown(1, SHUT_WR);
209                                         rea = 0;
210                                         goto outer;
211                                 }
212                                 struct suid_packet_header hdr = { .size = htonl(r + sizeof(struct suid_packet_header))};
213                                 r = 0;
214                                 while(r < sizeof(struct suid_packet_header)) {
215                                         int t = write(1, &hdr, sizeof(struct suid_packet_header) - r);
216                                         if (t < 0) {
217                                                 fprintf(stderr, "write-error 2: %m\n");
218                                                 shutdown(fd_tun, SHUT_RD);
219                                                 shutdown(1, SHUT_WR);
220                                                 rea = 0;
221                                                 goto outer;
222                                         }
223                                         r += t;
224                                 }
225                                 while(r < ntohl(hdr.size)) {
226                                         int t = write(1, buf, ntohl(hdr.size) - r);
227                                         if (t < 0) {
228                                                 fprintf(stderr, "write-error 1: %m, written %d/%d\n", r, ntohl(hdr.size));
229                                                 shutdown(fd_tun, SHUT_RD);
230                                                 shutdown(1, SHUT_WR);
231                                                 rea = 0;
232                                                 goto outer;
233                                         }
234                                         r += t;
235                                 }
236                         }
237                 }
238         }
239         fprintf(stderr, "Quitting!\n");
240
241         return 0;
242 }