summaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/src/cfg.c
blob: 527da54e7903b18f635ce0d512248fbc282bc4a6 (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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
/**
 * \file cfg.c
 * \brief Implementation of Face manager configuration
 */

#include <assert.h>
#include <hicn/ctrl.h> // HICN_DEFAULT_PORT
#include <hicn/facemgr/cfg.h>
#include <hicn/policy.h>
#include <hicn/util/ip_address.h>
#include "util/set.h"

/* Overlay */

typedef struct {
    bool is_local_port;
    uint16_t local_port;
    bool is_local_addr;
    ip_address_t local_addr;
    bool is_remote_port;
    uint16_t remote_port;
    bool is_remote_addr;
    ip_address_t remote_addr;
} facemgr_cfg_overlay_t;

int
facemgr_cfg_overlay_initialize(facemgr_cfg_overlay_t * overlay)
{
    overlay->is_local_port = false;
    overlay->local_port = 0;
    overlay->is_local_addr = false;
    overlay->local_addr = IP_ADDRESS_EMPTY;

    overlay->is_remote_port = false;
    overlay->remote_port = 0;
    overlay->is_remote_addr = false;
    overlay->remote_addr = IP_ADDRESS_EMPTY;

    return 0;
}

int
facemgr_cfg_overlay_finalize(facemgr_cfg_overlay_t * overlay)
{
    return 0;
}

facemgr_cfg_overlay_t * facemgr_cfg_overlay_create()
{
    facemgr_cfg_overlay_t * overlay = malloc(sizeof(facemgr_cfg_overlay_t));
    if (!overlay)
        return NULL;

    int rc = facemgr_cfg_overlay_initialize(overlay);
    if (rc < 0) {
        free(overlay);
        return NULL;
    }

    return overlay;
}

void facemgr_cfg_overlay_free(facemgr_cfg_overlay_t * overlay)
{
    facemgr_cfg_overlay_finalize(overlay);
    free(overlay);
}

typedef struct {
    facemgr_cfg_overlay_t * v4;
    facemgr_cfg_overlay_t * v6;
} facemgr_cfg_overlays_t;

typedef struct {
    const char * interface_name;
    netdevice_type_t interface_type;
} facemgr_cfg_match_t;


typedef struct {
    /* Interface specific */
    bool is_face_type; // default is auto
    facemgr_face_type_t face_type;

    /* This should be defaut for the global settings */
    bool is_ignore;
    bool ignore;
    bool is_discovery;
    bool discovery;
    bool is_ipv4;
    bool ipv4;
    bool is_ipv6;
    bool ipv6;

    facemgr_cfg_overlays_t overlays; // fallback unless discovery is disabled
} facemgr_cfg_override_t;

struct facemgr_cfg_rule_s {
    facemgr_cfg_match_t match;
    facemgr_cfg_override_t override;
};

int
facemgr_cfg_override_initialize(facemgr_cfg_override_t * override)
{
    override->is_face_type = false;
    override->face_type = FACEMGR_FACE_TYPE_UNDEFINED;

    override->is_ignore = false;
    override->ignore = false;

    override->is_discovery = false;
    override->discovery = false;

    override->is_ipv4 = false;
    override->ipv6 = false;
    override->is_ipv6 = false;
    override->ipv6 = false;

    override->overlays.v4 = NULL;
    override->overlays.v6 = NULL;

    return 0;
}

int
facemgr_cfg_override_finalize(facemgr_cfg_override_t * override)
{
    if (override->overlays.v4) {
        facemgr_cfg_overlay_free(override->overlays.v4);
        override->overlays.v4 = NULL;
    }
    if (override->overlays.v6) {
        facemgr_cfg_overlay_free(override->overlays.v6);
        override->overlays.v6 = NULL;
    }

    return 0;
}


/* Rule */
facemgr_cfg_rule_t * facemgr_cfg_rule_create()
{
    facemgr_cfg_rule_t * rule = malloc(sizeof(facemgr_cfg_rule_t));
    if (!rule)
        return NULL;

    int rc = facemgr_cfg_rule_initialize(rule);
    if (rc < 0)
        return NULL;

    return rule;
}

void facemgr_cfg_rule_free(facemgr_cfg_rule_t * rule)
{
    facemgr_cfg_rule_finalize(rule);
    free(rule);
}

int
facemgr_cfg_rule_initialize(facemgr_cfg_rule_t * rule)
{
    rule->match.interface_name = NULL;
    rule->match.interface_type = NETDEVICE_TYPE_UNDEFINED;

    int rc = facemgr_cfg_override_initialize(&rule->override);
    if (rc < 0)
        return -1;

    return 0;
}

int
facemgr_cfg_rule_finalize(facemgr_cfg_rule_t * rule)
{
    if (rule->match.interface_name) {
        free((void*)rule->match.interface_name);
        rule->match.interface_name = NULL;
    }
    return facemgr_cfg_override_finalize(&rule->override);
}

void
facemgr_cfg_rule_dump(facemgr_cfg_rule_t * rule)
{
    DEBUG("  <rule>");
    DEBUG("    <match interface_name=%s interface_type=%s>",
            rule->match.interface_name,
            netdevice_type_str[rule->match.interface_type]);
    DEBUG("    <override>");
    if (rule->override.is_face_type) {
        DEBUG("      <face_type>%d</face_type>", rule->override.face_type);
    }
    if (rule->override.is_ignore) {
        DEBUG("      <ignore>%d</ignore>", rule->override.ignore);
    }
    if (rule->override.is_discovery) {
        DEBUG("      <discovery>%d</discovery>", rule->override.discovery);
    }
    if (rule->override.is_ipv4) {
        DEBUG("      <ipv4>%d</ipv4>", rule->override.ipv4);
    }
    if (rule->override.is_ipv6) {
        DEBUG("      <ipv6>%d</ipv6>", rule->override.ipv6);
    }
    DEBUG("      <overlays>");
    if (rule->override.overlays.v4) {
        DEBUG("        <ipv4>");
        if (rule->override.overlays.v4->is_local_addr) {
            char buf[MAXSZ_IP_ADDRESS];
            ip_address_snprintf(buf, MAXSZ_IP_ADDRESS,
                    &rule->override.overlays.v4->local_addr, AF_INET);
            DEBUG("          <local_addr>%s</local_addr>", buf);
        }
        if (rule->override.overlays.v4->is_local_port) {
            DEBUG("          <local_port>%d</local_port>",
                    rule->override.overlays.v4->local_port);
        }
        if (rule->override.overlays.v4->is_remote_addr) {
            char buf[MAXSZ_IP_ADDRESS];
            ip_address_snprintf(buf, MAXSZ_IP_ADDRESS,
                    &rule->override.overlays.v4->remote_addr, AF_INET);
            DEBUG("          <remote_addr>%s</remote_addr>", buf);
        }
        if (rule->override.overlays.v4->is_remote_port) {
            DEBUG("          <remote_port>%d</remote_port>",
                    rule->override.overlays.v4->remote_port);
        }
        DEBUG("        </ipv4>");
    }
    if (rule->override.overlays.v6) {
        DEBUG("        <ipv6>");
        if (rule->override.overlays.v6->is_local_addr) {
            char buf[MAXSZ_IP_ADDRESS];
            ip_address_snprintf(buf, MAXSZ_IP_ADDRESS,
                    &rule->override.overlays.v6->local_addr, AF_INET6);
            DEBUG("          <local_addr>%s</local_addr>", buf);
        }
        if (rule->override.overlays.v6->is_local_port) {
            DEBUG("          <local_port>%d</local_port>",
                    rule->override.overlays.v6->local_port);
        }
        if (rule->override.overlays.v6->is_remote_addr) {
            char buf[MAXSZ_IP_ADDRESS];
            ip_address_snprintf(buf, MAXSZ_IP_ADDRESS,
                    &rule->override.overlays.v6->remote_addr, AF_INET6);
            DEBUG("          <remote_addr>%s</remote_addr>", buf);
        }
        if (rule->override.overlays.v6->is_remote_port) {
            DEBUG("          <remote_port>%d</remote_port>",
                    rule->override.overlays.v6->remote_port);
        }
        DEBUG("        </ipv6>");
    }
    DEBUG("      </overlays>");
    DEBUG("    </override>");
    DEBUG("  </rule>");
}

int
facemgr_cfg_rule_set_match(facemgr_cfg_rule_t * rule, const char * interface_name,
        netdevice_type_t interface_type)
{
    rule->match.interface_name = interface_name ? strdup(interface_name) : NULL;
    rule->match.interface_type = interface_type;
    return 0;
}

int
facemgr_cfg_rule_set_face_type(facemgr_cfg_rule_t * rule, facemgr_face_type_t * face_type)
{
    rule->override.is_face_type = true;
    rule->override.face_type = *face_type;
    return 0;
}

int
facemgr_cfg_rule_unset_face_type(facemgr_cfg_rule_t * rule)
{
    rule->override.is_face_type = false;
    rule->override.face_type = FACEMGR_FACE_TYPE_UNDEFINED; /* optional */
    return 0;
}

int
facemgr_cfg_rule_set_discovery(facemgr_cfg_rule_t * rule, bool status)
{
    rule->override.is_discovery = true;
    rule->override.discovery = status;
    return 0;
}

int
facemgr_cfg_rule_unset_discovery(facemgr_cfg_rule_t * rule)
{
    rule->override.is_discovery = false;
    return 0;
}

int
facemgr_cfg_rule_set_ignore(facemgr_cfg_rule_t * rule, bool status)
{
    rule->override.is_ignore = true;
    rule->override.ignore = status;
    return 0;
}

int
facemgr_cfg_rule_unset_ignore(facemgr_cfg_rule_t * rule)
{
    rule->override.is_ignore = false;
    return 0;
}

int
facemgr_cfg_rule_set_ipv4(facemgr_cfg_rule_t * rule, bool status)
{
    rule->override.is_ipv4 = true;
    rule->override.ipv4 = status;
    return 0;
}

int
facemgr_cfg_rule_unset_ipv4(facemgr_cfg_rule_t * rule)
{
    rule->override.is_ipv4 = false;
    return 0;
}

int
facemgr_cfg_rule_set_ipv6(facemgr_cfg_rule_t * rule, bool status)
{
    rule->override.is_ipv6 = true;
    rule->override.ipv6 = status;
    return 0;
}

int
facemgr_cfg_rule_unset_ipv6(facemgr_cfg_rule_t * rule)
{
    rule->override.is_ipv6 = false;
    return 0;
}

int
facemgr_cfg_rule_set_overlay(facemgr_cfg_rule_t * rule, int family,
    ip_address_t * local_addr, uint16_t local_port,
    ip_address_t * remote_addr, uint16_t remote_port) {
    if ((family != AF_INET) && (family != AF_INET6))
        return -1;

    facemgr_cfg_overlay_t * overlay = facemgr_cfg_overlay_create();
    if (local_addr) {
        overlay->is_local_addr = true;
        overlay->local_addr = *local_addr;
    }
    if (IS_VALID_PORT(local_port)) {
        overlay->is_local_port = true;
        overlay->local_port = local_port;
    }
    if (remote_addr) {
        overlay->is_remote_addr = true;
        overlay->remote_addr = *remote_addr;
    }
    if (IS_VALID_PORT(remote_port)) {
        overlay->is_remote_port = true;
        overlay->remote_port = remote_port;
    }

    switch(family) {
        case AF_INET:
            rule->override.overlays.v4 = overlay;
            break;

        case AF_INET6:
            rule->override.overlays.v6 = overlay;
            break;

        default:
            return -1;
    }

    return 0;
}

int
facemgr_rule_unset_overlay(facemgr_cfg_rule_t * rule, int family)
{
    if ((family != AF_INET) && (family != AF_INET6) && (family != AF_UNSPEC))
        return -1;

    if ((family == AF_UNSPEC) || (family == AF_INET)) {
        if (rule->override.overlays.v4) {
            facemgr_cfg_overlay_free(rule->override.overlays.v4);
            rule->override.overlays.v4 = NULL;
        }
    }
    if ((family == AF_UNSPEC) || (family == AF_INET6)) {
        if (rule->override.overlays.v6) {
            facemgr_cfg_overlay_free(rule->override.overlays.v6);
            rule->override.overlays.v6 = NULL;
        }
    }
    return 0;
}

int
facemgr_cfg_rule_cmp(const facemgr_cfg_rule_t * r1, const facemgr_cfg_rule_t * r2)
{
    /*
     * We implement a lexicographic order on the tuple (interface_name,
     * interface_type)
     */

    /* We need to handle NULL cases out of strcmp */
    if (!r1->match.interface_name) {
        if (r2->match.interface_name)
            return 1;
        else
            goto BOTH_NULL;
    } else {
        if (!r2->match.interface_name)
            return -1;
    }


    /* Only if both are non-NULL, we proceed to strcmp */
    int rc = strcmp(r1->match.interface_name, r2->match.interface_name);
    if (rc != 0)
        return rc;

BOTH_NULL:
    return r1->match.interface_type - r2->match.interface_type;
}

/* General */

TYPEDEF_SET_H(facemgr_cfg_rule_set, facemgr_cfg_rule_t *);
TYPEDEF_SET(facemgr_cfg_rule_set, facemgr_cfg_rule_t *, facemgr_cfg_rule_cmp, generic_snprintf);

struct facemgr_cfg_s {
    facemgr_cfg_override_t global;
    facemgr_cfg_rule_set_t * rule_set;
    //log_cfg_t log;
};

facemgr_cfg_t * facemgr_cfg_create()
{
    facemgr_cfg_t * cfg = malloc(sizeof(facemgr_cfg_t));
    if (!cfg)
        return NULL;

    int rc = facemgr_cfg_initialize(cfg);
    if (rc < 0) {
        free(cfg);
        return NULL;
    }

    return cfg;
}

void facemgr_cfg_free(facemgr_cfg_t * cfg)
{
    facemgr_cfg_finalize(cfg);
    free(cfg);
}

int
facemgr_cfg_initialize(facemgr_cfg_t * cfg)
{
    int rc = facemgr_cfg_override_initialize(&cfg->global);
    if (rc < 0)
        goto ERR_OVERRIDE;

    cfg->rule_set = facemgr_cfg_rule_set_create();
    if (!cfg->rule_set)
        goto ERR_RULE_SET;

    return 0;

ERR_RULE_SET:
    facemgr_cfg_override_finalize(&cfg->global);
ERR_OVERRIDE:
    return -1;
}

int
facemgr_cfg_finalize(facemgr_cfg_t * cfg)
{
    /* TODO Free all rules */
    facemgr_cfg_rule_t ** rule_array;
    int n = facemgr_cfg_rule_set_get_array(cfg->rule_set, &rule_array);
    if (n < 0) {
        ERROR("[facemgr_cfg_finalize] Could not retrieve rule set array from configuration");
    } else {
        for (unsigned i = 0; i < n; i++) {
            facemgr_cfg_rule_t * rule = rule_array[i];
            if (facemgr_cfg_rule_set_remove(cfg->rule_set, rule, NULL) < 0) {
                ERROR("[facemgr_cfg_finalize] Could not remove rule from set");
            }
            facemgr_cfg_rule_free(rule);
        }
        free(rule_array);
    }
    facemgr_cfg_rule_set_free(cfg->rule_set);
    return facemgr_cfg_override_finalize(&cfg->global);
}

void facemgr_cfg_dump(facemgr_cfg_t * cfg)
{
    return; /* NOT IMPLEMENTED */
}

/* General */
int
facemgr_cfg_set_face_type(facemgr_cfg_t * cfg, facemgr_face_type_t * face_type)
{
    cfg->global.is_face_type = true;
    cfg->global.face_type = *face_type;
    return 0;
}

int
facemgr_cfg_unset_face_type(facemgr_cfg_t * cfg)
{
    cfg->global.is_face_type = false;
    cfg->global.face_type = FACEMGR_FACE_TYPE_UNDEFINED; /* optional */
    return 0;
}

int
facemgr_cfg_set_discovery(facemgr_cfg_t * cfg, bool status)
{
    cfg->global.is_discovery = true;
    cfg->global.discovery = status;
    return 0;
}

int
facemgr_cfg_unset_discovery(facemgr_cfg_t * cfg)
{
    cfg->global.is_discovery = false;
    return 0;
}

int facemgr_cfg_set_ipv4(facemgr_cfg_t * cfg, bool status)
{
    cfg->global.is_ipv4 = true;
    cfg->global.ipv4 = status;
    DEBUG("<global>");
    DEBUG("      <ipv4>%d</ipv4>", cfg->global.ipv4);
    DEBUG("</global>");
    return 0;
}

int facemgr_cfg_unset_ipv4(facemgr_cfg_t * cfg)
{
    cfg->global.is_ipv4 = false;
    return 0;
}

int facemgr_cfg_set_ipv6(facemgr_cfg_t * cfg, bool status)
{
    cfg->global.is_ipv6 = true;
    cfg->global.ipv6 = status;
    DEBUG("<global>");
    DEBUG("      <ipv6>%d</ipv6>", cfg->global.ipv6);
    DEBUG("</global>");
    return 0;
}

int facemgr_cfg_unset_ipv6(facemgr_cfg_t * cfg)
{
    cfg->global.is_ipv6 = false;
    return 0;
}

int
facemgr_cfg_set_overlay(facemgr_cfg_t * cfg, int family,
    ip_address_t * local_addr, uint16_t local_port,
    ip_address_t * remote_addr, uint16_t remote_port)
{
    if ((family != AF_INET) && (family != AF_INET6))
        return -1;

    facemgr_cfg_overlay_t * overlay = facemgr_cfg_overlay_create();
    if (local_addr) {
        overlay->is_local_addr = true;
        overlay->local_addr = *local_addr;
    }
    if (IS_VALID_PORT(local_port)) {
        overlay->is_local_port = true;
        overlay->local_port = local_port;
    }
    if (remote_addr) {
        overlay->is_remote_addr = true;
        overlay->remote_addr = *remote_addr;
    }
    if (IS_VALID_PORT(remote_port)) {
        overlay->is_remote_port = true;
        overlay->remote_port = remote_port;
    }

    DEBUG("facemgr_cfg_set_overlay");

    switch(family) {
        case AF_INET:
            cfg->global.overlays.v4 = overlay;
            break;

        case AF_INET6:
            cfg->global.overlays.v6 = overlay;
            break;

        default:
            return -1;
    }

    DEBUG("<global>");
    DEBUG("  <overlay>");
    if (overlay) {
        DEBUG("    <ipv4>");
        if (overlay->is_local_addr) {
            char buf[MAXSZ_IP_ADDRESS];
            ip_address_snprintf(buf, MAXSZ_IP_ADDRESS,
                    &overlay->local_addr, AF_INET);
            DEBUG("      <local_addr>%s</local_addr>", buf);
        }
        if (overlay->is_local_port) {
            DEBUG("      <local_port>%d</local_port>",
                    overlay->local_port);
        }
        if (overlay->is_remote_addr) {
            char buf[MAXSZ_IP_ADDRESS];
            ip_address_snprintf(buf, MAXSZ_IP_ADDRESS,
                    &overlay->remote_addr, AF_INET);
            DEBUG("      <remote_addr>%s</remote_addr>", buf);
        }
        if (overlay->is_remote_port) {
            DEBUG("      <remote_port>%d</remote_port>",
                    overlay->remote_port);
        }
        DEBUG("    </ipv4>");
    }
    DEBUG("  </overlay>");
    DEBUG("</global>");

    return 0;
}

int
facemgr_cfg_unset_overlay(facemgr_cfg_t * cfg, int family)
{
    if ((family != AF_INET) && (family != AF_INET6) && (family != AF_UNSPEC))
        return -1;

    if ((family == AF_UNSPEC) || (family == AF_INET)) {
        if (cfg->global.overlays.v4) {
            facemgr_cfg_overlay_free(cfg->global.overlays.v4);
            cfg->global.overlays.v4 = NULL;
        }
    }
    if ((family == AF_UNSPEC) || (family == AF_INET6)) {
        if (cfg->global.overlays.v6) {
            facemgr_cfg_overlay_free(cfg->global.overlays.v6);
            cfg->global.overlays.v6 = NULL;
        }
    }
    return 0;
}

int
facemgr_cfg_add_rule(facemgr_cfg_t * cfg, facemgr_cfg_rule_t * rule)
{
    facemgr_cfg_rule_dump(rule);
    return facemgr_cfg_rule_set_add(cfg->rule_set, rule);
}

int
facemgr_cfg_del_rule(facemgr_cfg_t * cfg, facemgr_cfg_rule_t * rule)
{
    return facemgr_cfg_rule_set_remove(cfg->rule_set, rule, NULL);
}

int facemgr_cfg_get_rule(const facemgr_cfg_t * cfg, const char * interface_name,
        netdevice_type_t interface_type, facemgr_cfg_rule_t ** rule) {
    facemgr_cfg_rule_t rule_search = {
        .match = {
            .interface_name = interface_name,
            .interface_type = interface_type,
        },
    };
    return facemgr_cfg_rule_set_get(cfg->rule_set, &rule_search, rule);
}

/* Query API */

/*
 * Check whether there are override rules for the given netdevice
 *
 * TODO:
 *  - until we have proper indexes we loop through the whole structure
 */
int
facemgr_cfg_get_override(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        facemgr_cfg_override_t ** override)
{
    if (!netdevice) {
        *override = NULL;
        return 0;
    }

    facemgr_cfg_rule_t **rule_array;
    int rc = facemgr_cfg_rule_set_get_array(cfg->rule_set, &rule_array);
    if (rc < 0) {
        ERROR("facemgr_cfg_rule_set_get_array failed");
        return rc;
    }
    for (unsigned i = 0; i < rc; i++) {
        const char * interface_name = rule_array[i]->match.interface_name;
        /* Check match for interface name */
        if (interface_name && (strcmp(interface_name, netdevice->name) != 0))
            continue;
        /* Check match for interface type */
        if (rule_array[i]->match.interface_type != NETDEVICE_TYPE_UNDEFINED) {
#ifdef __ANDROID__
            if (netdevice_type != rule_array[i]->match.interface_type)
                continue;
#else
            ERROR("Match on interface type is currently not implemented");
            goto ERR_ARRAY;
#endif /* __ANDROID__ */
        }
        /* Found match... do we have an override for face_type */
        *override = &rule_array[i]->override;
        goto FOUND;
    }

    *override = NULL;

FOUND:
    free(rule_array);
    return 0;

#ifndef __ANDROID__
ERR_ARRAY:
    free(rule_array);
    return -1;
#endif /* __ANDROID__ */
}

int
facemgr_cfg_get_face_type(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        facemgr_face_type_t * face_type)
{
    facemgr_cfg_override_t * override;
    int rc = facemgr_cfg_get_override(cfg, netdevice, netdevice_type,
            &override);
    if (rc < 0) {
        ERROR("get override failed");
        return rc;
    }

    if ((override) && (override->is_face_type)) {
        *face_type = override->face_type;
        return 0;
    }

    *face_type = cfg->global.is_face_type
        ? cfg->global.face_type
        : FACEMGR_FACE_TYPE_DEFAULT;

    return 0;
}

int
facemgr_cfg_get_discovery(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        bool * discovery)
{
    facemgr_cfg_override_t * override;
    int rc = facemgr_cfg_get_override(cfg, netdevice, netdevice_type,
            &override);
    if (rc < 0)
        return rc;

    if ((override) && (override->is_discovery)) {
        *discovery = override->discovery;
        return 0;
    }

    *discovery = cfg->global.is_discovery
        ? cfg->global.discovery
        : FACEMGR_CFG_DEFAULT_DISCOVERY;
    return 0;
}

int
facemgr_cfg_get_ipv4(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        bool * ipv4)
{
    facemgr_cfg_override_t * override;
    int rc = facemgr_cfg_get_override(cfg, netdevice, netdevice_type,
            &override);
    if (rc < 0)
        return rc;

    if ((override) && (override->is_ipv4)) {
        *ipv4 = override->ipv4;
        return 0;
    }

    *ipv4 = cfg->global.is_ipv4
        ? cfg->global.ipv4
        : FACEMGR_CFG_DEFAULT_IPV4;
    return 0;
}

int
facemgr_cfg_get_ipv6(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        bool * ipv6)
{
    facemgr_cfg_override_t * override;
    int rc = facemgr_cfg_get_override(cfg, netdevice, netdevice_type,
            &override);
    if (rc < 0)
        return rc;

    if ((override) && (override->is_ipv6)) {
        *ipv6 = override->ipv6;
        return 0;
    }

    *ipv6 = cfg->global.is_ipv6
        ? cfg->global.ipv6
        : FACEMGR_CFG_DEFAULT_IPV6;
    return 0;
}

int
facemgr_cfg_get_ignore(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        bool * ignore)
{
    facemgr_cfg_override_t * override;
    int rc = facemgr_cfg_get_override(cfg, netdevice, netdevice_type,
            &override);
    if (rc < 0)
        return rc;

    if ((override) && (override->is_ignore)) {
        *ignore = override->ignore;
        return 0;
    }

    assert (!cfg->global.is_ignore);

    *ignore = (netdevice && (netdevice->name[0] != '\0') && strcmp(netdevice->name, "lo") == 0);

    return 0;
}

int
facemgr_cfg_get_overlay_local_addr(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        int family, ip_address_t * addr)
{
    facemgr_cfg_override_t * override;
    int rc = facemgr_cfg_get_override(cfg, netdevice, netdevice_type,
            &override);
    if (rc < 0)
        return rc;

    switch (family) {
        case AF_INET:
            if ((override) && (override->overlays.v4) && (override->overlays.v4->is_local_addr)) {
                *addr = override->overlays.v4->local_addr;
                return 0;
            }
            if ((cfg->global.overlays.v4) && (cfg->global.overlays.v4->is_local_addr)) {
                *addr = cfg->global.overlays.v4->local_addr;
                return 0;
            }
            break;
        case AF_INET6:
            if ((override) && (override->overlays.v6) && (override->overlays.v6->is_local_addr)) {
                *addr = override->overlays.v6->local_addr;
                return 0;
            }
            if ((cfg->global.overlays.v6) && (cfg->global.overlays.v6->is_local_addr)) {
                *addr = cfg->global.overlays.v6->local_addr;
                return 0;
            }
            break;
        case AF_UNSPEC:
            break;
        default:
            return -1;
    }

    *addr = IP_ADDRESS_EMPTY;
    return 0;
}

int
facemgr_cfg_get_overlay_local_port(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        int family, u16 * port)
{
    facemgr_cfg_override_t * override;
    int rc = facemgr_cfg_get_override(cfg, netdevice, netdevice_type,
            &override);
    if (rc < 0)
        return rc;

    switch (family) {
        case AF_INET:
            if ((override) && (override->overlays.v4) && (override->overlays.v4->is_local_port)) {
                *port = override->overlays.v4->local_port;
                return 0;
            }
            if ((cfg->global.overlays.v4) && (cfg->global.overlays.v4->is_local_port)) {
                *port = cfg->global.overlays.v4->local_port;
                return 0;
            }
            break;
        case AF_INET6:
            if ((override) && (override->overlays.v6) && (override->overlays.v6->is_local_port)) {
                *port = override->overlays.v6->local_port;
                return 0;
            }
            if ((cfg->global.overlays.v6) && (cfg->global.overlays.v6->is_local_port)) {
                *port = cfg->global.overlays.v6->local_port;
                return 0;
            }
            break;
        case AF_UNSPEC:
            break;
        default:
            return -1;
    }

    *port = HICN_DEFAULT_PORT;
    return 0;
}

int
facemgr_cfg_get_overlay_remote_addr(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        int family, ip_address_t * addr)
{
    facemgr_cfg_override_t * override;
    int rc = facemgr_cfg_get_override(cfg, netdevice, netdevice_type,
            &override);
    if (rc < 0)
        return rc;

    switch (family) {
        case AF_INET:
            if ((override) && (override->overlays.v4) && (override->overlays.v4->is_remote_addr)) {
                DEBUG("remote addr v4 from override");
                *addr = override->overlays.v4->remote_addr;
                return 0;
            }
            if ((cfg->global.overlays.v4) && (cfg->global.overlays.v4->is_remote_addr)) {
                DEBUG("remote addr v4 from global");
                *addr = cfg->global.overlays.v4->remote_addr;
                return 0;
            }
            break;
        case AF_INET6:
            if ((override) && (override->overlays.v6) && (override->overlays.v6->is_remote_addr)) {
                DEBUG("remote addr v6 from override");
                *addr = override->overlays.v6->remote_addr;
                return 0;
            }
            if ((cfg->global.overlays.v6) && (cfg->global.overlays.v6->is_remote_addr)) {
                DEBUG("remote addr v6 from global");
                *addr = cfg->global.overlays.v6->remote_addr;
                return 0;
            }
            break;
        case AF_UNSPEC:
            break;
        default:
            return -1;
    }

    DEBUG("remote addr empty");
    *addr = IP_ADDRESS_EMPTY;
    return 0;
}

int
facemgr_cfg_get_overlay_remote_port(const facemgr_cfg_t * cfg,
        const netdevice_t * netdevice, netdevice_type_t netdevice_type,
        int family, u16 * port)
{
    facemgr_cfg_override_t * override;
    int rc = facemgr_cfg_get_override(cfg, netdevice, netdevice_type,
            &override);
    if (rc < 0)
        return rc;

    switch (family) {
        case AF_INET:
            if ((override) && (override->overlays.v4) && (override->overlays.v4->is_remote_port)) {
                *port = override->overlays.v4->remote_port;
                return 0;
            }
            if ((cfg->global.overlays.v4) && (cfg->global.overlays.v4->is_remote_port)) {
                *port = cfg->global.overlays.v4->remote_port;
                return 0;
            }
            break;
        case AF_INET6:
            if ((override) && (override->overlays.v6) && (override->overlays.v6->is_remote_port)) {
                *port = override->overlays.v6->remote_port;
                return 0;
            }
            if ((cfg->global.overlays.v6) && (cfg->global.overlays.v6->is_remote_port)) {
                *port = cfg->global.overlays.v6->remote_port;
                return 0;
            }
            break;
        case AF_UNSPEC:
            break;
        default:
            return -1;
    }

    *port = HICN_DEFAULT_PORT;
    return 0;
}