libnetfilter_conntrack 1.0.9
test_connlabel.c
1#include <assert.h>
2#include <limits.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <time.h>
6
7#include <libmnl/libmnl.h>
8#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
9
10static void print_label(struct nfct_labelmap *map)
11{
12 int b = nfct_labelmap_get_bit(map, "test label 1");
13 assert(b == 1);
14
15 b = nfct_labelmap_get_bit(map, "zero");
16 assert(b == 0);
17
18 b = nfct_labelmap_get_bit(map, "test label 2");
19 assert(b == 2);
20
21 b = nfct_labelmap_get_bit(map, "duplicate");
22 assert(b < 0);
23
24 b = nfct_labelmap_get_bit(map, "invalid label");
25 assert(b < 0);
26
27 b = nfct_labelmap_get_bit(map, "T");
28 assert(b == 42);
29}
30
31static void dump_map(struct nfct_labelmap *map)
32{
33 unsigned int i = 0;
34
35 for (;;) {
36 const char *name = nfct_labelmap_get_name(map, i);
37 if (!name)
38 break;
39 if (name[0])
40 printf("\t\"%s\", bit %d\n", name, i);
41 i++;
42 }
43}
44
45int main(void)
46{
47 struct nfct_labelmap *l;
48
49 l = nfct_labelmap_new("/");
50 assert(l == NULL);
51
52 l = nfct_labelmap_new(NULL);
53 if (l) {
54 puts("default connlabel.conf:");
55 dump_map(l);
57 } else {
58 puts("no default config found");
59 }
60
61 l = nfct_labelmap_new("qa-connlabel.conf");
62 if (!l)
63 l = nfct_labelmap_new("tests/qa-connlabel.conf");
64 if (!l) {
65 char testconf[PATH_MAX];
66
67 snprintf(testconf, PATH_MAX,
68 "%s/qa-connlabel.conf", getenv("srcdir"));
69 l = nfct_labelmap_new(testconf);
70 }
71 assert(l);
72 puts("qa-connlabel.conf:");
73 dump_map(l);
74 print_label(l);
76
77 return 0;
78}
void nfct_labelmap_destroy(struct nfct_labelmap *map)
const char * nfct_labelmap_get_name(struct nfct_labelmap *m, unsigned int bit)
struct nfct_labelmap * nfct_labelmap_new(const char *mapfile)
int nfct_labelmap_get_bit(struct nfct_labelmap *m, const char *name)