1 /***************************************************************************
2 * Copyright (C) 2004-2006 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]);
45 printf ("Unknown command line option. Try: [-u] [-h] [-a] [-f] [-c] [-s] [-l name]\n");
46 printf("[-u] show kernel handle usage\n");
47 printf("[-h] free all kernel handles (experts only!)\n");
48 printf("[-a] list all table names\n\n");
49 printf("[-l name] show table data\n");
50 printf("[-f] flush data after show\n");
51 printf("[-c] loop every second (abort with CTRL+C)\n");
52 printf("[-s] CSV output (for spreadsheet import)\n");
56 int main(int argc, char *argv[])
58 struct ipt_ACCOUNT_context ctx;
59 struct ipt_acc_handle_ip *entry;
61 char optchar, doHandleUsage=0, doHandleFree=0, doTableNames=0,
62 doFlush=0, doContinue=0, doCSV=0;
64 char *table_name = NULL;
67 printf("\nipt_ACCOUNT userspace accounting tool v%s\n\n", VERSION);
75 while ((optchar = getopt (argc, argv, "uhacfsl:")) != -1)
98 table_name = (char *)strdup(optarg);
108 // install exit handler
109 if (signal(SIGTERM, sig_term) == SIG_ERR)
111 printf("can't install signal handler for SIGTERM\n");
114 if (signal(SIGINT, sig_term) == SIG_ERR)
116 printf("can't install signal handler for SIGINT\n");
119 if (signal(SIGQUIT, sig_term) == SIG_ERR)
121 printf("can't install signal handler for SIGQUIT\n");
125 if(ipt_ACCOUNT_init(&ctx))
127 printf("Init failed: %s\n", ctx.error_str);
134 int rtn = ipt_ACCOUNT_get_handle_usage(&ctx);
137 printf("get_handle_usage failed: %s\n", ctx.error_str);
141 printf("Current kernel handle usage: %d\n", ctx.handle.itemcount);
146 int rtn = ipt_ACCOUNT_free_all_handles(&ctx);
149 printf("handle_free_all failed: %s\n", ctx.error_str);
153 printf("Freed all handles in kernel space\n");
158 int rtn = ipt_ACCOUNT_get_table_names(&ctx);
161 printf("get_table_names failed: %s\n", ctx.error_str);
164 while ((name = ipt_ACCOUNT_get_next_name(&ctx)) != 0)
165 printf("Found table: %s\n", name);
172 printf("IP;SRC packets;SRC bytes;DST packets;DST bytes\n");
174 printf("Showing table: %s\n", table_name);
179 // Get entries from table test
180 if (ipt_ACCOUNT_read_entries(&ctx, table_name, !doFlush))
182 printf("Read failed: %s\n", ctx.error_str);
183 ipt_ACCOUNT_deinit(&ctx);
188 printf("Run #%d - %u %s found\n", i, ctx.handle.itemcount,
189 ctx.handle.itemcount == 1 ? "item" : "items");
191 // Output and free entries
192 while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL)
195 printf("%s;%u;%u;%u;%u\n",
196 addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes,
197 entry->dst_packets, entry->dst_bytes);
199 printf("IP: %s SRC packets: %u bytes: %u DST packets: %u bytes: %u\n",
200 addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes,
201 entry->dst_packets, entry->dst_bytes);
213 printf("Finished.\n");
214 ipt_ACCOUNT_deinit(&ctx);