-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforwarder.c
1180 lines (949 loc) · 23.2 KB
/
forwarder.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
#define FORWARDER
#define MSG_DF 0x80
#define MSG_MF 0x40
#define MSG_OFFSET 0x3F
#define PMTU 64
#define TRUE 1
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <sys/time.h>
#include <pthread.h>
#include "msg.h"
int s;
struct sockaddr_in adr_inet;
u32 mqfrag_hash_rnd;
int mtu = PMTU;
u16 msg_id = 0;
u32 local_ip;
int countdown;
int shuffle;
int drop;
int main(int argc, char **argv){
int z;
char *srvr_addr = NULL;
struct sockaddr_in adr_clnt;
int len_inet;
int portnumber;
u8 buffer[BUFSIZE];
struct msg_buff *p_mbuf;
struct list_head *pos;
u8 *data;
struct sigaction sigint_action;
if(argc < 3){
fprintf(stderr, "Usage: %s ip portnumber\n", argv[0]);
exit(1);
} else {
srvr_addr=argv[1];
if((portnumber = atoi(argv[2])) <= 0) {
fprintf(stderr, "Usage: %s ip portnumber\n", argv[0]);
exit(1);
}
}
read_conf();
local_ip = get_localip();
s = socket(PF_INET, SOCK_DGRAM, 0);
if(s == -1)
bail("socket()", EXIT);
memset(&adr_inet, 0, sizeof adr_inet);
adr_inet.sin_family=PF_INET;
adr_inet.sin_port=htons(portnumber);
if(!inet_aton(srvr_addr, &adr_inet.sin_addr))
bail("bad address.", EXIT);
len_inet=sizeof(adr_inet);
z = bind(s, (struct sockaddr *)&adr_inet, len_inet);
if(z == -1)
bail("bind()", EXIT);
int err = pthread_create(&tid, NULL, worker_run, NULL);
if(err != 0)
bail("can't create worker thread!", EXIT);
for(;;){
z = recvfrom(s,
buffer,
sizeof buffer,
0,
(struct sockaddr *)&adr_clnt,
&len_inet);
if(z < 0)
bail("recvfrom()", !EXIT);
if((data = malloc(z)) != NULL)
memcpy(data, buffer, z);
else
bail("can't malloc for this udp message!", !EXIT);
if((p_mbuf = malloc(sizeof(MBUF))) != NULL){
memset(p_mbuf, 0, sizeof(MBUF));
p_mbuf->data = data;
p_mbuf->msg_len = z;
memcpy(&p_mbuf->sk_addr, &adr_clnt, sizeof(adr_clnt));
pthread_mutex_lock(&mqlock_input);
list_add_tail(&p_mbuf->list, &input_msg_queue);
pthread_mutex_unlock(&mqlock_input);
pthread_cond_signal(&mqlock_input_ready);
}else {
bail("can't malloc MBUF!", !EXIT);
sleep(10);
}
}
out:
pthread_join(tid, NULL);
close(s);
return 0;
}
void *worker_run(void *arg)
{
struct list_head *pos;
struct msg_buff *p_mbuf = NULL;
mqfrag_hash_rnd = ranged_rand(1, RAND_MAX);
for(;;) {
pthread_mutex_lock(&mqlock_input);
while(list_empty(&input_msg_queue))
pthread_cond_wait(&mqlock_input_ready, &mqlock_input);
pos = (&input_msg_queue)->next;
p_mbuf = list_entry(pos, MBUF, list);
list_del((&input_msg_queue)->next);
pthread_mutex_unlock(&mqlock_input);
msg_rcv(p_mbuf);
}
pthread_exit((void *)0);
}
/*
* 消息处理函数
*/
int msg_rcv(struct msg_buff *p_mbuf)
{
struct msg_buff *msg = NULL;
struct msg_hdr *p_mh;
struct app_hdr *p_ah;
int len;
p_mh = (struct msg_hdr *)p_mbuf->data;
if(p_mh->ver != 1 || p_mbuf->msg_len <= p_mh->hlen)
return BAD_MSG;
if(p_mh->dest != local_ip)
return msg_forward(p_mbuf);
msg = do_msg_defrag(p_mbuf);
if(!msg)
return 0;
if(msg)
memcpy(&msg->sk_addr, &p_mbuf->sk_addr, sizeof(struct sockaddr_in));
p_ah = (struct app_hdr *)(msg->data + HLEN);
return (parse_app_data(msg, p_ah));
}
/*
* 根据消息类型设置对应的消息处理程序
*
* @p_mbuf, 消息结构指针
* @p_ah, 应用层数据指针
*/
int parse_app_data(struct msg_buff *p_mbuf, struct app_hdr *p_ah)
{
int app_len;
app_len = p_mbuf->msg_len - HLEN;
if(p_ah->len != app_len) {
bail("but app data is corrupted!", !EXIT);
goto out;
}
switch(p_ah->type)
{
case LOGIN_MSG:
appl.handler = app_do_login;
break;
case EXIT_MSG:
appl.handler = app_do_exit;
break;
default:
return UNKNOWN_APP;
}
return (appl.handler(p_mbuf, p_ah));
out:
free_msg_buff(p_mbuf);
return DEFRAG_FAIL;
}
/*
* 处理收到的用户登录消息
*
* @p_mbuff 重组好的"登录消息"业务数据报结构体
* @p_ah 这个数据报的app头部结构体
*/
int app_do_login(struct msg_buff * p_mbuf, struct app_hdr *p_ah)
{
struct list_head *pos;
struct usermap *p_usrmap, *p_who;
struct login *p_login;
u32 ip;
u32 ip_net;
u16 port;
u8 who[NAMESIZE];
u8 user_list[BUFSIZE];
u8 *where;
u8 sndbuf[BUFSIZE];
int nfrag, len, mlen, msg_len;
struct msg_hdr *mh;
int FOUND = 0;
p_login = (struct login *)p_ah;
where = &user_list[0];
memset(user_list, 0, sizeof(user_list));
memcpy(who, p_login->user, NAMESIZE);
ip = ntohl(p_mbuf->sk_addr.sin_addr.s_addr);
port = ntohs(p_mbuf->sk_addr.sin_port);
p_who = map_username(who, ip, port);
list_for_each_after_first(pos, &user_queue) {
p_usrmap = list_entry(pos, USERMAP, list);
memcpy(where, p_usrmap->name, NAMESIZE);
where += NAMESIZE;
/* 添加对应的用户IP信息 */
ip_net = htonl(p_usrmap->ip);
memcpy(where, &ip_net, IP_SIZE);
where += IP_SIZE;
len = make_fwd_login_msg(sndbuf, who, ip);
nfrag = append_data(mtu, sndbuf, len);
msg_fragment(nfrag, local_ip, htonl(p_usrmap->ip));
send_fragments(&frag_queue, p_usrmap);
free_msg_queue(&frag_queue, &mqlock_frag);
}
memset(sndbuf, 0, BUFSIZE);
mlen = where - &user_list[0];
len = make_reply_msg(sndbuf, who, ip, user_list, mlen);
nfrag = append_data(mtu, sndbuf, len);
msg_fragment(nfrag, local_ip, htonl(p_who->ip));
send_fragments(&frag_queue, p_who);
free_msg_queue(&frag_queue, &mqlock_frag);
#ifdef TEST
list_for_each(pos, &user_queue) {
p_usrmap = list_entry(pos, USERMAP, list);
printf("name=%s ip=%x port=%d login.\n", p_usrmap->name, p_usrmap->ip, p_usrmap->port);
}
#endif
return 0;
}
/*
* 构造登录应答消息REPLY_MSG
*
* @sndbuf, 消息缓存
* @who, 登录应答消息的目标用户
* @uip, 目标用户IP
* @user_list, 已经登录用户名-IP
* @len, user_list长度
*
* return 消息长度
*/
int make_reply_msg(u8 *sndbuf, u8 *who, u32 uip, u8 *user_list, int len)
{
struct login_reply *p_rep;
u32 ip;
ip = htonl(uip);
p_rep = (struct login_reply *)sndbuf;
p_rep->h.type = REPLY_MSG;
p_rep->h.len = sizeof(struct login_reply) + len;
memcpy(p_rep->user, who, NAMESIZE);
p_rep->ip = ip;
if(len)
memcpy(p_rep->others, user_list, len);
return p_rep->h.len;
}
/*
* 构造登录转发消息FWD_LOGIN_MSG
*
* @sndbuf, 消息缓存
* @who, 正在登录的用户
* @uip, 正在登录的用户IP
*
* return 消息长度
*/
int make_fwd_login_msg(u8 *sndbuf, u8 *who, u32 uip)
{
struct login_fwd * p_fwd;
u32 ip;
ip = htonl(uip);
p_fwd = (struct login_fwd *)sndbuf;
p_fwd->h.type = FWD_LOGIN_MSG;
p_fwd->h.len = sizeof(struct login_fwd);
memcpy(p_fwd->user, who, NAMESIZE);
p_fwd->ip = ip;
return p_fwd->h.len;
}
/*
* 预分段函数
*
* @len, 上层业务逻辑数据长度
* @mtu, 包含分段重组层得最大传输单元
* @from, 被分段的数据起始地址
*
* return 分段段数
*/
int append_data(int mtu, u8 *from, int len)
{
int n;
int maxfraglen;
int fragheaderlen;
int puredatalen;
int left;
int copy;
struct msg_buff *p_mbuf;
left = len;
fragheaderlen = HLEN;
maxfraglen = ((mtu - fragheaderlen) & ~3) + fragheaderlen;
puredatalen = maxfraglen - fragheaderlen;
n = 0;
while(left > 0){
n++;
copy = min(puredatalen, left);
if((p_mbuf = malloc(sizeof(MBUF))) != NULL){
if((p_mbuf->data = malloc(copy + fragheaderlen)) != NULL){
memset(p_mbuf->data, 0, copy + fragheaderlen);
memcpy(p_mbuf->data + fragheaderlen, from, copy);
p_mbuf->msg_len = copy;
/* 分段入队列frag_queue */
list_add_tail(&p_mbuf->list, &frag_queue);
}else
bail("can't malloc for this frag message!", EXIT);
}else
bail("can't malloc for this p_mbuf structure!", EXIT);
left -= copy;
from +=copy;
}
return n;
}
/*
* 为分段添加分段重组层头部
*
* @num, 分段个数
* @dest, 消息接收方IP
*
* return 可供使用的下一个消息ID
*/
int msg_fragment(int num, u32 src, u32 dest)
{
struct list_head *pos;
struct msg_buff *p_mbuf = NULL;
struct msg_hdr *mh;
int prev_frag_len;
int offset = 0;
int i = 0;
pos = (&frag_queue)->next;
p_mbuf = list_entry(pos, MBUF, list);
mh = (struct msg_hdr *)p_mbuf->data;
mh->ver = 1;
mh->hlen = HLEN;
mh->src = src;
mh->dest = dest;
mh->frag_off = MSG_MF;
mh->protocol = 0x1;
mh->id = msg_id;
prev_frag_len = p_mbuf->msg_len;
i++;
list_for_each_after_first(pos, &frag_queue){
p_mbuf = list_entry(pos, MBUF, list);
mh = (struct msg_hdr *)p_mbuf->data;
mh->ver = 1;
mh->hlen = HLEN;
mh->src = src;
mh->dest = dest;
offset += prev_frag_len;
mh->frag_off = offset >> 2;
mh->protocol = 0x01;
mh->id = msg_id;
if(i != num - 1 )
mh->frag_off |= MSG_MF;
else
mh->frag_off &= MSG_OFFSET;
prev_frag_len = p_mbuf->msg_len;
i++;
}
if(i == 1)
mh->frag_off &= MSG_OFFSET;
return msg_id++;
}
/*
* 依次发送所有分段给接收方
*
* @queue, 分段队列
* @dest, 消息接收方
*/
void send_fragments(struct list_head *queue, struct usermap *dest)
{
struct list_head *pos;
struct sockaddr_in receiver;
u8 sndbuf[2 * BUFSIZE];
struct msg_buff *p_mbuf;
int len, sk_len;
u32 ip;
u16 port;
ip = dest->ip;
port = dest->port;
sk_len = sizeof(receiver);
memset(&receiver, 0, sk_len);
receiver.sin_family = PF_INET;
receiver.sin_addr.s_addr = htonl(ip);
receiver.sin_port = htons(port);
list_for_each(pos, queue){
p_mbuf = list_entry(pos, MBUF, list);
len = p_mbuf->msg_len + HLEN;
memcpy(sndbuf, p_mbuf->data, len);
send_msg(sndbuf, len, &receiver, sk_len);
}
}
void send_msg(u8 *msg, int len, struct sockaddr_in *dest, int sk_len)
{
int z;
z=sendto(s,
msg,
len,
0,
(struct sockaddr *)dest,
sk_len);
if(z<0)
bail("send_msg()", EXIT);
}
/*
* 建立用户名-IP-PORT映射表,若该用户名已经存在,则更新该
* 用户名对应IP和端口;若不存在该用户,则创建一个USERMAP
* 节点,并将该节点加入到user_hash表
*/
struct usermap *map_username(u8 *name, u32 ip, u16 port)
{
unsigned int hash;
struct hlist_node *n;
struct usermap *p_usrmap;
hash = BKDRHash(name) & (HQ_HASHSZ - 1);
hlist_for_each_entry(p_usrmap, n, &user_hash[hash], hlist) {
if(!strcmp(p_usrmap->name, name)){
p_usrmap->ip = ip;
p_usrmap->port = port;
list_move(&p_usrmap->list, &user_queue);
return p_usrmap;
}
}
if((p_usrmap = malloc(sizeof(USERMAP))) != NULL){
memcpy(p_usrmap->name, name, NAMESIZE);
p_usrmap->ip = ip;
p_usrmap->port = port;
hlist_add_head(&p_usrmap->hlist, &user_hash[hash]); /* 加入到user_hash */
list_add(&p_usrmap->list, &user_queue); /* 加入到user_queue */
return p_usrmap;
}else
bail("can't malloc USERMAP!", EXIT);
}
void free_msg_queue(struct list_head *q, pthread_mutex_t *lock)
{
struct msg_buff *pos;
struct msg_buff *prev = NULL;
if(lock)
pthread_mutex_lock(lock);
list_for_each_entry(pos, q, list){
if(prev){
free(prev->data);
free(prev);
}
prev = pos;
list_del(&pos->list);
}
if(prev){
free(prev);
}
pthread_mutex_unlock(lock);
}
void free_usermap_list(struct list_head *q, pthread_mutex_t *lock)
{
struct usermap *pos;
struct usermap *prev = NULL;
pthread_mutex_lock(lock);
list_for_each_entry(pos, q, list){
if(prev){
printf("user %s is going to be freed\n", prev->name);
free(prev);
}
prev = pos;
list_del(&pos->list);
hlist_del(&pos->hlist);
}
if(prev){
printf("user %s is going to be freed\n", prev->name);
free(prev);
}
}
/*
* 返回重组成功的消息
*
* @p_mbuf, 正在被处理的消息分段
* return 0, 重组成功
* return 1, 重组失败
*/
struct msg_buff * do_msg_defrag(struct msg_buff *p_mbuf)
{
struct mfq *p_mfq;
struct msg_hdr *p_mh;
struct msg_buff *ret = NULL;
u32 src_ip;
u32 dest_ip;
u8 protocol;
u16 id;
p_mh = (struct msg_hdr *)p_mbuf->data;
if((p_mfq = msg_find(p_mh)) != NULL){
msg_frag_queue(p_mfq, p_mbuf);
if (p_mfq->last_in == (FIRST_IN|LAST_IN) && p_mfq->meat == p_mfq->len)
/* 开始重组 */
ret = msg_frag_reasm(p_mfq);
if(ret)
free_mfq(p_mfq);
return ret;
}
return NULL;
}
void free_mfq(struct mfq *p_mfq)
{
struct msg_buff *fp;
hlist_del(&p_mfq->hlist);
list_del(&(p_mfq->timer.list));
list_del(&p_mfq->lru_list);
fp = p_mfq->frags;
while(fp){
struct msg_buff *xp = fp->next;
free_msg_buff(fp);
fp = xp;
}
free(p_mfq);
}
int ranged_rand(int low, int high)
{
srand((unsigned)time(NULL));
return (double)rand() / (unsigned int)(RAND_MAX +1) * (high - low) + low;
}
u32 BKDRHash(u8 *str)
{
unsigned int seed = 131;
unsigned int hash = 0;
while(*str){
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
/*
* 查找是否存在和输入的(@src, @dest, mh)相对应的mfq存在
*
* @p_mh, 当前分段的msg_hdr指针
*/
struct mfq * msg_find(struct msg_hdr *p_mh)
{
unsigned int hash;
struct hlist_node *n;
struct mfq *p_mfq;
u16 id;
u32 src;
u32 dest;
u8 protocol;
id = ntohs(p_mh->id);
protocol = p_mh->protocol;
src = ntohl(p_mh->src);
dest = ntohl(p_mh->dest);
hash = mqhashfn(id, src, dest, protocol);
hlist_for_each_entry(p_mfq, n, &mfq_hash[hash], hlist) {
if(p_mfq->src == src &&
p_mfq->dest == dest &&
p_mfq->protocol == protocol &&
p_mfq->id == id) {
return p_mfq;
}
}
return msg_frag_create(p_mh);
}
/*
* 将新收到的分段插入到对应的mfq的分段链表的正确位置
*
* @p_mbuf, 新收到的消息分段
*/
void msg_frag_queue(struct mfq *p_mfq, struct msg_buff *p_mbuf)
{
struct msg_buff *prev, *next;
struct msg_hdr *p_mh;
int flags, offset;
int end;
if (p_mfq->last_in & COMPLETE)
goto err;
p_mh = (struct msg_hdr *)p_mbuf->data;
offset = p_mh->frag_off;
flags = offset & ~MSG_OFFSET;
offset &= MSG_OFFSET;
offset <<= 2;
end = offset + p_mbuf->msg_len - p_mh->hlen;
if ((flags & MSG_MF) == 0) {
if(end < p_mfq->len || ((p_mfq->last_in & LAST_IN ) && end != p_mfq->len))
goto err;
p_mfq->last_in |= LAST_IN;
p_mfq->len = end;
}else {
if (end > p_mfq->len) {
if (p_mfq->last_in & LAST_IN)
goto err;
p_mfq->len = end;
}
}
prev = NULL;
for (next = p_mfq->frags; next != NULL; next = next->next) {
if(next->offset >= offset)
break;
prev = next;
}
if(prev) {
int i = (prev->offset + (prev->msg_len - p_mh->hlen)) - offset;
if (i > 0) {
offset += i;
if (end <= offset)
goto err;
p_mbuf->PREV_OLAP = i;
}
}
while (next && next->offset < end) {
int i = end - next->offset;
if (i < (next->msg_len - p_mh->hlen)) {
next->NEXT_OLAP = i;
next->offset += i;
p_mfq->meat -= i;
break;
} else {
struct msg_buff *free_it = next;
next = next->next;
if (prev)
prev->next = next;
else
p_mfq->frags = next;
p_mfq->meat -= MSG_DATA_LEN(free_it) - p_mh->hlen;
free_msg_buff(free_it);
}
}
p_mbuf->offset = offset;
p_mbuf->next = next;
if (prev)
prev->next = p_mbuf;
else
p_mfq->frags = p_mbuf;
p_mfq->meat += (p_mbuf->msg_len - p_mh->hlen);
if (offset == 0)
p_mfq->last_in |= FIRST_IN;
list_move_tail(&p_mfq->lru_list, &mq_lru_list);
return;
err:
free_msg_buff(p_mbuf);
}
/*
* 重组分段为原始分段
*/
struct msg_buff *msg_frag_reasm(struct mfq *p_mfq)
{
struct msg_hdr mh, *p_mh;
struct msg_buff *p_mbuf;
struct msg_buff *p_new;
struct msg_buff *head = p_mfq->frags;
int total_len, len;
int data_len;
int where;
memcpy(&mh, head->data, HLEN);
p_mh = (struct msg_hdr *)head->data;
total_len = p_mh->hlen + p_mfq->len;
len = p_mfq->len;
if (len > 255)
goto out_oversize;
where = p_mh->hlen;
if((p_new = malloc(sizeof(MBUF))) != NULL) {
if(!(p_new->data = malloc(total_len)))
bail("can not malloc for p_new->data in msg_frag_reasm()\n", EXIT);
/* 重组 */
for (p_mbuf = head; p_mbuf; p_mbuf = head) {
p_mh = (struct msg_hdr *)p_mbuf->data;
data_len = MSG_DATA_LEN(p_mbuf) - p_mh->hlen;
memcpy(p_new->data + where, (u8 *)p_mh + p_mh->hlen, data_len);
head = head->next;
where += data_len;
}
} else
bail("reasm malloc failed!\n", EXIT);
memcpy(p_new->data, &mh, HLEN);
p_new->msg_len = total_len;
p_mh = (struct msg_hdr *)p_new->data;
p_mh->frag_off = 0;
p_mfq->frags = NULL;
return p_new;
out_oversize:
return NULL;
}
/*
* 创建MFQ
*/
struct mfq * msg_frag_create(struct msg_hdr *p_mh)
{
struct mfq *p_mfq;
if((p_mfq = malloc(sizeof(MFQ))) != NULL) {
p_mfq->src = ntohl(p_mh->src);
p_mfq->dest = ntohl(p_mh->dest);
p_mfq->id = ntohs(p_mh->id);
p_mfq->protocol = p_mh->protocol;
p_mfq->last_in = 0;
p_mfq->len = 0;
p_mfq->frags = NULL;
p_mfq->meat = 0;
app_timer_register(&(p_mfq->timer), countdown, 0, expired_deal, p_mfq);
} else {
bail("mfq malloc failed!\n", !EXIT);
return NULL;
}
return msg_frag_intern(p_mfq);
}
struct mfq * msg_frag_intern(struct mfq *p_mfq)
{
unsigned int hash;
hash = mqhashfn(p_mfq->id, p_mfq->src, p_mfq->dest, p_mfq->protocol);
hlist_add_head(&p_mfq->hlist, &mfq_hash[hash]);
list_add_tail(&p_mfq->lru_list, &mq_lru_list);
return p_mfq;
}
u32 mqhashfn(u16 id, u32 saddr, u32 daddr, u8 prot)
{
return jhash_3words((u32)id << 16 | prot, (u32)saddr, (u32)daddr, mqfrag_hash_rnd) & (HQ_HASHSZ - 1);
}
u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
{
a += JHASH_GOLDEN_RATIO;
b += JHASH_GOLDEN_RATIO;
c += initval;
jhash_mix(a, b, c);
return c;
}
void free_msg_buff(struct msg_buff *p_buf)
{
free(p_buf->data);
free(p_buf);
}
/*
* 消息转发函数
*
* @p_mbuf, 消息
*/
int msg_forward(struct msg_buff *p_mbuf)
{
int nfrag,app_len;
u8 *p_ah;
u32 src_ip;
u32 dest_ip;
struct list_head *pos;
struct msg_hdr *p_mh;
struct usermap *p_usrmap;
p_mh = (struct msg_hdr *)p_mbuf->data;
app_len = p_mbuf->msg_len - HLEN;
src_ip = ntohl(p_mh->src);
dest_ip = ntohl(p_mh->dest);
p_ah = p_mbuf->data + HLEN;
nfrag = append_data(mtu, p_ah, app_len);
append_frag_head(nfrag, p_mh);
list_for_each(pos, &user_queue) {
p_usrmap = list_entry(pos, USERMAP, list);
if(p_usrmap->ip != dest_ip){
continue;
}else{
send_fragments(&frag_queue, p_usrmap);
break;
}
}
free_msg_queue(&frag_queue, &mqlock_frag);
free(p_mbuf);
return 0;
}
/*
* 给转发报文片段加上头部
*
* @num, 分段数目
* @p_mh, 指向当前分段头部的指针
*/
int append_frag_head(int num, struct msg_hdr *p_mh)
{
struct list_head *pos;
struct msg_buff *p_mbuf = NULL;
struct msg_hdr *old_mh;
struct msg_hdr *mh;
int prev_frag_len = 0;
int flags,offset;
int i = 0;
int not_last_frag;
offset = p_mh->frag_off;
flags = offset & ~MSG_OFFSET;
offset &= MSG_OFFSET;
offset <<= 2;
not_last_frag = flags & MSG_MF;
list_for_each(pos, &frag_queue){
p_mbuf = list_entry(pos, MBUF, list);
memcpy(p_mbuf->data, p_mh, HLEN);
mh = (struct msg_hdr *)p_mbuf->data;
prev_frag_len = p_mbuf->msg_len;
mh->frag_off = offset >> 2;
offset += prev_frag_len;
if(not_last_frag || i != num - 1)
mh->frag_off |= MSG_MF;
i++;
}
}
void expired_deal(unsigned int clientreg, void *clientarg)
{
struct mfq *mfq_del = clientarg;
write_log(mfq_del);
free_mfq(mfq_del);
}