• Main Page
  • Modules
  • Files
  • File List

nfacct-del.c

00001 /* This example is in the public domain. */
00002 #include <stdlib.h>
00003 #include <time.h>
00004 #include <string.h>
00005 #include <libmnl/libmnl.h>
00006 #include <libnetfilter_acct/libnetfilter_acct.h>
00007 
00008 int main(int argc, char *argv[])
00009 {
00010         struct mnl_socket *nl;
00011         char buf[MNL_SOCKET_BUFFER_SIZE];
00012         struct nlmsghdr *nlh;
00013         uint32_t portid, seq;
00014         struct nfacct *nfacct;
00015         int ret;
00016 
00017         if (argc != 2) {
00018                 fprintf(stderr, "Usage: %s [name]\n", argv[0]);
00019                 exit(EXIT_FAILURE);
00020         }
00021 
00022         nfacct = nfacct_alloc();
00023         if (nfacct == NULL) {
00024                 perror("OOM");
00025                 exit(EXIT_FAILURE);
00026         }
00027 
00028         nfacct_attr_set(nfacct, NFACCT_ATTR_NAME, argv[1]);
00029 
00030         seq = time(NULL);
00031         nlh = nfacct_nlmsg_build_hdr(buf, NFNL_MSG_ACCT_DEL,
00032                                      NLM_F_ACK, seq);
00033         nfacct_nlmsg_build_payload(nlh, nfacct);
00034 
00035         nfacct_free(nfacct);
00036 
00037         nl = mnl_socket_open(NETLINK_NETFILTER);
00038         if (nl == NULL) {
00039                 perror("mnl_socket_open");
00040                 exit(EXIT_FAILURE);
00041         }
00042 
00043         if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
00044                 perror("mnl_socket_bind");
00045                 exit(EXIT_FAILURE);
00046         }
00047         portid = mnl_socket_get_portid(nl);
00048 
00049         if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
00050                 perror("mnl_socket_send");
00051                 exit(EXIT_FAILURE);
00052         }
00053 
00054         ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
00055         while (ret > 0) {
00056                 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
00057                 if (ret <= 0)
00058                         break;
00059                 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
00060         }
00061         if (ret == -1) {
00062                 perror("error");
00063                 exit(EXIT_FAILURE);
00064         }
00065         mnl_socket_close(nl);
00066 
00067         return EXIT_SUCCESS;
00068 }

Generated on Tue Mar 27 2012 12:36:24 for libnetfilter_acct by  doxygen 1.7.1