-
Notifications
You must be signed in to change notification settings - Fork 11
/
ToyVpnServer.c
397 lines (329 loc) · 11.1 KB
/
ToyVpnServer.c
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <linux/if_tun.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while(0)
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while(0)
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static int get_interface(char *name)
{
const int interface = open("/dev/net/tun", O_RDWR);
if (interface == -1) handle_error("open");
struct ifreq ifr;
bzero(&ifr, sizeof(struct ifreq));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(interface, TUNSETIFF, &ifr) == -1)
handle_error("Cannot get TUN interface");
strcpy(name, ifr.ifr_name);
return interface;
}
static int get_tunnel(char *port, char *secret)
{
// We use an IPv6 socket to cover both IPv4 and IPv6.
const int tunnel = socket(AF_INET6, SOCK_DGRAM, 0);
const int on = 1, off = 0;
if (tunnel == -1) handle_error("socket");
setsockopt(tunnel, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
setsockopt(tunnel, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
// Accept packets received on any local address.
struct sockaddr_in6 addr;
bzero(&addr, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(atoi(port));
if (bind(tunnel, (struct sockaddr *) &addr, sizeof(addr)) == -1)
handle_error("bind");
// Receive packets till the secret matches.
char packet[1024];
socklen_t addrlen;
do {
addrlen = sizeof(addr);
int n = recvfrom(tunnel, packet, sizeof(packet), 0,
(struct sockaddr *)&addr, &addrlen);
if (n < 0) {
return -1;
}
packet[n] = 0;
if (n == 1 && packet[0] == 0) {
n = sendto(tunnel, packet, n, 0, (const struct sockaddr *)&addr, addrlen);
if (n == -1) handle_error("sendto");
continue;
}
} while (packet[0] != 0 || strcmp(secret, &packet[1]));
// Connect to the client as we only handle one client at a time.
connect(tunnel, (struct sockaddr *)&addr, addrlen);
return tunnel;
}
static int build_parameters(char *parameters, int size, char *address) {
const char *mtu = "m,1400";
const char *dns = "d,8.8.8.8";
const char *route = "r,0.0.0.0,0";
int write_size;
strcpy(parameters, " ");
strcat(parameters, mtu);
strcat(parameters, " ");
strcat(parameters, "a,");
strcat(parameters, address);
strcat(parameters, ",32");
strcat(parameters, " ");
strcat(parameters, dns);
strcat(parameters, " ");
strcat(parameters, route);
write_size = 1 + strlen(mtu) + 1 + 2 + strlen(address) + 3 + 1 + strlen(dns) + 1 + strlen(route);
// Control messages always start with zero.
parameters[0] = 0;
return write_size;
}
struct int_sock {
int interface;
int socket;
char *addrs;
int clnt_addr;
int tun_addr;
};
static void release_addr(char *addrs, int addr) {
pthread_mutex_lock(&mutex);
addrs[addr] = 0;
pthread_mutex_unlock(&mutex);
}
static void *read_send(void *ptr) {
struct int_sock *pint_sock = (struct int_sock *) ptr;
int socket = pint_sock->socket;
int interface = pint_sock->interface;
char *addrs = pint_sock->addrs;
int tun_addr = pint_sock->tun_addr;
char packet[32767];
int length;
do {
struct pollfd intf_pollfd;
intf_pollfd.fd = interface;
intf_pollfd.events = POLLIN;
int res = poll(&intf_pollfd, 1, 60 * 60 * 1000); // a hour time-out
if (res == 0) {
printf("tun interface read is timed out.\n");
break;
} else if (res == -1) {
perror("interface poll");
break;
}
length = read(interface, packet, sizeof(packet));
if (length == 0) {
printf("CANNOT read tun interface.\n");
break;
} else if (length == -1) {
perror("read");
break;
}
//printf("read %d bytes from interface.\n", length);
if ((length = send(socket, packet, length, MSG_NOSIGNAL)) == -1) {
perror("send");
break;
}
//printf("send %d bytes to client.\n", length);
} while(1);
close(interface);
release_addr(addrs, tun_addr);
return NULL;
}
static void *recv_write(void *ptr) {
struct int_sock *pint_sock = (struct int_sock *) ptr;
int socket = pint_sock->socket;
int interface = pint_sock->interface;
char *addrs = pint_sock->addrs;
int clnt_addr = pint_sock->clnt_addr;
char packet[32767];
int length;
do {
struct pollfd sock_pollfd;
sock_pollfd.fd = socket;
sock_pollfd.events = POLLIN;
int res = poll(&sock_pollfd, 1, 60 * 60 * 1000); // a hour time-out
if (res == 0) {
printf("Socket recv() is timed out.\n");
break;
} else if (res == -1) {
perror("socket poll");
break;
}
length = recv(socket, packet, sizeof(packet), 0);
if (length == 0) {
printf("Blocked recv() return 0.\n");
break;
}
else if (length == -1) {
perror("recv");
break;
}
//printf("received %d bytes from socket\n", length);
if (packet[0] != 0) {
if ((length = write(interface, packet, length)) == -1) {
perror("write");
break;
}
//printf("write %d bytes to interface\n", length);
} else if (packet[1] == 0) {
printf("Client closed connection.\n");
close(interface);
break;
}
} while(1);
close(socket);
release_addr(addrs, clnt_addr);
return NULL;
}
#define MAX_ADDR 0xFFFF // 24 bit A class
// allocate a address in range 10.0.0.1 ~ 10.0.255.254
/*
static int choose_random(char *addresses) {
unsigned int random;
do {
srand(time(NULL));
random = rand() / MAX_ADDR;
} while (random == 0 || random == 1 || addresses[random] == 1);
addresses[random] = 1;
return random;
}
*/
static int choose_addr(char *addrs) {
pthread_mutex_lock(&mutex);
for (int i = 1; i < MAX_ADDR; i++) {
if (addrs[i] == 0) {
addrs[i] = 1;
pthread_mutex_unlock(&mutex);
return i;
}
}
pthread_mutex_unlock(&mutex);
return MAX_ADDR;
}
static void set_addr(char *dev, char *addr) {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) handle_error("socket");
struct ifreq ifr;
bzero(&ifr, sizeof(struct ifreq));
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
struct sockaddr_in *sa_in = (struct sockaddr_in *) &ifr.ifr_dstaddr;
inet_pton(AF_INET, addr, &sa_in->sin_addr);
if (ioctl(sock, SIOCSIFADDR, &ifr) == -1)
handle_error("Cannot set address");
close(sock);
}
static void set_flag_up(char *dev) {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) handle_error("socket");
struct ifreq ifr;
bzero(&ifr, sizeof(struct ifreq));
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
handle_error("Cannot get flags");
ifr.ifr_flags |= IFF_UP;
if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
handle_error("Cannot set flags");
close(sock);
}
static void set_dstaddr(char *dev, char *addr) {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) handle_error("socket");
struct ifreq ifr;
bzero(&ifr, sizeof(struct ifreq));
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
struct sockaddr_in *sa_in = (struct sockaddr_in *) &ifr.ifr_dstaddr;
inet_pton(AF_INET, addr, &sa_in->sin_addr);
if (ioctl(sock, SIOCSIFDSTADDR, &ifr) == -1)
handle_error("Cannot set destination address");
close(sock);
}
static int setup_interface(char *dev, char *addrs, char *addr_str, int *intf_addr, int *clnt_addr) {
const int addr = choose_addr(addrs);
if (addr == MAX_ADDR) {
printf("Address is completely full.\n");
return -1;
}
sprintf(addr_str + 5, "%d.%d", addr >> 8, addr & 0xFF);
set_addr(dev, addr_str);
set_flag_up(dev);
const int dst_addr = choose_addr(addrs);
if (dst_addr == MAX_ADDR) {
printf("Address is completely full.\n");
return -1;
}
sprintf(addr_str + 5, "%d.%d", dst_addr >> 8, dst_addr & 0xFF);
set_dstaddr(dev, addr_str);
*intf_addr = addr;
*clnt_addr = dst_addr;
return 0;
}
int main(int argc, char *argv[])
{
//const FILE *fp = start_logger("log");
char priv_addr[MAX_ADDR];
bzero(priv_addr, sizeof(char)*MAX_ADDR);
// Wait for a tunnel.
int tunnel;
while ((tunnel = get_tunnel("8000", "test")) != -1) {
printf("Here comes a new tunnel\n");
// tun name is assgined systematically
char name[IFNAMSIZ] = { 0 };
int interface = get_interface(name);
char client_addr[15] = {'1', '0', '.', '0', '.', 0};
int tun_addr, clnt_addr;
int success = setup_interface(name, priv_addr, client_addr, &tun_addr, &clnt_addr);
if (success == -1) {
// TODO send the notification which server CANNOT assign a address to client
continue;
}
// Parse the arguments and set the parameters.
char parameters[1024];
int size = build_parameters(parameters, 1024, client_addr);
// Send the parameters several times in case of packet loss.
for (int i = 0; i < 3; ++i)
if (send(tunnel, parameters, size, MSG_NOSIGNAL) == -1)
handle_error("send");
pthread_t t_id[2];
int res;
struct int_sock connection;
connection.socket = tunnel;
connection.interface = interface;
connection.addrs = priv_addr;
connection.tun_addr = tun_addr;
connection.clnt_addr = clnt_addr;
if ((res = pthread_create(t_id, NULL, read_send, (void *) &connection)) != 0)
handle_error_en(res, "pthread_create");
if ((res = pthread_create(t_id + 1, NULL, recv_write, (void *) &connection)) != 0)
handle_error_en(res, "pthread_create");
}
perror("Cannot create tunnels");
//if (fp != NULL) stop_logger(fp);
return 0;
}