• Main Page
  • Modules
  • Files
  • File List

nfacct-get.c

00001 /* This example is in the public domain. */
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <time.h>
00005 #include <libmnl/libmnl.h>
00006 #include <libnetfilter_acct/libnetfilter_acct.h>
00007 
00008 static int nfacct_cb(const struct nlmsghdr *nlh, void *data)
00009 {
00010         struct nfacct *nfacct;
00011         char buf[4096];
00012 
00013         nfacct = nfacct_alloc();
00014         if (nfacct == NULL) {
00015                 perror("OOM");
00016                 goto err;
00017         }
00018 
00019         if (nfacct_nlmsg_parse_payload(nlh, nfacct) < 0) {
00020                 perror("nfacct_parse_nl_msg");
00021                 goto err_free;
00022         }
00023 
00024         nfacct_snprintf(buf, sizeof(buf), nfacct,
00025                         NFACCT_SNPRINTF_T_PLAIN, NFACCT_SNPRINTF_F_FULL);
00026         printf("%s\n", buf);
00027 
00028 err_free:
00029         nfacct_free(nfacct);
00030 err:
00031         return MNL_CB_OK;
00032 }
00033 
00034 int main(int argc, char *argv[])
00035 {
00036         struct mnl_socket *nl;
00037         char buf[MNL_SOCKET_BUFFER_SIZE];
00038         struct nlmsghdr *nlh;
00039         uint32_t portid, seq;
00040         int ret, full = 1;
00041         bool zeroctr = false;
00042 
00043         if (argc > 2) {
00044                 fprintf(stderr, "Usage: %s [-z]\n", argv[0]);
00045                 exit(EXIT_FAILURE);
00046         }
00047 
00048         if (argc == 2 && strncmp(argv[1], "-z", strlen("-z")) == 0)
00049                 zeroctr = true;
00050 
00051         seq = time(NULL);
00052         nlh = nfacct_nlmsg_build_hdr(buf, zeroctr ?
00053                                         NFNL_MSG_ACCT_GET_CTRZERO :
00054                                         NFNL_MSG_ACCT_GET,
00055                                      NLM_F_DUMP, seq);
00056 
00057         nl = mnl_socket_open(NETLINK_NETFILTER);
00058         if (nl == NULL) {
00059                 perror("mnl_socket_open");
00060                 exit(EXIT_FAILURE);
00061         }
00062 
00063         if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
00064                 perror("mnl_socket_bind");
00065                 exit(EXIT_FAILURE);
00066         }
00067         portid = mnl_socket_get_portid(nl);
00068 
00069         if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
00070                 perror("mnl_socket_send");
00071                 exit(EXIT_FAILURE);
00072         }
00073 
00074         ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
00075         while (ret > 0) {
00076                 ret = mnl_cb_run(buf, ret, seq, portid, nfacct_cb, &full);
00077                 if (ret <= 0)
00078                         break;
00079                 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
00080         }
00081         if (ret == -1) {
00082                 perror("error");
00083                 exit(EXIT_FAILURE);
00084         }
00085         mnl_socket_close(nl);
00086 
00087         return EXIT_SUCCESS;
00088 }

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