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