summaryrefslogtreecommitdiff
path: root/src/extensions/pptp_protocol_plug.c
blob: dec2a54ff014e150ddd2d39c97f3813e46933aa3 (plain)
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
#ifdef __cplusplus
extern "C" {
#endif

#include "sapp_api.h"
#include "sapp_private_api.h"
#include "sapp_private_api.h"
#include "sapp_declaration.h"

const int pptp_protocol_version_VERSION_20170105 = 20170105;

extern void pptp_stream_free(struct streaminfo *pptp_stream);
/*
	����pptp������, 
	1)���ڻ�ȡȫ�ֲ��ID, ���洢��g_pptp_plugid;
	2)���������Ϊpptp, ���ڴ���һ��"pptp"���Э����������,
	  ������ҵ������Ҫ���ص�"pptp"��.
	3)���������pptp�Ŀ�������, ������������ؽ����߼�����deal_ppp.c��.

	���RFC: RFC2637.
*/

#define PPTP_CONTROL_LINK_PORT		(1723)

#define PPTP_CONTROL_LINK_MAGIC		(0x1A2B3C4D)

enum pptp_messge_type_t{
	PPTP_MESSAGE_CONTROL 	= 1,
	PPTP_MESSAGE_MANAGE 		= 2, /* RFC���嵫δʵ��ʹ�� */
};


struct pptp_control_out_go_req{
	unsigned short length; /* ȫ�����ݳ���, ������ͷ�� */
	unsigned short pptp_message_type;
	unsigned int magic_cookie;
	unsigned short control_message_type;	
	unsigned short reserved0;
	unsigned short call_id;
	unsigned short call_serial_num;
	unsigned int min_bps;
	unsigned int max_bps;
	unsigned int bearer_type;
	unsigned int framing_type;
	unsigned short pkt_rcv_win;
	unsigned short pkt_process_delay;
	unsigned short pbone_number_len;
	unsigned short reserved1;
	char phone_number[64];
	char subaddress[64];
};

struct pptp_control_out_go_reply{
	unsigned short length; /* ȫ�����ݳ���, ������ͷ�� */
	unsigned short pptp_message_type;
	unsigned int magic_cookie;
	unsigned short control_message_type;	
	unsigned short reserved0;
	unsigned short call_id;
	unsigned short peer_call_id;
	unsigned char result_code;
	unsigned char error_code;
	unsigned short cause_code;
	unsigned int connect_speed;
	unsigned short pkt_rcv_win;
	unsigned short pkt_process_delay;
	unsigned int phy_channel_id;
};

struct pptp_control_set_link_info{
	unsigned short length; /* ȫ�����ݳ���, ������ͷ�� */
	unsigned short pptp_message_type;
	unsigned int magic_cookie;
	unsigned short control_message_type;	
	unsigned short reserved0;
	unsigned short peer_call_id;
	unsigned short reserved1;
	unsigned int send_accm;
	unsigned int recv_accm;
};

extern unsigned short g_pptp_plugid;

extern MESA_htable_handle *g_pptp_htable_array;


static int pptp_control_protocol_identify(const struct streaminfo *a_tcp)
{
	const struct stream_tuple4_v4 *tuple4_v4;
	const struct stream_tuple4_v6 *tuple4_v6;
	const struct mesa_pptp_control_hdr *pptp_ctrl_hdr;

	if(a_tcp->ptcpdetail->datalen < sizeof(struct mesa_pptp_control_hdr)){
		/* ����С����С��ʶ�𳤶�, �򳤶Ⱥ�pptpͷ������� */
		return 0;
	}

	pptp_ctrl_hdr = (const struct mesa_pptp_control_hdr *)a_tcp->ptcpdetail->pdata;

	if(ntohs(pptp_ctrl_hdr->length) != a_tcp->ptcpdetail->datalen){
		/* ���ݳ��Ⱥ�pptpͷ������� */
		return 0;
	}

	if(ADDR_TYPE_IPV4 == a_tcp->addr.addrtype){
		tuple4_v4 = a_tcp->addr.tuple4_v4;
		if((PPTP_CONTROL_LINK_PORT != ntohs(tuple4_v4->source))
		 && (PPTP_CONTROL_LINK_PORT != ntohs(tuple4_v4->dest))){
			return 0;
		}
	}else{
		tuple4_v6 = a_tcp->addr.tuple4_v6;
		if((PPTP_CONTROL_LINK_PORT != ntohs(tuple4_v6->source))
		 && (PPTP_CONTROL_LINK_PORT != ntohs(tuple4_v6->dest))){
			return 0;
		}
	}


	if(PPTP_CONTROL_LINK_MAGIC != ntohl(pptp_ctrl_hdr->magic_cookie)){
		return 0;
	}

	/* manage type ʵ��δʹ�� */
	if(PPTP_MESSAGE_CONTROL != ntohs(pptp_ctrl_hdr->pptp_message_type)){
		return 0;
	}

	if(ntohs(pptp_ctrl_hdr->control_message_type) > PPTP_CTRL_SET_LINK_INFO){
		return 0;
	}

	/* ������PPTPЭ��淶, ������Ϊ��PPTP����Э���� */
	return 1;
}

static char pptp_control_call_biz_plug(const struct streaminfo *stream, struct pptp_info_pri *pptp_pri, const void *ip_hdr)
{
	char biz_plug_ret;

	biz_plug_ret = PROT_PROCESS(&pptp_pri->ssinfo, &pptp_pri->biz_pme, stream->threadnum, (struct streaminfo * )stream, ip_hdr);
	pptp_pri->ssinfo.prot_flag = TUNNEL_PHONY_PROT_FLAG;
	pptp_pri->ssinfo.buf = NULL;
	pptp_pri->ssinfo.buflen = 0;
	
	return biz_retval_to_platform(biz_plug_ret);
}


static inline void pptp_context_init(struct pptp_info_pri *pptp_pri, unsigned char threadnum)
{
	memset(pptp_pri, 0, sizeof(struct pptp_info_pri)); 

	pptp_pri->threadnum = threadnum;

	pptp_pri->tunnel_context.tunnel_type = STREAM_TYPE_PPTP;

	pptp_pri->ssinfo.plugid = g_pptp_plugid;
	pptp_pri->ssinfo.session_state = SESSION_STATE_PENDING;
	pptp_pri->ssinfo.prot_flag = TUNNEL_PHONY_PROT_FLAG;

	pptp_pri->ssinfo.app_info = &pptp_pri->tunnel_context;
}

/*
	�������ӽ׶�, ��PPTP�㻹û���������ṹ, �����ڿ������ӽ׶�, Ϊpptp���ݽ׶���ʱ����һ��streaminfo�ṹ, ������pfather����,
	��֤pptpҵ������������pptp�������ṹ.

	�������Ӵ�TCP��ʼ, pptp���������ȴ���һ��PPTP��, �ٴ���һ����IPv4��, 
	Ȼ�����µĴ�a_tcp->pfather��ʼ���Ƶײ�����Ϣ.
*/
struct streaminfo *pptp_build_data_stream_info(const struct streaminfo *a_tcp, struct pptp_stream_key *pptp_key)
{
	UCHAR threadnum = a_tcp->threadnum;
	struct streaminfo *pptp_data_stream = (struct streaminfo *)dictator_malloc(threadnum, sizeof(struct streaminfo));

	/* PPTP��, �ȸ���һ������, ��������������޸� */
	memcpy(pptp_data_stream, a_tcp, sizeof(struct streaminfo));

	pptp_data_stream->addr.addrtype = ADDR_TYPE_PPTP;
	pptp_data_stream->addr.addrlen =  sizeof(struct layer_addr_pptp);
	pptp_data_stream->addr.paddr = dictator_malloc(threadnum, sizeof(struct layer_addr_pptp));
	pptp_data_stream->addr.pptp->C2S_call_id = pptp_key->sip_side_call_id;
	pptp_data_stream->addr.pptp->S2C_call_id = pptp_key->dip_side_call_id;
	pptp_data_stream->type = STREAM_TYPE_PPTP;
	pptp_data_stream->pdetail = NULL;

	/* IP pair�� */
	pptp_data_stream->pfather = (struct streaminfo *)dictator_malloc(threadnum, sizeof(struct streaminfo));
	struct streaminfo *ip_layer_stream = pptp_data_stream->pfather;
	memcpy(ip_layer_stream, a_tcp, sizeof(struct streaminfo));
	if(ADDR_TYPE_IPV4 == a_tcp->addr.addrtype){
		ip_layer_stream->addr.addrtype = __ADDR_TYPE_IP_PAIR_V4;
		ip_layer_stream->addr.addrlen =  sizeof(struct layer_addr_ipv4);
		ip_layer_stream->addr.paddr = dictator_malloc(threadnum, sizeof(struct layer_addr_ipv4));
		ip_layer_stream->addr.ipv4->saddr = a_tcp->addr.tuple4_v4->saddr;
		ip_layer_stream->addr.ipv4->daddr = a_tcp->addr.tuple4_v4->daddr;
		ip_layer_stream->addr.ipv4->source = 0;
		ip_layer_stream->addr.ipv4->dest = 0;
		ip_layer_stream->type = STREAM_TYPE_NON;
		ip_layer_stream->pdetail = NULL;
		ip_layer_stream->pfather = NULL;
	}else{
		ip_layer_stream->addr.addrtype = __ADDR_TYPE_IP_PAIR_V6;
		ip_layer_stream->addr.addrlen =  sizeof(struct layer_addr_ipv6);
		ip_layer_stream->addr.paddr = dictator_malloc(threadnum, sizeof(struct layer_addr_ipv6));
		memcpy(ip_layer_stream->addr.ipv6->saddr,  a_tcp->addr.tuple4_v6->saddr, IPV6_ADDR_LEN);
		memcpy(ip_layer_stream->addr.ipv6->daddr,  a_tcp->addr.tuple4_v6->daddr, IPV6_ADDR_LEN);
		ip_layer_stream->addr.ipv6->source = 0;
		ip_layer_stream->addr.ipv4->dest = 0;
		ip_layer_stream->type = STREAM_TYPE_NON;
		ip_layer_stream->pdetail = NULL;
		ip_layer_stream->pfather = NULL;
	}

	/* ԭʼ����IP�����µ��� */
	const struct streaminfo *raw_stream_ip_low_layer = a_tcp->pfather;
	struct streaminfo *tmp_stream;
	
	while(raw_stream_ip_low_layer != NULL){
		tmp_stream = (struct streaminfo *)dictator_malloc(threadnum, sizeof(struct streaminfo));
		memcpy(tmp_stream, raw_stream_ip_low_layer, sizeof(struct streaminfo));
		tmp_stream->addr.paddr = dictator_malloc(threadnum, raw_stream_ip_low_layer->addr.addrlen);
		memcpy(tmp_stream->addr.paddr, raw_stream_ip_low_layer->addr.paddr, raw_stream_ip_low_layer->addr.addrlen);
		tmp_stream->pfather = NULL;
		ip_layer_stream->pfather = tmp_stream;
		ip_layer_stream = ip_layer_stream->pfather;
		raw_stream_ip_low_layer = raw_stream_ip_low_layer->pfather;
	}

	return pptp_data_stream;
}

static int pptp_stream_key_insert_hash(const struct streaminfo *a_tcp, int tid, struct pptp_stream_key *pptp_key)
{
	struct pptp_info_pri *pptp_pri;
	int hash_ret;

	/* ��Ϊ�����������ȴ���HASH�ڵ�, ͬʱ����һ���յ�pptp_pri�ṹ, HASH���е�Ԫ����data_channel������ֹʱ, ��dataͨ����ɾ�� */
	pptp_pri = (struct pptp_info_pri *)dictator_malloc(tid, sizeof(struct pptp_info_pri));
	pptp_context_init(pptp_pri, tid);
	pptp_pri->ssinfo.session_state = 0; /* PPTP���ݴ���׶�, �������ж�pending, data, close״̬, ���жϵ�ǰsession-stateֵ���� */
	memcpy(&pptp_pri->pptp_key, pptp_key, sizeof(struct pptp_stream_key));

	pptp_pri->my_stream = pptp_build_data_stream_info(a_tcp, pptp_key);

	/* TODO 1, pptp_key����Ƕ�׵�ַ���� */
	hash_ret = MESA_htable_add(g_pptp_htable_array[tid], (const uchar *)pptp_key, sizeof(struct pptp_stream_key), pptp_pri);
	if(hash_ret < 0){
		/* NOTE: �ײ�����һ��, ���������ж��TCP����ʱ, ��HASH��ͻ, ��Ҫfree */
		pptp_stream_free(pptp_pri->my_stream);
		dictator_free(tid, pptp_pri);
	}else{
#if 0	
		if(hash_ret > 29){
			/* 
				2016-12-30 lijia add, �������Ӵ��ж�:
				������HASH�㷨ֻ����IP��,
				��HASH��ͻ����ʱ, ������������PPTP��, ��ֹ����������ʱ̫��, ��֤��Ӱ������ҵ��. 
			*/
			MESA_htable_del(g_pptp_htable_array[tid], (const uchar *)pptp_key, sizeof(struct pptp_stream_key), NULL);
		}
#endif
#ifdef MESA_HTABLE_VERSION_MACRO
	#if MESA_HTABLE_VERSION_MACRO >= 20170104
		int len, max_times;
		double avg_times;
		len = sizeof(int);
		MESA_htable_get_opt(g_pptp_htable_array[tid], MHO_HASH_SEARCH_MAX_TIMES, &max_times, &len);

		len = sizeof(double);
		MESA_htable_get_opt(g_pptp_htable_array[tid], MHO_HASH_SEARCH_AVG_TIMES, &avg_times, &len);

		if(max_times > sapp_global_mthread[tid].pptp_hash_max_search_times){
			sapp_global_mthread[tid].pptp_hash_max_search_times = max_times;
			sapp_runtime_log(30, "pptp hash max search times: %d, thread seq:%d.\n", max_times, tid);
		}
		if(avg_times > sapp_global_mthread[tid].pptp_hash_avg_search_times){
			sapp_global_mthread[tid].pptp_hash_avg_search_times = avg_times;
			sapp_runtime_log(30, "pptp hash avg search times: %.2f, thread seq:%d.\n", avg_times, tid);
		}
	#endif
#else
		/* �ھɰ������, Ϊ�˰�ȫ��д��ͻ��־, ����Ҫ�ж�����ͻ���ڵ��� */
		if(hash_ret > 29){
			/* 
				2016-12-30 lijia add, �������Ӵ��ж�:
				������HASH�㷨ֻ����IP��,
				��HASH��ͻ����ʱ, ������������PPTP��, ��ֹ����������ʱ̫��, ��֤��Ӱ������ҵ��. 
			*/
			MESA_htable_del(g_pptp_htable_array[tid], (const uchar *)pptp_key, sizeof(struct pptp_stream_key), NULL);
		}
#endif	
	}
	return hash_ret;
}


static inline void pptp_control_set_key(const struct streaminfo *a_tcp, int tid, struct pptp_info_pri *pptp_pri, 
		unsigned int sip, unsigned int dip, unsigned short sip_side_callid, unsigned short dip_side_callid)
{
	pptp_pri->pptp_key.sip = sip;
	pptp_pri->pptp_key.dip = dip;
	pptp_pri->pptp_key.sip_side_call_id = sip_side_callid;
	pptp_pri->pptp_key.dip_side_call_id = dip_side_callid;

	/* ˫���call_id���ѻ�ȡ��, ���Բ���HASH��, ����ʶ���Ӧ���������� */
	if((0 == pptp_pri->insert_hash_flag)
	  && (pptp_pri->pptp_key.sip_side_call_id != 0)
	  &&(pptp_pri->pptp_key.dip_side_call_id != 0)){
   		pptp_stream_key_insert_hash(a_tcp, tid, &pptp_pri->pptp_key);
   		pptp_pri->insert_hash_flag = 1;
	}
	
	return;
}

static int pptp_control_protocol_process_data(const struct streaminfo *a_tcp,  
				struct pptp_info_pri *pptp_pri, const struct mesa_ip4_hdr *ip_hdr)
{
	/*
		TODO 1,
		���ٿ�������, ������callid, �ݴ���struct pptp_stream_key,
		�����������callid����ȡ��(���㵥����, Ҳ���Ի�ȡ�Է���),
		����ȫ�ֵ�PPTP_HASH��, 
		��PPTP����ͨ����, ����LCP��CCPЭ��, ��¼��֤�㷨�������㷨��ѹ���㷨�ȵ�.
		����IPCP�׶κ�, ����struct pptp_stream_key(����ͨ��ֻ����Ԫ��),
		��HASH���ҵ����Ԫ��,
		����biz_plug, �������ͨ�������Ϣ.

		PPTPȫ��HASH����������ͨ������biz_plug֮�󼴿�����ɾ��, 
		���߿�������ʱ��̭.
	*/

	const struct mesa_pptp_control_hdr *pptp_ctrl_hdr;
	unsigned short control_message_type;

	if(NULL == a_tcp->ptcpdetail->pdata){
		return APP_STATE_GIVEME;
	}
	
	pptp_ctrl_hdr = (const struct mesa_pptp_control_hdr *)a_tcp->ptcpdetail->pdata;
	control_message_type = ntohs(pptp_ctrl_hdr->control_message_type);
	
	if(ntohs(pptp_ctrl_hdr->pptp_message_type) != PPTP_MESSAGE_CONTROL){
		return APP_STATE_GIVEME;
	}

	switch(control_message_type){
		case PPTP_CTRL_OUT_GO_REQ:
		{
			const struct pptp_control_out_go_req *pptp_ctrl_out_go_req_hdr;
			pptp_ctrl_out_go_req_hdr = (const struct pptp_control_out_go_req *)pptp_ctrl_hdr;
			pptp_control_set_key(a_tcp, a_tcp->threadnum, pptp_pri, ip_hdr->ip_src.s_addr,  
								 ip_hdr->ip_dst.s_addr, 0, pptp_ctrl_out_go_req_hdr->call_id);
		}
		break;

		/* ��ʵ������PPTP_CTRL_OUT_GO_REPLY��Ϣ�л�ȡ˫����callid, �����ǵ��������, ��Ȼ���PPTP_CTRL_OUT_GO_REQ��PPTP_CTRL_SET_LINK_INFO */
		case PPTP_CTRL_OUT_GO_REPLY:
		{
			const struct pptp_control_out_go_reply *pptp_ctrl_out_go_rep_hdr;
			pptp_ctrl_out_go_rep_hdr = (const struct pptp_control_out_go_reply *)pptp_ctrl_hdr;
			pptp_control_set_key(a_tcp, a_tcp->threadnum, pptp_pri, ip_hdr->ip_src.s_addr,  ip_hdr->ip_dst.s_addr,  
								pptp_ctrl_out_go_rep_hdr->peer_call_id, 
								pptp_ctrl_out_go_rep_hdr->call_id);
		}
		break;

		/* �������ӷ�����һ��SET_LINK_INFO�����, �������ӿ�ʼ����PPP_LCPͨ�� */
		case PPTP_CTRL_SET_LINK_INFO:
		{
			const struct pptp_control_set_link_info *pptp_ctrl_set_link_info;
			pptp_ctrl_set_link_info = (const struct pptp_control_set_link_info *)pptp_ctrl_hdr;
			/* �������ǶԷ���callid */
			pptp_control_set_key(a_tcp, a_tcp->threadnum, pptp_pri, ip_hdr->ip_src.s_addr,  ip_hdr->ip_dst.s_addr, 
								pptp_ctrl_set_link_info->peer_call_id, 0);
		}
		break;
	}

	pptp_pri->ssinfo.session_state = SESSION_STATE_DATA;
	pptp_control_call_biz_plug(a_tcp, pptp_pri, ip_hdr);

	if(PPTP_CTRL_STOP_CONN_REPLY == control_message_type){
		/* �յ������������ư�, ��ʱɾ��pptp�� */
		MESA_htable_del(g_pptp_htable_array[a_tcp->threadnum], 
						(const uchar *)&pptp_pri->pptp_key, sizeof(struct pptp_stream_key), 
						NULL);
	}

	return APP_STATE_GIVEME;
}


static int pptp_control_protocol_process_close(const struct streaminfo *a_tcp,  struct pptp_info_pri *pptp_pri, const struct mesa_ip4_hdr *ip_hdr)
{
	/*
		CLOSE״̬����Ҳ��������, �ڵ���pptp_control_protocol_process_data()һ��.
	*/
	pptp_control_protocol_process_data(a_tcp, pptp_pri, ip_hdr);
	pptp_pri->ssinfo.session_state = SESSION_STATE_CLOSE;
	pptp_control_call_biz_plug(a_tcp, pptp_pri, ip_hdr);

	/*
		NOTE: ��������������ʱ, ��������ʱ��LRU��̭����, ����һ����PPTP���������ر���, 
		���Դ˴���������ɾ��pptp��������.
	*/

	return APP_STATE_GIVEME;
}

static int pptp_control_protocol_process_pending(const struct streaminfo *a_tcp,  void **pme, const struct mesa_ip4_hdr *ip_hdr)
{
	struct pptp_info_pri *pptp_pri;
	
	pptp_pri = (struct pptp_info_pri *)dictator_malloc(a_tcp->threadnum, sizeof(struct pptp_info_pri)); /* ��ʱ�ñ�׼malloc, free_cb��û�д����߳�ID, ֻ����free */
	pptp_context_init(pptp_pri, a_tcp->threadnum);
	pptp_pri->tunnel_context.pptp_info.link_type = TUNNEL_CHANNEL_TYPE_CONTROL;
	pptp_pri->tunnel_context.pptp_info.encrypt_pro = -1;
	pptp_pri->tunnel_context.pptp_info.authentication_pro = -1;
	
	*pme = pptp_pri;

	pptp_pri->ssinfo.prot_flag = PPTP_OPT_LINK_TYPE;
	pptp_pri->ssinfo.buf = &pptp_pri->tunnel_context.pptp_info.link_type;
	pptp_pri->ssinfo.buflen = sizeof(int);
	pptp_control_call_biz_plug(a_tcp, pptp_pri, ip_hdr);
	
	/*
		PENDING״̬����Ҳ��������, �ٵ���pptp_control_protocol_process_data()һ��.
	*/
	pptp_control_protocol_process_data(a_tcp, pptp_pri, ip_hdr);

	return APP_STATE_GIVEME;
}


void pptp_protocol_funstat(unsigned long long protflag)
{
	;
}

long long pptp_protocol_flag_change(char* flag_str)
{
	return 0xFFFF;
}

void pptp_protocol_get_plugid(unsigned short plugid)
{
	g_pptp_plugid = plugid;
}

static uint pptp_stream_key2index(const MESA_htable_handle table, const uchar * key, uint size)
{
	const struct pptp_stream_key *pptp_key = (const struct pptp_stream_key *)key;
	/* NOTE:
	   ����ʹ��call_id��Ϊkey, 
	   ��Ϊ��ǰ��ֻ��һ��������callid, ��һ������û��, ����˫��hash��һ�� 
	 */
	unsigned int i;
	unsigned char *ptr;
	unsigned int seed = 13131;
	unsigned int hash = 0;
	unsigned int sip, dip;

	if(pptp_key->sip >= pptp_key->dip){
		sip = pptp_key->sip;
		dip = pptp_key->dip;
	}else{
		sip = pptp_key->dip;
		dip = pptp_key->sip;
	}
	
	ptr = (unsigned char *)&sip;
	for(i = 0; i < sizeof(int); i++){
		hash = hash * seed + (*ptr++);
	}

	ptr = (unsigned char *)&dip;
	for(i = 0; i < sizeof(int); i++){
		hash = hash * seed + (*ptr++);
	}

	return hash;
}

static  int pptp_stream_key_comp(const uchar * key1, uint size1, const uchar * key2, uint size2)
{
	const struct pptp_stream_key *pptp_key1 = (const struct pptp_stream_key *)key1;
	const struct pptp_stream_key *pptp_key2 = (const struct pptp_stream_key *)key2;	

	/* ���ж�IP��ַ, ���������� */
	if((pptp_key1->sip == pptp_key2->sip) && (pptp_key1->dip == pptp_key2->dip)){ 
		/* ��������ֻ�ܻ�ȡ�������callid, ���Դ˴�ֻ������Ԫ��Ƚ� */
		if(pptp_key1->sip_side_call_id == pptp_key2->sip_side_call_id){
			return 0;
		}
		if(pptp_key1->dip_side_call_id == pptp_key2->dip_side_call_id){
			return 0;
		}
	}
	/* ����Ƚ� */
       else if((pptp_key1->sip == pptp_key2->dip) && (pptp_key1->dip == pptp_key2->sip)){
       	/* ��������ֻ�ܻ�ȡ�������callid, ���Դ˴�ֻ������Ԫ��Ƚ� */
		if(pptp_key1->sip_side_call_id == pptp_key2->dip_side_call_id){
			return 0;
		}
		if(pptp_key1->dip_side_call_id == pptp_key2->sip_side_call_id){
			return 0;
		}
	}

	/* TODO 1, �ж�Ƕ�׵�ַ����, Ŀǰ���ж���������IP��call_id */

	return -1;
}

static uchar *pptp_stream_key_dup(const uchar *key, uint key_size)
{
	const struct pptp_stream_key *pptp_stack_key = (const struct pptp_stream_key *)key;
	struct pptp_stream_key *pptp_heap_key;

	/* TODO 1, ��Ϊdictator_malloc(), ����threadnum, freeʱҲҪ�ñ�׼free() */
	pptp_heap_key = (struct pptp_stream_key *)malloc(sizeof(struct pptp_stream_key));
	memcpy(pptp_heap_key, pptp_stack_key, sizeof(struct pptp_stream_key));

	return (uchar *)pptp_heap_key;
}

static void pptp_stream_key_free(uchar *key, uint key_size)
{
	/* TODO 1, ��Ϊdictator_malloc(), ��Ҫ����threadnum */
	free(key);
}

/* ǿ����̭�ص�, ��Ҫ������ҵ����� */
static void pptp_stream_data_free_passive(void *data)
{
	struct pptp_info_pri *pptp_pri = (struct pptp_info_pri *)data;
	unsigned char tid = pptp_pri->threadnum;

	pptp_pri->ssinfo.session_state = SESSION_STATE_CLOSE;
	PROT_PROCESS(&pptp_pri->ssinfo, &pptp_pri->biz_pme, tid, pptp_pri->my_stream, NULL);

	pptp_stream_free(pptp_pri->my_stream);
	
	dictator_free(tid, data);
}

/* ����PPTP ����, ���ڴ洢data�׶ε� struct pptp_info_pri�ṹ */
static MESA_htable_handle pptp_stream_hash_create(void)
{
	MESA_htable_handle htable;
	int opt_int;
	
	htable = MESA_htable_born();

	opt_int = 0;
	MESA_htable_set_opt(htable, MHO_THREAD_SAFE, &opt_int, sizeof(int));

	opt_int = 1048576;
	MESA_htable_set_opt(htable, MHO_HASH_SLOT_SIZE, &opt_int, sizeof(int));

	opt_int = 100;
	MESA_htable_set_opt(htable, MHO_EXPIRE_TIME, &opt_int, sizeof(int));

	opt_int = 10000;
	MESA_htable_set_opt(htable, MHO_HASH_MAX_ELEMENT_NUM, (void *)&opt_int, sizeof(int));

	MESA_htable_set_opt(htable, MHO_CBFUN_DATA_FREE, (void *)&pptp_stream_data_free_passive, sizeof(void *));
	
	MESA_htable_set_opt(htable, MHO_CBFUN_KEY_TO_INDEX, (void *)&pptp_stream_key2index, sizeof(void *));

	MESA_htable_set_opt(htable, MHO_CBFUN_KEY_COMPARE, (void *)&pptp_stream_key_comp, sizeof(void *));	

	MESA_htable_set_opt(htable, MHO_CBFUN_COMPLEX_KEY_DUP, (void *)&pptp_stream_key_dup, sizeof(void *));	

	MESA_htable_set_opt(htable, MHO_CBFUN_COMPLEX_KEY_FREE, (void *)&pptp_stream_key_free, sizeof(void *));

/* ��20161205�汾��ʼ, MESA_htable.h���к궨��汾��, ���Կ��Ʊ������ */
#ifdef MESA_HTABLE_VERSION_MACRO
	#if MESA_HTABLE_VERSION_MACRO >= 20161205
	opt_int = 0; /* �ݲ�д��ͻ��־ */
	MESA_htable_set_opt(htable, MHO_HASH_LIST_COLLIDE_THRESHOLD, (void *)&opt_int, sizeof(int));

	MESA_htable_set_opt(htable, MHO_HASH_LOG_FILE, (void *)"./log/pptp_hash_collide.log", strlen("./log/pptp_hash_collide.log"));
	
	#endif
#endif

	MESA_htable_mature(htable);

	return htable;
}

/* 
	PPTP�������ӽ�������, ע����TCP��, biz_plug��entry��ΪPPTP

*/
char pptp_control_protocol_entry(const struct streaminfo *a_tcp,  void **pme, int thread_seq,const void *ip_hdr)
{
	char pro_plug_ret = APP_STATE_GIVEME;
	struct pptp_info_pri *pptp_pri;

	switch(a_tcp->opstate){
		case OP_STATE_PENDING:
			if(pptp_control_protocol_identify(a_tcp) != 0){
				pro_plug_ret = pptp_control_protocol_process_pending(a_tcp, pme, (const struct mesa_ip4_hdr *)ip_hdr);
			}else{
				pro_plug_ret = APP_STATE_DROPME;
				break;
			}
		break;

		case OP_STATE_DATA:
			pptp_pri = (struct pptp_info_pri *)(*pme);
			pro_plug_ret = pptp_control_protocol_process_data(a_tcp, pptp_pri, (const struct mesa_ip4_hdr *)ip_hdr);
		break;

		case OP_STATE_CLOSE:
			pptp_pri = (struct pptp_info_pri *)(*pme);
			pptp_control_protocol_process_close(a_tcp, pptp_pri, (const struct mesa_ip4_hdr *)ip_hdr);
			pro_plug_ret = APP_STATE_DROPME;
		break;

		default:
			break;
	}

	if(APP_STATE_DROPME == pro_plug_ret){
		if(*pme){
			dictator_free(thread_seq, *pme);
		}
	}
	
	return pro_plug_ret;
}

int pptp_protocol_init(void)
{
	int thread_cnt = get_thread_count();
	int i;
	
	g_pptp_htable_array = (MESA_htable_handle *)malloc(sizeof(MESA_htable_handle) * thread_cnt);
	memset(g_pptp_htable_array, 0, sizeof(MESA_htable_handle) * thread_cnt);

	for(i = 0; i < thread_cnt; i++){
		g_pptp_htable_array[i] = pptp_stream_hash_create();
	}
	
	return g_pptp_plugid;
}

void pptp_protocol_destroy(void)
{
	g_pptp_plugid = 0;
}


#ifdef __cplusplus
}
#endif