-
Notifications
You must be signed in to change notification settings - Fork 316
/
mptun.c
893 lines (797 loc) · 18.4 KB
/
mptun.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <sys/socket.h>
#if defined(__linux__)
#include <linux/if.h>
#include <linux/if_tun.h>
#elif defined(__APPLE__)
#include <sys/ioctl.h>
#include <sys/kern_control.h>
#include <sys/uio.h>
#include <sys/sys_domain.h>
#include <net/if_utun.h>
#include <netinet/ip.h>
#define IFNAMSIZ 16
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h>
#include <signal.h>
#include <inttypes.h>
#include <time.h>
#if defined(IFF_TUN)
#define tun_read(...) read(__VA_ARGS__)
#define tun_write(...) write(__VA_ARGS__)
#elif defined(__APPLE__)
#define tun_read(...) utun_read(__VA_ARGS__)
#define tun_write(...) utun_write(__VA_ARGS__)
#endif
#define MAX_ADDRESS 16
#define BASE_COUNT 64
#define MAX_COUNT 16384
/* buffer for reading , must be >= 1500 */
#define BUFF_SIZE 2000
#define IP_SIZE 128
/* 1 hour time diff */
#define TIME_DIFF 3600
// todo: support ipv6
typedef struct sockaddr_in SOCKADDR;
typedef struct in_addr INADDR;
static int SIG = 0;
struct tundev {
uint64_t key;
time_t ti;
int port;
int tunfd;
int remote_n;
int local_n;
SOCKADDR remote[MAX_ADDRESS];
INADDR local[MAX_ADDRESS];
int localfd[MAX_ADDRESS];
int remote_count[MAX_ADDRESS];
int local_count[MAX_ADDRESS];
uint64_t in[MAX_ADDRESS];
uint64_t out[MAX_ADDRESS];
uint64_t drop;
uint64_t untrack;
uint64_t invalid;
};
struct rc4_sbox {
int i;
int j;
uint8_t sbox[256];
};
static void
rc4_init(struct rc4_sbox *rs, uint64_t seed) {
rs->i=0;
rs->j=0;
int i;
uint8_t k[8];
for (i=0;i<8;i++) {
k[i] = seed & 0xff;
seed >>= 8;
}
for (i=0;i<256;i++) {
rs->sbox[i] = (uint8_t)((i + k[i%8]) & 0xff);
}
}
static void
rc4_encode(struct rc4_sbox *rs, const uint8_t *src, uint8_t *des, size_t sz) {
size_t i;
for (i=0;i<sz;i++) {
rs->i = (rs->i + 1) % 256;
rs->j = (rs->j + rs->sbox[rs->i]) % 256;
uint8_t si = rs->sbox[rs->i];
uint8_t sj = rs->sbox[rs->j];
rs->sbox[rs->i] = sj;
rs->sbox[rs->j] = si;
uint8_t d = src[i] ^ rs->sbox[(si+sj) % 256];
des[i] = d;
}
}
static uint64_t
hash_key(const char * str, int sz) {
uint32_t djb_hash = 5381L;
uint32_t js_hash = 1315423911L;
int i;
for (i=0;i<sz;i++) {
uint8_t c = (uint8_t)str[i];
djb_hash += (djb_hash << 5) + c;
js_hash ^= ((js_hash << 5) + c + (js_hash >> 2));
}
return (uint64_t) djb_hash << 32 | js_hash;
}
// leftrotate function definition
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
static uint64_t
hmac(uint64_t x, uint64_t y) {
// Constants are the integer part of the sines of integers (in radians) * 2^32.
static const uint32_t k[64] = {
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee ,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 ,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be ,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 ,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa ,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 ,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed ,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a ,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c ,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 ,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 ,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 ,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 ,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 ,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 ,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };
// r specifies the per-round shift amounts
static const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
uint32_t w[16];
uint32_t a, b, c, d, f, g, temp;
int i;
a = 0x67452301u;
b = 0xefcdab89u;
c = 0x98badcfeu;
d = 0x10325476u;
for (i=0;i<16;i+=4) {
w[i] = (uint32_t)(x << 32);
w[i+1] = (uint32_t)x;
w[i+2] = (uint32_t)(y << 32);
w[i+3] = (uint32_t)y;
}
for(i = 0; i<64; i++) {
if (i < 16) {
f = (b & c) | ((~b) & d);
g = i;
} else if (i < 32) {
f = (d & b) | ((~d) & c);
g = (5*i + 1) % 16;
} else if (i < 48) {
f = b ^ c ^ d;
g = (3*i + 5) % 16;
} else {
f = c ^ (b | (~d));
g = (7*i) % 16;
}
temp = d;
d = c;
c = b;
b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]);
a = temp;
}
return (uint64_t)(a^b) << 32 | (c^d);
}
static inline int
mptun_encrypt(const char in[BUFF_SIZE], int sz, char out[BUFF_SIZE], uint64_t key, time_t ti) {
uint64_t h = hash_key(in, sz);
uint32_t tmp;
struct rc4_sbox rs;
if (sz > BUFF_SIZE - 8)
return -1;
key = hmac(key, ti);
rc4_init(&rs, key);
key ^= h;
tmp = htonl(ti);
memcpy(out, &tmp, 4);
tmp = htonl((uint32_t)key ^ (uint32_t)(key >> 32));
memcpy(out+4, &tmp, 4);
rc4_encode(&rs, (const uint8_t *)in, (uint8_t *)out+8, sz);
return sz + 8;
}
static inline int
mptun_decrypt(const char in[BUFF_SIZE], int sz, char out[BUFF_SIZE], uint64_t key, time_t ti) {
uint32_t pt, check;
uint64_t h;
struct rc4_sbox rs;
sz -= 8;
if (sz < 0) {
return -1;
}
memcpy(&pt, in, 4);
memcpy(&check, in+4, 4);
pt = ntohl(pt);
check = ntohl(check);
if (abs((int)(pt - ti)) > TIME_DIFF) {
return -1;
}
key = hmac(key, pt);
rc4_init(&rs, key);
rc4_encode(&rs, (const uint8_t *)in+8, (uint8_t *)out, sz);
h = hash_key(out, sz);
key ^= h;
if (check != ((uint32_t)key ^ (uint32_t)(key >> 32))) {
return -1;
}
return sz;
}
static void
dumpinfo(struct tundev *tdev) {
char tmp[1024];
int i;
uint64_t s = 0;
for (i=0;i<tdev->local_n;i++) {
s += tdev->out[i];
inet_ntop(AF_INET, &tdev->local[i], tmp, sizeof(tmp));
printf("-> %s %" PRId64 "\n", tmp, tdev->out[i]);
}
printf("Total out %" PRId64 "\n", s);
printf("Drop out %" PRId64 "\n", tdev->drop);
s = 0;
for (i=0;i<tdev->remote_n;i++) {
s += tdev->in[i];
inet_ntop(AF_INET, &tdev->remote[i].sin_addr, tmp, sizeof(tmp));
printf("<- %s %" PRId64 "\n", tmp, tdev->in[i]);
}
printf("Total in %" PRId64 "\n", s);
printf("Untrack in %" PRId64 "\n", tdev->untrack);
printf("Invalid in %" PRId64 "\n", tdev->invalid);
}
static void
dumpinfo_hup(struct tundev *tdev) {
if (SIG) {
dumpinfo(tdev);
SIG = 0;
}
}
#if defined(IFF_TUN)
static int
tun_alloc(char *dev) {
struct ifreq ifr;
int fd, err;
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
perror("Opening /dev/net/tun");
return fd;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if( (err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0 ) {
perror("ioctl(TUNSETIFF)");
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
#elif defined(__APPLE__)
static int utun_open_helper (struct ctl_info ctlInfo, int utunnum)
{
struct sockaddr_ctl sc;
int fd;
fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
if (fd < 0)
{
return -2;
}
if (ioctl(fd, CTLIOCGINFO, &ctlInfo) == -1)
{
close (fd);
return -2;
}
sc.sc_id = ctlInfo.ctl_id;
sc.sc_len = sizeof(sc);
sc.sc_family = AF_SYSTEM;
sc.ss_sysaddr = AF_SYS_CONTROL;
sc.sc_unit = utunnum+1;
/* If the connect is successful, a utun%d device will be created, where "%d"
* is (sc.sc_unit - 1) */
if (connect (fd, (struct sockaddr *)&sc, sizeof(sc)) < 0)
{
close(fd);
return -1;
}
return fd;
}
static int
tun_alloc (char *dev)
{
struct ctl_info ctlInfo;
int fd;
char utunname[20];
int utunnum =-1;
socklen_t utunname_len = sizeof(utunname);
if (strlcpy(ctlInfo.ctl_name, UTUN_CONTROL_NAME, sizeof(ctlInfo.ctl_name)) >=
sizeof(ctlInfo.ctl_name))
{
printf("Opening utun: UTUN_CONTROL_NAME too long\n");
return -1;
}
/* try to open first available utun device if no specific utun is requested */
if (utunnum == -1)
{
for (utunnum=0; utunnum<255; utunnum++)
{
fd = utun_open_helper (ctlInfo, utunnum);
/* Break if the fd is valid,
* or if early initalization failed (-2) */
if (fd !=-1)
break;
}
}
else
{
fd = utun_open_helper (ctlInfo, utunnum);
}
if(fd < 0) {
printf("failed to create fd\n");
return fd; //error
}
/* Retrieve the assigned interface name. */
if (getsockopt (fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, utunname, &utunname_len)) {
printf("Error retrieving utun interface name\n");
return -1;
}
printf("Opened utun device %s\n", utunname);
strcpy(dev, utunname); //return device name
return fd;
}
//remove the IP version header from the result of bytes read or written.
static inline ssize_t header_modify_read_write_return (ssize_t len)
{
if (len > 0)
return len > (ssize_t) sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
else
return len;
}
//read from utun
static inline ssize_t
utun_read(int fd, char *buf, int len) {
u_int32_t type;
struct iovec iv[2];
struct ip *iph;
iph = (struct ip *) buf;
if(iph->ip_v == 6)
type = htonl(AF_INET6);
else
type = htonl(AF_INET);
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof (type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return header_modify_read_write_return(readv(fd, iv, 2));
}
//write to utun
static inline ssize_t
utun_write(int fd, char *buf, int len)
{
u_int32_t type;
struct iovec iv[2];
struct ip *iph;
iph = (struct ip *) buf;
if(iph->ip_v == 6)
type = htonl(AF_INET6);
else
type = htonl(AF_INET);
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof (type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return header_modify_read_write_return(writev(fd, iv, 2));
}
#endif
static int
inet_bind(INADDR *addr, int port) {
int reuse = 1;
SOCKADDR address;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return fd;
}
address.sin_family = AF_INET;
address.sin_addr = *addr;
address.sin_port = htons(port);
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int)) ==-1) {
close(fd);
return -1;
}
if (bind(fd, (struct sockaddr*)&address, sizeof(address)) != 0) {
perror("bind");
close(fd);
return -1;
}
return fd;
}
static void
usage(void) {
fprintf(stderr,
"Usage:\n"
"\t-i <ifacename>: Name of interface to use (for example: tun0). Final interface name may change on OSX\n"
"\t-v <vpnlocalIP> : specify vpn address (for example: 10.0.0.1)\n"
"\t-t <vpnremoteIP> : specify vpn P-t-P address (for example: 10.0.0.2)\n"
"\t-r <remoteIP> : specify remote address, it can specify multi times. (or zero, if you run as server)\n"
"\t-l <localIP> : specify local address, it can specify multi times. (or zero, if you run as server)\n"
"\t-p <port> : specify port for tunnel\n"
"\t-k <key> : optional password\n"
);
exit(1);
}
static void
add_remote(struct tundev *tdev, SOCKADDR *addr, int bytes) {
int i;
int mincount = MAX_COUNT;
int minidx = -1;
for (i=0;i<tdev->remote_n;i++) {
if (memcmp(&addr->sin_addr, &tdev->remote[i].sin_addr, sizeof(INADDR))==0) {
tdev->remote[i].sin_port = addr->sin_port; // update port (NAT may change port)
if (++tdev->remote_count[i] > MAX_COUNT) {
int j;
for (j=0;j<tdev->remote_n;j++) {
tdev->remote_count[j] /= 2;
}
}
tdev->in[i] += bytes;
return;
} else if (tdev->remote_count[i] < mincount) {
mincount = tdev->remote_count[i];
minidx = i;
}
}
if (tdev->remote_n < MAX_ADDRESS) {
i = tdev->remote_n++;
} else {
i = minidx;
}
tdev->remote[i] = *addr;
tdev->remote_count[i] = 0;
tdev->untrack += tdev->in[i];
tdev->in[i] = bytes;
}
// forward ip packet from internet to tun , and return peer address
static void
inet_to_tun(struct tundev *tdev, int index) {
SOCKADDR sa;
int inetfd = tdev->localfd[index];
int tunfd = tdev->tunfd;
char buf[BUFF_SIZE], outbuff[BUFF_SIZE];
ssize_t n, rn;
for (;;) {
socklen_t addrlen = sizeof(sa);
n = recvfrom(inetfd, buf, BUFF_SIZE, 0,(struct sockaddr *)&sa, &addrlen);
if (n < 0) {
if (errno == EINTR) {
continue;
}
else {
perror("recvfrom");
exit(1);
// fail
}
} else {
break;
}
}
rn = mptun_decrypt(buf, n, outbuff, tdev->key, tdev->ti);
if (rn < 0) {
tdev->invalid += n;
return;
}
for (;;) {
int ret = tun_write(tunfd, outbuff, rn);
if (ret < 0) {
if (errno == EINTR) {
continue;
}
else {
perror("write tun");
exit(1);
}
} else {
break;
}
}
// succ
add_remote(tdev, &sa, (int)n);
}
static void
drop_tun(struct tundev *tdev) {
int tunfd = tdev->tunfd;
char buf[BUFF_SIZE];
ssize_t n;
for (;;) {
n = tun_read(tunfd, buf, BUFF_SIZE);
if (n < 0) {
if (errno == EINTR) {
continue;
}
else {
perror("drop");
exit(1);
return;
}
} else {
break;
}
}
tdev->drop += n;
}
static int
choose_local(struct tundev *tdev, fd_set *set) {
int i;
int t = 0;
int r;
if (tdev->local_n == 1) {
return 0;
}
for (i=0;i<tdev->local_n;i++) {
if (FD_ISSET(tdev->localfd[i], set)) {
t += BASE_COUNT + tdev->local_count[i];
}
}
if (t == 0)
return 0;
r = random() % t;
t = 0;
for (i=0;i<tdev->local_n;i++) {
if (FD_ISSET(tdev->localfd[i], set)) {
t += BASE_COUNT + tdev->local_count[i];
if (r < t) {
return i;
}
}
}
return 0;
}
static int
choose_remote(struct tundev *tdev) {
int i;
int t = 0;
int r;
if (tdev->remote_n <= 1)
return 0;
for (i=0;i<tdev->remote_n;i++) {
t += BASE_COUNT + tdev->remote_count[i];
}
r = random() % t;
t = 0;
for (i=0;i<tdev->remote_n;i++) {
t += BASE_COUNT + tdev->remote_count[i];
if (r < t) {
return i;
}
}
return 0;
}
// forward ip packet from tun to internet with address
static void
tun_to_inet(struct tundev *tdev, fd_set *wt) {
int tunfd = tdev->tunfd;
int localindex = choose_local(tdev, wt);
int inetfd = tdev->localfd[localindex];
int remoteindex = choose_remote(tdev);
SOCKADDR * addr = &tdev->remote[remoteindex];
char buf[BUFF_SIZE], outbuf[BUFF_SIZE];
ssize_t n;
for (;;) {
n = tun_read(tunfd, buf, BUFF_SIZE);
if (n < 0) {
if (errno == EINTR) {
continue;
}
else {
perror("read tun");
exit(1);
return;
}
} else {
break;
}
}
n = mptun_encrypt(buf, n, outbuf, tdev->key, tdev->ti);
if (n < 0) {
fprintf(stderr, "Invalid tun package size %d", (int)n);
return;
}
for (;;) {
int ret = sendto(inetfd, outbuf, n, 0, (struct sockaddr *)addr, sizeof(SOCKADDR));
if (ret < 0 && errno == EINTR) {
continue;
} else {
break;
}
}
tdev->out[remoteindex] += n;
}
static void
forwarding(struct tundev *tdev, int maxrd, fd_set *rdset, int maxwt, fd_set *wtset) {
int i;
fd_set rd,wt;
// read
rd = *rdset;
for (;;) {
int ret = select(maxrd, &rd, NULL, NULL, NULL);
if (ret < 0) {
if (errno == EINTR) {
dumpinfo_hup(tdev);
continue;
}
perror("select read");
exit(1);
} else {
break;
}
}
for (i=0;i<tdev->local_n;i++) {
if (FD_ISSET(tdev->localfd[i], &rd)) {
// forward ip packet of inet to tun
if (++tdev->local_count[i] > MAX_COUNT) {
int j;
for (j=0;j<tdev->local_n;j++) {
tdev->local_count[j] /= 2;
}
}
inet_to_tun(tdev, i);
}
}
if (FD_ISSET(tdev->tunfd, &rd)) {
// forward ip packet of tun to inet
wt = *wtset;
for (;;) {
int ret = select(maxwt, NULL, &wt, NULL, NULL);
if (ret < 0) {
if (errno == EINTR) {
dumpinfo_hup(tdev);
continue;
}
perror("select write");
exit(1);
} else {
break;
}
}
if (tdev->remote_n == 0) {
drop_tun(tdev);
} else {
tun_to_inet(tdev, &wt);
}
}
}
static void
handle_hup(int signal) {
if (signal == SIGHUP) {
SIG = 1;
}
}
static void
start(struct tundev *tdev) {
struct sigaction sa;
int i;
int maxrd_fd = tdev->tunfd;
int maxwt_fd = -1;
fd_set rdset, wtset;
FD_ZERO(&rdset);
FD_ZERO(&wtset);
FD_SET(tdev->tunfd, &rdset);
for (i=0;i<tdev->local_n;i++) {
int fd = tdev->localfd[i];
if (fd >= FD_SETSIZE) {
fprintf(stderr,"fd %d (%d) is larger than FD_SETSIZE", i, fd);
exit(1);
}
FD_SET(fd, &rdset);
FD_SET(fd, &wtset);
if (fd > maxrd_fd)
maxrd_fd = fd;
if (fd > maxwt_fd)
maxwt_fd = fd;
}
sa.sa_handler = &handle_hup;
sa.sa_flags = SA_RESTART;
sigfillset(&sa.sa_mask);
if (sigaction(SIGHUP, &sa, NULL) == -1) {
perror("handle SIGHUP");
exit(1);
}
for (;;) {
dumpinfo_hup(tdev);
tdev->ti = time(NULL);
forwarding(tdev, maxrd_fd+1, &rdset, maxwt_fd+1, &wtset);
}
}
static void
ifconfig(const char * ifname, const char * va, const char *pa) {
char cmd[1024];
#if defined(__APPLE__)
snprintf(cmd, sizeof(cmd), "ifconfig %s %s %s mtu 1380 netmask 255.255.255.255 up",
ifname, va, pa);
#else
snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask 255.255.255.255 pointopoint %s",
ifname, va, pa);
#endif
if (system(cmd) < 0) {
perror(cmd);
exit(1);
}
}
int
main(int argc, char *argv[]) {
int i;
int option;
char ifname[IFNAMSIZ] = "";
char vpnaddress[IP_SIZE] = "";
char ptpaddress[IP_SIZE] = "";
struct tundev tdev;
memset(&tdev, 0, sizeof(tdev));
while ((option = getopt(argc, argv, "i:v:t:r:l:p:k:")) > 0) {
INADDR addr;
switch(option) {
case 'i':
strncpy(ifname,optarg,IFNAMSIZ-1);
break;
case 'v':
strncpy(vpnaddress,optarg,IP_SIZE-1);
break;
case 't':
strncpy(ptpaddress,optarg,IP_SIZE-1);
break;
case 'p':
tdev.port = strtol(optarg, NULL, 0);
break;
case 'l':
case 'r':
if (inet_pton(AF_INET, optarg, &addr) <= 0) {
fprintf(stderr, "Invalid ip : %s\n", optarg);
return 1;
}
if (option == 'l') {
if (tdev.local_n >= MAX_ADDRESS) {
fprintf(stderr, "Too many local ip\n");
return 1;
}
tdev.local[tdev.local_n++] = addr;
} else {
SOCKADDR *sa = &tdev.remote[tdev.remote_n];
if (tdev.remote_n >= MAX_ADDRESS) {
fprintf(stderr, "Too many remote ip\n");
return 1;
}
++tdev.remote_n;
sa->sin_addr = addr;
}
break;
case 'k':
tdev.key = hash_key(optarg, strlen(optarg));
break;
default:
usage();
break;
}
}
if (tdev.port == 0 || ifname[0] == '\0' || vpnaddress[0] == '\0' || ptpaddress[0] == '\0') {
usage();
return 1;
}
if ((tdev.tunfd = tun_alloc(ifname)) < 0) {
return 1;
}
ifconfig(ifname, vpnaddress, ptpaddress);
if (tdev.local_n == 0) {
INADDR *addr = &tdev.local[tdev.local_n++];
addr->s_addr = htonl(INADDR_ANY);
}
for (i=0;i<tdev.local_n;i++) {
int fd = inet_bind(&tdev.local[i], tdev.port);
if (fd < 0) {
// no need to close tdev.localfd[], because exit 1
return 1;
}
tdev.localfd[i] = fd;
}
for (i=0;i<MAX_ADDRESS;i++) {
tdev.remote[i].sin_family = AF_INET;
tdev.remote[i].sin_port = htons(tdev.port);
}
start(&tdev);
return 0;
}