1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
3 * opensource@intra2net.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License *
7 * version 2.1 as published by the Free Software Foundation; *
9 ***************************************************************************/
20 #include <ipt_ACCOUNT_cl.h>
23 static void sig_term(int signr)
25 signal(SIGINT, SIG_IGN);
26 signal(SIGQUIT, SIG_IGN);
27 signal(SIGTERM, SIG_IGN);
32 char *addr_to_dotted(unsigned int addr)
35 const unsigned char *bytep;
37 bytep = (const unsigned char *) &addr;
38 snprintf(buf, 16, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
44 printf ("Unknown command line option. Try: [-u] [-h] [-a] [-f] [-l name]\n");
45 printf("[-u] show kernel handle usage\n");
46 printf("[-h] free all kernel handles (experts only!)\n");
47 printf("[-a] list all table names\n\n");
48 printf("[-l name] show table data\n");
49 printf("[-f] flush data after show\n");
50 printf("[-c] loop every second (abort with CTRL+C)\n");
54 int main(int argc, char *argv[])
56 struct ipt_ACCOUNT_context ctx;
57 struct ipt_account_handle_ip *entry;
59 char optchar, doHandleUsage=0, doHandleFree=0, doTableNames=0, doFlush=0, doContinue=0;
60 char *table_name = NULL;
62 printf("\nipt_ACCOUNT userspace accounting tool v%s\n\n", VERSION);
70 while ((optchar = getopt (argc, argv, "uhacfl:")) != -1)
90 table_name = (char *)strdup(optarg);
100 // install exit handler
101 if (signal(SIGTERM, sig_term) == SIG_ERR)
103 printf("can't install signal handler for SIGTERM\n");
106 if (signal(SIGINT, sig_term) == SIG_ERR)
108 printf("can't install signal handler for SIGINT\n");
111 if (signal(SIGQUIT, sig_term) == SIG_ERR)
113 printf("can't install signal handler for SIGQUIT\n");
118 if(ipt_ACCOUNT_init(&ctx))
120 printf("Init failed: %s\n", ctx.error_str);
127 int rtn = ipt_ACCOUNT_get_handle_usage(&ctx);
130 printf("get_handle_usage failed: %s\n", ctx.error_str);
134 printf("Current kernel handle usage: %d\n", ctx.handle.itemcount);
139 int rtn = ipt_ACCOUNT_free_all_handles(&ctx);
142 printf("handle_free_all failed: %s\n", ctx.error_str);
146 printf("Freed all handles in kernel space\n");
151 int rtn = ipt_ACCOUNT_get_table_names(&ctx);
154 printf("get_table_names failed: %s\n", ctx.error_str);
158 while ((name = ipt_ACCOUNT_get_next_name(&ctx)) != NULL)
159 printf("Found table: %s\n", name);
165 printf("Showing table: %s\n", table_name);
169 // Get entries from table test
170 if (ipt_ACCOUNT_read_entries(&ctx, table_name, !doFlush))
172 printf("Read failed: %s\n", ctx.error_str);
173 ipt_ACCOUNT_deinit(&ctx);
177 printf("Run #%d - %u %s found\n", i, ctx.handle.itemcount, ctx.handle.itemcount == 1 ? "item" : "items");
179 // Output and free entries
180 while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL)
182 printf("IP: %s SRC packets: %u bytes: %u DST packets: %u bytes: %u\n",
183 addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes, entry->dst_packets, entry->dst_bytes);
195 printf("Finished.\n");
196 ipt_ACCOUNT_deinit(&ctx);