summaryrefslogtreecommitdiff
path: root/src/mctrl.cpp
blob: d321f4bee0ff4b1b9be43bd50b02ffddca091120 (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
664
665
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <time.h>
#include "MESA_prof_load.h"
#include "MESA_handle_logger.h"
#include "Maat_rule.h"
#include "Maat_command.h"
#include "mctrl.h"
#include "MESA_htable.h"

struct mctrl_glocal_info mctrl_g;

void mctrl_get_cur_time(char *date)
{   
	time_t t;
    struct tm *lt;
    time(&t);
    lt = localtime(&t);
    snprintf(date, MAX_TIME_LEN,"%d/%d/%d/%d:%d:%d",lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
}

int set_ir_line(Maat_feather_t feather,const char *ir_table_name,const char *table_line,int rule_id)
{
	const struct Maat_line_t *p_line;
	struct Maat_line_t line_rule;
	int ret = 0;
	char m_table_line[HTABLE_DATA_LEN];
	struct IR_MCTRL_INFO mctrl_info;
	memset(&mctrl_info,0,sizeof(mctrl_info));
	memset(&line_rule, 0,sizeof(line_rule));

	line_rule.label_id=0;
	line_rule.rule_id=rule_id;
	line_rule.table_name=ir_table_name;

	sscanf(table_line, "%d\t%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%s\t%s", 
		&mctrl_info.htable_flag,&mctrl_info.region_id,&mctrl_info.group_id,&mctrl_info.addr_type,
		mctrl_info.src_ip,mctrl_info.mask_src_ip,mctrl_info.src_port,mctrl_info.mask_src_port,mctrl_info.dst_ip,mctrl_info.mask_dst_ip,
		mctrl_info.dst_port,mctrl_info.mask_dst_port,&mctrl_info.procotol,&mctrl_info.direction,&mctrl_info.addr_pool_id,&mctrl_info.is_valid,
		&mctrl_info.action,&mctrl_info.service,mctrl_info.user_region,mctrl_info.effective_range,mctrl_info.op_time);

	mctrl_get_cur_time(mctrl_info.op_time);
	
	if(!memcmp(ir_table_name,NOMINEE_TABLE_NAME,strlen(NOMINEE_TABLE_NAME)))
	{
		snprintf(m_table_line,sizeof(m_table_line),"%d\t%d\t%d\t%s\t%d\t%s\t%s",
			rule_id,mctrl_info.addr_pool_id,mctrl_info.addr_type,mctrl_info.src_ip,mctrl_info.is_valid,mctrl_info.effective_range,mctrl_info.op_time);
	}
	else 
	{
		snprintf(m_table_line,sizeof(m_table_line),"%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%s\t%s\t%s",
			rule_id,mctrl_info.group_id,mctrl_info.addr_type,
			mctrl_info.src_ip,mctrl_info.mask_src_ip,mctrl_info.src_port,mctrl_info.mask_src_port,mctrl_info.dst_ip,mctrl_info.mask_dst_ip,
			mctrl_info.dst_port,mctrl_info.mask_dst_port,mctrl_info.procotol,mctrl_info.direction,
			mctrl_info.is_valid,mctrl_info.action,mctrl_info.service,mctrl_info.user_region,mctrl_info.effective_range,mctrl_info.op_time);
	}
	
	line_rule.table_line=m_table_line;
	line_rule.expire_after=0;
	p_line=&line_rule;

	ret=Maat_cmd_set_line(feather, p_line, MAAT_OP_ADD);

	MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_FATAL, (char*)"maat_update", "set_table_name:%s  set_table_line:%s",ir_table_name,m_table_line);
	
	if(ret==-1)
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"SET_LINE","%s:set rule_id is %d  redis  line error",ir_table_name,rule_id);
	}
	else
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"SET_LINE","%s:set rule_id is %d  redis line success",ir_table_name,rule_id);
	}
	return ret;
}


int del_ir_line(Maat_feather_t feather,const char *ir_table_name,const char *table_line,int rule_id)
{
	int ret=0;
	
	const struct Maat_line_t *p_line;
	struct Maat_line_t line_rule;
	memset(&line_rule,0,sizeof(line_rule));
	
	line_rule.label_id=0;
	line_rule.rule_id=rule_id;
	line_rule.table_name=ir_table_name;
	line_rule.table_line=NULL;
	if(!memcmp(ir_table_name,INTERCEPT_TABLE_NAME,strlen(NOMINEE_TABLE_NAME)))
	{
		line_rule.expire_after=TIME_OUT;
		p_line=&line_rule;
		ret=Maat_cmd_set_line(feather, p_line, MAAT_OP_RENEW_TIMEOUT);
	}
	else
	{
		line_rule.expire_after=0;
		p_line=&line_rule;
		ret=Maat_cmd_set_line(feather, p_line, MAAT_OP_DEL);
	}

	MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_DEBUG, (char*)"maat_update", "del_table_name:%s  del_rule_id is:%d  table_line:%s",ir_table_name,rule_id,table_line);
	
	if(ret==-1)
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"DEL_LINE","%s:del rule_id is %d  redis line error",ir_table_name,rule_id);
	}
	else
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"DEL_LINE","%s:del rule_id is %d  redis line success",ir_table_name,rule_id);
	}
	
	return ret;
	
}

void set_ir_redis_info(int flag,const char* table_line,int rule_id)
{
	if((!(flag & INTERCEPT_FLAG))&&((flag & STATIC_NOMINEE_FLAG)||(flag & DYNAMIC_NOMINEE_FLAG)||(flag & DNAT_POLICY_FLAG)))
	{
		set_ir_line(mctrl_g.i_feather, (char*)INTERCEPT_TABLE_NAME,table_line,rule_id);
	}

	if((!(flag & NOMINEE_FLAG))&&((flag & STATIC_NOMINEE_FLAG)||(flag & DYNAMIC_NOMINEE_FLAG)))
	{
		set_ir_line(mctrl_g.n_feather,(char*)NOMINEE_TABLE_NAME,table_line,rule_id);
	}

	return;
}

void del_ir_redis_info(int flag,const char* table_line,int rule_id)
{
	if(flag & INTERCEPT_FLAG)
	{
		del_ir_line(mctrl_g.i_feather,(char*)INTERCEPT_TABLE_NAME,table_line,rule_id);
	}

	if(flag & NOMINEE_FLAG)
	{
		del_ir_line(mctrl_g.n_feather,(char*)NOMINEE_TABLE_NAME,table_line,rule_id);
	}

	if(flag & CANDIDATE_FLAG)
	{
		del_ir_line(mctrl_g.n_feather,(char*)CANDIDATE_TABLE_NAME,table_line,rule_id);
	}
	
	return;
}

long new_update_search_htable_cb(void *data, const uchar *key, uint size, void *user_arg)
{
	int rule_id=0;
	int htable_flag=*(int*)user_arg;
	char *htable_data=(char*)data;
	if(htable_data!=NULL)
	{
		sscanf(htable_data,"%d\t%d",&htable_flag,&rule_id);
		set_ir_redis_info(htable_flag,htable_data,rule_id);
	}
	return htable_flag;
}

long free_update_search_htable_cb(void *data, const uchar *key, uint size, void *user_arg)
{
	int rule_id=0;
	int htable_flag=0;
	char *htable_data=(char*)data;
	if(htable_data!=NULL)
	{
		sscanf(htable_data,"%d\t%d",&htable_flag,&rule_id);
		del_ir_redis_info(htable_flag,htable_data,rule_id);	
	}
	return htable_flag;
}


void htable_data_free(void *data)
{
	if(data!=NULL)
	{
		int rule_id=0;
		sscanf((char*)data,"%*d\t%d",&rule_id);
		MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_INFO, (char*)"htable_data_free","rule_id is:%d",rule_id);
		free(data);
		data=NULL;
	}
	return;
}

void static_dynamic_dpolicy_table_new_cb(int table_id, const char* key, const char* table_line, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp)
{
	struct IR_MCTRL_INFO mctrl_info;
	memset(&mctrl_info,0,sizeof(mctrl_info));
	char *htable_data=NULL;
	void *search_result=NULL;
	int *add_data=NULL;
	int add_ret=0;
	long cb_ret=0;
	
	switch(table_id)
	{
		case DYNAMIC_NOMINEE_ID:
			mctrl_info.addr_pool_id=0;
			memcpy(mctrl_info.src_port,"0",sizeof("0"));
			memcpy(mctrl_info.mask_src_ip,"0.0.0.0",sizeof("0.0.0.0"));
			memcpy(mctrl_info.mask_src_port,"0",sizeof("0"));
			memcpy(mctrl_info.dst_ip,"0.0.0.0",sizeof("0.0.0.0"));
			memcpy(mctrl_info.mask_dst_ip,"0.0.0.0",sizeof("0.0.0.0"));
			memcpy(mctrl_info.dst_port,"0",sizeof("0"));
			memcpy(mctrl_info.mask_dst_port,"0",sizeof("0"));
			memcpy(mctrl_info.effective_range,"{}",sizeof("{}"));
			mctrl_info.action=96;
			mctrl_info.service=832;
			mctrl_info.htable_flag=DYNAMIC_NOMINEE_FLAG;
			sscanf(table_line,"%d\t%d\t%d\t%s\t%*s\t%d\t%s\t%*d\t%d\t%d\t%*d\t%*s\t%*s", 
				&mctrl_info.region_id,&mctrl_info.addr_type,&mctrl_info.procotol,mctrl_info.src_ip,
				&mctrl_info.direction,mctrl_info.user_region,&mctrl_info.is_valid,&mctrl_info.service);
			mctrl_info.region_id=(mctrl_info.region_id%SNAT_DYNAMIC_NUMBER)+SNAT_DYNAMIC_NUMBER;
			mctrl_info.group_id=mctrl_info.region_id;
			break;
		case STATIC_NOMINEE_ID:
			memcpy(mctrl_info.user_region,"0",sizeof("0"));
			memcpy(mctrl_info.effective_range,"{}",sizeof("{}"));
			mctrl_info.service=832; 
			mctrl_info.htable_flag=STATIC_NOMINEE_FLAG;
			sscanf(table_line,"%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%*d\t%*s\t%*s", 
				&mctrl_info.region_id,&mctrl_info.group_id,&mctrl_info.addr_type,
				mctrl_info.src_ip,mctrl_info.mask_src_ip,mctrl_info.src_port,mctrl_info.mask_src_port,mctrl_info.dst_ip,mctrl_info.mask_dst_ip,
				mctrl_info.dst_port,mctrl_info.mask_dst_port,&mctrl_info.procotol,&mctrl_info.direction,&mctrl_info.addr_pool_id,&mctrl_info.is_valid,
				&mctrl_info.action);
			mctrl_info.region_id=(mctrl_info.region_id%DNAT_NUMBER)+DNAT_NUMBER;
			break;
		case DNAT_POLICY_ID:
			memcpy(mctrl_info.src_ip,"0.0.0.0",sizeof("0.0.0.0"));
			memcpy(mctrl_info.src_port,"0",sizeof("0"));
			memcpy(mctrl_info.mask_src_ip,"0.0.0.0",sizeof("0.0.0.0"));
			memcpy(mctrl_info.mask_src_port,"0",sizeof("0"));
			memcpy(mctrl_info.mask_dst_ip,"0.0.0.0",sizeof("0.0.0.0"));
			memcpy(mctrl_info.mask_dst_port,"0",sizeof("0"));
			memcpy(mctrl_info.user_region,"0",sizeof("0"));
			memcpy(mctrl_info.effective_range,"{}",sizeof("{}"));
			mctrl_info.direction=0;
			mctrl_info.action=96;
			mctrl_info.service=832;
			mctrl_info.htable_flag=DNAT_POLICY_FLAG;
			sscanf(table_line,"%d\t%d\t%s\t%s\t%d\t%*s\t%*s\t%*s\t%d\t%*d\t%*d\t%d\t%*s\t%*s", 
				&mctrl_info.region_id,&mctrl_info.addr_type,mctrl_info.dst_ip,mctrl_info.dst_port,&mctrl_info.procotol,
				&mctrl_info.do_log,&mctrl_info.is_valid);
			mctrl_info.group_id=mctrl_info.region_id;
			mctrl_info.region_id=mctrl_info.region_id%DNAT_NUMBER;
			break;
		default:	
			MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"static_dynamic_dpolicy_table_new_cb","read_snat_or_dnat table flag error!!");
			assert(0);
			break;
	}
	
	search_result=MESA_htable_search_cb(mctrl_g.mctrl_htable_handle,(unsigned char*)&mctrl_info.region_id,sizeof(int),new_update_search_htable_cb,argp,&cb_ret);
	if(search_result==NULL)
	{	
		htable_data=(char*)malloc(HTABLE_DATA_LEN);
		
		if(htable_data==NULL)
		{
			MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"static_dynamic_dpolicy_table_new_cb","malloc htable_data error!!");
			exit(1);
		}
		snprintf(htable_data,HTABLE_DATA_LEN,"%d\t%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%s\t%s", 
			mctrl_info.htable_flag,mctrl_info.region_id,mctrl_info.group_id,mctrl_info.addr_type,
			mctrl_info.src_ip,mctrl_info.mask_src_ip,mctrl_info.src_port,mctrl_info.mask_src_port,mctrl_info.dst_ip,mctrl_info.mask_dst_ip,
			mctrl_info.dst_port,mctrl_info.mask_dst_port,mctrl_info.procotol,mctrl_info.direction,mctrl_info.addr_pool_id,mctrl_info.is_valid,
			mctrl_info.action,mctrl_info.service,mctrl_info.user_region,mctrl_info.effective_range,mctrl_info.op_time);
		add_ret=MESA_htable_add(mctrl_g.mctrl_htable_handle,(unsigned char*)&mctrl_info.region_id,sizeof(int),htable_data);

		if(add_ret<0)
		{
			MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_DEBUG, (char*)"static_dynamic_dpolicy_table_new_cb","add htable error:%d",add_ret);
			assert(0);
		}
		else
		{
			MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_DEBUG, (char*)"static_dynamic_dpolicy_table_new_cb","add htable succeed:%d",add_ret);
		}
		set_ir_redis_info(cb_ret,htable_data,mctrl_info.region_id);
		add_data=(int*)calloc(sizeof(int),1);
		if(add_data==NULL)
		{
			MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"static_dynamic_dpolicy_table_new_cb","malloc add_data error!!");
			exit(1);
		}
		*add_data=mctrl_info.region_id;
		*ad=add_data;
	}
	else
	{	
		MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_INFO, (char*)"static_dynamic_dpolicy_table_new_cb","htable_info exit! rule_id is:%d",mctrl_info.region_id);
	}

	return;
}
void static_dynamic_dpolicy_table_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp)
{
	void* search_result=NULL;
	int del_ret=0;
	long cb_ret=0;
	search_result=MESA_htable_search_cb(mctrl_g.mctrl_htable_handle,(unsigned char*)*ad,sizeof(int),free_update_search_htable_cb,NULL,&cb_ret);
	
	if(search_result==NULL)
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_FATAL, (char*)"static_dynamic_dpolicy_table_free_cb", "already del!!!");
	}
	else
	{	
		del_ret=MESA_htable_del(mctrl_g.mctrl_htable_handle,(unsigned char*)*ad,sizeof(int),htable_data_free);
		
		if(del_ret<0)
		{
			MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"static_dynamic_dpolicy_table_free_cb","del htable error:%d",del_ret);
			assert(0);
		}
	}

	if(*ad!=NULL)
	{
		free(*ad);
	}
	
	*ad=NULL;
	return;
}

long nominee_intercept_candidate_search_htable_cb(void *data, const uchar *key, uint size, void *user_arg)
{
	int htable_flag=0;
	struct IR_MCTRL_INFO mctrl_info;
	memset(&mctrl_info,0,sizeof(mctrl_info));
	char *htable_data=(char*)data;

	if(htable_data==NULL)
	{
		return htable_flag;
	}
	
	sscanf(htable_data, "%d\t%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%s\t%s", 
		&htable_flag,&mctrl_info.region_id,&mctrl_info.group_id,&mctrl_info.addr_type,
		mctrl_info.src_ip,mctrl_info.mask_src_ip,mctrl_info.src_port,mctrl_info.mask_src_port,mctrl_info.dst_ip,mctrl_info.mask_dst_ip,
		mctrl_info.dst_port,mctrl_info.mask_dst_port,&mctrl_info.procotol,&mctrl_info.direction,&mctrl_info.addr_pool_id,&mctrl_info.is_valid,
		&mctrl_info.action,&mctrl_info.service,mctrl_info.user_region,mctrl_info.effective_range,mctrl_info.op_time);

	switch(*(int*)user_arg)
	{
		case INTERCEPT_FLAG:
			if(!(htable_flag & INTERCEPT_FLAG))	
			{	
				htable_flag+=INTERCEPT_FLAG;
			}
			break;
		case NOMINEE_FLAG:
			if(!(htable_flag & NOMINEE_FLAG))
			{
				htable_flag+=NOMINEE_FLAG;
			}
			break;
		case CANDIDATE_FLAG:
			if(!(htable_flag & CANDIDATE_FLAG))
			{
				htable_flag+=CANDIDATE_FLAG;
			}
			break;
		default:
			MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"nominee_intercept_candidate_search_htable_cb","read nom_intercept_candidate_table error!!!");	
			assert(0);
			break;
	}
	
	snprintf(htable_data,HTABLE_DATA_LEN, "%d\t%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%s\t%s", 
		htable_flag,mctrl_info.region_id,mctrl_info.group_id,mctrl_info.addr_type,
		mctrl_info.src_ip,mctrl_info.mask_src_ip,mctrl_info.src_port,mctrl_info.mask_src_port,mctrl_info.dst_ip,mctrl_info.mask_dst_ip,
		mctrl_info.dst_port,mctrl_info.mask_dst_port,mctrl_info.procotol,mctrl_info.direction,mctrl_info.addr_pool_id,mctrl_info.is_valid,
		mctrl_info.action,mctrl_info.service,mctrl_info.user_region,mctrl_info.effective_range,mctrl_info.op_time);
		
	MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_DEBUG, (char*)"nominee_intercept_candidate_search_htable_cb","table_flag is:%d ,Data rule id is:%d  htable flag is:%d",*(int*)user_arg,mctrl_info.region_id, htable_flag);	

	return htable_flag;
}

void nominee_intercept_candidate_table_new_cb(int table_id, const char* key, const char* table_line, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp)
{
	long search_ret=0;
	void* search_htable_result=NULL;
	int rule_id=0;
	int *add_data=NULL;
	sscanf(table_line,"%d\t",&rule_id);

	if(*(int*)argp==NOMINEE_FLAG&&rule_id<DNAT_NUMBER)
	{
		del_ir_line(mctrl_g.n_feather,(char*)NOMINEE_TABLE_NAME,table_line,rule_id);
		return;
	}
	
	search_htable_result=MESA_htable_search_cb(mctrl_g.mctrl_htable_handle,(unsigned char*)&rule_id,sizeof(int), nominee_intercept_candidate_search_htable_cb,argp,&search_ret);

	if(search_htable_result==NULL)
	{
		del_ir_redis_info(*(int*)argp,table_line,rule_id);
		MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_DEBUG, (char*)"nominee_intercept_candidate_table_new_cb","htable data not exist,del it!");
	}
	else
	{
		add_data=(int*)calloc(sizeof(int),1);
		if(add_data==NULL)
		{
			MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_FATAL, (char*)"nominee_intercept_candidate_table_new_cb","malloc add_data error!!");
			exit(1);
		}
		*add_data=rule_id;
		*ad=add_data;
		MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_DEBUG, (char*)"nominee_intercept_candidate_table_new_cb","htable data exist!rule_id is:%d",*(int*)*ad);
	}

	return;
}
void nominee_intercept_candidate_table_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp)
{
	MESA_handle_runtime_log(mctrl_g.logger_handle, RLOG_LV_INFO, (char*)"nominee_intercept_candidate_table_free_cb"," del table_id:%d,del succeed",table_id);

	if(*ad!=NULL)
	{
		free(*ad);
	}
	
	*ad=NULL;
	return;
}

void plugin_EX_dup_cb(int table_id,	MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *from, long argl, void *argp)
{
	*to=*from;
	return;
}

int register_plugin_ex_table(Maat_feather_t feather, int table_id,
										Maat_plugin_EX_new_func_t* new_func,
										Maat_plugin_EX_free_func_t* free_func,
										Maat_plugin_EX_dup_func_t* dup_func,
										Maat_plugin_EX_key2index_func_t* key2index_func,
										long argl, void *argp)
{
	int ret=0;

	ret=Maat_plugin_EX_register(feather,table_id,new_func,free_func,dup_func,key2index_func,argl,argp);
	if(ret<0)
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_FATAL, (char*)"REGISTER_TABLE", "Maat callback register table %s error.\n",argp);
		assert(0);
	}
	
	return ret;
}

void htable_iterate(const uchar * key, uint size, void * data, void * user)
{
	int htable_flag=0;
	int rule_id=0;
	char *table_line=(char*)data;
	sscanf(table_line,"%d\t%d",&htable_flag,&rule_id);
										
	set_ir_redis_info(htable_flag,table_line,rule_id);
											
	return;
}

void Maat_init()
{
// load conf
	const char *section = "Mctrl";
    const char *section_d = "Mctrl_D";
	const char *section_s = "Mctrl_S";
    const char *section_i = "Mctrl_I";
	const char *section_n = "Mctrl_N";
    char table_info_path[MAX_PATH_LEN];
	char logger_path[MAX_PATH_LEN];
    int max_thread_num=0;
	int logger_level=0;
	
    char Maat_redis_ip_d[MAX_STRING_LEN];
    int Maat_redis_port_d=0;
	int Maat_redis_index_d=0;
	
	char Maat_redis_ip_s[MAX_STRING_LEN];
    int Maat_redis_port_s=0;
	int Maat_redis_index_s=0;
	
	char Maat_redis_ip_i[MAX_STRING_LEN];
    int Maat_redis_port_i=0;
	int Maat_redis_index_i=0;
	
	char Maat_redis_ip_n[MAX_STRING_LEN];
    int Maat_redis_port_n=0;
	int Maat_redis_index_n=0;
		
    MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section,"table_info_path", table_info_path, sizeof(table_info_path), "./conf/table_info.conf");
	MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section,"logger_path", logger_path, sizeof(logger_path), "./log/ir_mctrl.log");
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section,"logger_level", &logger_level,RLOG_LV_FATAL);
    MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section,"max_thread_num", &max_thread_num, 1);
//dynamic server conf	
    MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section_d,"Maat_redis_ip", Maat_redis_ip_d, sizeof(Maat_redis_ip_d), "127.0.0.1");
    MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_d,"Maat_redis_port", &Maat_redis_port_d,6379);
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_d,"Maat_redis_index", &Maat_redis_index_d,1);
//static server conf
	MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section_s, "Maat_redis_ip", Maat_redis_ip_s, sizeof(Maat_redis_ip_s), "127.0.0.1");
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_s,"Maat_redis_port", &Maat_redis_port_s,6379);
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_s,"Maat_redis_index", &Maat_redis_index_s,0);
//nominee and candidate conf
	MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section_n,"Maat_redis_ip", Maat_redis_ip_n, sizeof(Maat_redis_ip_n), "127.0.0.1");
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_n,"Maat_redis_port", &Maat_redis_port_n,6379);
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_n,"Maat_redis_index", &Maat_redis_index_n,0);
//intercept conf
	MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section_i,"Maat_redis_ip", Maat_redis_ip_i, sizeof(Maat_redis_ip_i), "127.0.0.1");
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_i,"Maat_redis_port", &Maat_redis_port_i,6379);
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_i,"Maat_redis_index", &Maat_redis_index_i,0);

//log
	mctrl_g.logger_handle=MESA_create_runtime_log_handle(logger_path,logger_level);
	if(mctrl_g.logger_handle == NULL)
	{
		printf("IR MESA_create_runtime_log_handle() error!\n");
		assert(0);
	}

//redis	
	mctrl_g.d_feather = Maat_feather(max_thread_num,table_info_path,mctrl_g.logger_handle);
	mctrl_g.s_feather = Maat_feather(max_thread_num,table_info_path,mctrl_g.logger_handle);
	mctrl_g.n_feather = Maat_feather(max_thread_num,table_info_path,mctrl_g.logger_handle);
	mctrl_g.i_feather = Maat_feather(max_thread_num,table_info_path,mctrl_g.logger_handle);
	
	if(mctrl_g.d_feather==NULL||mctrl_g.s_feather==NULL||mctrl_g.n_feather==NULL||mctrl_g.i_feather==NULL)
	{
	   MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_FATAL,(char*)"MAAT","IR maat_feather error!");
	   assert(0);
	}
		   
	Maat_set_feather_opt(mctrl_g.d_feather,MAAT_OPT_REDIS_IP,Maat_redis_ip_d,MAX_STRING_LEN);
	Maat_set_feather_opt(mctrl_g.d_feather,MAAT_OPT_REDIS_PORT,&Maat_redis_port_d,sizeof(Maat_redis_port_d));
	Maat_set_feather_opt(mctrl_g.d_feather,MAAT_OPT_REDIS_INDEX,&Maat_redis_index_d,sizeof(Maat_redis_index_d));
	Maat_set_feather_opt(mctrl_g.d_feather, MAAT_OPT_INSTANCE_NAME, DYNAMIC_NOMINEE_TABLE_NAME, strlen(DYNAMIC_NOMINEE_TABLE_NAME)+1);
	
	Maat_set_feather_opt(mctrl_g.s_feather,MAAT_OPT_REDIS_IP,Maat_redis_ip_s,MAX_STRING_LEN);
	Maat_set_feather_opt(mctrl_g.s_feather,MAAT_OPT_REDIS_PORT,&Maat_redis_port_s,sizeof(Maat_redis_port_s));
	Maat_set_feather_opt(mctrl_g.s_feather,MAAT_OPT_REDIS_INDEX,&Maat_redis_index_s,sizeof(Maat_redis_index_s));
	Maat_set_feather_opt(mctrl_g.s_feather, MAAT_OPT_INSTANCE_NAME, STATIC_NOMINEE_TABLE_NAME, strlen(STATIC_NOMINEE_TABLE_NAME)+1);
	
	Maat_set_feather_opt(mctrl_g.i_feather,MAAT_OPT_REDIS_IP,Maat_redis_ip_i,MAX_STRING_LEN);
	Maat_set_feather_opt(mctrl_g.i_feather,MAAT_OPT_REDIS_PORT,&Maat_redis_port_i,sizeof(Maat_redis_port_i));
	Maat_set_feather_opt(mctrl_g.i_feather,MAAT_OPT_REDIS_INDEX,&Maat_redis_index_i,sizeof(Maat_redis_index_i));
	Maat_set_feather_opt(mctrl_g.i_feather, MAAT_OPT_INSTANCE_NAME, INTERCEPT_TABLE_NAME, strlen(INTERCEPT_TABLE_NAME)+1);

	Maat_set_feather_opt(mctrl_g.n_feather,MAAT_OPT_REDIS_IP,Maat_redis_ip_n,MAX_STRING_LEN);
	Maat_set_feather_opt(mctrl_g.n_feather,MAAT_OPT_REDIS_PORT,&Maat_redis_port_n,sizeof(Maat_redis_port_n));
	Maat_set_feather_opt(mctrl_g.n_feather,MAAT_OPT_REDIS_INDEX,&Maat_redis_index_n,sizeof(Maat_redis_index_n));
	Maat_set_feather_opt(mctrl_g.n_feather, MAAT_OPT_INSTANCE_NAME, NOMINEE_TABLE_NAME, strlen(NOMINEE_TABLE_NAME)+1);
	Maat_set_feather_opt(mctrl_g.n_feather, MAAT_OPT_INSTANCE_NAME, CANDIDATE_TABLE_NAME, strlen(CANDIDATE_TABLE_NAME)+1);
	
	Maat_initiate_feather(mctrl_g.d_feather);
	Maat_initiate_feather(mctrl_g.s_feather);
	Maat_initiate_feather(mctrl_g.i_feather);
	Maat_initiate_feather(mctrl_g.n_feather);
	
}


int htable_init()
{
	int htable_ret=0;
	mctrl_g.mctrl_htable_handle = MESA_htable_born();
	if(mctrl_g.mctrl_htable_handle == NULL)
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_FATAL,(char*)"htable","htable born failed");
		assert(0);
		return -1;
	}
	
	htable_ret = MESA_htable_mature(mctrl_g.mctrl_htable_handle);
	
	if(0 == htable_ret)
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_INFO,(char*)"htable","htable mature succ");
		return 0;
	}
	else
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_FATAL,(char*)"htable","htable mature failed");
		assert(0);
		return -1;
	}
}

int main(int argc, char * argv [ ])
{
	
	Maat_init();
	htable_init();
	
	int static_flag=STATIC_NOMINEE_FLAG;
	int	dynamic_flag=DYNAMIC_NOMINEE_FLAG;
	int dnat_policy_flag=DNAT_POLICY_FLAG;
	int nominee_flag=NOMINEE_FLAG;
	int candidate_flag=CANDIDATE_FLAG;
	int intercept_flag=INTERCEPT_FLAG;

	int static_id=-1;
	int	dynamic_id=-1;
	int dnat_policy_id=-1;
	int nominee_id=-1;
	int candidate_id=-1;
	int intercept_id=-1;
	
	static_id=Maat_table_register(mctrl_g.s_feather,STATIC_NOMINEE_TABLE_NAME);
	dynamic_id=Maat_table_register(mctrl_g.d_feather,DYNAMIC_NOMINEE_TABLE_NAME);
	dnat_policy_id=Maat_table_register(mctrl_g.s_feather,DNAT_POLICY_TABLE_NAME);

	if(static_id==-1||dynamic_id==-1||dnat_policy_id==-1)
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_FATAL,(char*)"main","Sta_Dyn_policy Database table register failed\n");
		assert(0);
	}
	
	register_plugin_ex_table(mctrl_g.s_feather,static_id,static_dynamic_dpolicy_table_new_cb,static_dynamic_dpolicy_table_free_cb,plugin_EX_dup_cb,NULL,0,&static_flag);
	register_plugin_ex_table(mctrl_g.d_feather,dynamic_id,static_dynamic_dpolicy_table_new_cb,static_dynamic_dpolicy_table_free_cb,plugin_EX_dup_cb,NULL,0,&dynamic_flag);
	register_plugin_ex_table(mctrl_g.s_feather,dnat_policy_id,static_dynamic_dpolicy_table_new_cb,static_dynamic_dpolicy_table_free_cb,plugin_EX_dup_cb,NULL,0,&dnat_policy_flag);
	
	nominee_id=Maat_table_register(mctrl_g.n_feather,NOMINEE_TABLE_NAME);
	candidate_id=Maat_table_register(mctrl_g.n_feather,CANDIDATE_TABLE_NAME);
	intercept_id=Maat_table_register(mctrl_g.i_feather,INTERCEPT_TABLE_NAME);
	
	if(nominee_id==-1||candidate_id==-1||intercept_id==-1)
	{
		MESA_handle_runtime_log(mctrl_g.logger_handle,RLOG_LV_FATAL,(char*)"main","Nom_Candidate_Intercept Database table register failed\n");
		assert(0);
	}

	register_plugin_ex_table(mctrl_g.i_feather,intercept_id,nominee_intercept_candidate_table_new_cb,nominee_intercept_candidate_table_free_cb,plugin_EX_dup_cb,NULL,0,&intercept_flag);
	register_plugin_ex_table(mctrl_g.n_feather,nominee_id,nominee_intercept_candidate_table_new_cb,nominee_intercept_candidate_table_free_cb,plugin_EX_dup_cb,NULL,0,&nominee_flag);
	register_plugin_ex_table(mctrl_g.n_feather,candidate_id,nominee_intercept_candidate_table_new_cb,nominee_intercept_candidate_table_free_cb,plugin_EX_dup_cb,NULL,0,&candidate_flag);

	while(1)
	{
		MESA_htable_iterate(mctrl_g.mctrl_htable_handle, htable_iterate, NULL);
		sleep(SLEEP_TIME);
	}
		
	Maat_burn_feather(mctrl_g.d_feather);
	Maat_burn_feather(mctrl_g.s_feather);
	Maat_burn_feather(mctrl_g.i_feather);
	Maat_burn_feather(mctrl_g.n_feather);
	MESA_destroy_runtime_log_handle(mctrl_g.logger_handle);
	return 0;
	
}