summaryrefslogtreecommitdiff
path: root/access/src/ip_mgr.cpp
blob: 980eb7860a1b90db87daa800126378b4d1640de7 (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
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


#include "mgw_utils.h"
#include "ip_mgr.h"
#define SNAT_CAND_IP_GROUP_MAX 4096

static struct field_stat_handle *g_fs_handle = NULL;

struct ip_mgr_handle
{
    void *logger;
    Maat_feather_t Maat_feather_snat_policy;
    Maat_feather_t Maat_feather_dnat_policy;
    Maat_feather_t Maat_feather_snat_cand_ip;
    Maat_feather_t Maat_feather_mrl_ip;
    MESA_htable_handle snat_policy_htable;  //expire_time: 0
    MESA_htable_handle dnat_policy_htable;  //expire_time: 0
    MESA_htable_handle snat_cand_ip_htable; //expire_time: 0
    MESA_htable_handle mrl_ip_htable; //expire_time: 0
};

struct snat_policy_htable_value
{
    char user_name[MGW_SYMBOL_MAX];
    int group_id;
};

struct dnat_policy_htable_key
{
    uint32_t orig_ip;
    uint16_t orig_port;
};

struct dnat_policy_htable_value
{
    uint32_t trans_ip;
    uint16_t trans_port;
};

struct mrl_ip_htable_value
{
    uint32_t back_ip;
    uint32_t mrl_ip;
};

struct snat_cand_ip_detail
{
    uint32_t ip;
    int group_id;
    int addr_type;
    int location;
    int reference;
};

struct snat_cand_ip_group
{
    struct snat_cand_ip_detail *ip_list[SNAT_CAND_IP_GROUP_MAX];
    unsigned int num;
};

struct snat_cand_ip_query_cb_get_args
{
    struct ip_mgr_handle *handle;
    int group_id;
    uint32_t *ip;
    bool existed;
};

struct snat_cand_ip_query_cb_add_args
{
    struct ip_mgr_handle *handle;
    int group_id;
    struct snat_cand_ip_detail ip_detail;
};

struct snat_cand_ip_query_cb_del_args
{
    struct ip_mgr_handle *handle;
    int group_id;
    uint32_t ip;
};

static long snat_policy_query_cb(void *data, const uchar *key, uint size, void *user_args)
{
    struct snat_policy_htable_value *dup_value = (struct snat_policy_htable_value *)user_args;
    if(data != NULL)
    {
        struct snat_policy_htable_value *_data = (struct snat_policy_htable_value *)data;
        strncpy(dup_value->user_name, _data->user_name, MGW_SYMBOL_MAX);
        dup_value->group_id = _data->group_id;
        return MGW_HTABLE_KEY_EXISTED;
    }
    return MGW_HTABLE_KEY_NOT_EXISTED;
}

static long dnat_policy_query_cb(void *data, const uchar *key, uint size, void *user_args)
{
    struct dnat_policy_htable_value *dup_value = (struct dnat_policy_htable_value *)user_args;
    if(data != NULL)
    {
        struct dnat_policy_htable_value *_data = (struct dnat_policy_htable_value *)data;
        dup_value->trans_ip = _data->trans_ip;
        dup_value->trans_port = _data->trans_port;
        return MGW_HTABLE_KEY_EXISTED;
    }
    return MGW_HTABLE_KEY_NOT_EXISTED;
}

static long mrl_ip_query_cb(void *data, const uchar *key, uint size, void *user_args)
{
    struct mrl_ip_htable_value *dup_value = (struct mrl_ip_htable_value *)user_args;
    if(data != NULL)
    {
        struct mrl_ip_htable_value *_data = (struct mrl_ip_htable_value *)data;
        dup_value->back_ip = _data->back_ip;
        dup_value->mrl_ip = _data->mrl_ip;
        return MGW_HTABLE_KEY_EXISTED;
    }
    return MGW_HTABLE_KEY_NOT_EXISTED;
}

int ip_mgr_dnat_policy_query(struct ip_mgr_handle *handle, uint32_t orig_ip, uint16_t orig_port, uint32_t *trans_ip, uint16_t *trans_port)
{
    void *logger = handle->logger;
    long cb_rtn = -1;
    struct dnat_policy_htable_key *key = ALLOC(struct dnat_policy_htable_key, 1);
    key->orig_ip = orig_ip;
    key->orig_port = orig_port;
    char orig_ip_str[MGW_SYMBOL_MAX];
    mgw_utils_inet_ntoa(orig_ip, orig_ip_str);
    struct dnat_policy_htable_value dup_value;
    MESA_htable_search_cb(handle->dnat_policy_htable, (const unsigned char *)key, sizeof(struct dnat_policy_htable_key), 
                        dnat_policy_query_cb, &dup_value, &cb_rtn);
    FREE(&key);
    FS_operate(g_fs_handle->handle, g_fs_handle->line_dnat_policy, g_fs_handle->cloumn_query_num, FS_OP_ADD, 1);
    if(cb_rtn == MGW_HTABLE_KEY_NOT_EXISTED)
    {
        FS_operate(g_fs_handle->handle, g_fs_handle->line_dnat_policy, g_fs_handle->cloumn_cache_miss, FS_OP_ADD, 1);
        MGW_LOG_ERROR(logger, "MESA_htable: key not existed, table is %s, original dest is <%s:%d>", "dnat_policy_htable", orig_ip_str, ntohs(orig_port));
        return -1;
    }
    *trans_ip = dup_value.trans_ip;
    *trans_port = dup_value.trans_port;
    FS_operate(g_fs_handle->handle, g_fs_handle->line_dnat_policy, g_fs_handle->cloumn_cache_hit, FS_OP_ADD, 1);
    MGW_LOG_INFO(logger, "MESA_htable: key existed, table is %s, original dest is <%s:%d>", "dnat_policy_htable", orig_ip_str, ntohs(orig_port));
    return 0; 
}

int ip_mgr_mrl_ip_query(struct ip_mgr_handle *handle, uint32_t back_ip, uint32_t *mrl_ip)
{
    void *logger = handle->logger;
    long cb_rtn = -1;
    char back_ip_str[MGW_SYMBOL_MAX];
    char mrl_ip_str[MGW_SYMBOL_MAX];
    mgw_utils_inet_ntoa(back_ip, back_ip_str);
    struct mrl_ip_htable_value dup_value;
    MESA_htable_search_cb(handle->mrl_ip_htable, (const unsigned char *)(&back_ip), sizeof(uint32_t), 
                        mrl_ip_query_cb, &dup_value, &cb_rtn);
    FS_operate(g_fs_handle->handle, g_fs_handle->line_mrl_ip, g_fs_handle->cloumn_query_num, FS_OP_ADD, 1);
    if(cb_rtn == MGW_HTABLE_KEY_NOT_EXISTED)
    {
        FS_operate(g_fs_handle->handle, g_fs_handle->line_mrl_ip, g_fs_handle->cloumn_cache_miss, FS_OP_ADD, 1);
        MGW_LOG_ERROR(logger, "MESA_htable: key not existed, table is %s, back ip is %s", "mrl_ip_htable", back_ip_str);
        return -1;
    }
    mgw_utils_inet_ntoa(dup_value.mrl_ip, mrl_ip_str);
    *mrl_ip = dup_value.mrl_ip;
    FS_operate(g_fs_handle->handle, g_fs_handle->line_mrl_ip, g_fs_handle->cloumn_cache_hit, FS_OP_ADD, 1);
    MGW_LOG_INFO(logger, "MESA_htable: key existed, table is %s, back ip is %s, mrl ip is %s", "mrl_ip_htable", back_ip_str, mrl_ip_str);
    return 0; 
}

static long snat_cand_ip_query_cb_get(void *data, const uchar *key, uint size, void *user_args)
{
    struct snat_cand_ip_query_cb_get_args *args = (struct snat_cand_ip_query_cb_get_args *)user_args;
    struct ip_mgr_handle *handle = args->handle;
    void *logger = handle->logger;
    int group_id = args->group_id;
    if(data == NULL)
    {
        MGW_LOG_INFO(logger, "MESA_htable: key not existed, table is %s, group_id is %d", "snat_cand_ip_htable", group_id);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_cache_miss, FS_OP_ADD, 1);
        args->existed = false;
    }
    else
    {
        struct snat_cand_ip_group *ip_group = (struct snat_cand_ip_group *)data;
        MGW_LOG_INFO(logger, "MESA_htable: key existed, table is %s, group_id is %d", "snat_cand_ip_htable", group_id);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_cache_hit, FS_OP_ADD, 1);
        unsigned int num = ip_group->num;
        if(num <= 0)
        {
            MGW_LOG_ERROR(logger, "group_id is %d, num of snat candidate ip is %d", group_id, num);
            args->existed = false;
            return MGW_HTABLE_QUERY_CB_SUCCESS;
        }
        unsigned int index = mgw_utils_get_random(num);
        *(args->ip) = ip_group->ip_list[index]->ip;
        args->existed = true;
        return MGW_HTABLE_QUERY_CB_SUCCESS;
    }
    return MGW_HTABLE_QUERY_CB_SUCCESS;
}

int ip_mgr_snat_cand_ip_get(struct ip_mgr_handle *handle, const char *user_name, uint32_t *selected_ip)
{
    void *logger = handle->logger;
    long cb_rtn = 0;
    int group_id;
    int key_size = strnlen(user_name, MGW_SYMBOL_MAX);
    //query snat_policy
    MESA_htable_search_cb(handle->snat_policy_htable, (const unsigned char *)user_name, key_size, 
            snat_policy_query_cb, &group_id, &cb_rtn);
    FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_policy, g_fs_handle->cloumn_query_num, FS_OP_ADD, 1);
    if(cb_rtn == MGW_HTABLE_KEY_EXISTED)
    {
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_policy, g_fs_handle->cloumn_cache_hit, FS_OP_ADD, 1);
        MGW_LOG_INFO(logger, "MESA_htable: key existed, table is %s, user_name is %s, group id is %d", "snat_policy", user_name, group_id);
        //query snat_cand_ip_htable
        long cb_rtn = -1;
        struct snat_cand_ip_query_cb_get_args args;
        args.handle = handle;
        args.ip = selected_ip;
        MESA_htable_search_cb(handle->snat_cand_ip_htable, (const unsigned char *)(&group_id), sizeof(group_id), 
            snat_cand_ip_query_cb_get, &args, &cb_rtn);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_query_num, FS_OP_ADD, 1);
        if(args.existed)
        {
            return 0;
        }
        else
        {
            MGW_LOG_ERROR(logger, "Failed at find available snat candidate ip, group id is %d", group_id);
            return -1;
        }
    }
    else
    {
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_policy, g_fs_handle->cloumn_cache_miss, FS_OP_ADD, 1);
        MGW_LOG_ERROR(logger, "MESA_htable: key not existed. table is %s, key is %s", "snat_policy_htable", user_name);
        return -1;
    }
}

static void snat_policy_htable_data_free_cb(void *data)
{
    FREE(&data);
    FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_policy, g_fs_handle->cloumn_element_num, FS_OP_ADD, -1);
}

static void dnat_policy_htable_data_free_cb(void *data)
{
    FREE(&data);
    FS_operate(g_fs_handle->handle, g_fs_handle->line_dnat_policy, g_fs_handle->cloumn_element_num, FS_OP_ADD, -1);
}

static void snat_cand_ip_htable_data_free_cb(void *data)
{
    struct snat_cand_ip_group *ip_group = (struct snat_cand_ip_group *)data;
    int num = ip_group->num;
    for(int i = 0; i < num; i++)
    {
        FREE(&(ip_group->ip_list[i]));
    }
    FREE(&data);
    FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_element_num, FS_OP_ADD, -1);
}

static void mrl_ip_htable_data_free_cb(void *data)
{
    FS_operate(g_fs_handle->handle, g_fs_handle->line_mrl_ip, g_fs_handle->cloumn_element_num, FS_OP_ADD, -1);
    FREE(&data);
}

static void Maat_snat_policy_start_cb(int update_type, void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: start callback, table is IP_SNAT_POLICY");
	return;
}

static void Maat_snat_policy_update_cb(int table_id, const char* table_line, void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: update callback, table is IP_SNAT_POLICY");
    int config_id, group_id, is_valid;
    char user_type[MGW_SYMBOL_MAX];
    char user_name[MGW_SYMBOL_MAX];
    char translate_param[MGW_STRING_MAX];
    int do_log;
    int action;
    int service_id;
    //printf("table line is %s\n", table_line);
    sscanf(table_line, "%d %d %s %s %s %d %d %d %d", &config_id, &group_id, user_type, user_name, translate_param, &do_log, &action, &service_id, &is_valid);
    //printf("user name is %s\n", user_name);
    struct snat_policy_htable_value *value = ALLOC(struct snat_policy_htable_value, 1);
    strncpy(value->user_name, user_name, MGW_SYMBOL_MAX);
    value->group_id = group_id;
    int key_size = strnlen(user_name, MGW_SYMBOL_MAX);
    if(is_valid == 1)
    {
        //add snat_policy_htable
        int rtn = MESA_htable_add(handle->snat_policy_htable, (const unsigned char *)user_name, key_size, value);
        if(rtn < 0 && rtn != MESA_HTABLE_RET_DUP_ITEM)
        {
            MGW_LOG_ERROR(handle->logger, "MESA_htable: Failed at add, table is %s, user_name is %s, group_id is %d, rtn is %d", 
                        "snat_policy_htable", user_name, group_id, rtn);
            return;
        }
        if(rtn >= 0)
        {
            MGW_LOG_INFO(handle->logger, "MESA_htable: Succeed at add, table is %s, user_name is %s, group_id is %d", "snat_policy_htable", user_name, group_id);
            FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_policy, g_fs_handle->cloumn_element_num, FS_OP_ADD, 1);
        }
    }
    else
    {
        //del snat_policy_htable
        int rtn = MESA_htable_del(handle->snat_policy_htable, (const unsigned char *)user_name, key_size, NULL);
        if(rtn < 0)
        {
            MGW_LOG_ERROR(handle->logger, "MESA_htable: Failed at del, table is %s, user_name is %s, group_id is %d, rtn is %d", "snat_policy_htable", user_name, group_id, rtn);
            return;
        }
        MGW_LOG_INFO(handle->logger, "MESA_htable: Succeed at del, table is %s, user_name is %s, group_id is %d", "snat_policy_htable", user_name, group_id);
    }
	return;
}


static void Maat_snat_policy_finish_cb(void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: finish callback, table is snat_policy");
    //Maat_feather_t feather = handle->feather;
    /*
	long long version=0;
	int ret=0, is_last_updating_table = 0;
	ret = Maat_read_state(feather, MAAT_STATE_VERSION, &version, sizeof(version));
    printf("version is %d\n", version);
	assert(ret==0);
	ret=Maat_read_state(feather,MAAT_STATE_LAST_UPDATING_TABLE, &is_last_updating_table, sizeof(is_last_updating_table));
    printf("is_last_updating_table is %d\n", is_last_updating_table);
	assert(ret==0);
    */
	return;
}

static void Maat_snat_cand_ip_start_cb(int update_type,void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: start callback, table is IR_CANDIDATE_IP, updata_type is %d\n", update_type);
	return;
}

static long snat_cand_ip_query_cb_add(void *data, const uchar *key, uint size, void *user_args)
{
    struct snat_cand_ip_query_cb_add_args *args = (struct snat_cand_ip_query_cb_add_args *)user_args;
    struct ip_mgr_handle *handle = args->handle;
    int group_id = args->group_id;
    struct snat_cand_ip_detail ip_detail = args->ip_detail;
    void *logger = handle->logger;
    struct snat_cand_ip_group *ip_group = NULL;
    if(data == NULL)
    {
        MGW_LOG_INFO(logger, "MESA_htable: key not existed, table is %s, group_id is %d", "snat_cand_ip_htable", group_id);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_cache_miss, FS_OP_ADD, 1);
        ip_group = ALLOC(struct snat_cand_ip_group, 1);
        ip_group->num = 0;
        int rtn = MESA_htable_add(handle->snat_cand_ip_htable, (const unsigned char *)&group_id, sizeof(group_id), (void *)ip_group);
        if(rtn < 0)
        {
            FREE(&ip_group);
            MGW_LOG_ERROR(logger, "MESA_htable: Failed at add, table is %s, group_id is %d, rtn is %d", "snat_cand_ip_htable", group_id, rtn);
            MGW_LOG_ERROR(logger, "Failed at add ip to snat_cand_ip_group, group not existed, group_id is %d", group_id); 
            return MGW_HTABLE_QUERY_CB_SUCCESS;
        }
        MGW_LOG_INFO(logger, "MESA_htable: Succeed at add, table is %s, group_id is %d", "snat_cand_ip_htable", group_id);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_element_num, FS_OP_ADD, 1);
    }
    else
    {
        ip_group = (struct snat_cand_ip_group *)data;
        MGW_LOG_INFO(logger, "MESA_htable: key existed, table is %s, group_id is %d", "snat_cand_ip_htable", group_id);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_cache_hit, FS_OP_ADD, 1);
    }
    struct snat_cand_ip_detail *cand_ip_detail = ALLOC(struct snat_cand_ip_detail, 1);
    cand_ip_detail->ip = ip_detail.ip;
    cand_ip_detail->group_id = ip_detail.group_id;
    cand_ip_detail->addr_type = ip_detail.addr_type;
    cand_ip_detail->location = ip_detail.location;
    int num = ip_group->num;
    char ip_str[MGW_SYMBOL_MAX];
    mgw_utils_inet_ntoa(ip_detail.ip, ip_str);
    if(num >= SNAT_CAND_IP_GROUP_MAX)
    {
        FREE(&cand_ip_detail);
        MGW_LOG_ERROR(logger, "Failed at add ip to snat_cand_ip_group, reach the max num %d of group, ip is %s, group_id is %d", 
                            SNAT_CAND_IP_GROUP_MAX, ip_str, group_id);
        return MGW_HTABLE_QUERY_CB_SUCCESS;
    }
    ip_group->ip_list[num] = cand_ip_detail;
    ip_group->num++;
    MGW_LOG_INFO(logger, "Succeed at add ip to snat_cand_ip_group, ip is %s, group_id is %d", ip_str, group_id);
    return MGW_HTABLE_QUERY_CB_SUCCESS;
}

static long snat_cand_ip_query_cb_del(void *data, const uchar *key, uint size, void *user_args)
{
    struct snat_cand_ip_query_cb_del_args *args = (struct snat_cand_ip_query_cb_del_args *)user_args;
    struct ip_mgr_handle *handle = args->handle;
    uint32_t ip = args->ip;
    int group_id = args->group_id;
    void *logger = handle->logger;
    char ip_str[MGW_SYMBOL_MAX];
    mgw_utils_inet_ntoa(ip, ip_str);
    if(data == NULL)
    {
        MGW_LOG_ERROR(logger, "MESA_htable: key not existed, table is %s, group_id is %d", "snat_cand_ip_htable", group_id);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_cache_miss, FS_OP_ADD, 1);
        MGW_LOG_ERROR(logger, "Failed at del ip from snat_cand_ip_group, group_id not existed, ip is %s, group_id is %d", ip_str, group_id);
        return MGW_HTABLE_QUERY_CB_SUCCESS;
    }
    else
    {
        MGW_LOG_INFO(logger, "MESA_htable: key existed, table is %s, group_id is %d", "snat_cand_ip_htable", group_id);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_cache_hit, FS_OP_ADD, 1);
        struct snat_cand_ip_group *ip_group = (struct snat_cand_ip_group *)data;
        int num = ip_group->num;
        int pos = -1;
        for(int i = 0; i < num; i++)
        {
            if(ip_group->ip_list[i]->ip == ip)
            {
                pos = i;
                break;
            }
        }
        if(pos == -1)
        {
            MGW_LOG_ERROR(logger, "Failed at del ip from snat_cand_ip_group, ip not in group, ip is %s, group_id is %d", ip_str, group_id);
        }
        else
        {
            FREE(&(ip_group->ip_list[pos]));
            ip_group->num --;
            for(int i = pos; i < num - 1; i++)
            {
                ip_group->ip_list[i] = ip_group->ip_list[i + 1];
            }
        }
        if(ip_group->num <= 0)
        {
            int rtn = MESA_htable_del(handle->snat_cand_ip_htable, (const unsigned char *)&group_id, sizeof(group_id), NULL);
            if(rtn < 0)
            {
                MGW_LOG_ERROR(handle->logger, "MESA_htable: Failed at del, table is %s, group_id is %d, rtn is %d", "snat_cand_ip_htable", group_id, rtn);
            }
            else
            {
                MGW_LOG_INFO(handle->logger, "MESA_htable: Succeed at del, table is %s, group_id is %d", "snat_cand_ip_htable", group_id);
            }
        }
        return MGW_HTABLE_QUERY_CB_SUCCESS;
    }
}

static void Maat_snat_cand_ip_update_cb(int table_id, const char* table_line, void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
	MGW_LOG_INFO(logger, "Maat_redis: update callback, table is IR_CANDIDATE_IP");
    int config_id, group_id, addr_type, location, is_valid;
    char ip_addr[MGW_SYMBOL_MAX];
    sscanf(table_line, "%d %d %d %s %d %d", &config_id, &group_id, &addr_type, ip_addr, &location, &is_valid);
    uint32_t ip = inet_addr(ip_addr);
    if(is_valid == 1)
    {
        //add cand ip to snat_cand_ip_htable
        struct snat_cand_ip_query_cb_add_args args;
        args.handle = handle;
        args.group_id = group_id;
        args.ip_detail.ip = ip;
        args.ip_detail.group_id = group_id;
        args.ip_detail.addr_type = addr_type;
        args.ip_detail.location = location;
        long cb_rtn = -1;
        MESA_htable_search_cb(handle->snat_cand_ip_htable, (const unsigned char *)(&group_id), sizeof(group_id), snat_cand_ip_query_cb_add, (void *)&args, &cb_rtn);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_query_num, FS_OP_ADD, 1);
    }
    else
    {
        //delete cand ip from snat_cand_ip_htable
        struct snat_cand_ip_query_cb_del_args args;
        args.group_id = group_id;
        args.handle = handle;
        args.ip = ip;
        long cb_rtn = -1;
        MESA_htable_search_cb(handle->snat_cand_ip_htable, (const unsigned char *)(&group_id), sizeof(group_id), snat_cand_ip_query_cb_del, (void *)&args, &cb_rtn);
        FS_operate(g_fs_handle->handle, g_fs_handle->line_snat_cand_ip, g_fs_handle->cloumn_query_num, FS_OP_ADD, 1);
    }
	return;
}


static void Maat_snat_cand_ip_finish_cb(void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: finish callback, table is CAND_IP_TABLE");
	return;
}


static void Maat_dnat_policy_start_cb(int update_type, void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: start callback, table is IP_DNAT_POLICY");
	return;
}

static void Maat_dnat_policy_update_cb(int table_id, const char* table_line, void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: update callback, table is IP_DNAT_POLICY");
    int config_id, addr_type, orig_proto, do_log, action, service_id, is_valid;
    char orig_ip[MGW_SYMBOL_MAX];
    int temp_orig_port;
    uint16_t orig_port;
    char trans_ip[MGW_SYMBOL_MAX];
    int temp_trans_port;
    uint16_t trans_port;
    //printf("table line is %s\n", table_line);
    sscanf(table_line, "%d %d %s %d %d %s %d %d %d %d %d", &config_id, &addr_type, orig_ip, &temp_orig_port, &orig_proto, 
                                trans_ip, &temp_trans_port, &do_log, &action, &service_id, &is_valid);
    orig_port = (uint16_t)temp_orig_port;
    trans_port = (uint16_t)temp_trans_port;
    struct dnat_policy_htable_key *key = ALLOC(struct dnat_policy_htable_key, 1);
    key->orig_ip = inet_addr(orig_ip);
    key->orig_port = htons(orig_port);
    if(is_valid == 1)
    {
        //add dnat_policy_htable
        struct dnat_policy_htable_value *value = ALLOC(struct dnat_policy_htable_value, 1);
        int rtn = MESA_htable_add(handle->dnat_policy_htable, (const unsigned char *)key, sizeof(key), (void *)value);
        if(rtn < 0)
        {
            FREE(&value);
            if(rtn != MESA_HTABLE_RET_DUP_ITEM)
            {
                MGW_LOG_ERROR(handle->logger, "MESA_htable: Failed at add, table is %s, original pair is <%s:%d>, translated pair is <%s:%d>, rtn is %d", 
                        "dnat_policy_htable", orig_ip, orig_port, trans_ip, trans_port, rtn);
            }
        }
        else
        {
            MGW_LOG_INFO(handle->logger, "MESA_htable: Succeed at add, table is %s, original pair is <%s:%d>, translated pair is <%s:%d>", 
                        "dnat_policy_htable", orig_ip, orig_port, trans_ip, trans_port, rtn);
            FS_operate(g_fs_handle->handle, g_fs_handle->line_dnat_policy, g_fs_handle->cloumn_element_num, FS_OP_ADD, 1);
        }
    }
    else
    {
        //del dnat_policy_htable
        int rtn = MESA_htable_del(handle->dnat_policy_htable, (const unsigned char *)key, sizeof(key), NULL);
        if(rtn < 0)
        {
            MGW_LOG_ERROR(handle->logger, "MESA_htable: Failed at del, table is %s, original pair is <%s:%d>, translated pair is <%s:%d>, rtn is %d", 
                        "dnat_policy_htable", orig_ip, orig_port, trans_ip, trans_port, rtn);
        }
        else
        {
            MGW_LOG_INFO(handle->logger, "MESA_htable: Succeed at del, table is %s, original pair is <%s:%d>, translated pair is <%s:%d>", 
                        "dnat_policy_htable", orig_ip, orig_port, trans_ip, trans_port);
        }
    }
    FREE(&key);
	return;
}


static void Maat_dnat_policy_finish_cb(void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: finish callback, table is IP_DNAT_POLICY");
	return;
}

static void Maat_mrl_ip_start_cb(int update_type, void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: start callback, table is IP_MRL_IP_INFO");
	return;
}

static void Maat_mrl_ip_update_cb(int table_id, const char* table_line, void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: update callback, table is IP_MRL_IP_INFO");
    int config_id, back_type, is_valid;
    char back_ip[MGW_SYMBOL_MAX];
    char mrl_ip[MGW_SYMBOL_MAX];
    uint32_t key = inet_addr(back_ip);
    //printf("table line is %s\n", table_line);
    sscanf(table_line, "%d %s %d %s %d", &config_id, back_ip, &back_type, mrl_ip, &is_valid);
    if(is_valid == 1)
    {
        //add snat_policy_htable
        struct mrl_ip_htable_value *value = ALLOC(struct mrl_ip_htable_value, 1);
        value->back_ip = key;
        value->mrl_ip = inet_addr(mrl_ip);
        int rtn = MESA_htable_add(handle->mrl_ip_htable, (const unsigned char *)&key, sizeof(key), (void *)value);
        if(rtn < 0)
        {
            FREE(&value);
            if(rtn != MESA_HTABLE_RET_DUP_ITEM)
            {
                MGW_LOG_ERROR(handle->logger, "MESA_htable: Failed at add, table is %s, back_ip is %s, mrl_ip is %s, rtn is %d", 
                        "mrl_ip_htable", back_ip, mrl_ip, rtn);
            }
        }
        else
        {
            MGW_LOG_INFO(handle->logger, "MESA_htable: Succeed at add, table is %s, back_ip is %s, mrl_ip is %s", 
                        "mrl_ip_htable", back_ip, mrl_ip);
            FS_operate(g_fs_handle->handle, g_fs_handle->line_mrl_ip, g_fs_handle->cloumn_element_num, FS_OP_ADD, 1);
        }
    }
    else
    {
        //del snat_policy_htable
        int rtn = MESA_htable_del(handle->mrl_ip_htable, (const unsigned char *)&key, sizeof(key), NULL);
        if(rtn < 0)
        {
            MGW_LOG_ERROR(handle->logger, "MESA_htable: Failed at del, table is %s, back_ip is %s, mrl_ip is %s, rtn is %d", 
                        "mrl_ip_htable", back_ip, mrl_ip, rtn);
        }
        else
        {
            MGW_LOG_INFO(handle->logger, "MESA_htable: Succeed at del, table is %s, back_ip is %s, mrl_ip is %s", 
                        "mrl_ip_htable", back_ip, mrl_ip);
        }
    }
	return;
}


static void Maat_mrl_ip_finish_cb(void* args)
{
    struct ip_mgr_handle *handle = (struct ip_mgr_handle *)args;
    void *logger = handle->logger;
    MGW_LOG_INFO(logger, "Maat_redis: finish callback, table is IP_MRL_IP_INFO");
	return;
}

static int Maat_plugin_register(Maat_feather_t feather, const char* table_name, Maat_start_callback_t *start, 
                Maat_update_callback_t *update, Maat_finish_callback_t *finish, struct ip_mgr_handle *handle)
{
	int table_id = 0, ret = 0;
    void *logger = handle->logger;
	table_id = Maat_table_register(feather, table_name);
	if(table_id == -1)
	{
        MGW_LOG_ERROR(logger, "Maat_redis: Failed at register table %s", table_name);
        return -1;
	}
	else
	{
		ret = Maat_table_callback_register(feather, table_id, start, update, finish, (void *)handle);
		if(ret < 0)
		{
            MGW_LOG_ERROR(logger, "Maat_redis: Failed to register callback of table %s", table_name);
            return -1;
		}
	}
	return 0;
}

static void wrapped_Maat_set_feather_opt(void *logger, Maat_feather_t feather, enum MAAT_INIT_OPT type, const void* value, int size)
{
    int rtn = Maat_set_feather_opt(feather, type, value, size);
    if(unlikely(rtn < 0))
    {
        MGW_LOG_ERROR(logger, "Failed at Maat_set_feather_opt, type is %d, rtn is %d", type, rtn);
        exit(EXIT_FAILURE);
    }
}

static Maat_feather_t  Maat_init(const char *profile, const char *section, void *logger)
{
    // load conf
    char table_info_path[MGW_PATH_MAX];
    int max_thread_num;
    char Maat_redis_ip[MGW_SYMBOL_MAX];
    int Maat_redis_port;
    int Maat_redis_index;
    char stat_file_path[MGW_PATH_MAX];
    MESA_load_profile_string_def(profile, section, "table_info_path", table_info_path, sizeof(table_info_path), "./conf/table_info.conf");
    MESA_load_profile_int_def(profile, section, "max_thread_num", &max_thread_num, 1);
    MESA_load_profile_string_def(profile, section, "Maat_redis_ip", Maat_redis_ip, sizeof(Maat_redis_ip), "127.0.0.1");
    MESA_load_profile_int_def(profile, section, "Maat_redis_port", &Maat_redis_port, 6379);
    MESA_load_profile_int_def(profile, section, "Maat_redis_index", &Maat_redis_index, 1);
    MESA_load_profile_string_def(profile, section, "stat_file_path", stat_file_path, sizeof(stat_file_path), "./log/Maat_stat.log");
    MGW_LOG_INFO(logger, "MESA_prof_load, [%s]:\n table_info_path: %s\n max_thread_num: %d\n Maat_redis_ip: %s\n Maat_redis_port: %d\n Maat_redis_index: %d\n"
    "stat_file_path: %s", section, table_info_path, max_thread_num, Maat_redis_ip, Maat_redis_port, Maat_redis_index, stat_file_path);
    // init Maat
    Maat_feather_t feather = NULL;
    feather = Maat_feather(max_thread_num, table_info_path, logger);
    if(feather == NULL)
    {
        MGW_LOG_ERROR(logger, "Failed at Maat_feather");
        exit(EXIT_FAILURE);
    }
    wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_INSTANCE_NAME, "mgw", strlen("mgw")+1);
	wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_REDIS_IP, Maat_redis_ip, strlen(Maat_redis_ip)+1);
	wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_REDIS_PORT, &Maat_redis_port, sizeof(Maat_redis_port));
    wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_REDIS_INDEX, &Maat_redis_index, sizeof(Maat_redis_index));
    //wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_STAT_FILE_PATH, stat_file_path, strlen(stat_file_path)+1);
    //wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_STAT_ON, NULL, 0);
	//wrapped_Maat_set_feather_opt(logger, feather, MAAT_OPT_PERF_ON, NULL, 0);
    //wrapped_Maat_set_feather_opt(feather, MAAT_OPT_SCANDIR_INTERVAL_MS,&scan_interval_ms, sizeof(scan_interval_ms));
	//wrapped_Maat_set_feather_opt(feather, MAAT_OPT_EFFECT_INVERVAL_MS, &effective_interval_ms, sizeof(effective_interval_ms));
	//wrapped_Maat_set_feather_opt(feather, MAAT_OPT_SCAN_DETAIL, &scan_detail, sizeof(scan_detail));
	//wrapped_Maat_set_feather_opt(feather, MAAT_OPT_ACCEPT_TAGS, accept_tags, strlen(accept_tags)+1);
	int rtn = Maat_initiate_feather(feather);
    if(unlikely(rtn < 0))
    {
        MGW_LOG_ERROR(logger, "Failed at Maat_initiate_feather");
		exit(EXIT_FAILURE);
    }
    return feather;
}

struct ip_mgr_handle *ip_mgr_init(const char *profile, struct field_stat_handle *fs_handle, void *logger)
{
    struct ip_mgr_handle *handle = ALLOC(struct ip_mgr_handle, 1); 
    handle->Maat_feather_snat_policy = Maat_init(profile, "Maat_snat_policy", logger);
    handle->Maat_feather_dnat_policy = Maat_init(profile, "Maat_dnat_policy", logger);
    handle->Maat_feather_snat_cand_ip = Maat_init(profile, "Maat_snat_cand_ip", logger);
    handle->Maat_feather_mrl_ip = Maat_init(profile, "Maat_mrl_ip", logger);
    handle->logger = logger;
    handle->snat_policy_htable = mgw_utils_create_htable(profile, "snat_policy_htable", (void *)snat_policy_htable_data_free_cb, NULL, logger);
    handle->dnat_policy_htable = mgw_utils_create_htable(profile, "dnat_policy_htable", (void *)dnat_policy_htable_data_free_cb, NULL, logger);
    handle->snat_cand_ip_htable = mgw_utils_create_htable(profile, "snat_cand_ip_htable", (void *)snat_cand_ip_htable_data_free_cb, NULL, logger);
    handle->mrl_ip_htable = mgw_utils_create_htable(profile, "mrl_ip_htable", (void *)mrl_ip_htable_data_free_cb, NULL, logger);
    g_fs_handle = fs_handle;
    int rtn = Maat_plugin_register(handle->Maat_feather_snat_policy, "IR_SNAT_POLICY", Maat_snat_policy_start_cb, Maat_snat_policy_update_cb, Maat_snat_policy_finish_cb, handle);
    if(rtn == -1)
    {
        return NULL;   
    }
    rtn = Maat_plugin_register(handle->Maat_feather_dnat_policy, "IR_DNAT_POLICY", Maat_dnat_policy_start_cb, Maat_dnat_policy_update_cb, Maat_dnat_policy_finish_cb, handle);
    if(rtn == -1)
    {
        return NULL;   
    }
    rtn = Maat_plugin_register(handle->Maat_feather_snat_cand_ip, "IR_CANDIDATE_IP", Maat_snat_cand_ip_start_cb, Maat_snat_cand_ip_update_cb, Maat_snat_cand_ip_finish_cb, handle);
    if(rtn == -1)
    {
        return NULL;
    }
    rtn = Maat_plugin_register(handle->Maat_feather_mrl_ip, "IR_MRL_IP_INFO", Maat_mrl_ip_start_cb, Maat_mrl_ip_update_cb, Maat_mrl_ip_finish_cb, handle);
    if(rtn == -1)
    {
        return NULL;
    }
    return handle;
}

void ip_mgr_destroy(struct ip_mgr_handle *handle)
{
    MESA_htable_destroy(handle->snat_policy_htable, NULL);
    MESA_htable_destroy(handle->dnat_policy_htable, NULL);
    MESA_htable_destroy(handle->snat_cand_ip_htable, NULL);
    MESA_htable_destroy(handle->mrl_ip_htable, NULL);
    Maat_burn_feather(handle->Maat_feather_snat_policy);
    Maat_burn_feather(handle->Maat_feather_dnat_policy);
    Maat_burn_feather(handle->Maat_feather_snat_cand_ip);
    Maat_burn_feather(handle->Maat_feather_mrl_ip);
    FREE(&handle);
}