summaryrefslogtreecommitdiffstats
path: root/examples/nfacct-del.c
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@netfilter.org>2011-12-28 20:16:46 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2011-12-29 15:40:36 +0100
commit3388d7df304f26617c4487418c05734ae4fce5b8 (patch)
treec4fe8bb3cd31a8d85a908485e0b79e1817fefdad /examples/nfacct-del.c
parenta69ac90ec039509ef6ec2684bda156ed6d41fc83 (diff)
src: major API redesign
This patch reworks the initial API. Now it provides functions to: - allocate/release accounting objects. - set/unset/get attributes of accounting objects. - build one netlink message from one accounting object. - parse one netlink message to one accounting object. - print one accounting object into a buffer. Binary layout of nfacct objects are opaque. This is good for extensibility without breaking backward compatibility. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'examples/nfacct-del.c')
-rw-r--r--examples/nfacct-del.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/nfacct-del.c b/examples/nfacct-del.c
new file mode 100644
index 0000000..7a1190c
--- /dev/null
+++ b/examples/nfacct-del.c
@@ -0,0 +1,67 @@
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include <libmnl/libmnl.h>
+#include <libnetfilter_acct/libnetfilter_acct.h>
+
+int main(int argc, char *argv[])
+{
+ struct mnl_socket *nl;
+ char buf[MNL_SOCKET_BUFFER_SIZE];
+ struct nlmsghdr *nlh;
+ uint32_t portid, seq;
+ struct nfacct *nfacct;
+ int ret;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s [name]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ nfacct = nfacct_alloc();
+ if (nfacct == NULL) {
+ perror("OOM");
+ exit(EXIT_FAILURE);
+ }
+
+ nfacct_attr_set(nfacct, NFACCT_ATTR_NAME, argv[1]);
+
+ seq = time(NULL);
+ nlh = nfacct_nlmsg_build_hdr(buf, NFNL_MSG_ACCT_DEL,
+ NLM_F_ACK, seq);
+ nfacct_nlmsg_build_payload(nlh, nfacct);
+
+ nfacct_free(nfacct);
+
+ 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, NULL, 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;
+}