1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
3 * opensource@intra2net.com *
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; *
9 ***************************************************************************/
11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <netinet/in.h>
16 #include <linux/netfilter_ipv4/ip_tables.h>
17 #include <ipt_ACCOUNT_cl.h>
19 int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
21 memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
23 ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
27 ctx->error_str = "Can't open socket to kernel. Permission denied or ipt_ACCOUNT module not loaded";
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)
37 ctx->error_str = "Out of memory for data buffer";
40 ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE;
45 void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx)
47 if (ctx->handle.handle_nr != -1)
49 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, &ctx->handle, sizeof (struct ipt_account_handle_sockopt));
50 ctx->handle.handle_nr = -1;
53 ctx->handle.itemcount = 0;
57 void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx)
62 ipt_ACCOUNT_free_entries(ctx);
68 int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx, char *table, char dont_flush)
70 unsigned int s = sizeof (struct ipt_account_handle_sockopt);
73 strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
75 // Get table information
77 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
79 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ, &ctx->handle, &s);
83 ctx->error_str = "Can't get table information from kernel. Is the table existing?";
87 // Check data buffer size
89 unsigned int new_size = ctx->handle.itemcount * sizeof(struct ipt_account_handle_ip);
90 // We want to prevent reallocations all the time
91 if (new_size < IPT_ACCOUNT_MIN_BUFSIZE)
92 new_size = IPT_ACCOUNT_MIN_BUFSIZE;
94 // Reallocate if it's too small or twice as big
95 if (ctx->data_size < new_size || ctx->data_size > new_size*2)
101 if ((ctx->data = (void*)malloc(new_size)) == NULL)
103 ctx->error_str = "Out of memory for data buffer";
104 ipt_ACCOUNT_free_entries(ctx);
108 ctx->data_size = new_size;
111 // Copy data from kernel
112 memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_account_handle_sockopt));
113 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_DATA, ctx->data, &ctx->data_size);
116 ctx->error_str = "Can't get data from kernel. Check /var/log/messages for details.";
117 ipt_ACCOUNT_free_entries(ctx);
121 // Free kernel handle but don't reset pos/itemcount
122 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, &ctx->handle, sizeof (struct ipt_account_handle_sockopt));
123 ctx->handle.handle_nr = -1;
128 struct ipt_account_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
130 struct ipt_account_handle_ip *rtn;
132 // Empty or no more items left to return?
133 if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount)
137 rtn = (struct ipt_account_handle_ip *)(ctx->data + ctx->pos*sizeof(struct ipt_account_handle_ip));
143 int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx)
145 unsigned int s = sizeof (struct ipt_account_handle_sockopt);
146 if (getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE, &ctx->handle, &s) < 0)
148 ctx->error_str = "Can't get handle usage information from kernel";
152 return ctx->handle.itemcount;
155 int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx)
157 if (setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL, NULL, 0) < 0)
159 ctx->error_str = "Can't free all kernel handles";
166 int ipt_ACCOUNT_get_table_names(struct ipt_ACCOUNT_context *ctx)
168 int rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES, ctx->data, &ctx->data_size);
171 ctx->error_str = "Can't get table names from kernel. Out of memory, MINBUFISZE too small?";
178 char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx)
180 if (((char *)ctx->data)[ctx->pos] == NULL)
183 char *rtn = ctx->data + ctx->pos;
184 ctx->pos += strlen(ctx->data+ctx->pos) + 1;