summaryrefslogtreecommitdiff
path: root/src/ir_mctrl.cpp
blob: a23cdd853b783a9ddcae3458f058bc682aa87d04 (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
#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 "ir_mctrl.h"
#include "MESA_htable.h"

void *logger_handle;
Maat_feather_t mctrl_d_feather;
Maat_feather_t mctrl_s_feather;
MESA_htable_handle s_and_d_nominee_htable;


void Maat_start_cb(int update_type,void* u_para)
{
	if(update_type==MAAT_RULE_UPDATE_TYPE_FULL)
	{
		MESA_handle_runtime_log(logger_handle,RLOG_LV_INFO,(char*)"maat_rule_type", "table_name:%s,Maat rule type is full",u_para);
	}
	else
	{
		MESA_handle_runtime_log(logger_handle,RLOG_LV_INFO,(char*)"maat_rule_type", "table_name:%s,Maat rule type is inc",u_para);
	}
	return;
}

void 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, 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 nom_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%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", 
		&nom_info.region_id,&nom_info.group_id,&nom_info.addr_type,
		nom_info.src_ip,nom_info.mask_src_ip,nom_info.src_port,nom_info.mask_src_port,nom_info.dst_ip,nom_info.mask_dst_ip,
		nom_info.dst_port,nom_info.mask_dst_port,&nom_info.procotol,&nom_info.direction,&nom_info.is_valid,
		&nom_info.action,&nom_info.service,&nom_info.addr_pool_id,nom_info.op_time);

	get_cur_time(nom_info.op_time);
	
	if(memcmp(ir_table_name,INTERCEPT_IP,strlen(INTERCEPT_IP)))
	{
		snprintf(m_table_line,sizeof(m_table_line),"%d\t%d\t%d\t%s\t%d\t%s",
			rule_id,nom_info.addr_pool_id,nom_info.addr_type,nom_info.src_ip,nom_info.is_valid,nom_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",
			rule_id,nom_info.group_id,nom_info.addr_type,
			nom_info.src_ip,nom_info.mask_src_ip,nom_info.src_port,nom_info.mask_src_port,nom_info.dst_ip,nom_info.mask_dst_ip,
			nom_info.dst_port,nom_info.mask_dst_port,nom_info.procotol,nom_info.direction,
			nom_info.is_valid,nom_info.action,nom_info.service,nom_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(logger_handle,RLOG_LV_INFO, (char*)"maat_update", "set_table_line:%s",m_table_line);
	
	if(ret==-1)
	{
		MESA_handle_runtime_log(logger_handle, RLOG_LV_INFO, (char*)"SET_LINE","%s:set redis line error",ir_table_name);
	}
	else
	{
		MESA_handle_runtime_log(logger_handle, RLOG_LV_INFO, (char*)"SET_LINE","%s:set redis line success",ir_table_name);
	}
	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;
	if(!memcmp(ir_table_name,INTERCEPT_IP,strlen(INTERCEPT_IP)))
	{
		line_rule.table_line=table_line;
		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.table_line=NULL;
		line_rule.expire_after=0;
		p_line=&line_rule;
		ret=Maat_cmd_set_line(feather, p_line, MAAT_OP_DEL);
	}

	MESA_handle_runtime_log(logger_handle,RLOG_LV_INFO, (char*)"maat_update", "del_table_line:%s",table_line);

	
	if(ret==-1)
	{
		MESA_handle_runtime_log(logger_handle, RLOG_LV_INFO, (char*)"DEL_LINE","%s:del redis line error",ir_table_name);
	}
	else if(ret==1)
	{
		MESA_handle_runtime_log(logger_handle, RLOG_LV_INFO, (char*)"DEL_LINE","%s:del redis line success",ir_table_name);
	}
	else 
	{
		MESA_handle_runtime_log(logger_handle, RLOG_LV_INFO, (char*)"DEL_LINE","%s:del redis line not sure",ir_table_name);
	}
	return ret;
	
}


long htable_write_search(void *data, const uchar *key, uint size, void *user_arg)
{
	int htable_flag=0;
	char *htable_data=(char*)data;
	if(htable_data!=NULL)
	{
		sscanf(htable_data,"%d\t",&htable_flag);
		MESA_handle_runtime_log(logger_handle, RLOG_LV_DEBUG, (char*)"htable_write_search","get htable flag:%d!",htable_flag);
	}
	return htable_flag;
}

long htable_read_search(void *data, const uchar *key, uint size, void *user_arg)
{
	int htable_flag=0;
	struct IR_MCTRL_INFO nom_info;
	char *htable_data=(char*)data;
	
	if(htable_data!=NULL)
	{
		sscanf((char*)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", 
			&htable_flag,&nom_info.region_id,&nom_info.group_id,&nom_info.addr_type,
			nom_info.src_ip,nom_info.mask_src_ip,nom_info.src_port,nom_info.mask_src_port,nom_info.dst_ip,nom_info.mask_dst_ip,
			nom_info.dst_port,nom_info.mask_dst_port,&nom_info.procotol,&nom_info.direction,&nom_info.is_valid,
			&nom_info.action,&nom_info.service,&nom_info.addr_pool_id,nom_info.op_time);
		
		if(!memcmp(user_arg,INTERCEPT_IP,strlen(INTERCEPT_IP))&&(htable_flag==S_OR_D_ORIGIN_FLAG||htable_flag==O_AND_C_FLAG||
			htable_flag==O_AND_N_FLAG||htable_flag==O_AND_C_AND_N_FLAG))
		{
			htable_flag+=INTERCEPT_FLAG;
		}
		else if(!memcmp(user_arg,NOMINEE_IP,strlen(NOMINEE_IP))&&(htable_flag==S_OR_D_ORIGIN_FLAG||htable_flag==O_AND_C_FLAG||
			htable_flag==O_AND_I_FLAG||htable_flag==O_AND_C_AND_I_FLAG))
		{
			htable_flag+=NOMINEE_FLAG;
		}
		else if(!memcmp(user_arg,CANDIDATE_IP,strlen(CANDIDATE_IP))&&(htable_flag==S_OR_D_ORIGIN_FLAG||htable_flag==O_AND_N_FLAG||
			htable_flag==O_AND_I_FLAG||htable_flag==O_AND_I_AND_N_FLAG))
		{
			htable_flag+=CANDIDATE_FLAG;
		}
		else 
		{
			MESA_handle_runtime_log(logger_handle, RLOG_LV_DEBUG, (char*)"htable_read_search","htable flag is:%d",htable_flag);	
		}
		
		snprintf(htable_data,sizeof(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", 
			htable_flag,nom_info.region_id,nom_info.group_id,nom_info.addr_type,
			nom_info.src_ip,nom_info.mask_src_ip,nom_info.src_port,nom_info.mask_src_port,nom_info.dst_ip,nom_info.mask_dst_ip,
			nom_info.dst_port,nom_info.mask_dst_port,nom_info.procotol,nom_info.direction,nom_info.is_valid,
			nom_info.action,nom_info.service,nom_info.addr_pool_id,nom_info.op_time);
		
		MESA_handle_runtime_log(logger_handle, RLOG_LV_DEBUG, (char*)"htable_read_search","htable flag change:%d",htable_flag);	
	}
	
	return htable_flag;
}


void read_table_update_cb(int table_id,const char* table_line,void* u_para)
{
	int rule_id=0;
	void *htable_data=NULL;
	int is_valid=-1;
	long search_ret=0;

	if(!memcmp(u_para,INTERCEPT_IP,strlen(INTERCEPT_IP)))
	{
		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%*s",&rule_id,&is_valid);
	}
	else if(!memcmp(u_para,NOMINEE_IP,strlen(NOMINEE_IP)))
	{
		sscanf(table_line,"%d\t%*d\t%*d\t%*s\t%d\t%*s",&rule_id,&is_valid);
	}
	else
	{
		sscanf(table_line,"%d\t%*d\t%*d\t%*s\t%*d\t%*s\t%*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",&rule_id,&is_valid);
	}

	unsigned char *key_id=(unsigned char*)&rule_id;

	if(is_valid==0)
	{
		MESA_handle_runtime_log(logger_handle, RLOG_LV_DEBUG, (char*)"READ_TABLE_UPDATA","table_name:%s del redis is_valid==0",u_para);
		return;
	}
	
	htable_data=MESA_htable_search_cb(s_and_d_nominee_htable,key_id,sizeof(rule_id), htable_read_search,u_para,&search_ret);

	if(htable_data==NULL)
	{
		del_ir_line(mctrl_d_feather,(char*)u_para,table_line,rule_id);
	}
	else
	{
		MESA_handle_runtime_log(logger_handle, RLOG_LV_DEBUG, (char*)"READ_TABLE_UPDATA","already exist!");
	}
	return;
}

void htable_data_free(void *data)
{
	if(data!=NULL)
	{
		MESA_handle_runtime_log(logger_handle, RLOG_LV_DEBUG, (char*)"DATA_FREE","htable_data_free!");
		free(data);
		data=NULL;
	}
	return;
}

void write_table_update_cb(int table_id,const char* table_line,void* u_para)
{
	int add_ret=0;
	struct IR_MCTRL_INFO nom_info;
	nom_info.htable_flag=S_OR_D_ORIGIN_FLAG;
	int rule_id=0;
	int del_ret=0;
	long cb_ret=0;

	if(!memcmp(u_para,DYNAMIC_NOMINEE_IP,strlen(DYNAMIC_NOMINEE_IP)))
	{
		sscanf(table_line,"%d\t%d\t%d\t%s\t%s\t%d\t%*s\t%*d\t%d\t%s", 
			&nom_info.region_id,&nom_info.addr_type,&nom_info.procotol,nom_info.src_ip,nom_info.src_port,
			&nom_info.direction,&nom_info.is_valid,nom_info.op_time);
		nom_info.region_id+=1000000000;
		nom_info.addr_pool_id=0;
		nom_info.group_id=nom_info.region_id;
		memcpy(nom_info.mask_src_ip,"0.0.0.0",sizeof("0.0.0.0"));
		memcpy(nom_info.mask_src_port,"0",sizeof("0"));
		memcpy(nom_info.dst_ip,"0.0.0.0",sizeof("0.0.0.0"));
		memcpy(nom_info.mask_dst_ip,"0.0.0.0",sizeof("0.0.0.0"));
		memcpy(nom_info.dst_port,"0",sizeof("0"));
		memcpy(nom_info.mask_dst_port,"0",sizeof("0"));
		nom_info.action=0x60;
		nom_info.service=19;
	}
	else
	{
		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", 
			&nom_info.region_id,&nom_info.group_id,&nom_info.addr_type,
			nom_info.src_ip,nom_info.mask_src_ip,nom_info.src_port,nom_info.mask_src_port,nom_info.dst_ip,nom_info.mask_dst_ip,
			nom_info.dst_port,nom_info.mask_dst_port,&nom_info.procotol,&nom_info.direction,&nom_info.is_valid,
			&nom_info.action,&nom_info.service,&nom_info.addr_pool_id,nom_info.op_time);
	}

	unsigned char *key_id=(unsigned char*)&nom_info.region_id;
	rule_id=nom_info.region_id;

	if(nom_info.is_valid==1) 
	{
		char *htable_data=(char*)malloc(HTABLE_DATA_LEN);
		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", 
			nom_info.htable_flag,nom_info.region_id,nom_info.group_id,nom_info.addr_type,
			nom_info.src_ip,nom_info.mask_src_ip,nom_info.src_port,nom_info.mask_src_port,nom_info.dst_ip,nom_info.mask_dst_ip,
			nom_info.dst_port,nom_info.mask_dst_port,nom_info.procotol,nom_info.direction,nom_info.is_valid,
			nom_info.action,nom_info.service,nom_info.addr_pool_id,nom_info.op_time);
		
		add_ret=MESA_htable_add(s_and_d_nominee_htable,key_id,sizeof(int),htable_data);

		if(add_ret<0)
		{
			MESA_handle_runtime_log(logger_handle, RLOG_LV_DEBUG, (char*)"ADD_S_AND_D_NOMINEE_HASH","add htable error:%d",add_ret);
		}
		else
		{
			MESA_handle_runtime_log(logger_handle, RLOG_LV_DEBUG, (char*)"ADD_S_AND_D_NOMINEE_HASH","add htable succeed:%d",add_ret);
		}
		
	}
	else
	{
		assert(nom_info.is_valid<=1);
	}
	
	MESA_htable_search_cb(s_and_d_nominee_htable,key_id,sizeof(rule_id),htable_write_search,NULL,&cb_ret);

	switch(nom_info.is_valid)
	{
		case 0:	
			del_ir_line(mctrl_d_feather,(char*)CANDIDATE_IP,table_line,rule_id);
			del_ir_line(mctrl_d_feather,(char*)INTERCEPT_IP,table_line,rule_id);
			del_ir_line(mctrl_d_feather,(char*)NOMINEE_IP,table_line,rule_id);
			del_ret=MESA_htable_del(s_and_d_nominee_htable,key_id,sizeof(rule_id),htable_data_free);
		
			if(del_ret<0)
			{
				MESA_handle_runtime_log(logger_handle, RLOG_LV_DEBUG, (char*)"DEL_S_AND_D_NOMINEE","del htable error:%d",del_ret);
			}
			break;
			
		case 1:
			if(cb_ret==S_OR_D_ORIGIN_FLAG||cb_ret==O_AND_C_FLAG)
			{
				set_ir_line(mctrl_d_feather, (char*)INTERCEPT_IP,table_line,rule_id);
				set_ir_line(mctrl_d_feather,(char*)NOMINEE_IP,table_line,rule_id);
			}
			else if(cb_ret==O_AND_N_FLAG||cb_ret==O_AND_C_AND_N_FLAG)
			{
				set_ir_line(mctrl_d_feather, (char*)INTERCEPT_IP,table_line,rule_id);
			}
			else if(cb_ret==O_AND_I_FLAG||cb_ret==O_AND_C_AND_I_FLAG)
			{
				set_ir_line(mctrl_d_feather,(char*)NOMINEE_IP,table_line,rule_id);
			}
			else
			{
				MESA_handle_runtime_log(logger_handle,RLOG_LV_INFO, (char*)"write_update", "exist already !! htable_flag is:%d",cb_ret);
				assert(cb_ret<=ALL_EXIST_FLAG);
			}
			break;
			
		default:
			MESA_handle_runtime_log(logger_handle,RLOG_LV_INFO, (char*)"write_update", "is_valid default !!!");
			assert(0);
			break;
	}

	return;
}

void Maat_finish_cb(void* u_para)
{
	MESA_handle_runtime_log(logger_handle,RLOG_LV_INFO, (char*)"maat_finish", "table_name:%s,finish succeed",u_para);
	return;
}


int read_plugin_table(Maat_feather_t feather,const char* table_name,
							Maat_start_callback_t *start,Maat_update_callback_t *update,Maat_finish_callback_t *finish,
							void *u_para,void* logger,int table_id)
{
	int ret=0;

	ret=Maat_table_callback_register(feather, table_id,
								start,
								update, 
								finish,
								u_para);
	if(ret<0)
	{
		MESA_handle_runtime_log(logger_handle,RLOG_LV_FATAL, (char*)"REGISTER_TABLE", "Maat callback register table %s error.\n",table_name);
		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[HTABLE_DATA_LEN];
	struct IR_MCTRL_INFO nom_info;
	
	sscanf((char*)data,"%d\t%d",&htable_flag,&rule_id);
	
	if(htable_flag<O_AND_I_AND_N_FLAG)
	{
		sscanf((char*)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", 
			&nom_info.region_id,&nom_info.group_id,&nom_info.addr_type,
			nom_info.src_ip,nom_info.mask_src_ip,nom_info.src_port,nom_info.mask_src_port,nom_info.dst_ip,nom_info.mask_dst_ip,
			nom_info.dst_port,nom_info.mask_dst_port,&nom_info.procotol,&nom_info.direction,&nom_info.is_valid,
			&nom_info.action,&nom_info.service,&nom_info.addr_pool_id,nom_info.op_time);
		
		get_cur_time(nom_info.op_time);
		
		snprintf(table_line,sizeof(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", 
			nom_info.region_id,nom_info.group_id,nom_info.addr_type,
			nom_info.src_ip,nom_info.mask_src_ip,nom_info.src_port,nom_info.mask_src_port,nom_info.dst_ip,nom_info.mask_dst_ip,
			nom_info.dst_port,nom_info.mask_dst_port,nom_info.procotol,nom_info.direction,nom_info.is_valid,
			nom_info.action,nom_info.service,nom_info.addr_pool_id,nom_info.op_time);

		if(htable_flag==S_OR_D_ORIGIN_FLAG||htable_flag==O_AND_C_FLAG)
		{
			set_ir_line(mctrl_d_feather, (char*)INTERCEPT_IP,table_line,rule_id);
			set_ir_line(mctrl_d_feather,(char*)NOMINEE_IP,table_line,rule_id);
		}
		else if(htable_flag==O_AND_N_FLAG||htable_flag==O_AND_C_AND_N_FLAG)
		{
			set_ir_line(mctrl_d_feather, (char*)INTERCEPT_IP,table_line,rule_id);
		}
		else if(htable_flag==O_AND_I_FLAG||htable_flag==O_AND_C_AND_I_FLAG)
		{
			set_ir_line(mctrl_d_feather,(char*)NOMINEE_IP,table_line,rule_id);
		}
		else
		{
			MESA_handle_runtime_log(logger_handle,RLOG_LV_INFO, (char*)"htable_iterate", "htable flag >=14: %d!",htable_flag);
			assert(htable_flag>=S_OR_D_ORIGIN_FLAG);
		}
	}
	else 
	{
		MESA_handle_runtime_log(logger_handle,RLOG_LV_INFO, (char*)"htable_iterate", "already exist,htable flag is:%d!",htable_flag);
		assert(htable_flag<=ALL_EXIST_FLAG);
	}

	return;
}

void Maat_init()
{
// load conf
    const char *section_d = "Mctrl";
	const char *section_s = "Mctrl_S";
    char table_info_path[MAX_PATH_LEN];
	char logger_path[MAX_PATH_LEN];
    int max_thread_num=0;
    char Maat_redis_ip[MAX_IP4_LEN];
    int Maat_redis_port=0;
	char Maat_redis_ip_s[MAX_IP4_LEN];
    int Maat_redis_port_s=0;
    char stat_file_path[MAX_PATH_LEN];
	int logger_level=0;
	
//dynamic server conf
    MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section_d, "table_info_path", table_info_path, sizeof(table_info_path), "./conf/table_info.conf");
	MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section_d, "logger_path", logger_path, sizeof(logger_path), "./log/ir.log");
    MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_d, "max_thread_num", &max_thread_num, 1);
    MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section_d, "Maat_redis_ip", Maat_redis_ip, sizeof(Maat_redis_ip), "127.0.0.1");
    MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_d, "Maat_redis_port", &Maat_redis_port,6379);
    MESA_load_profile_string_def((char*)MCTRL_CONF_FILE, section_d, "stat_file_path", stat_file_path, sizeof(stat_file_path), "./log/Maat_stat.log");
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_d, "logger_level", &logger_level,RLOG_LV_DEBUG);
//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), "127.0.0.1");
	MESA_load_profile_int_def((char*)MCTRL_CONF_FILE, section_s, "Maat_redis_port", &Maat_redis_port_s,6379);


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

//redis	
	mctrl_d_feather = Maat_feather(max_thread_num, table_info_path,logger_handle);
	mctrl_s_feather = Maat_feather(max_thread_num, table_info_path,logger_handle);
	if(mctrl_d_feather==NULL||mctrl_s_feather==NULL)
	{
	   MESA_handle_runtime_log(logger_handle,RLOG_LV_FATAL,(char*)"MAAT","IR maat_feather error!");
	   assert(0);
	}
		   
	Maat_set_feather_opt(mctrl_d_feather,MAAT_OPT_REDIS_IP,Maat_redis_ip,MAX_IP4_LEN);
	Maat_set_feather_opt(mctrl_d_feather,MAAT_OPT_REDIS_PORT,&Maat_redis_port,sizeof(Maat_redis_port));
	Maat_set_feather_opt(mctrl_s_feather,MAAT_OPT_REDIS_IP,Maat_redis_ip_s,MAX_IP4_LEN);
	Maat_set_feather_opt(mctrl_s_feather,MAAT_OPT_REDIS_PORT,&Maat_redis_port_s,sizeof(Maat_redis_port));
	
}


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

int main(int argc, char * argv [ ])
{
	
	Maat_init();
	
	Maat_set_feather_opt(mctrl_d_feather, MAAT_OPT_INSTANCE_NAME, DYNAMIC_NOMINEE_IP, strlen(DYNAMIC_NOMINEE_IP)+1);
	Maat_set_feather_opt(mctrl_d_feather, MAAT_OPT_INSTANCE_NAME, INTERCEPT_IP, strlen(INTERCEPT_IP)+1);
	Maat_set_feather_opt(mctrl_d_feather, MAAT_OPT_INSTANCE_NAME, NOMINEE_IP, strlen(NOMINEE_IP)+1);
	Maat_set_feather_opt(mctrl_d_feather, MAAT_OPT_INSTANCE_NAME, CANDIDATE_IP, strlen(CANDIDATE_IP)+1);

	Maat_set_feather_opt(mctrl_s_feather, MAAT_OPT_INSTANCE_NAME, STATIC_NOMINEE_IP, strlen(STATIC_NOMINEE_IP)+1);
	
	Maat_initiate_feather(mctrl_d_feather);
	Maat_initiate_feather(mctrl_s_feather);

	htable_init();

	char  static_nominee[]=STATIC_NOMINEE_IP;
	char  dynamic_nominee[]=DYNAMIC_NOMINEE_IP;
	char  nominee[]=NOMINEE_IP;
	char  candidate[]=CANDIDATE_IP;
	char  intercept[]=INTERCEPT_IP;

	int static_id=-1;
	int	dynamic_id=-1;
	int nominee_id=-1;
	int candidate_id=-1;
	int intercept_id=-1;

	static_id=Maat_table_register(mctrl_s_feather,static_nominee);
	dynamic_id=Maat_table_register(mctrl_d_feather,dynamic_nominee);
	nominee_id=Maat_table_register(mctrl_d_feather,nominee);
	candidate_id=Maat_table_register(mctrl_d_feather,candidate);
	intercept_id=Maat_table_register(mctrl_d_feather, intercept);
	
	if(static_id==-1||dynamic_id==-1||nominee_id==-1||candidate_id==-1||intercept_id==-1)
	{
		MESA_handle_runtime_log(logger_handle,RLOG_LV_FATAL,(char*)"REGISTER_TABLE","Database table register failed\n");
	}

	read_plugin_table(mctrl_s_feather,STATIC_NOMINEE_IP,Maat_start_cb,write_table_update_cb,Maat_finish_cb,
		static_nominee,logger_handle,static_id);
	read_plugin_table(mctrl_d_feather,DYNAMIC_NOMINEE_IP,Maat_start_cb,write_table_update_cb,Maat_finish_cb,
		dynamic_nominee,logger_handle,dynamic_id);
		
	read_plugin_table(mctrl_d_feather,INTERCEPT_IP,Maat_start_cb,read_table_update_cb,Maat_finish_cb,
		intercept,logger_handle,intercept_id);
	read_plugin_table(mctrl_d_feather,NOMINEE_IP,Maat_start_cb,read_table_update_cb,Maat_finish_cb,
		nominee,logger_handle,nominee_id);
	read_plugin_table(mctrl_d_feather,CANDIDATE_IP,Maat_start_cb,read_table_update_cb,Maat_finish_cb,
		candidate,logger_handle,candidate_id);


	while(1)
	{
		MESA_htable_iterate(s_and_d_nominee_htable, htable_iterate, NULL);
		sleep(3600);
	}
		
	Maat_burn_feather(mctrl_d_feather);
	Maat_burn_feather(mctrl_s_feather);
	MESA_destroy_runtime_log_handle(logger_handle);
	MESA_htable_destroy(s_and_d_nominee_htable,htable_data_free);
	return 0;
	
}