2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
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.
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.
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.
22 * @file vpn/gnunet-daemon-vpn.c
24 * @author Philipp Tölke
27 #include <arpa/inet.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
43 #include "gnunet-vpn-tun.h"
44 #include "gnunet_common.h"
45 #include "gnunet_protocols.h"
46 #include "gnunet-vpn-helper-p.h"
49 // This is in linux/include/net/ipv6.h.
51 #define MAX_SIZE (65535 - sizeof(struct GNUNET_MessageHeader))
54 struct in6_addr ifr6_addr;
56 unsigned int ifr6_ifindex;
64 fprintf(stderr, "Got SIGTERM...\n");
69 static void set_address(char* dev, char* address, unsigned long prefix_len) { /* {{{ */
70 int fd = socket(AF_INET6, SOCK_DGRAM, 0);
73 struct in6_ifreq ifr6;
75 struct sockaddr_in6 sa6;
76 memset(&sa6, 0, sizeof(struct sockaddr_in6));
78 sa6.sin6_family = AF_INET6;
80 /* FIXME */ inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr);
82 memcpy((char *) &ifr6.ifr6_addr, (char *) &sa6.sin6_addr, sizeof(struct in6_addr));
84 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
86 if (ioctl(fd, SIOGIFINDEX, &ifr) < 0) {
87 perror("SIOGIFINDEX");
90 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
91 ifr6.ifr6_prefixlen = prefix_len;
93 if (ioctl(fd, SIOCSIFADDR, &ifr6) < 0) {
94 perror("SIOCSIFADDR");
97 /* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
98 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
99 /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
102 void setnonblocking(int fd) {/*{{{*/
105 opts = fcntl(fd,F_GETFL);
107 perror("fcntl(F_GETFL)");
109 opts = (opts | O_NONBLOCK);
110 if (fcntl(fd,F_SETFL,opts) < 0) {
111 perror("fcntl(F_SETFL)");
116 int main(int argc, char** argv) {
117 unsigned char buf[MAX_SIZE];
120 memset(dev, 0, IFNAMSIZ);
122 signal(SIGTERM, &term);
124 int fd_tun = init_tun(dev);
125 fprintf(stderr, "Initialized the interface %s as %d.\n", dev, fd_tun);
127 // TODO: get this out of argv
128 char address[] = "1234::1";
129 unsigned long prefix_len = 16;
131 set_address(dev, address, prefix_len);
133 uid_t uid = getuid ();
134 if (setresuid (uid, uid, uid) != 0 )
135 fprintf (stderr, "Failed to setresuid: %m\n");
139 setnonblocking(fd_tun);
147 int write_fd_possible = 0;
148 int write_stdout_possible = 0;
150 while((rea == 1 || wri == 1) && running == 1) {
155 FD_SET(fd_tun, &fds_r);
156 if (!write_stdout_possible)
162 if (!write_fd_possible)
163 FD_SET(fd_tun, &fds_w);
166 int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0);
169 if (FD_ISSET(fd_tun, &fds_w)) write_fd_possible = 1;
170 if (FD_ISSET(1, &fds_w)) write_stdout_possible = 1;
172 if (FD_ISSET(0, &fds_r) && write_fd_possible) {
173 write_fd_possible = 0;
174 struct suid_packet *pkt = (struct suid_packet*) buf;
175 r = read(0, buf, sizeof(struct GNUNET_MessageHeader));
177 fprintf(stderr, "read-error: %m\n");
178 shutdown(fd_tun, SHUT_WR);
179 shutdown(0, SHUT_RD);
183 while (r < ntohs(pkt->hdr.size)) {
184 int t = read(0, buf + r, ntohs(pkt->hdr.size) - r);
186 fprintf(stderr, "read-error: %m\n");
187 shutdown(fd_tun, SHUT_WR);
188 shutdown(0, SHUT_RD);
195 while (r < ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader)) {
196 int t = write(fd_tun, pkt->data, ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader) - r);
198 fprintf(stderr, "write-error 3: %m\n");
199 shutdown(fd_tun, SHUT_WR);
200 shutdown(0, SHUT_RD);
206 } else if (write_stdout_possible && FD_ISSET(fd_tun, &fds_r)) {
207 write_stdout_possible = 0;
208 r = read(fd_tun, buf, MAX_SIZE);
210 fprintf(stderr, "read-error: %m\n");
211 shutdown(fd_tun, SHUT_RD);
212 shutdown(1, SHUT_WR);
216 struct GNUNET_MessageHeader hdr = { .size = htons(r + sizeof(struct GNUNET_MessageHeader)), .type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER) };
218 while(r < sizeof(struct GNUNET_MessageHeader)) {
219 int t = write(1, &hdr, sizeof(struct GNUNET_MessageHeader) - r);
221 fprintf(stderr, "write-error 2: %m\n");
222 shutdown(fd_tun, SHUT_RD);
223 shutdown(1, SHUT_WR);
229 while(r < ntohs(hdr.size)) {
230 int t = write(1, buf, ntohs(hdr.size) - r);
232 fprintf(stderr, "write-error 1: %m, written %d/%d\n", r, ntohs(hdr.size));
233 shutdown(fd_tun, SHUT_RD);
234 shutdown(1, SHUT_WR);
243 fprintf(stderr, "Quitting!\n");