libipt_ACCOUNT: (tomj) renamed example application to "iptaccount", include it in...
[libipt_ACCOUNT] / iptaccount / iptaccount.c
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>
17
18 #include <ipt_ACCOUNT_cl.h>
19
20 char *addr_to_dotted(unsigned int addr)
21 {
22     static char buf[20];
23     const unsigned char *bytep;
24
25     bytep = (const unsigned char *) &addr;
26     sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
27     return buf;
28 }
29
30 int main(int argc, char *argv[])
31 {
32     struct ipt_ACCOUNT_context ctx;
33     struct ipt_account_handle_ip *entry;
34     int i = 0;
35     
36     if (argc != 2)
37     {
38         printf("Syntax: %s TABLE-NAME\n", argv[0]);
39         exit (-1);
40     }
41     
42     if(ipt_ACCOUNT_init(&ctx))
43     {
44         printf("Init failed: %s\n", ctx.error_str);
45         exit (-1);
46     }
47
48     while (1)
49     {
50         printf("Run #%d\n", i);
51         
52         // Get entries from table test
53         if (ipt_ACCOUNT_read_entries(&ctx, argv[1], 0))
54         {
55             printf("Read failed: %s\n", ctx.error_str);
56             ipt_ACCOUNT_deinit(&ctx);
57             exit (-1);
58         }
59         
60         // Output and free entries
61         while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL)
62         {
63             printf("IP: %s SRC packets: %u bytes: %u DST packets: %u bytes: %u\n",
64                    addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes, entry->dst_packets, entry->dst_bytes);
65         }
66         sleep(1);
67         i++;
68     }
69             
70     ipt_ACCOUNT_deinit(&ctx);
71     exit (0);
72 }