libipt_ACCOUNT: (tomj) fixed illegal handle free issue on deinit
[libipt_ACCOUNT] / iptaccount / iptaccount.c
CommitLineData
3a07e3fb
TJ
1/***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
3 * opensource@intra2net.com *
4 * *
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; *
8 * *
9 ***************************************************************************/
10
11#ifdef HAVE_CONFIG_H
12#include <config.h>
13#endif
14
15#include <stdio.h>
16#include <stdlib.h>
bdcab405
TJ
17#include <getopt.h>
18#include <signal.h>
3a07e3fb
TJ
19
20#include <ipt_ACCOUNT_cl.h>
21
bdcab405
TJ
22char exit_now = 0;
23static void sig_term(int signr)
24{
25 signal(SIGINT, SIG_IGN);
26 signal(SIGQUIT, SIG_IGN);
27 signal(SIGTERM, SIG_IGN);
28
29 exit_now=1;
30}
31
3a07e3fb
TJ
32char *addr_to_dotted(unsigned int addr)
33{
bdcab405 34 static char buf[17];
3a07e3fb
TJ
35 const unsigned char *bytep;
36
37 bytep = (const unsigned char *) &addr;
bdcab405 38 snprintf(buf, 16, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
3a07e3fb
TJ
39 return buf;
40}
41
bdcab405
TJ
42void show_usage(void)
43{
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");
d7e0bb9c 47 printf("[-a] list all table names\n\n");
bdcab405
TJ
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");
51 printf("\n");
52}
53
3a07e3fb
TJ
54int main(int argc, char *argv[])
55{
56 struct ipt_ACCOUNT_context ctx;
57 struct ipt_account_handle_ip *entry;
bdcab405 58 int i;
d7e0bb9c 59 char optchar, doHandleUsage=0, doHandleFree=0, doTableNames=0, doFlush=0, doContinue=0;
bdcab405
TJ
60 char *table_name = NULL;
61
bdcab405
TJ
62 printf("\nipt_ACCOUNT userspace accounting tool v%s\n\n", VERSION);
63
64 if (argc == 1)
65 {
66 show_usage();
67 exit(0);
68 }
69
70 while ((optchar = getopt (argc, argv, "uhacfl:")) != -1)
71 {
72 switch (optchar)
73 {
74 case 'u':
75 doHandleUsage=1;
76 break;
77 case 'h':
78 doHandleFree=1;
79 break;
80 case 'a':
d7e0bb9c 81 doTableNames=1;
bdcab405
TJ
82 break;
83 case 'f':
84 doFlush=1;
85 break;
86 case 'c':
87 doContinue=1;
88 break;
89 case 'l':
90 table_name = (char *)strdup(optarg);
91 break;
92 case '?':
93 default:
94 show_usage();
95 exit (0);
96 break;
97 }
98 }
3a07e3fb 99
bdcab405
TJ
100 // install exit handler
101 if (signal(SIGTERM, sig_term) == SIG_ERR)
102 {
103 printf("can't install signal handler for SIGTERM\n");
104 exit (-1);
105 }
106 if (signal(SIGINT, sig_term) == SIG_ERR)
107 {
108 printf("can't install signal handler for SIGINT\n");
109 exit (-1);
110 }
111 if (signal(SIGQUIT, sig_term) == SIG_ERR)
3a07e3fb 112 {
bdcab405 113 printf("can't install signal handler for SIGQUIT\n");
3a07e3fb
TJ
114 exit (-1);
115 }
116
bdcab405 117
3a07e3fb
TJ
118 if(ipt_ACCOUNT_init(&ctx))
119 {
120 printf("Init failed: %s\n", ctx.error_str);
121 exit (-1);
122 }
123
d7e0bb9c
TJ
124 // Get handle usage?
125 if (doHandleUsage)
3a07e3fb 126 {
d7e0bb9c
TJ
127 int rtn = ipt_ACCOUNT_get_handle_usage(&ctx);
128 if (rtn < 0)
3a07e3fb 129 {
d7e0bb9c 130 printf("get_handle_usage failed: %s\n", ctx.error_str);
3a07e3fb
TJ
131 exit (-1);
132 }
133
d7e0bb9c
TJ
134 printf("Current kernel handle usage: %d\n", ctx.handle.itemcount);
135 }
136
137 if (doHandleFree)
138 {
139 int rtn = ipt_ACCOUNT_free_all_handles(&ctx);
140 if (rtn < 0)
3a07e3fb 141 {
d7e0bb9c
TJ
142 printf("handle_free_all failed: %s\n", ctx.error_str);
143 exit (-1);
3a07e3fb 144 }
bdcab405 145
d7e0bb9c
TJ
146 printf("Freed all handles in kernel space\n");
147 }
148
149 if (doTableNames)
150 {
151 int rtn = ipt_ACCOUNT_get_table_names(&ctx);
152 if (rtn < 0)
bdcab405 153 {
d7e0bb9c
TJ
154 printf("get_table_names failed: %s\n", ctx.error_str);
155 exit (-1);
156 }
157 char *name;
158 while ((name = ipt_ACCOUNT_get_next_name(&ctx)) != NULL)
159 printf("Found table: %s\n", name);
3a07e3fb 160 }
d7e0bb9c
TJ
161
162 if (table_name)
163 {
164 // Read out data
165 printf("Showing table: %s\n", table_name);
166 i = 0;
167 while (!exit_now)
168 {
169 // Get entries from table test
170 if (ipt_ACCOUNT_read_entries(&ctx, table_name, !doFlush))
171 {
172 printf("Read failed: %s\n", ctx.error_str);
173 ipt_ACCOUNT_deinit(&ctx);
174 exit (-1);
175 }
3a07e3fb 176
d7e0bb9c
TJ
177 printf("Run #%d - %u %s found\n", i, ctx.handle.itemcount, ctx.handle.itemcount == 1 ? "item" : "items");
178
179 // Output and free entries
180 while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL)
181 {
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);
184 }
185
186 if (doContinue)
187 {
188 sleep(1);
189 i++;
190 } else
191 exit_now = 1;
192 }
193 }
194
bdcab405 195 printf("Finished.\n");
3a07e3fb
TJ
196 ipt_ACCOUNT_deinit(&ctx);
197 exit (0);
198}