-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.c
executable file
·1279 lines (1262 loc) · 53.4 KB
/
server.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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
// 包入tun相关的头部
#include <net/if.h>
#include <linux/if_tun.h>
// 包入网络相关的头部
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/epoll.h>
// 包入路由相关的头部
#include <net/route.h>
// 包入openssl的头部
#include <openssl/ssl.h>
// 包入json解析头部
#include "yyjson.h"
// 监听异常中断
#include "exception.h"
#define MAXDATASIZE 2*1024*1024
#define MAX_EVENT 1024
#define MAX_CONNECT 1024
#define MTU_SIZE 1500
const char PAGE404[] = "HTTP/1.1 404 Not Found\r\nServer: nginx/1.14.2\r\nDate: %a, %d %b %Y %H:%M:%S GMT\r\n" \
"Content-Type: text/html\r\nContent-Length: 169\r\nConnection: keep-alive\r\n\r\n" \
"<html>\r\n<head><title>404 Not Found</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>404 Not Found</h1></center>\r\n" \
"<hr><center>nginx/1.14.2</center>\r\n</body>\r\n</html>\r\n";
const char PAGE400[] = "HTTP/1.1 400 Bad Request\r\nServer: nginx/1.14.2\r\nDate: %a, %d %b %Y %H:%M:%S GMT\r\n" \
"Content-Type: text/html\r\nContent-Length: 173\r\nConnection: close\r\n\r\n" \
"<html>\r\n<head><title>400 Bad Request</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>400 Bad Request</h1></center>\r\n" \
"<hr><center>nginx/1.14.2</center>\r\n</body>\r\n</html>\r\n";
const char PAGE101[] = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n";
struct PACKAGELIST {
unsigned char data[MTU_SIZE + 14];
unsigned int size;
struct PACKAGELIST *tail;
};
struct PACKAGELIST *remainpackagelisthead = NULL;
struct CLIENTLIST {
struct FDCLIENT *fdclient; // 与自己相关联的fdclient对象
unsigned char mac[6]; // 该端口的源mac地址
struct PACKAGELIST *packagelisthead; // 发给自己这个端口的数据包列表头部
struct PACKAGELIST *packagelisttail; // 发给自己这个端口的数据包列表尾部
unsigned char remainpackage[MTU_SIZE + 14]; // 自己接收到的数据出现数据不全,将不全的数据存在这里,等待新的数据将其补全
unsigned int remainsize; // 不全的数据大小
unsigned char canwrite;
struct CLIENTLIST *hashhead; // 从哈希表中寻找上一个clientlist
struct CLIENTLIST *hashtail; // 从哈希表中寻找下一个clientlist
struct CLIENTLIST *head; // 从remainclientlist中寻找下一个可用的clientlist
struct CLIENTLIST *tail; // 从remainclientlist中寻找下一个可用的clientlist
struct CLIENTLIST *writetail; // 用于存储在readdata中发现有写入过程的node
};
struct CLIENTLIST *clientlisthead = NULL;
struct CLIENTLIST *remainclientlisthead = NULL;
struct CLIENTLIST *machashlist[0x10000]; // mac地址的hash表,用于快速找到对应的mac
struct CLIENTLIST *tapclient;
struct FDCLIENT {
int fd;
SSL *tls;
unsigned char tlsconnected;
unsigned char watch;
struct CLIENTLIST *client;
struct FDCLIENT *tail; // 从remainclientlist中寻找下一个可用的clientlist
};
struct FDCLIENT *remainfdclienthead = NULL;
struct FDCLIENT *fdserver;
int epollfd;
SSL_CTX *ctx;
unsigned char readbuf[MAXDATASIZE];
struct KEYS {
unsigned char key[33];
struct KEYS *tail;
};
struct ROUTERS {
unsigned char dstip[4];
unsigned char dstmask[4];
unsigned char gateway[4];
struct ROUTERS *tail;
};
struct CONFIG {
unsigned short bindport;
bool tcpkeepalive;
unsigned char tcpkeepidle;
unsigned char tcpkeepintvl;
unsigned char tcpkeepcnt;
bool ssl;
unsigned char crtpath[256];
unsigned char keypath[256];
unsigned char ip[4];
unsigned char mask[4];
unsigned char httppath[256];
unsigned char tapname[16];
struct KEYS *keys;
struct ROUTERS *routers;
};
struct CONFIG c;
int setnonblocking (int fd) {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
int flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
printf("get flags fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
return -1;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
printf("set flags fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
return -2;
}
return 0;
}
int addtoepoll (struct FDCLIENT *fdclient, uint32_t flags) {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
struct epoll_event ev;
ev.data.ptr = fdclient;
ev.events = EPOLLERR | EPOLLHUP | EPOLLRDHUP | flags; // 水平触发,保证所有数据都能读到
return epoll_ctl(epollfd, EPOLL_CTL_ADD, fdclient->fd, &ev);
}
int modepoll (struct FDCLIENT *fdclient, uint32_t flags) {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
struct epoll_event ev;
ev.data.ptr = fdclient;
ev.events = EPOLLERR | EPOLLHUP | EPOLLRDHUP | flags; // 水平触发,保证所有数据都能读到
return epoll_ctl(epollfd, EPOLL_CTL_MOD, fdclient->fd, &ev);
}
int tap_alloc () {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
int fd = open("/dev/net/tun", O_RDWR);
if (fd < 0) {
printf("open tun node fail, in %s, at %d\n", __FILE__, __LINE__);
return -1;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strcpy(ifr.ifr_name, c.tapname);
if (ioctl(fd, TUNSETIFF, (void*) &ifr) < 0) {
printf("ioctl tun node fail, in %s, at %d\n", __FILE__, __LINE__);
close(fd);
return -2;
}
printf("create tap device success, in %s, at %d\n", __FILE__, __LINE__);
int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (ioctl(socket_fd, SIOCGIFFLAGS, (void *) &ifr) < 0) {
printf("ioctl SIOCGIFFLAGS fail, in %s, at %d\n", __FILE__, __LINE__);
close(socket_fd);
close(fd);
return -3;
}
ifr.ifr_flags |= IFF_UP;
if (ioctl(socket_fd, SIOCSIFFLAGS, &ifr) < 0) {
printf("up tap device fail, in %s, at %d\n", __FILE__, __LINE__);
close(socket_fd);
close(fd);
return -4;
}
printf("up tap device success, in %s, at %d\n", __FILE__, __LINE__);
struct sockaddr_in sin;
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, c.ip, 4);
memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
if (ioctl(socket_fd, SIOCSIFADDR, &ifr) < 0) {
printf("set ip addr for tap device fail, in %s, at %d\n", __FILE__, __LINE__);
close(socket_fd);
close(fd);
return -5;
}
printf("set ip addr for tap device to %d.%d.%d.%d success, in %s, at %d\n", c.ip[0], c.ip[1], c.ip[2], c.ip[3], __FILE__, __LINE__);
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, c.mask, 4);
memcpy(&ifr.ifr_netmask, &sin, sizeof(struct sockaddr));
if (ioctl(socket_fd, SIOCSIFNETMASK, &ifr) < 0) {
printf("set netmask for tap device fail, in %s, at %d\n", __FILE__, __LINE__);
close(socket_fd);
close(fd);
return -6;
}
printf("set netmask for tap device to %d.%d.%d.%d success, in %s, at %d\n", c.mask[0], c.mask[1], c.mask[2], c.mask[3], __FILE__, __LINE__);
struct rtentry rt;
struct ROUTERS *routers = c.routers;
while (routers != NULL) {
memset(&rt, 0, sizeof(struct rtentry));
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, routers->dstip, 4);
memcpy(&rt.rt_dst, &sin, sizeof(struct sockaddr_in));
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, routers->dstmask, 4);
memcpy(&rt.rt_genmask, &sin, sizeof(struct sockaddr_in));
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, routers->gateway, 4);
memcpy(&rt.rt_gateway, &sin, sizeof(struct sockaddr_in));
rt.rt_flags = RTF_GATEWAY;
if (ioctl(socket_fd, SIOCADDRT, &rt) < 0) {
printf("add static route %d.%d.%d.%d mask %d.%d.%d.%d via %d.%d.%d.%d fail, in %s, at %d\n", routers->dstip[0], routers->dstip[1], routers->dstip[2], routers->dstip[3], routers->dstmask[0], routers->dstmask[1], routers->dstmask[2], routers->dstmask[3], routers->gateway[0], routers->gateway[1], routers->gateway[2], routers->gateway[3], __FILE__, __LINE__);
} else {
printf("add static route %d.%d.%d.%d mask %d.%d.%d.%d via %d.%d.%d.%d success, in %s, at %d\n", routers->dstip[0], routers->dstip[1], routers->dstip[2], routers->dstip[3], routers->dstmask[0], routers->dstmask[1], routers->dstmask[2], routers->dstmask[3], routers->gateway[0], routers->gateway[1], routers->gateway[2], routers->gateway[3], __FILE__, __LINE__);
}
struct ROUTERS *r = routers;
routers = routers->tail;
free(r);
}
close(socket_fd);
if (setnonblocking(fd) < 0) {
printf("set nonblocking fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -8;
}
printf("tap device name is %s, tapfd %d, in %s, at %d\n", ifr.ifr_name, fd, __FILE__, __LINE__);
if (remainclientlisthead) {
tapclient = remainclientlisthead;
remainclientlisthead = remainclientlisthead->tail;
} else {
tapclient = (struct CLIENTLIST*) malloc(sizeof(struct CLIENTLIST));
if (tapclient == NULL) {
printf("malloc fail, in %s, at %d\n", __FILE__, __LINE__);
close(fd);
return -9;
}
}
memset(tapclient->mac, 0, 6);
tapclient->packagelisthead = NULL;
tapclient->remainsize = 0;
tapclient->canwrite = 1;
tapclient->hashhead = NULL;
tapclient->hashtail = NULL;
tapclient->head = NULL;
if (clientlisthead) {
clientlisthead->head = tapclient;
}
tapclient->tail = clientlisthead;
clientlisthead = tapclient;
struct FDCLIENT* fdclient;
if (remainfdclienthead) { // 有存货,直接拿出来用
fdclient = remainfdclienthead;
remainfdclienthead = remainfdclienthead->tail;
} else { // 没有存货,malloc一个
fdclient = (struct FDCLIENT*) malloc(sizeof(struct FDCLIENT));
if (fdclient == NULL) {
printf("malloc fail, in %s, at %d\n", __FILE__, __LINE__);
clientlisthead = clientlisthead->tail;
tapclient->tail = remainclientlisthead;
remainclientlisthead = tapclient;
close(fd);
return -10;
}
}
tapclient->fdclient = fdclient;
fdclient->fd = fd;
fdclient->tls = NULL;
fdclient->tlsconnected = 0;
fdclient->watch = 0;
fdclient->client = tapclient;
if (addtoepoll(fdclient, EPOLLIN)) {
printf("tapfd add to epoll fail, in %s, at %d\n", __FILE__, __LINE__);
fdclient->tail = remainfdclienthead;
remainfdclienthead = fdclient;
clientlisthead = clientlisthead->tail;
tapclient->tail = remainclientlisthead;
remainclientlisthead = tapclient;
close(fd);
return -11;
}
fdclient->watch = 1;
return 0;
}
int create_socketfd () {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
struct sockaddr_in sin;
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
printf("run socket function is fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
return -1;
}
unsigned int socksval = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &socksval, sizeof(socksval))) {
printf("set socket reuseaddr fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -2;
}
socksval = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &socksval, sizeof(socksval))) {
printf("set socket reuseport fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -3;
}
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET; // ipv4
sin.sin_addr.s_addr = INADDR_ANY; // 本机任意ip
sin.sin_port = htons(c.bindport);
if (bind(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
printf("bind port %d fail, fd:%d, in %s, at %d\n", c.bindport, fd, __FILE__, __LINE__);
close(fd);
return -4;
}
if (listen(fd, MAX_CONNECT) < 0) {
printf("listen port %d fail, fd:%d, in %s, at %d\n", c.bindport, fd, __FILE__, __LINE__);
close(fd);
return -5;
}
if (remainfdclienthead) { // 有存货,直接拿出来用
fdserver = remainfdclienthead;
remainfdclienthead = remainfdclienthead->tail;
} else { // 没有存货,malloc一个
fdserver = (struct FDCLIENT*) malloc(sizeof(struct FDCLIENT));
if (fdserver == NULL) {
printf("malloc fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -6;
}
}
fdserver->fd = fd;
fdserver->watch = 0;
fdserver->client = NULL;
if (addtoepoll(fdserver, EPOLLIN)) {
printf("serverfd add to epoll fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
fdserver->tail = remainfdclienthead;
remainfdclienthead = fdserver;
close(fd);
return -7;
}
fdserver->watch = 1;
return 0;
}
int addclient (int serverfd) {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
struct sockaddr_in sin;
socklen_t in_addr_len = sizeof(struct sockaddr_in);
int fd = accept(serverfd, (struct sockaddr*)&sin, &in_addr_len);
if (fd < 0) {
printf("accept a new fd fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
return -1;
}
printf("new socket:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
if (setnonblocking(fd) < 0) { // 设置为非阻塞IO
printf("set nonblocking fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -2;
}
unsigned int socksval = 1;
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (unsigned char*)&socksval, sizeof(socksval))) { // 关闭Nagle协议
printf("close Nagle protocol fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -3;
}
if (c.tcpkeepalive) {
socksval = 1;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (unsigned char*)&socksval, sizeof(socksval))) { // 启动tcp心跳包
printf("set socket keepalive fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -4;
}
socksval = c.tcpkeepidle;
if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, (unsigned char*)&socksval, sizeof(socksval))) { // 设置tcp心跳包参数
printf("set socket keepidle fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -5;
}
socksval = c.tcpkeepintvl;
if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, (unsigned char*)&socksval, sizeof(socksval))) { // 设置tcp心跳包参数
printf("set socket keepintvl fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -6;
}
socksval = c.tcpkeepcnt;
if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, (unsigned char*)&socksval, sizeof(socksval))) { // 设置tcp心跳包参数
printf("set socket keepcnt fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -7;
}
}
// 修改发送缓冲区大小
socklen_t socksval_len = sizeof(socksval);
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (unsigned char*)&socksval, &socksval_len)) {
printf("get send buffer fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -8;
}
printf("old send buffer is %d, socksval_len:%d, fd:%d, in %s, at %d\n", socksval, socksval_len, fd, __FILE__, __LINE__);
socksval = MAXDATASIZE;
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (unsigned char*)&socksval, sizeof (socksval))) {
printf("set send buffer fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -9;
}
socksval_len = sizeof(socksval);
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (unsigned char*)&socksval, &socksval_len)) {
printf("get send buffer fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -10;
}
printf("new send buffer is %d, socksval_len:%d, fd:%d, in %s, at %d\n", socksval, socksval_len, fd, __FILE__, __LINE__);
// 修改接收缓冲区大小
socksval_len = sizeof(socksval);
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (unsigned char*)&socksval, &socksval_len)) {
printf("get receive buffer fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -11;
}
printf("old receive buffer is %d, socksval_len:%d, fd:%d, in %s, at %d\n", socksval, socksval_len, fd, __FILE__, __LINE__);
socksval = MAXDATASIZE;
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&socksval, sizeof(socksval))) {
printf("set receive buffer fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -12;
}
socksval_len = sizeof(socksval);
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (unsigned char*)&socksval, &socksval_len)) {
printf("get receive buffer fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -13;
}
printf("new receive buffer is %d, socksval_len:%d, fd:%d, in %s, at %d\n", socksval, socksval_len, fd, __FILE__, __LINE__);
struct FDCLIENT *fdclient;
if (remainfdclienthead) { // 有存货,直接拿出来用
fdclient = remainfdclienthead;
remainfdclienthead = remainfdclienthead->tail;
} else { // 没有存货,malloc一个
fdclient = (struct FDCLIENT*) malloc(sizeof(struct FDCLIENT));
if (fdclient == NULL) {
printf("malloc new fdclient object fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
close(fd);
return -14;
}
}
fdclient->fd = fd;
fdclient->watch = 0;
fdclient->client = NULL;
uint32_t flags;
if (ctx) {
SSL *tls = SSL_new(ctx);
if (tls == NULL) {
printf("SSL new fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
fdclient->tail = remainfdclienthead;
remainfdclienthead = fdclient;
close(fd);
return -15;
}
if (SSL_set_fd(tls, fd) != 1) {
printf("SSL set fd fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
fdclient->tail = remainfdclienthead;
remainfdclienthead = fdclient;
SSL_shutdown(tls);
SSL_free(tls);
close(fd);
return -16;
}
fdclient->tls = tls;
int r_code = SSL_accept(tls);
if (r_code != 1) {
int errcode = SSL_get_error(tls, r_code);
if (errcode == SSL_ERROR_WANT_WRITE) {
flags = EPOLLOUT;
} else if (errcode == SSL_ERROR_WANT_READ) {
flags = EPOLLIN;
} else {
perror("tls connect error");
printf("errno:%d, errcode:%d, in %s, at %d\n", errno, errcode, __FILE__, __LINE__);
fdclient->tail = remainfdclienthead;
remainfdclienthead = fdclient;
SSL_shutdown(tls);
SSL_free(tls);
close(fd);
return -17;
}
fdclient->tlsconnected = 0;
} else {
fdclient->tlsconnected = 1;
flags = EPOLLIN;
}
} else {
fdclient->tls = NULL;
fdclient->tlsconnected = 0;
flags = EPOLLIN;
}
if (addtoepoll(fdclient, flags)) {
printf("add to epoll fail, fd:%d, in %s, at %d\n", fd, __FILE__, __LINE__);
fdclient->tail = remainfdclienthead;
remainfdclienthead = fdclient;
if (fdclient->tls) {
SSL_shutdown(fdclient->tls);
SSL_free(fdclient->tls);
}
close(fd);
return -18;
}
fdclient->watch = 1;
return 0;
}
int removeclient (struct FDCLIENT *fdclient) {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
if (!fdclient->watch) {
return 0;
}
if (epoll_ctl(epollfd, EPOLL_CTL_DEL, fdclient->fd, NULL)) {
printf("errno: %d, fd:%d, in %s, at %d\n", errno, fdclient->fd, __FILE__, __LINE__);
perror("EPOLL CTL DEL fail");
}
fdclient->watch = 0;
if (fdclient->tls) {
SSL_shutdown(fdclient->tls);
SSL_free(fdclient->tls);
}
struct CLIENTLIST *client = fdclient->client;
close(fdclient->fd);
if (client) { // 已经注册成功
if (client->mac[0] != 0x00 || client->mac[1] != 0x00 || client->mac[2] != 0x00 || client->mac[3] != 0x00 || client->mac[4] != 0x00 || client->mac[5] != 0x00) { // 已经学习完毕
if (client->hashhead) {
client->hashhead->hashtail = client->hashtail;
} else {
machashlist[256 * client->mac[4] + client->mac[5]] = client->hashtail;
}
if (client->hashtail) {
client->hashtail->hashhead = client->hashhead;
}
}
struct PACKAGELIST *package = client->packagelisthead;
while (package) {
struct PACKAGELIST *tmppackage = package;
package = package->tail;
tmppackage->tail = remainpackagelisthead;
remainpackagelisthead = tmppackage;
}
if (client->head) {
client->head->tail = client->tail;
} else {
clientlisthead = client->tail;
}
if (client->tail) {
client->tail->head = client->head;
}
client->tail = remainclientlisthead;
remainclientlisthead = client;
printf("host %02x:%02x:%02x:%02x:%02x:%02x disconnect, in %s, at %d\n", client->mac[0], client->mac[1], client->mac[2], client->mac[3], client->mac[4], client->mac[5], __FILE__, __LINE__);
}
fdclient->tail = remainfdclienthead;
remainfdclienthead = fdclient;
if (client == tapclient) { // 基本不可能情况
printf("errno: %d, exit 0, in %s, at %d\n", errno, __FILE__, __LINE__);
perror("tap driver lose");
exit(0);
}
return 0;
}
int writenode (struct CLIENTLIST *writeclient) {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
for (struct CLIENTLIST *client = writeclient ; client != NULL ; client = client->writetail) {
struct PACKAGELIST *package = client->packagelisthead;
client->packagelisthead = NULL;
while (package) {
ssize_t len;
if (client->fdclient->tls) {
len = SSL_write(client->fdclient->tls, package->data, package->size);
if (len < 0) {
client->fdclient->tlsconnected = 0;
int errcode = SSL_get_error(client->fdclient->tls, len);
if (errcode == SSL_ERROR_WANT_READ) {
if (modepoll(client->fdclient, EPOLLIN)) {
perror("modify epoll error");
printf("fd:%d, errno:%d, ssl errcode:%d, in %s, at %d\n", client->fdclient->fd, errno, errcode, __FILE__, __LINE__);
removeclient(client->fdclient);
break;
}
client->packagelisthead = package;
break;
} else if (errcode == SSL_ERROR_WANT_WRITE) {
client->packagelisthead = package;
break;
} else {
printf("fd:%d, errno:%d, ssl errcode:%d, in %s, at %d\n", client->fdclient->fd, errno, errcode, __FILE__, __LINE__);
removeclient(client->fdclient);
break;
}
}
} else {
len = write(client->fdclient->fd, package->data, package->size);
}
if (len < 0) {
if (errno != EAGAIN) {
printf("errno:%d, in %s, at %d\n", errno, __FILE__, __LINE__);
if (client == tapclient) {
perror("tap write error");
} else {
perror("socket write error");
}
removeclient(client->fdclient);
break;
}
client->packagelisthead = package;
if (client->canwrite) { // 之前缓冲区是可以写入的,现在不行了
if (modepoll(client->fdclient, EPOLLIN | EPOLLOUT)) { // 监听可写事件
printf("modepoll fail, in %s, at %d\n", __FILE__, __LINE__);
removeclient(client->fdclient);
break;
}
client->canwrite = 0;
}
break;
}
if (len < package->size) { // 缓冲区不足,已无法继续写入数据。
unsigned int size = package->size - len;
memcpy(package->data, package->data + len, size);
package->size = size;
client->packagelisthead = package;
if (client->canwrite) { // 之前缓冲区是可以写入的,现在不行了
if (modepoll(client->fdclient, EPOLLIN | EPOLLOUT)) { // 监听可写事件
printf("modepoll fail, in %s, at %d\n", __FILE__, __LINE__);
removeclient(client->fdclient);
break;
}
client->canwrite = 0;
}
break;
}
if (client->canwrite == 0) { // 缓冲区尚有空间,并且之前已经提示不足
if (modepoll(client->fdclient, EPOLLIN)) { // 取消监听可写事件
printf("modepoll fail, in %s, at %d\n", __FILE__, __LINE__);
client->packagelisthead = package;
removeclient(client->fdclient);
break;
}
client->canwrite = 1;
}
struct PACKAGELIST *tmppackage = package;
package = package->tail;
tmppackage->tail = remainpackagelisthead;
remainpackagelisthead = tmppackage;
}
}
return 0;
}
struct CLIENTLIST* broadcastdata (struct CLIENTLIST *sourceclient, unsigned char *buff, int32_t packagesize) {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
struct CLIENTLIST *writeclient = NULL;
for (struct CLIENTLIST *targetclient = clientlisthead ; targetclient != NULL ; targetclient = targetclient->tail) {
if (targetclient == sourceclient) {
continue;
}
struct PACKAGELIST *package;
if (remainpackagelisthead) { // 全局数据包回收站不为空
package = remainpackagelisthead;
remainpackagelisthead = remainpackagelisthead->tail;
} else {
package = (struct PACKAGELIST*) malloc(sizeof(struct PACKAGELIST));
if (package == NULL) {
printf("malloc fail, in %s, at %d\n", __FILE__, __LINE__);
continue;
}
}
if (targetclient == tapclient) {
memcpy(package->data, buff + 2, packagesize - 2);
package->size = packagesize - 2;
} else {
memcpy(package->data, buff, packagesize);
package->size = packagesize;
}
package->tail = NULL;
if (targetclient->packagelisthead == NULL) {
targetclient->packagelisthead = package;
targetclient->packagelisttail = targetclient->packagelisthead;
} else {
targetclient->packagelisttail->tail = package;
targetclient->packagelisttail = targetclient->packagelisttail->tail;
}
if (targetclient->canwrite) {
targetclient->writetail = writeclient;
writeclient = targetclient;
}
}
return writeclient;
}
int checkkeys (char *key) {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
struct KEYS *keys = c.keys;
while (keys != NULL) {
if (!strcmp(keys->key, key)) {
return 1;
}
keys = keys->tail;
}
return 0;
}
int readdata (struct FDCLIENT *fdclient) {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
static unsigned char *readbuff = NULL; // 这里是用于存储全部的需要写入的数据buf,
static unsigned int maxtotalsize = 0;
struct CLIENTLIST *sourceclient = fdclient->client;
ssize_t len;
if (sourceclient == tapclient) { // tap驱动,原始数据,需要自己额外添加数据包长度。
len = read(fdclient->fd, readbuf + 2, MAXDATASIZE); // 这里最大只可能是1514
if (len < 0) {
if (errno != EAGAIN) {
printf("errno:%d, in %s, at %d\n", errno, __FILE__, __LINE__);
perror("tap read error");
removeclient(fdclient);
}
return -1;
}
readbuf[0] = len >> 8;
readbuf[1] = len & 0xff;
len += 2;
} else { // 网络套接字。
if (fdclient->tls) {
len = SSL_read(fdclient->tls, readbuf, MAXDATASIZE);
if (len < 0) {
fdclient->tlsconnected = 0;
int errcode = SSL_get_error(fdclient->tls, len);
if (errcode == SSL_ERROR_WANT_WRITE) {
if (modepoll(fdclient, EPOLLOUT)) {
printf("modify epoll fd fail, in %s, at %d\n", __FILE__, __LINE__);
printf("fd:%d, errno:%d, ssl errcode:%d, in %s, at %d\n", fdclient->fd, errno, errcode, __FILE__, __LINE__);
removeclient(fdclient);
return -1;
}
return 0;
} else if (errcode == SSL_ERROR_WANT_READ) {
return 0;
} else {
removeclient(fdclient);
printf("fd:%d, errno:%d, ssl errcode:%d, in %s, at %d\n", fdclient->fd, errno, errcode, __FILE__, __LINE__);
return -2;
}
}
} else {
len = read(fdclient->fd, readbuf, MAXDATASIZE);
}
if (len < 0) {
if (errno != EAGAIN) {
printf("errno:%d, in %s, at %d\n", errno, __FILE__, __LINE__);
perror("socket read error");
removeclient(fdclient);
}
return -3;
}
}
readbuf[len] = '\0';
if (sourceclient == NULL) { // 用户没有找到
if (remainclientlisthead) {
sourceclient = remainclientlisthead;
remainclientlisthead = remainclientlisthead->tail;
} else {
sourceclient = (struct CLIENTLIST*) malloc(sizeof(struct CLIENTLIST));
if (sourceclient == NULL) {
printf("malloc fail, in %s, at %d\n", __FILE__, __LINE__);
removeclient(fdclient);
return -4;
}
}
memset(sourceclient->mac, 0, 6);
sourceclient->fdclient = fdclient;
sourceclient->remainsize = 0;
sourceclient->canwrite = 1;
struct PACKAGELIST *package;
if (remainpackagelisthead) { // 全局数据包回收站不为空
package = remainpackagelisthead;
remainpackagelisthead = remainpackagelisthead->tail;
} else {
package = (struct PACKAGELIST*) malloc(sizeof(struct PACKAGELIST));
if (package == NULL) {
printf("malloc fail, in %s, at %d\n", __FILE__, __LINE__);
return -5;
}
}
package->tail = NULL;
sourceclient->packagelisthead = package;
unsigned char *headend = strstr(readbuf, "\r\n\r\n");
unsigned char httppath[256];
unsigned char httpprot[16];
if (sscanf(readbuf, "GET %s %s\r\n", httppath, httpprot) != 2 || strcmp(httpprot, "HTTP/1.1") || headend == NULL) {
time_t now;
time(&now);
int len = strftime(package->data, MTU_SIZE+14, PAGE400, gmtime(&now));
package->size = len;
sourceclient->writetail = NULL;
writenode(sourceclient);
removeclient(fdclient);
return -6;
}
*headend = '\0'; // 给http头部结尾添加字符串结束符,不解析body。
unsigned char *key = strstr(readbuf, "Authorization: ");
if (key != NULL) {
key = key + 15;
unsigned char *keyend = strchr(key, '\r');
if (keyend != NULL) {
*keyend = '\0';
}
}
if (key == NULL || strcmp(httppath, c.httppath) || checkkeys(key)) {
time_t now;
time(&now);
int len = strftime(package->data, MTU_SIZE+14, PAGE404, gmtime(&now));
package->size = len;
sourceclient->writetail = NULL;
writenode(sourceclient);
removeclient(fdclient);
return -7;
}
memcpy(package->data, PAGE101, sizeof(PAGE101)-1);
package->size = sizeof(PAGE101)-1;
sourceclient->writetail = NULL;
writenode(sourceclient);
if (clientlisthead) {
clientlisthead->head = sourceclient;
}
sourceclient->head = NULL;
sourceclient->tail = clientlisthead;
clientlisthead = sourceclient;
fdclient->client = sourceclient;
printf("add client success, fd:%d, in %s, at %d\n", sourceclient->fdclient->fd, __FILE__, __LINE__);
return 0;
}
unsigned int offset = 0;
unsigned int totalsize;
unsigned char *buff;
if (sourceclient->remainsize > 0) {
totalsize = sourceclient->remainsize + len;
if (totalsize > maxtotalsize) {
if (maxtotalsize > 0) {
free(readbuff);
}
readbuff = (unsigned char*) malloc(totalsize * sizeof(unsigned char));
if (readbuff == NULL) {
printf("malloc fail, in %s, at %d\n", __FILE__, __LINE__);
maxtotalsize = 0;
return -9;
}
maxtotalsize = totalsize;
}
memcpy(readbuff, sourceclient->remainpackage, sourceclient->remainsize);
memcpy(readbuff + sourceclient->remainsize, readbuf, len);
sourceclient->remainsize = 0;
buff = readbuff;
} else {
totalsize = len;
buff = readbuf;
}
struct CLIENTLIST *writeclient = NULL;
while (offset < totalsize) {
if (offset + 60 > totalsize) { // mac帧单个最小必须是60个,小于这个的数据包一定不完整
int remainsize = totalsize - offset;
memcpy(sourceclient->remainpackage, buff + offset, remainsize);
sourceclient->remainsize = remainsize;
break;
}
unsigned int packagesize = 256 * buff[offset] + buff[offset+1] + 2; // 当前数据帧大小
if (offset + packagesize > totalsize) {
int remainsize = totalsize - offset;
memcpy(sourceclient->remainpackage, buff + offset, remainsize);
sourceclient->remainsize = remainsize;
break;
}
// 自学习算法开始
unsigned int srchash = 256 * buff[offset+12] + buff[offset+13];
if (sourceclient != machashlist[srchash]) {
if (sourceclient->mac[0] != 0x00 || sourceclient->mac[1] != 0x00 || sourceclient->mac[2] != 0x00 || sourceclient->mac[3] != 0x00 || sourceclient->mac[4] != 0x00 || sourceclient->mac[5] != 0x00) {
unsigned int oldhash = 256 * sourceclient->mac[5] + sourceclient->mac[6];
if (sourceclient->hashhead) {
sourceclient->hashhead->hashtail = sourceclient->hashtail;
} else {
machashlist[oldhash] = sourceclient->hashtail;
}
if (sourceclient->hashtail) {
sourceclient->hashtail->hashhead = sourceclient->hashhead;
}
sourceclient->hashhead = NULL;
}
if (machashlist[srchash]) {
machashlist[srchash]->hashhead = sourceclient;
}
sourceclient->hashtail = machashlist[srchash];
machashlist[srchash] = sourceclient;
}
memcpy(sourceclient->mac, buff + offset + 8, 6);
// 自学习算法结束
unsigned char targetmac[6];
memcpy(targetmac, buff + offset + 2, 6);
if (targetmac[0] == 0xff && targetmac[1] == 0xff && targetmac[2] == 0xff && targetmac[3] == 0xff && targetmac[4] == 0xff && targetmac[5] == 0xff) { // 广播帧
writeclient = broadcastdata(sourceclient, buff + offset, packagesize);
offset += packagesize;
continue;
}
unsigned int dsthash = 256 * buff[offset+6] + buff[offset+7];
struct CLIENTLIST *targetclient;
for (targetclient = machashlist[dsthash] ; targetclient != NULL ; targetclient = targetclient->tail) {
if (!memcmp(targetclient->mac, targetmac, 4)) {
break;
}
}
if (targetclient == NULL) { // mac地址表中不存在,使用广播策略
writeclient = broadcastdata(sourceclient, buff + offset, packagesize);
offset += packagesize;
continue;
}
struct PACKAGELIST *package;
if (remainpackagelisthead) { // 全局数据包回收站不为空
package = remainpackagelisthead;
remainpackagelisthead = remainpackagelisthead->tail;
} else {
package = (struct PACKAGELIST*) malloc(sizeof(struct PACKAGELIST));
if (package == NULL) {
printf("malloc fail, in %s, at %d\n", __FILE__, __LINE__);
offset += packagesize;
continue;
}
}
if (targetclient == tapclient) {
memcpy(package->data, buff + offset + 2, packagesize - 2);
package->size = packagesize - 2;
} else {
memcpy(package->data, buff + offset, packagesize);
package->size = packagesize;
}
package->tail = NULL;
if (targetclient->packagelisthead == NULL) {
targetclient->packagelisthead = package;
targetclient->packagelisttail = targetclient->packagelisthead;
} else {
targetclient->packagelisttail->tail = package;
targetclient->packagelisttail = targetclient->packagelisttail->tail;
}
if (!targetclient->canwrite) {
offset += packagesize;
continue;
}
struct CLIENTLIST *tmpclient;
for (tmpclient = writeclient ; tmpclient != NULL ; tmpclient = tmpclient->writetail) {
if (tmpclient == targetclient) {
break;
}
}
if (tmpclient == NULL) {
targetclient->writetail = writeclient;
writeclient = targetclient;
}
offset += packagesize;
}
writenode(writeclient);
return 0;
}
int parseconfigfile () {
#ifdef EXCEPTION_DEBUG
addTrace(__func__, sizeof(__func__));
#endif
int fd = open("config.json", O_RDONLY);
if (fd < 0) {
printf("open config.json fail, in %s, at %d\n", __FILE__, __LINE__);
return -1;
}
ssize_t len = read(fd, readbuf, MAXDATASIZE);
close(fd);
if (len < 0) {
printf("read config.json fail, in %s, at %d\n", __FILE__, __LINE__);
return -2;
}
readbuf[len] = '\0';
yyjson_doc *doc = yyjson_read(readbuf, len, YYJSON_READ_ALLOW_COMMENTS);
yyjson_val *root = yyjson_doc_get_root(doc);