arnied, arnielizer, arniesetup, generate, info_mqueue_ui, libncfcheck: (tomj) compile...
[libipt_ACCOUNT] / src / ipt_ACCOUNT_cl.c
... / ...
CommitLineData
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#include <string.h>
20#include <stdlib.h>
21
22int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
23{
24 memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
25 ctx->handle.handle_nr = -1;
26
27 ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
28 if (ctx->sockfd < 0) {
29 ctx->sockfd = -1;
30 ctx->error_str = "Can't open socket to kernel. "
31 "Permission denied or ipt_ACCOUNT module not loaded";
32 return -1;
33 }
34
35 // 4096 bytes default buffer should save us from reallocations
36 // as it fits 200 concurrent active clients
37 if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL) {
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{
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));
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
71int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx,
72 const char *table, char dont_flush)
73{
74 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
75 unsigned int new_size;
76 int rtn;
77
78 strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
79
80 // Get table information
81 if (!dont_flush)
82 rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
83 IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
84 else
85 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ,
86 &ctx->handle, &s);
87
88 if (rtn < 0) {
89 ctx->error_str = "Can't get table information from kernel. "
90 "Is the table existing?";
91 return -1;
92 }
93
94 // Check data buffer size
95 ctx->pos = 0;
96 new_size = ctx->handle.itemcount * sizeof(struct ipt_acc_handle_ip);
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
102 if (ctx->data_size < new_size || ctx->data_size > new_size*2) {
103 // Free old buffer
104 free (ctx->data);
105 ctx->data_size = 0;
106
107 if ((ctx->data = (void*)malloc(new_size)) == NULL) {
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
117 memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_acc_handle_sockopt));
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.";
123 ipt_ACCOUNT_free_entries(ctx);
124 return -1;
125 }
126
127 // Free kernel handle but don't reset pos/itemcount
128 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE,
129 &ctx->handle, sizeof (struct ipt_acc_handle_sockopt));
130 ctx->handle.handle_nr = -1;
131
132 return 0;
133}
134
135struct ipt_acc_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
136{
137 struct ipt_acc_handle_ip *rtn;
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
144 rtn = (struct ipt_acc_handle_ip *)(ctx->data + ctx->pos
145 * sizeof(struct ipt_acc_handle_ip));
146 ctx->pos++;
147
148 return rtn;
149}
150
151int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx)
152{
153 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
154 if (getsockopt(ctx->sockfd, IPPROTO_IP,
155 IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE, &ctx->handle, &s) < 0) {
156 ctx->error_str = "Can't get handle usage information from kernel";
157 return -1;
158 }
159 ctx->handle.handle_nr = -1;
160
161 return ctx->handle.itemcount;
162 }
163
164int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx)
165{
166 if (setsockopt(ctx->sockfd, IPPROTO_IP,
167 IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL, NULL, 0) < 0) {
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{
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?";
183 return -1;
184 }
185 ctx->pos = 0;
186 return 0;
187}
188
189const char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx)
190{
191 const char *rtn;
192 if (((char *)ctx->data)[ctx->pos] == 0)
193 return 0;
194
195 rtn = ctx->data + ctx->pos;
196 ctx->pos += strlen(ctx->data+ctx->pos) + 1;
197
198 return rtn;
199}