From 1cfe3cf24db29bd9274c2e59587cc0960e9db47c Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Sun, 16 Oct 2011 17:50:55 +0200 Subject: intial import of libnetfilter_acct --- examples/nfacct-get.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/nfacct-get.c (limited to 'examples/nfacct-get.c') diff --git a/examples/nfacct-get.c b/examples/nfacct-get.c new file mode 100644 index 0000000..8ded1f4 --- /dev/null +++ b/examples/nfacct-get.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +int main(void) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + uint32_t portid, seq; + int ret; + + nlh = nfacct_list(buf); + seq = nlh->nlmsg_seq = time(NULL); + + nl = mnl_socket_open(NETLINK_NETFILTER); + if (nl == NULL) { + perror("mnl_socket_open"); + exit(EXIT_FAILURE); + } + + if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { + perror("mnl_socket_bind"); + exit(EXIT_FAILURE); + } + portid = mnl_socket_get_portid(nl); + + if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { + perror("mnl_socket_send"); + exit(EXIT_FAILURE); + } + + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + while (ret > 0) { + ret = mnl_cb_run(buf, ret, seq, portid, nfacct_list_cb, NULL); + if (ret <= 0) + break; + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + } + if (ret == -1) { + perror("error"); + exit(EXIT_FAILURE); + } + mnl_socket_close(nl); + + return EXIT_SUCCESS; +} -- cgit v1.2.3