Commit | Line | Data |
---|---|---|
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 |
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); | |
28 | ||
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]); |
3a07e3fb TJ |
39 | return buf; |
40 | } | |
41 | ||
bdcab405 TJ |
42 | void 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"); | |
47 | printf("[-u] list all table names\n\n"); | |
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 |
54 | int main(int argc, char *argv[]) |
55 | { | |
56 | struct ipt_ACCOUNT_context ctx; | |
57 | struct ipt_account_handle_ip *entry; | |
bdcab405 TJ |
58 | int i; |
59 | char optchar, doHandleUsage=0, doHandleFree=0, doListAll=0, doFlush=0, doContinue=0; | |
60 | char *table_name = NULL; | |
61 | ||
62 | // Show handle use count | |
63 | // Free all handles | |
64 | // List all tables | |
65 | // do flush | |
66 | // list TABLENAME | |
67 | ||
68 | printf("\nipt_ACCOUNT userspace accounting tool v%s\n\n", VERSION); | |
69 | ||
70 | if (argc == 1) | |
71 | { | |
72 | show_usage(); | |
73 | exit(0); | |
74 | } | |
75 | ||
76 | while ((optchar = getopt (argc, argv, "uhacfl:")) != -1) | |
77 | { | |
78 | switch (optchar) | |
79 | { | |
80 | case 'u': | |
81 | doHandleUsage=1; | |
82 | break; | |
83 | case 'h': | |
84 | doHandleFree=1; | |
85 | break; | |
86 | case 'a': | |
87 | doListAll=1; | |
88 | break; | |
89 | case 'f': | |
90 | doFlush=1; | |
91 | break; | |
92 | case 'c': | |
93 | doContinue=1; | |
94 | break; | |
95 | case 'l': | |
96 | table_name = (char *)strdup(optarg); | |
97 | break; | |
98 | case '?': | |
99 | default: | |
100 | show_usage(); | |
101 | exit (0); | |
102 | break; | |
103 | } | |
104 | } | |
3a07e3fb | 105 | |
bdcab405 TJ |
106 | // install exit handler |
107 | if (signal(SIGTERM, sig_term) == SIG_ERR) | |
108 | { | |
109 | printf("can't install signal handler for SIGTERM\n"); | |
110 | exit (-1); | |
111 | } | |
112 | if (signal(SIGINT, sig_term) == SIG_ERR) | |
113 | { | |
114 | printf("can't install signal handler for SIGINT\n"); | |
115 | exit (-1); | |
116 | } | |
117 | if (signal(SIGQUIT, sig_term) == SIG_ERR) | |
3a07e3fb | 118 | { |
bdcab405 | 119 | printf("can't install signal handler for SIGQUIT\n"); |
3a07e3fb TJ |
120 | exit (-1); |
121 | } | |
122 | ||
bdcab405 | 123 | |
3a07e3fb TJ |
124 | if(ipt_ACCOUNT_init(&ctx)) |
125 | { | |
126 | printf("Init failed: %s\n", ctx.error_str); | |
127 | exit (-1); | |
128 | } | |
129 | ||
bdcab405 TJ |
130 | // Read out data |
131 | printf("Showing table: %s\n", table_name); | |
132 | i = 0; | |
133 | while (!exit_now) | |
3a07e3fb | 134 | { |
3a07e3fb | 135 | // Get entries from table test |
bdcab405 | 136 | if (ipt_ACCOUNT_read_entries(&ctx, table_name, !doFlush)) |
3a07e3fb TJ |
137 | { |
138 | printf("Read failed: %s\n", ctx.error_str); | |
139 | ipt_ACCOUNT_deinit(&ctx); | |
140 | exit (-1); | |
141 | } | |
142 | ||
bdcab405 TJ |
143 | printf("Run #%d - %u %s found\n", i, ctx.handle.itemcount, ctx.handle.itemcount == 1 ? "item" : "items"); |
144 | ||
3a07e3fb TJ |
145 | // Output and free entries |
146 | while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL) | |
147 | { | |
148 | printf("IP: %s SRC packets: %u bytes: %u DST packets: %u bytes: %u\n", | |
149 | addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes, entry->dst_packets, entry->dst_bytes); | |
150 | } | |
bdcab405 TJ |
151 | |
152 | if (doContinue) | |
153 | { | |
154 | sleep(1); | |
155 | i++; | |
156 | } else | |
157 | exit_now = 1; | |
3a07e3fb TJ |
158 | } |
159 | ||
bdcab405 | 160 | printf("Finished.\n"); |
3a07e3fb TJ |
161 | ipt_ACCOUNT_deinit(&ctx); |
162 | exit (0); | |
163 | } |