diff options
author | Pablo Neira Ayuso <pablo@netfilter.org> | 2011-10-16 17:50:55 +0200 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2011-10-16 21:28:13 +0200 |
commit | 1cfe3cf24db29bd9274c2e59587cc0960e9db47c (patch) | |
tree | 4e71628daab46e7197d15960cbfd7618a5976db2 /examples/nfacct-get.c |
intial import of libnetfilter_acct
Diffstat (limited to 'examples/nfacct-get.c')
-rw-r--r-- | examples/nfacct-get.c | 48 |
1 files changed, 48 insertions, 0 deletions
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 <stdlib.h> +#include <time.h> +#include <libmnl/libmnl.h> +#include <libnetfilter_acct/libnetfilter_acct.h> + +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; +} |