Commit | Line | Data |
---|---|---|
3a07e3fb | 1 | /*************************************************************************** |
1913c894 | 2 | * Copyright (C) 2004-2006 by Intra2net AG * |
3a07e3fb TJ |
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 |
22 | char exit_now = 0; |
23 | static void sig_term(int signr) | |
24 | { | |
25 | signal(SIGINT, SIG_IGN); | |
26 | signal(SIGQUIT, SIG_IGN); | |
27 | signal(SIGTERM, SIG_IGN); | |
1913c894 | 28 | |
bdcab405 TJ |
29 | exit_now=1; |
30 | } | |
31 | ||
3a07e3fb TJ |
32 | char *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 |
43 | void show_usage(void) |
44 | { | |
1913c894 | 45 | printf ("Unknown command line option. Try: [-u] [-h] [-a] [-f] [-c] [-s] [-l name]\n"); |
bdcab405 TJ |
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"); | |
1913c894 | 52 | printf("[-s] CSV output (for spreadsheet import)\n"); |
bdcab405 TJ |
53 | printf("\n"); |
54 | } | |
55 | ||
3a07e3fb TJ |
56 | int main(int argc, char *argv[]) |
57 | { | |
58 | struct ipt_ACCOUNT_context ctx; | |
e3ccc02f | 59 | struct ipt_acc_handle_ip *entry; |
bdcab405 | 60 | int i; |
e3ccc02f | 61 | char optchar, doHandleUsage=0, doHandleFree=0, doTableNames=0, |
1913c894 TJ |
62 | doFlush=0, doContinue=0, doCSV=0; |
63 | ||
bdcab405 | 64 | char *table_name = NULL; |
28ea7aa8 | 65 | const char *name; |
1913c894 | 66 | |
bdcab405 TJ |
67 | printf("\nipt_ACCOUNT userspace accounting tool v%s\n\n", VERSION); |
68 | ||
69 | if (argc == 1) | |
70 | { | |
71 | show_usage(); | |
72 | exit(0); | |
73 | } | |
74 | ||
1913c894 | 75 | while ((optchar = getopt (argc, argv, "uhacfsl:")) != -1) |
bdcab405 TJ |
76 | { |
77 | switch (optchar) | |
78 | { | |
79 | case 'u': | |
80 | doHandleUsage=1; | |
81 | break; | |
82 | case 'h': | |
83 | doHandleFree=1; | |
84 | break; | |
85 | case 'a': | |
d7e0bb9c | 86 | doTableNames=1; |
bdcab405 TJ |
87 | break; |
88 | case 'f': | |
89 | doFlush=1; | |
90 | break; | |
91 | case 'c': | |
92 | doContinue=1; | |
93 | break; | |
1913c894 TJ |
94 | case 's': |
95 | doCSV=1; | |
96 | break; | |
bdcab405 TJ |
97 | case 'l': |
98 | table_name = (char *)strdup(optarg); | |
99 | break; | |
100 | case '?': | |
101 | default: | |
102 | show_usage(); | |
103 | exit (0); | |
104 | break; | |
105 | } | |
106 | } | |
1913c894 | 107 | |
bdcab405 TJ |
108 | // install exit handler |
109 | if (signal(SIGTERM, sig_term) == SIG_ERR) | |
110 | { | |
111 | printf("can't install signal handler for SIGTERM\n"); | |
112 | exit (-1); | |
113 | } | |
114 | if (signal(SIGINT, sig_term) == SIG_ERR) | |
115 | { | |
116 | printf("can't install signal handler for SIGINT\n"); | |
117 | exit (-1); | |
118 | } | |
119 | if (signal(SIGQUIT, sig_term) == SIG_ERR) | |
3a07e3fb | 120 | { |
bdcab405 | 121 | printf("can't install signal handler for SIGQUIT\n"); |
3a07e3fb TJ |
122 | exit (-1); |
123 | } | |
1913c894 | 124 | |
3a07e3fb TJ |
125 | if(ipt_ACCOUNT_init(&ctx)) |
126 | { | |
127 | printf("Init failed: %s\n", ctx.error_str); | |
128 | exit (-1); | |
129 | } | |
130 | ||
d7e0bb9c TJ |
131 | // Get handle usage? |
132 | if (doHandleUsage) | |
3a07e3fb | 133 | { |
d7e0bb9c TJ |
134 | int rtn = ipt_ACCOUNT_get_handle_usage(&ctx); |
135 | if (rtn < 0) | |
3a07e3fb | 136 | { |
d7e0bb9c | 137 | printf("get_handle_usage failed: %s\n", ctx.error_str); |
3a07e3fb TJ |
138 | exit (-1); |
139 | } | |
1913c894 | 140 | |
d7e0bb9c TJ |
141 | printf("Current kernel handle usage: %d\n", ctx.handle.itemcount); |
142 | } | |
1913c894 | 143 | |
d7e0bb9c TJ |
144 | if (doHandleFree) |
145 | { | |
146 | int rtn = ipt_ACCOUNT_free_all_handles(&ctx); | |
147 | if (rtn < 0) | |
3a07e3fb | 148 | { |
d7e0bb9c TJ |
149 | printf("handle_free_all failed: %s\n", ctx.error_str); |
150 | exit (-1); | |
3a07e3fb | 151 | } |
1913c894 | 152 | |
d7e0bb9c TJ |
153 | printf("Freed all handles in kernel space\n"); |
154 | } | |
155 | ||
156 | if (doTableNames) | |
157 | { | |
158 | int rtn = ipt_ACCOUNT_get_table_names(&ctx); | |
159 | if (rtn < 0) | |
bdcab405 | 160 | { |
d7e0bb9c TJ |
161 | printf("get_table_names failed: %s\n", ctx.error_str); |
162 | exit (-1); | |
163 | } | |
e3ccc02f | 164 | while ((name = ipt_ACCOUNT_get_next_name(&ctx)) != 0) |
d7e0bb9c | 165 | printf("Found table: %s\n", name); |
3a07e3fb | 166 | } |
1913c894 | 167 | |
d7e0bb9c TJ |
168 | if (table_name) |
169 | { | |
170 | // Read out data | |
1913c894 TJ |
171 | if (doCSV) |
172 | printf("IP;SRC packets;SRC bytes;DST packets;DST bytes\n"); | |
173 | else | |
174 | printf("Showing table: %s\n", table_name); | |
175 | ||
d7e0bb9c TJ |
176 | i = 0; |
177 | while (!exit_now) | |
178 | { | |
179 | // Get entries from table test | |
180 | if (ipt_ACCOUNT_read_entries(&ctx, table_name, !doFlush)) | |
181 | { | |
182 | printf("Read failed: %s\n", ctx.error_str); | |
183 | ipt_ACCOUNT_deinit(&ctx); | |
184 | exit (-1); | |
185 | } | |
1913c894 TJ |
186 | |
187 | if (!doCSV) | |
188 | printf("Run #%d - %u %s found\n", i, ctx.handle.itemcount, | |
189 | ctx.handle.itemcount == 1 ? "item" : "items"); | |
190 | ||
d7e0bb9c TJ |
191 | // Output and free entries |
192 | while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL) | |
193 | { | |
1913c894 TJ |
194 | if (doCSV) |
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); | |
198 | else | |
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); | |
d7e0bb9c | 202 | } |
1913c894 | 203 | |
d7e0bb9c TJ |
204 | if (doContinue) |
205 | { | |
206 | sleep(1); | |
207 | i++; | |
208 | } else | |
209 | exit_now = 1; | |
210 | } | |
1913c894 TJ |
211 | } |
212 | ||
bdcab405 | 213 | printf("Finished.\n"); |
3a07e3fb TJ |
214 | ipt_ACCOUNT_deinit(&ctx); |
215 | exit (0); | |
216 | } |