Include limits.h in ipt_ACCOUNT_cl.c to work around compilation problems reported...
[libipt_ACCOUNT] / src / ipt_ACCOUNT_cl.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 #include <limits.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14
15 #include <netinet/in.h>
16 #include <linux/if.h>
17 #include <linux/netfilter_ipv4/ip_tables.h>
18 #include <ipt_ACCOUNT_cl.h>
19
20 #include <string.h>
21 #include <stdlib.h>
22
23 int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
24 {
25     memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
26     ctx->handle.handle_nr = -1;
27     
28     ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
29     if (ctx->sockfd < 0) {
30         ctx->sockfd = -1;
31         ctx->error_str = "Can't open socket to kernel. "
32                          "Permission denied or ipt_ACCOUNT module not loaded";
33         return -1;
34     }
35     
36     // 4096 bytes default buffer should save us from reallocations
37     // as it fits 200 concurrent active clients
38     if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL) {
39         close (ctx->sockfd);
40         ctx->sockfd = -1;
41         ctx->error_str = "Out of memory for data buffer";
42         return -1;
43     }
44     ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE;
45     
46     return 0;
47 }
48
49 void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx)
50 {
51     if (ctx->handle.handle_nr != -1) {
52         setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE,
53                    &ctx->handle, sizeof (struct ipt_acc_handle_sockopt));
54         ctx->handle.handle_nr = -1;
55     }
56
57     ctx->handle.itemcount = 0;
58     ctx->pos = 0;
59 }
60
61 void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx)
62 {
63     free(ctx->data);
64     ctx->data = NULL;
65
66     ipt_ACCOUNT_free_entries(ctx);
67         
68     close(ctx->sockfd);
69     ctx->sockfd =-1;
70 }
71
72 int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx,
73                              const char *table, char dont_flush)
74 {
75     unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
76     unsigned int new_size;
77     int rtn;
78     
79     strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
80     
81     // Get table information
82     if (!dont_flush)
83         rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
84                          IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
85     else
86         rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ,
87                          &ctx->handle, &s);
88     
89     if (rtn < 0) {
90         ctx->error_str = "Can't get table information from kernel. "
91                          "Is the table existing?";
92         return -1;
93     }
94     
95     // Check data buffer size
96     ctx->pos = 0;
97     new_size = ctx->handle.itemcount * sizeof(struct ipt_acc_handle_ip);
98     // We want to prevent reallocations all the time
99     if (new_size < IPT_ACCOUNT_MIN_BUFSIZE)
100         new_size = IPT_ACCOUNT_MIN_BUFSIZE;
101     
102     // Reallocate if it's too small or twice as big
103     if (ctx->data_size < new_size || ctx->data_size > new_size*2) {
104         // Free old buffer
105         free (ctx->data);
106         ctx->data_size = 0;
107         
108         if ((ctx->data = (void*)malloc(new_size)) == NULL) {
109             ctx->error_str = "Out of memory for data buffer";
110             ipt_ACCOUNT_free_entries(ctx);
111             return -1;
112         }
113         
114         ctx->data_size = new_size;
115     }
116     
117     // Copy data from kernel
118     memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_acc_handle_sockopt));
119     rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_DATA,
120                      ctx->data, &ctx->data_size);
121     if (rtn < 0) {
122         ctx->error_str = "Can't get data from kernel. "
123                          "Check /var/log/messages for details.";
124         ipt_ACCOUNT_free_entries(ctx);
125         return -1;
126     }
127
128     // Free kernel handle but don't reset pos/itemcount
129     setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE,
130                &ctx->handle, sizeof (struct ipt_acc_handle_sockopt));
131     ctx->handle.handle_nr = -1;
132     
133     return 0;
134 }
135
136 struct ipt_acc_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
137 {
138     struct ipt_acc_handle_ip *rtn;
139     
140     // Empty or no more items left to return?
141     if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount)
142         return NULL;
143     
144     // Get next entry
145     rtn = (struct ipt_acc_handle_ip *)(ctx->data + ctx->pos
146                                        * sizeof(struct ipt_acc_handle_ip));
147     ctx->pos++;
148             
149     return rtn;
150 }
151
152 int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx)
153 {
154     unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
155     if (getsockopt(ctx->sockfd, IPPROTO_IP,
156                    IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE, &ctx->handle, &s) < 0) {
157         ctx->error_str = "Can't get handle usage information from kernel";
158         return -1;
159     }
160     ctx->handle.handle_nr = -1;
161     
162     return ctx->handle.itemcount;
163  }
164     
165 int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx)
166 {
167     if (setsockopt(ctx->sockfd, IPPROTO_IP,
168                    IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL, NULL, 0) < 0) {
169         ctx->error_str = "Can't free all kernel handles";
170         return -1;
171     }
172     
173     return 0;
174 }
175     
176 int ipt_ACCOUNT_get_table_names(struct ipt_ACCOUNT_context *ctx)
177 {
178     int rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
179                          IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES,
180                          ctx->data, &ctx->data_size);
181     if (rtn < 0) {
182         ctx->error_str = "Can't get table names from kernel. Out of memory, "
183                          "MINBUFISZE too small?";
184         return -1;
185     }
186     ctx->pos = 0;
187     return 0;
188 }
189
190 const char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx)
191 {
192     const char *rtn;
193     if (((char *)ctx->data)[ctx->pos] == 0)
194         return 0;
195
196     rtn = ctx->data + ctx->pos;
197     ctx->pos += strlen(ctx->data+ctx->pos) + 1;
198         
199     return rtn;
200 }