libnetfilter_conntrack 1.0.9
conntrack_events.c
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
8static int event_cb(enum nf_conntrack_msg_type type,
9 struct nf_conntrack *ct,
10 void *data)
11{
12 static int n = 0;
13 char buf[1024];
14
15 nfct_snprintf(buf, sizeof(buf), ct, type, NFCT_O_XML, NFCT_OF_TIME | NFCT_OF_TIMESTAMP);
16 printf("%s\n", buf);
17
18 if (++n == 10)
19 return NFCT_CB_STOP;
20
21 return NFCT_CB_CONTINUE;
22}
23
24int main(void)
25{
26 int ret;
27 struct nfct_handle *h;
28
29 h = nfct_open(CONNTRACK, NFCT_ALL_CT_GROUPS);
30 if (!h) {
31 perror("nfct_open");
32 return 0;
33 }
34
35 nfct_callback_register(h, NFCT_T_ALL, event_cb, NULL);
36
37 printf("TEST: waiting for 10 events...\n");
38
39 ret = nfct_catch(h);
40
41 printf("TEST: conntrack events ");
42 if (ret == -1)
43 printf("(%d)(%s)\n", ret, strerror(errno));
44 else
45 printf("(OK)\n");
46
47 nfct_close(h);
48
49 ret == -1 ? exit(EXIT_FAILURE) : exit(EXIT_SUCCESS);
50}
int nfct_callback_register(struct nfct_handle *h, enum nf_conntrack_msg_type type, int(*cb)(enum nf_conntrack_msg_type type, struct nf_conntrack *ct, void *data), void *data)
struct nfct_handle * nfct_open(uint8_t, unsigned)
Definition: main.c:84
int nfct_close(struct nfct_handle *cth)
Definition: main.c:105
int nfct_catch(struct nfct_handle *h)
int nfct_snprintf(char *buf, unsigned int size, const struct nf_conntrack *ct, const unsigned int msg_type, const unsigned int out_type, const unsigned int out_flags)