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