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