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