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
28 #include "gnunet-vpn-tun.h"
29 #include "gnunet_common.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet-vpn-helper-p.h"
34 // This is in linux/include/net/ipv6.h.
36 #define MAX_SIZE (65535 - sizeof(struct GNUNET_MessageHeader))
39 struct in6_addr ifr6_addr;
40 uint32_t ifr6_prefixlen;
41 unsigned int ifr6_ifindex;
49 fprintf(stderr, "Got SIGTERM...\n");
54 static void set_address6(char* dev, char* address, unsigned long prefix_len) { /* {{{ */
55 int fd = socket(AF_INET6, SOCK_DGRAM, 0);
58 struct in6_ifreq ifr6;
60 struct sockaddr_in6 sa6;
61 memset(&sa6, 0, sizeof(struct sockaddr_in6));
63 sa6.sin6_family = AF_INET6;
65 /* FIXME */ inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr);
67 memcpy((char *) &ifr6.ifr6_addr, (char *) &sa6.sin6_addr, sizeof(struct in6_addr));
69 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
71 if (ioctl(fd, SIOGIFINDEX, &ifr) < 0) {
72 perror("SIOGIFINDEX");
75 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
76 ifr6.ifr6_prefixlen = prefix_len;
78 if (ioctl(fd, SIOCSIFADDR, &ifr6) < 0) {
79 perror("SIOCSIFADDR");
82 /* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
83 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
84 /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
88 static void set_address4(char* dev, char* address, char* mask) { /* {{{ */
90 struct sockaddr_in* addr;
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);
99 /* FIXME */ inet_pton(AF_INET, address, &addr->sin_addr.s_addr);
101 fd = socket(PF_INET, SOCK_DGRAM, 0);
107 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
109 if(ioctl(fd, SIOCSIFADDR, &ifr) != 0 ) {
110 perror("SIOCSIFADDR");
115 addr = (struct sockaddr_in*)&(ifr.ifr_netmask);
116 /* FIXME */ inet_pton(AF_INET, mask, &addr->sin_addr.s_addr);
118 if(ioctl(fd, SIOCSIFNETMASK, &ifr) != 0 ) {
119 perror("SIOCSIFNETMASK");
124 /* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
125 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
126 /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
130 void setnonblocking(int fd) {/*{{{*/
133 opts = fcntl(fd,F_GETFL);
135 perror("fcntl(F_GETFL)");
137 opts = (opts | O_NONBLOCK);
138 if (fcntl(fd,F_SETFL,opts) < 0) {
139 perror("fcntl(F_SETFL)");
144 int main(int argc, char** argv) {
145 unsigned char buf[MAX_SIZE];
148 memset(dev, 0, IFNAMSIZ);
150 signal(SIGTERM, &term);
152 int fd_tun = init_tun(dev);
155 fprintf(stderr, "Could not initialize tun-interface: %s\n", strerror(errno));
160 // TODO: get this out of argv
161 char address[] = "1234::1";
162 unsigned long prefix_len = 16;
164 set_address6(dev, address, prefix_len);
168 char address[] = "10.10.10.1";
169 char mask[] = "255.255.255.252";
171 set_address4(dev, address, mask);
174 uid_t uid = getuid ();
175 if (setresuid (uid, uid, uid) != 0 )
176 fprintf (stderr, "Failed to setresuid: %s\n", strerror(errno));
180 setnonblocking(fd_tun);
188 int write_fd_possible = 0;
189 int write_stdout_possible = 0;
191 while((rea == 1 || wri == 1) && running == 1) {
196 FD_SET(fd_tun, &fds_r);
197 if (!write_stdout_possible)
203 if (!write_fd_possible)
204 FD_SET(fd_tun, &fds_w);
207 int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0);
210 if (FD_ISSET(fd_tun, &fds_w)) write_fd_possible = 1;
211 if (FD_ISSET(1, &fds_w)) write_stdout_possible = 1;
213 if (FD_ISSET(0, &fds_r) && write_fd_possible) {
214 write_fd_possible = 0;
215 struct suid_packet *pkt = (struct suid_packet*) buf;
216 r = read(0, buf, sizeof(struct GNUNET_MessageHeader));
218 fprintf(stderr, "read-error: %m\n");
219 shutdown(fd_tun, SHUT_WR);
220 shutdown(0, SHUT_RD);
224 if(pkt->hdr.type != ntohs(GNUNET_MESSAGE_TYPE_VPN_HELPER)) abort();
225 while (r < ntohs(pkt->hdr.size)) {
226 int t = read(0, buf + r, ntohs(pkt->hdr.size) - r);
228 fprintf(stderr, "read-error: %m\n");
229 shutdown(fd_tun, SHUT_WR);
230 shutdown(0, SHUT_RD);
237 while (r < ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader)) {
238 int t = write(fd_tun, pkt->data, ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader) - r);
240 fprintf(stderr, "write-error 3: %m\n");
241 shutdown(fd_tun, SHUT_WR);
242 shutdown(0, SHUT_RD);
248 } else if (write_stdout_possible && FD_ISSET(fd_tun, &fds_r)) {
249 write_stdout_possible = 0;
250 r = read(fd_tun, buf, MAX_SIZE);
252 fprintf(stderr, "read-error: %m\n");
253 shutdown(fd_tun, SHUT_RD);
254 shutdown(1, SHUT_WR);
258 struct GNUNET_MessageHeader hdr = { .size = htons(r + sizeof(struct GNUNET_MessageHeader)), .type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER) };
260 while(r < sizeof(struct GNUNET_MessageHeader)) {
261 int t = write(1, &hdr, sizeof(struct GNUNET_MessageHeader) - r);
263 fprintf(stderr, "write-error 2: %m\n");
264 shutdown(fd_tun, SHUT_RD);
265 shutdown(1, SHUT_WR);
271 while(r < ntohs(hdr.size)) {
272 int t = write(1, buf, ntohs(hdr.size) - r);
274 fprintf(stderr, "write-error 1: %s, written %d/%d\n", strerror(errno), r, ntohs(hdr.size));
275 shutdown(fd_tun, SHUT_RD);
276 shutdown(1, SHUT_WR);