aboutsummaryrefslogtreecommitdiff
path: root/src/vpn/gnunet-helper-vpn.c
blob: 4a367e9c9af87809a5d80b80e0be5aaf208139a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
     This file is part of GNUnet.
     (C) 2010 Christian Grothoff

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file vpn/gnunet-daemon-vpn.c
 * @brief
 * @author Philipp Tölke
 */
#include <platform.h>

#include "gnunet-vpn-tun.h"
#include "gnunet_common.h"
#include "gnunet_protocols.h"
#include "gnunet-vpn-helper-p.h"

#ifndef _LINUX_IN6_H
// This is in linux/include/net/ipv6.h.

#define MAX_SIZE (65535 - sizeof(struct GNUNET_MessageHeader))

struct in6_ifreq {
    struct in6_addr ifr6_addr;
    uint32_t ifr6_prefixlen;
    unsigned int ifr6_ifindex;
};

#endif

int running = 1;

void term(int sig) {
	fprintf(stderr, "Got SIGTERM...\n");
	if (sig == SIGTERM)
		running = 0;
}

static void set_address6(char* dev, char* address, unsigned long prefix_len) { /* {{{ */
	int fd = socket(AF_INET6, SOCK_DGRAM, 0);

	struct ifreq ifr;
	struct in6_ifreq ifr6;

	struct sockaddr_in6 sa6;
	memset(&sa6, 0, sizeof(struct sockaddr_in6));

	sa6.sin6_family = AF_INET6;

	/* FIXME */ inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr);

	memcpy((char *) &ifr6.ifr6_addr, (char *) &sa6.sin6_addr, sizeof(struct in6_addr));

	strncpy(ifr.ifr_name, dev, IFNAMSIZ);

	if (ioctl(fd, SIOGIFINDEX, &ifr) < 0) {
		perror("SIOGIFINDEX");
	}

	ifr6.ifr6_ifindex = ifr.ifr_ifindex;
	ifr6.ifr6_prefixlen = prefix_len;

	if (ioctl(fd, SIOCSIFADDR, &ifr6) < 0) {
		perror("SIOCSIFADDR");
	}

	/* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
	ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
	/* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
	close(fd);
} /* }}} */

static void set_address4(char* dev, char* address, char* mask) { /* {{{ */
	int fd=0;
	struct sockaddr_in* addr;
	struct ifreq ifr;

	memset(&ifr, 0, sizeof(struct ifreq));
	addr = (struct sockaddr_in *)&(ifr.ifr_addr);
	memset(addr, 0, sizeof(struct sockaddr_in));
	addr->sin_family = AF_INET;
	addr->sin_addr.s_addr = inet_addr(address);

	/* FIXME */ inet_pton(AF_INET, address, &addr->sin_addr.s_addr);

	fd = socket(PF_INET, SOCK_DGRAM, 0);
	if(fd < 0) {
		perror("socket()");
		return;
	}

	strncpy(ifr.ifr_name, dev, IFNAMSIZ);

	if(ioctl(fd, SIOCSIFADDR, &ifr) != 0 ) {
		perror("SIOCSIFADDR");
		close(fd);
		return;
	}

	addr = (struct sockaddr_in*)&(ifr.ifr_netmask);
	/* FIXME */ inet_pton(AF_INET, mask, &addr->sin_addr.s_addr);

	if(ioctl(fd, SIOCSIFNETMASK, &ifr) != 0 ) {
		perror("SIOCSIFNETMASK");
		close(fd);
		return;
	}

	/* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
	ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
	/* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
	close(fd);
} /* }}} */

void setnonblocking(int fd) {/*{{{*/
	int opts;

	opts = fcntl(fd,F_GETFL);
	if (opts < 0) {
			perror("fcntl(F_GETFL)");
	}
	opts = (opts | O_NONBLOCK);
	if (fcntl(fd,F_SETFL,opts) < 0) {
			perror("fcntl(F_SETFL)");
	}
	return;
}/*}}}*/

int main(int argc, char** argv) {
	unsigned char buf[MAX_SIZE];

	char dev[IFNAMSIZ];
	memset(dev, 0, IFNAMSIZ);

	signal(SIGTERM, &term);

	int fd_tun = init_tun(dev);

	if (fd_tun < 0) {
		fprintf(stderr, "Could not initialize tun-interface: %s\n", strerror(errno));
		exit(1);
	}

	{
	// TODO: get this out of argv
	char address[] = "1234::1";
	unsigned long prefix_len = 16;

	set_address6(dev, address, prefix_len);
	}

	{
	char address[] = "10.10.10.1";
	char mask[] = "255.255.255.252";

	set_address4(dev, address, mask);
	}

	uid_t uid = getuid ();
	if (setresuid (uid, uid, uid) != 0 )
		fprintf (stderr, "Failed to setresuid: %s\n", strerror(errno));

	setnonblocking(0);
	setnonblocking(1);
	setnonblocking(fd_tun);

	fd_set fds_w;
	fd_set fds_r;

	int rea = 1;
	int wri = 1;

	int write_fd_possible = 0;
	int write_stdout_possible = 0;
outer:
	while((rea == 1 || wri == 1) && running == 1) {
		FD_ZERO(&fds_w);
		FD_ZERO(&fds_r);

		if (rea) {
			FD_SET(fd_tun, &fds_r);
			if (!write_stdout_possible)
				FD_SET(1, &fds_w);
		}

		if (wri) {
			FD_SET(0, &fds_r);
			if (!write_fd_possible)
				FD_SET(fd_tun, &fds_w);
		}

		int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0);

		if(r > 0) {
			if (FD_ISSET(fd_tun, &fds_w)) write_fd_possible = 1;
			if (FD_ISSET(1, &fds_w)) write_stdout_possible = 1;

			if (FD_ISSET(0, &fds_r) && write_fd_possible) {
				write_fd_possible = 0;
				struct suid_packet *pkt = (struct suid_packet*) buf;
				r = read(0, buf, sizeof(struct GNUNET_MessageHeader));
				if (r <= 0) {
					fprintf(stderr, "read-error: %m\n");
					shutdown(fd_tun, SHUT_WR);
					shutdown(0, SHUT_RD);
					wri=0;
					goto outer;
				}
				if(pkt->hdr.type != ntohs(GNUNET_MESSAGE_TYPE_VPN_HELPER)) abort();
				while (r < ntohs(pkt->hdr.size)) {
					int t = read(0, buf + r, ntohs(pkt->hdr.size) - r);
					if (r < 0) {
						fprintf(stderr, "read-error: %m\n");
						shutdown(fd_tun, SHUT_WR);
						shutdown(0, SHUT_RD);
						wri=0;
						goto outer;
					}
					r += t;
				}
				r = 0;
				while (r < ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader)) {
					int t = write(fd_tun, pkt->data, ntohs(pkt->hdr.size) - sizeof(struct GNUNET_MessageHeader) - r);
					if (t < 0) {
						fprintf(stderr, "write-error 3: %m\n");
						shutdown(fd_tun, SHUT_WR);
						shutdown(0, SHUT_RD);
						wri = 0;
						goto outer;
					}
					r += t;
				}
			} else if (write_stdout_possible && FD_ISSET(fd_tun, &fds_r)) {
				write_stdout_possible = 0;
				r = read(fd_tun, buf, MAX_SIZE);
				if (r <= 0) {
					fprintf(stderr, "read-error: %m\n");
					shutdown(fd_tun, SHUT_RD);
					shutdown(1, SHUT_WR);
					rea = 0;
					goto outer;
				}
				struct GNUNET_MessageHeader hdr = { .size = htons(r + sizeof(struct GNUNET_MessageHeader)), .type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER) };
				r = 0;
				while(r < sizeof(struct GNUNET_MessageHeader)) {
					int t = write(1, &hdr, sizeof(struct GNUNET_MessageHeader) - r);
					if (t < 0) {
						fprintf(stderr, "write-error 2: %m\n");
						shutdown(fd_tun, SHUT_RD);
						shutdown(1, SHUT_WR);
						rea = 0;
						goto outer;
					}
					r += t;
				}
				while(r < ntohs(hdr.size)) {
					int t = write(1, buf, ntohs(hdr.size) - r);
					if (t < 0) {
						fprintf(stderr, "write-error 1: %s, written %d/%d\n", strerror(errno), r, ntohs(hdr.size));
						shutdown(fd_tun, SHUT_RD);
						shutdown(1, SHUT_WR);
						rea = 0;
						goto outer;
					}
					r += t;
				}
			}
		}
	}

	close(fd_tun);

	return 0;
}