removed unused variable
[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 #include <platform.h>
27
28 #include "gnunet-vpn-tun.h"
29 #include "gnunet_common.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet-vpn-helper-p.h"
32
33 #ifndef _LINUX_IN6_H
34 // This is in linux/include/net/ipv6.h.
35
36 #define MAX_SIZE (65535 - sizeof(struct GNUNET_MessageHeader))
37
38 struct in6_ifreq {
39     struct in6_addr ifr6_addr;
40     uint32_t ifr6_prefixlen;
41     unsigned int ifr6_ifindex;
42 };
43
44 #endif
45
46 int running = 1;
47
48 void term(int sig) {
49         fprintf(stderr, "Got SIGTERM...\n");
50         if (sig == SIGTERM)
51                 running = 0;
52 }
53
54 static void set_address6(char* dev, char* address, unsigned long prefix_len) { /* {{{ */
55         int fd = socket(AF_INET6, SOCK_DGRAM, 0);
56
57         struct ifreq ifr;
58         struct in6_ifreq ifr6;
59
60         struct sockaddr_in6 sa6;
61         memset(&sa6, 0, sizeof(struct sockaddr_in6));
62
63         sa6.sin6_family = AF_INET6;
64
65         /* FIXME */ inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr);
66
67         memcpy((char *) &ifr6.ifr6_addr, (char *) &sa6.sin6_addr, sizeof(struct in6_addr));
68
69         strncpy(ifr.ifr_name, dev, IFNAMSIZ);
70
71         if (ioctl(fd, SIOGIFINDEX, &ifr) < 0) {
72                 perror("SIOGIFINDEX");
73         }
74
75         ifr6.ifr6_ifindex = ifr.ifr_ifindex;
76         ifr6.ifr6_prefixlen = prefix_len;
77
78         if (ioctl(fd, SIOCSIFADDR, &ifr6) < 0) {
79                 perror("SIOCSIFADDR");
80         }
81
82         /* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
83         ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
84         /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
85         close(fd);
86 } /* }}} */
87
88 static void set_address4(char* dev, char* address, char* mask) { /* {{{ */
89         int fd=0;
90         struct sockaddr_in* addr;
91         struct ifreq ifr;
92
93         memset(&ifr, 0, sizeof(struct ifreq));
94         addr = (struct sockaddr_in *)&(ifr.ifr_addr);
95         memset(addr, 0, sizeof(struct sockaddr_in));
96         addr->sin_family = AF_INET;
97         addr->sin_addr.s_addr = inet_addr(address);
98
99         /* FIXME */ inet_pton(AF_INET, address, &addr->sin_addr.s_addr);
100
101         fd = socket(PF_INET, SOCK_DGRAM, 0);
102         if(fd < 0) {
103                 perror("socket()");
104                 return;
105         }
106
107         strncpy(ifr.ifr_name, dev, IFNAMSIZ);
108
109         if(ioctl(fd, SIOCSIFADDR, &ifr) != 0 ) {
110                 perror("SIOCSIFADDR");
111                 close(fd);
112                 return;
113         }
114
115         addr = (struct sockaddr_in*)&(ifr.ifr_netmask);
116         /* FIXME */ inet_pton(AF_INET, mask, &addr->sin_addr.s_addr);
117
118         if(ioctl(fd, SIOCSIFNETMASK, &ifr) != 0 ) {
119                 perror("SIOCSIFNETMASK");
120                 close(fd);
121                 return;
122         }
123
124         /* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
125         ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
126         /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
127         close(fd);
128 } /* }}} */
129
130 void setnonblocking(int fd) {/*{{{*/
131         int opts;
132
133         opts = fcntl(fd,F_GETFL);
134         if (opts < 0) {
135                         perror("fcntl(F_GETFL)");
136         }
137         opts = (opts | O_NONBLOCK);
138         if (fcntl(fd,F_SETFL,opts) < 0) {
139                         perror("fcntl(F_SETFL)");
140         }
141         return;
142 }/*}}}*/
143
144 int main(int argc, char** argv) {
145         unsigned char buf[MAX_SIZE];
146
147         char dev[IFNAMSIZ];
148         memset(dev, 0, IFNAMSIZ);
149
150         signal(SIGTERM, &term);
151
152         int fd_tun = init_tun(dev);
153
154         if (fd_tun < 0) {
155                 fprintf(stderr, "Could not initialize tun-interface: %s\n", strerror(errno));
156                 exit(1);
157         }
158
159         fprintf(stderr, "Initialized the interface %s as %d.\n", dev, fd_tun);
160
161         {
162         // TODO: get this out of argv
163         char address[] = "1234::1";
164         unsigned long prefix_len = 16;
165
166         set_address6(dev, address, prefix_len);
167         }
168
169         {
170         char address[] = "10.10.10.1";
171         char mask[] = "255.255.255.252";
172
173         set_address4(dev, address, mask);
174         }
175
176         uid_t uid = getuid ();
177         if (setresuid (uid, uid, uid) != 0 )
178                 fprintf (stderr, "Failed to setresuid: %s\n", strerror(errno));
179
180         setnonblocking(0);
181         setnonblocking(1);
182         setnonblocking(fd_tun);
183
184         fd_set fds_w;
185         fd_set fds_r;
186
187         int rea = 1;
188         int wri = 1;
189
190         int write_fd_possible = 0;
191         int write_stdout_possible = 0;
192 outer:
193         while((rea == 1 || wri == 1) && running == 1) {
194                 FD_ZERO(&fds_w);
195                 FD_ZERO(&fds_r);
196
197                 if (rea) {
198                         FD_SET(fd_tun, &fds_r);
199                         if (!write_stdout_possible)
200                                 FD_SET(1, &fds_w);
201                 }
202
203                 if (wri) {
204                         FD_SET(0, &fds_r);
205                         if (!write_fd_possible)
206                                 FD_SET(fd_tun, &fds_w);
207                 }
208
209                 int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0);
210
211                 if(r > 0) {
212                         if (FD_ISSET(fd_tun, &fds_w)) write_fd_possible = 1;
213                         if (FD_ISSET(1, &fds_w)) write_stdout_possible = 1;
214
215                         if (FD_ISSET(0, &fds_r) && write_fd_possible) {
216                                 write_fd_possible = 0;
217                                 struct suid_packet *pkt = (struct suid_packet*) buf;
218                                 r = read(0, buf, sizeof(struct GNUNET_MessageHeader));
219                                 if (r <= 0) {
220                                         fprintf(stderr, "read-error: %m\n");
221                                         shutdown(fd_tun, SHUT_WR);
222                                         shutdown(0, SHUT_RD);
223                                         wri=0;
224                                         goto outer;
225                                 }
226                                 while (r < ntohs(pkt->hdr.size)) {
227                                         int t = read(0, buf + r, ntohs(pkt->hdr.size) - r);
228                                         if (r < 0) {
229                                                 fprintf(stderr, "read-error: %m\n");
230                                                 shutdown(fd_tun, SHUT_WR);
231                                                 shutdown(0, SHUT_RD);
232                                                 wri=0;
233                                                 goto outer;
234                                         }
235                                         r += t;
236                                 }
237                                 r = 0;
238                                 while (r < ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader)) {
239                                         int t = write(fd_tun, pkt->data, ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader) - r);
240                                         if (t < 0) {
241                                                 fprintf(stderr, "write-error 3: %m\n");
242                                                 shutdown(fd_tun, SHUT_WR);
243                                                 shutdown(0, SHUT_RD);
244                                                 wri = 0;
245                                                 goto outer;
246                                         }
247                                         r += t;
248                                 }
249                         } else if (write_stdout_possible && FD_ISSET(fd_tun, &fds_r)) {
250                                 write_stdout_possible = 0;
251                                 r = read(fd_tun, buf, MAX_SIZE);
252                                 if (r <= 0) {
253                                         fprintf(stderr, "read-error: %m\n");
254                                         shutdown(fd_tun, SHUT_RD);
255                                         shutdown(1, SHUT_WR);
256                                         rea = 0;
257                                         goto outer;
258                                 }
259                                 struct GNUNET_MessageHeader hdr = { .size = htons(r + sizeof(struct GNUNET_MessageHeader)), .type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER) };
260                                 r = 0;
261                                 while(r < sizeof(struct GNUNET_MessageHeader)) {
262                                         int t = write(1, &hdr, sizeof(struct GNUNET_MessageHeader) - r);
263                                         if (t < 0) {
264                                                 fprintf(stderr, "write-error 2: %m\n");
265                                                 shutdown(fd_tun, SHUT_RD);
266                                                 shutdown(1, SHUT_WR);
267                                                 rea = 0;
268                                                 goto outer;
269                                         }
270                                         r += t;
271                                 }
272                                 while(r < ntohs(hdr.size)) {
273                                         int t = write(1, buf, ntohs(hdr.size) - r);
274                                         if (t < 0) {
275                                                 fprintf(stderr, "write-error 1: %s, written %d/%d\n", strerror(errno), r, ntohs(hdr.size));
276                                                 shutdown(fd_tun, SHUT_RD);
277                                                 shutdown(1, SHUT_WR);
278                                                 rea = 0;
279                                                 goto outer;
280                                         }
281                                         r += t;
282                                 }
283                         }
284                 }
285         }
286         fprintf(stderr, "Quitting!\n");
287
288         close(fd_tun);
289
290         return 0;
291 }