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/Makefile.am | 9 +++++++++ examples/nfacct-add.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ examples/nfacct-get.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 examples/Makefile.am create mode 100644 examples/nfacct-add.c create mode 100644 examples/nfacct-get.c (limited to 'examples') diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..07387f0 --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,9 @@ +include $(top_srcdir)/Make_global.am + +check_PROGRAMS = nfacct-get nfacct-add + +nfacct_add_SOURCES = nfacct-add.c +nfacct_add_LDADD = ../src/libnetfilter_acct.la ${LIBMNL_LIBS} + +nfacct_get_SOURCES = nfacct-get.c +nfacct_get_LDADD = ../src/libnetfilter_acct.la ${LIBMNL_LIBS} diff --git a/examples/nfacct-add.c b/examples/nfacct-add.c new file mode 100644 index 0000000..df3175e --- /dev/null +++ b/examples/nfacct-add.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +int main(void) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + uint32_t portid, seq; + struct nfacct nfacct = { + .name = "example", + .pkts = 10, + .bytes = 10, + }; + int ret; + + nlh = nfacct_add(buf, &nfacct); + 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; +} 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