compile fixes
[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]);
c2cd6bde 39 buf[16] = 0;
3a07e3fb
TJ
40 return buf;
41}
42
bdcab405
TJ
43void show_usage(void)
44{
45 printf ("Unknown command line option. Try: [-u] [-h] [-a] [-f] [-l name]\n");
46 printf("[-u] show kernel handle usage\n");
47 printf("[-h] free all kernel handles (experts only!)\n");
d7e0bb9c 48 printf("[-a] list all table names\n\n");
bdcab405
TJ
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("\n");
53}
54
3a07e3fb
TJ
55int main(int argc, char *argv[])
56{
57 struct ipt_ACCOUNT_context ctx;
e3ccc02f 58 struct ipt_acc_handle_ip *entry;
bdcab405 59 int i;
e3ccc02f
TJ
60 char optchar, doHandleUsage=0, doHandleFree=0, doTableNames=0,
61 doFlush=0, doContinue=0;
62
bdcab405 63 char *table_name = NULL;
28ea7aa8 64 const char *name;
bdcab405 65
bdcab405
TJ
66 printf("\nipt_ACCOUNT userspace accounting tool v%s\n\n", VERSION);
67
68 if (argc == 1)
69 {
70 show_usage();
71 exit(0);
72 }
73
74 while ((optchar = getopt (argc, argv, "uhacfl:")) != -1)
75 {
76 switch (optchar)
77 {
78 case 'u':
79 doHandleUsage=1;
80 break;
81 case 'h':
82 doHandleFree=1;
83 break;
84 case 'a':
d7e0bb9c 85 doTableNames=1;
bdcab405
TJ
86 break;
87 case 'f':
88 doFlush=1;
89 break;
90 case 'c':
91 doContinue=1;
92 break;
93 case 'l':
94 table_name = (char *)strdup(optarg);
95 break;
96 case '?':
97 default:
98 show_usage();
99 exit (0);
100 break;
101 }
102 }
3a07e3fb 103
bdcab405
TJ
104 // install exit handler
105 if (signal(SIGTERM, sig_term) == SIG_ERR)
106 {
107 printf("can't install signal handler for SIGTERM\n");
108 exit (-1);
109 }
110 if (signal(SIGINT, sig_term) == SIG_ERR)
111 {
112 printf("can't install signal handler for SIGINT\n");
113 exit (-1);
114 }
115 if (signal(SIGQUIT, sig_term) == SIG_ERR)
3a07e3fb 116 {
bdcab405 117 printf("can't install signal handler for SIGQUIT\n");
3a07e3fb
TJ
118 exit (-1);
119 }
120
bdcab405 121
3a07e3fb
TJ
122 if(ipt_ACCOUNT_init(&ctx))
123 {
124 printf("Init failed: %s\n", ctx.error_str);
125 exit (-1);
126 }
127
d7e0bb9c
TJ
128 // Get handle usage?
129 if (doHandleUsage)
3a07e3fb 130 {
d7e0bb9c
TJ
131 int rtn = ipt_ACCOUNT_get_handle_usage(&ctx);
132 if (rtn < 0)
3a07e3fb 133 {
d7e0bb9c 134 printf("get_handle_usage failed: %s\n", ctx.error_str);
3a07e3fb
TJ
135 exit (-1);
136 }
137
d7e0bb9c
TJ
138 printf("Current kernel handle usage: %d\n", ctx.handle.itemcount);
139 }
140
141 if (doHandleFree)
142 {
143 int rtn = ipt_ACCOUNT_free_all_handles(&ctx);
144 if (rtn < 0)
3a07e3fb 145 {
d7e0bb9c
TJ
146 printf("handle_free_all failed: %s\n", ctx.error_str);
147 exit (-1);
3a07e3fb 148 }
bdcab405 149
d7e0bb9c
TJ
150 printf("Freed all handles in kernel space\n");
151 }
152
153 if (doTableNames)
154 {
155 int rtn = ipt_ACCOUNT_get_table_names(&ctx);
156 if (rtn < 0)
bdcab405 157 {
d7e0bb9c
TJ
158 printf("get_table_names failed: %s\n", ctx.error_str);
159 exit (-1);
160 }
e3ccc02f 161 while ((name = ipt_ACCOUNT_get_next_name(&ctx)) != 0)
d7e0bb9c 162 printf("Found table: %s\n", name);
3a07e3fb 163 }
d7e0bb9c
TJ
164
165 if (table_name)
166 {
167 // Read out data
168 printf("Showing table: %s\n", table_name);
169 i = 0;
170 while (!exit_now)
171 {
172 // Get entries from table test
173 if (ipt_ACCOUNT_read_entries(&ctx, table_name, !doFlush))
174 {
175 printf("Read failed: %s\n", ctx.error_str);
176 ipt_ACCOUNT_deinit(&ctx);
177 exit (-1);
178 }
3a07e3fb 179
e3ccc02f
TJ
180 printf("Run #%d - %u %s found\n", i, ctx.handle.itemcount,
181 ctx.handle.itemcount == 1 ? "item" : "items");
d7e0bb9c
TJ
182
183 // Output and free entries
184 while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL)
185 {
186 printf("IP: %s SRC packets: %u bytes: %u DST packets: %u bytes: %u\n",
e3ccc02f
TJ
187 addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes,
188 entry->dst_packets, entry->dst_bytes);
d7e0bb9c
TJ
189 }
190
191 if (doContinue)
192 {
193 sleep(1);
194 i++;
195 } else
196 exit_now = 1;
197 }
198 }
199
bdcab405 200 printf("Finished.\n");
3a07e3fb
TJ
201 ipt_ACCOUNT_deinit(&ctx);
202 exit (0);
203}