1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
7
8 static int n = 0;
9
10 static int
ct_event_cb(enum nf_conntrack_msg_type type,struct nf_conntrack * ct,void * data)11 ct_event_cb(enum nf_conntrack_msg_type type,struct nf_conntrack *ct, void *data)
12 {
13 char buf[1024];
14
15 nfct_snprintf(buf, sizeof(buf), ct, type, NFCT_O_PLAIN, NFCT_OF_TIME);
16 printf("[CT] %s\n", buf);
17
18 if (++n == 20)
19 return NFCT_CB_STOP;
20
21 return NFCT_CB_CONTINUE;
22 }
23
24 static int
exp_event_cb(enum nf_conntrack_msg_type type,struct nf_expect * exp,void * data)25 exp_event_cb(enum nf_conntrack_msg_type type,struct nf_expect *exp, void *data)
26 {
27 char buf[1024];
28
29 nfexp_snprintf(buf, 1024, exp, type, NFCT_O_DEFAULT, 0);
30 printf("[EXP] %s\n", buf);
31
32 if (++n == 20)
33 return NFCT_CB_STOP;
34
35 return NFCT_CB_CONTINUE;
36 }
37
main(void)38 int main(void)
39 {
40 int ret = 0;
41 struct nfct_handle *h;
42
43 h = nfct_open(NFNL_SUBSYS_NONE, NF_NETLINK_CONNTRACK_EXP_NEW |
44 NF_NETLINK_CONNTRACK_EXP_UPDATE |
45 NF_NETLINK_CONNTRACK_EXP_DESTROY |
46 NF_NETLINK_CONNTRACK_NEW |
47 NF_NETLINK_CONNTRACK_UPDATE |
48 NF_NETLINK_CONNTRACK_DESTROY);
49 if (h == NULL) {
50 perror("nfct_open");
51 return -1;
52 }
53
54 nfexp_callback_register(h, NFCT_T_ALL, exp_event_cb, NULL);
55 nfct_callback_register(h, NFCT_T_ALL, ct_event_cb, NULL);
56
57 printf("TEST: waiting for 20 expectation events...\n");
58
59 /* we may use nfexp_catch() instead, it would also work. */
60 ret = nfct_catch(h);
61
62 printf("TEST: expectation events ");
63 if (ret == -1)
64 printf("(%d)(%s)\n", ret, strerror(errno));
65 else
66 printf("(OK)\n");
67
68 nfct_close(h);
69
70 ret == -1 ? exit(EXIT_FAILURE) : exit(EXIT_SUCCESS);
71 }
72