Include limits.h in ipt_ACCOUNT_cl.c to work around compilation problems reported...
[libipt_ACCOUNT] / src / ipt_ACCOUNT_cl.c
CommitLineData
a61040d6
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
b604d48c 11#include <limits.h>
322f2b5b
TJ
12#include <sys/types.h>
13#include <sys/socket.h>
322f2b5b 14
49ba8949
TJ
15#include <netinet/in.h>
16#include <linux/if.h>
17#include <linux/netfilter_ipv4/ip_tables.h>
322f2b5b
TJ
18#include <ipt_ACCOUNT_cl.h>
19
95ad024f
TJ
20#include <string.h>
21#include <stdlib.h>
22
322f2b5b
TJ
23int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
24{
25 memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
a92b7b94 26 ctx->handle.handle_nr = -1;
322f2b5b
TJ
27
28 ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
e3ccc02f 29 if (ctx->sockfd < 0) {
322f2b5b 30 ctx->sockfd = -1;
e3ccc02f
TJ
31 ctx->error_str = "Can't open socket to kernel. "
32 "Permission denied or ipt_ACCOUNT module not loaded";
322f2b5b
TJ
33 return -1;
34 }
35
36 // 4096 bytes default buffer should save us from reallocations
37 // as it fits 200 concurrent active clients
e3ccc02f 38 if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL) {
322f2b5b
TJ
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
49void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx)
50{
e3ccc02f
TJ
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));
322f2b5b
TJ
54 ctx->handle.handle_nr = -1;
55 }
56
57 ctx->handle.itemcount = 0;
58 ctx->pos = 0;
59}
60
61void 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
e3ccc02f
TJ
72int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx,
73 const char *table, char dont_flush)
322f2b5b 74{
8721821e 75 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
28ea7aa8 76 unsigned int new_size;
322f2b5b
TJ
77 int rtn;
78
79 strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
80
81 // Get table information
82 if (!dont_flush)
e3ccc02f
TJ
83 rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
84 IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
322f2b5b 85 else
e3ccc02f
TJ
86 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ,
87 &ctx->handle, &s);
322f2b5b 88
e3ccc02f
TJ
89 if (rtn < 0) {
90 ctx->error_str = "Can't get table information from kernel. "
91 "Is the table existing?";
322f2b5b
TJ
92 return -1;
93 }
94
95 // Check data buffer size
96 ctx->pos = 0;
e3ccc02f 97 new_size = ctx->handle.itemcount * sizeof(struct ipt_acc_handle_ip);
322f2b5b
TJ
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
e3ccc02f 103 if (ctx->data_size < new_size || ctx->data_size > new_size*2) {
322f2b5b
TJ
104 // Free old buffer
105 free (ctx->data);
106 ctx->data_size = 0;
107
e3ccc02f 108 if ((ctx->data = (void*)malloc(new_size)) == NULL) {
322f2b5b
TJ
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
8721821e 118 memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_acc_handle_sockopt));
e3ccc02f
TJ
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.";
322f2b5b
TJ
124 ipt_ACCOUNT_free_entries(ctx);
125 return -1;
126 }
127
128 // Free kernel handle but don't reset pos/itemcount
e3ccc02f
TJ
129 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE,
130 &ctx->handle, sizeof (struct ipt_acc_handle_sockopt));
322f2b5b
TJ
131 ctx->handle.handle_nr = -1;
132
133 return 0;
134}
135
8721821e 136struct ipt_acc_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
322f2b5b 137{
8721821e 138 struct ipt_acc_handle_ip *rtn;
322f2b5b
TJ
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
e3ccc02f
TJ
145 rtn = (struct ipt_acc_handle_ip *)(ctx->data + ctx->pos
146 * sizeof(struct ipt_acc_handle_ip));
322f2b5b
TJ
147 ctx->pos++;
148
149 return rtn;
150}
d7e0bb9c
TJ
151
152int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx)
153{
8721821e 154 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
e3ccc02f
TJ
155 if (getsockopt(ctx->sockfd, IPPROTO_IP,
156 IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE, &ctx->handle, &s) < 0) {
d7e0bb9c
TJ
157 ctx->error_str = "Can't get handle usage information from kernel";
158 return -1;
159 }
a92b7b94 160 ctx->handle.handle_nr = -1;
d7e0bb9c
TJ
161
162 return ctx->handle.itemcount;
163 }
164
165int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx)
166{
e3ccc02f
TJ
167 if (setsockopt(ctx->sockfd, IPPROTO_IP,
168 IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL, NULL, 0) < 0) {
d7e0bb9c
TJ
169 ctx->error_str = "Can't free all kernel handles";
170 return -1;
171 }
172
173 return 0;
174}
175
176int ipt_ACCOUNT_get_table_names(struct ipt_ACCOUNT_context *ctx)
177{
e3ccc02f
TJ
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?";
d7e0bb9c
TJ
184 return -1;
185 }
186 ctx->pos = 0;
187 return 0;
188}
189
42b9e9f8 190const char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx)
d7e0bb9c 191{
28ea7aa8 192 const char *rtn;
8721821e
TJ
193 if (((char *)ctx->data)[ctx->pos] == 0)
194 return 0;
d7e0bb9c 195
28ea7aa8 196 rtn = ctx->data + ctx->pos;
d7e0bb9c
TJ
197 ctx->pos += strlen(ctx->data+ctx->pos) + 1;
198
199 return rtn;
200}