libipt_ACCOUNT_cl: (tomj) wrote complete userspace client library for ipt_ACCOUNT
[libipt_ACCOUNT] / src / ipt_ACCOUNT_cl.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4
5 #include <ipt_ACCOUNT_cl.h>
6
7 int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
8 {
9     memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
10     
11     ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
12     if (ctx->sockfd < 0)
13     {
14         ctx->sockfd = -1;
15         ctx->error_str = "Can't open socket to kernel. Permission denied or ipt_ACCOUNT module not loaded";
16         return -1;
17     }
18     
19     // 4096 bytes default buffer should save us from reallocations
20     // as it fits 200 concurrent active clients
21     if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL)
22     {
23         close (ctx->sockfd);
24         ctx->sockfd = -1;
25         ctx->error_str = "Out of memory for data buffer";
26         return -1;
27     }
28     ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE;
29     
30     return 0;
31 }
32
33 void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx)
34 {
35     if (ctx->handle.handle_nr >= 0)
36     {
37         setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, &ctx->handle, sizeof (struct ipt_account_handle_sockopt));
38         ctx->handle.handle_nr = -1;
39     }
40
41     ctx->handle.itemcount = 0;
42     ctx->pos = 0;
43 }
44
45 void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx)
46 {
47     free(ctx->data);
48     ctx->data = NULL;
49
50     ipt_ACCOUNT_free_entries(ctx);
51         
52     close(ctx->sockfd);
53     ctx->sockfd =-1;
54 }
55
56 int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx, char *table, char dont_flush)
57 {
58     unsigned int s = sizeof (struct ipt_account_handle_sockopt);
59     int rtn;
60     
61     strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
62     
63     // Get table information
64     if (!dont_flush)
65         rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
66     else
67         rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ, &ctx->handle, &s);
68     
69     if (rtn < 0)
70     {
71         ctx->error_str = "Can't get table information from kernel. Is the table existing?";
72         return -1;
73     }
74     
75     // Check data buffer size
76     ctx->pos = 0;
77     unsigned int new_size = ctx->handle.itemcount * sizeof(struct ipt_account_handle_ip);
78     // We want to prevent reallocations all the time
79     if (new_size < IPT_ACCOUNT_MIN_BUFSIZE)
80         new_size = IPT_ACCOUNT_MIN_BUFSIZE;
81     
82     // Reallocate if it's too small or twice as big
83     if (ctx->data_size < new_size || ctx->data_size > new_size*2)
84     {
85         // Free old buffer
86         free (ctx->data);
87         ctx->data_size = 0;
88         
89         if ((ctx->data = (void*)malloc(new_size)) == NULL)
90         {
91             ctx->error_str = "Out of memory for data buffer";
92             ipt_ACCOUNT_free_entries(ctx);
93             return -1;
94         }
95         
96         ctx->data_size = new_size;
97     }
98     
99     // Copy data from kernel
100     memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_account_handle_sockopt));
101     rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_DATA, ctx->data, &ctx->data_size);
102     if (rtn < 0)
103     {
104         ctx->error_str = "Can't get data from kernel. Check /var/log/messages for details.";
105         ipt_ACCOUNT_free_entries(ctx);
106         return -1;
107     }
108
109     // Free kernel handle but don't reset pos/itemcount
110     setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, &ctx->handle, sizeof (struct ipt_account_handle_sockopt));
111     ctx->handle.handle_nr = -1;
112     
113     return 0;
114 }
115
116 struct ipt_account_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
117 {
118     struct ipt_account_handle_ip *rtn;
119     
120     // Empty or no more items left to return?
121     if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount)
122         return NULL;
123     
124     // Get next entry
125     rtn = (struct ipt_account_handle_ip *)(ctx->data + ctx->pos*sizeof(struct ipt_account_handle_ip));
126     ctx->pos++;
127             
128     return rtn;
129 }
130
131 char *addr_to_dotted(unsigned int addr)
132 {
133     static char buf[20];
134     const unsigned char *bytep;
135
136     bytep = (const unsigned char *) &addr;
137     sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
138     return buf;
139 }
140
141 int main(void)
142 {
143     struct ipt_ACCOUNT_context ctx;
144     struct ipt_account_handle_ip *entry;
145     int i;
146     
147     if(ipt_ACCOUNT_init(&ctx))
148     {
149         printf("Init failed: %s\n", ctx.error_str);
150         exit (-1);
151     }
152
153     for (i = 0; i < 3; i++)
154     {
155         printf("Run #%d\n", i);
156         
157         // Get entries from table test
158         if (ipt_ACCOUNT_read_entries(&ctx, "test", 0))
159         {
160             printf("Read failed: %s\n", ctx.error_str);
161             ipt_ACCOUNT_deinit(&ctx);
162             exit (-1);
163         }
164         
165         // Output and free entries
166         while ((entry = ipt_ACCOUNT_get_next_entry(&ctx)) != NULL)
167         {
168             printf("IP: %s SRC packets: %u bytes: %u DST packets: %u bytes: %u\n",
169                    addr_to_dotted(entry->ip), entry->src_packets, entry->src_bytes, entry->dst_packets, entry->dst_bytes);
170         }
171         sleep(1);
172     }
173             
174     ipt_ACCOUNT_deinit(&ctx);
175     exit (0);
176 }