Home | History | Annotate | Download | only in qa
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <signal.h>
      5 #include <errno.h>
      6 
      7 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
      8 
      9 static int events = 0;
     10 static int new, update, destroy;
     11 
     12 static int event_cb(enum nf_conntrack_msg_type type,
     13 		    struct nf_conntrack *ct,
     14 		    void *data)
     15 {
     16 	if (type == NFCT_T_NEW)
     17 		new++;
     18 	else if (type == NFCT_T_UPDATE)
     19 		update++;
     20 	else if (type == NFCT_T_DESTROY)
     21 		destroy++;
     22 
     23 	if ((++events % 10000) == 0)
     24 		printf("%d events received (%d new, %d update, %d destroy)\n",
     25 			events, new, update, destroy);
     26 
     27 	return NFCT_CB_CONTINUE;
     28 }
     29 
     30 static void sighandler(int foo)
     31 {
     32 	printf("%d events received (%d new, %d update, %d destroy)\n",
     33 		events, new, update, destroy);
     34 	exit(EXIT_SUCCESS);
     35 }
     36 
     37 int main(void)
     38 {
     39 	int ret;
     40 	struct nfct_handle *h;
     41 	int on = 1;
     42 
     43 	signal(SIGINT, sighandler);
     44 
     45 	h = nfct_open(CONNTRACK, NFCT_ALL_CT_GROUPS);
     46 	if (!h) {
     47 		perror("nfct_open");
     48 		return 0;
     49 	}
     50 
     51 	setsockopt(nfct_fd(h), SOL_NETLINK,
     52 			NETLINK_BROADCAST_SEND_ERROR, &on, sizeof(int));
     53 	setsockopt(nfct_fd(h), SOL_NETLINK,
     54 			NETLINK_NO_ENOBUFS, &on, sizeof(int));
     55 
     56 	nfct_callback_register(h, NFCT_T_ALL, event_cb, NULL);
     57 
     58 	printf("TEST: waiting for events...\n");
     59 
     60 	ret = nfct_catch(h);
     61 
     62 	printf("TEST: conntrack 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