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 <ipt_ACCOUNT_cl.h>
16 int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
18 memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
20 ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
24 ctx->error_str = "Can't open socket to kernel. Permission denied or ipt_ACCOUNT module not loaded";
28 // 4096 bytes default buffer should save us from reallocations
29 // as it fits 200 concurrent active clients
30 if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL)
34 ctx->error_str = "Out of memory for data buffer";
37 ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE;
42 void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx)
44 if (ctx->handle.handle_nr >= 0)
46 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, &ctx->handle, sizeof (struct ipt_account_handle_sockopt));
47 ctx->handle.handle_nr = -1;
50 ctx->handle.itemcount = 0;
54 void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx)
59 ipt_ACCOUNT_free_entries(ctx);
65 int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx, char *table, char dont_flush)
67 unsigned int s = sizeof (struct ipt_account_handle_sockopt);
70 strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
72 // Get table information
74 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
76 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ, &ctx->handle, &s);
80 ctx->error_str = "Can't get table information from kernel. Is the table existing?";
84 // Check data buffer size
86 unsigned int new_size = ctx->handle.itemcount * sizeof(struct ipt_account_handle_ip);
87 // We want to prevent reallocations all the time
88 if (new_size < IPT_ACCOUNT_MIN_BUFSIZE)
89 new_size = IPT_ACCOUNT_MIN_BUFSIZE;
91 // Reallocate if it's too small or twice as big
92 if (ctx->data_size < new_size || ctx->data_size > new_size*2)
98 if ((ctx->data = (void*)malloc(new_size)) == NULL)
100 ctx->error_str = "Out of memory for data buffer";
101 ipt_ACCOUNT_free_entries(ctx);
105 ctx->data_size = new_size;
108 // Copy data from kernel
109 memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_account_handle_sockopt));
110 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_DATA, ctx->data, &ctx->data_size);
113 ctx->error_str = "Can't get data from kernel. Check /var/log/messages for details.";
114 ipt_ACCOUNT_free_entries(ctx);
118 // Free kernel handle but don't reset pos/itemcount
119 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, &ctx->handle, sizeof (struct ipt_account_handle_sockopt));
120 ctx->handle.handle_nr = -1;
125 struct ipt_account_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
127 struct ipt_account_handle_ip *rtn;
129 // Empty or no more items left to return?
130 if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount)
134 rtn = (struct ipt_account_handle_ip *)(ctx->data + ctx->pos*sizeof(struct ipt_account_handle_ip));