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