Commit | Line | Data |
---|---|---|
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 | ||
19 | int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx) | |
20 | { | |
21 | memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context)); | |
22 | ||
23 | ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); | |
24 | if (ctx->sockfd < 0) | |
25 | { | |
26 | ctx->sockfd = -1; | |
27 | ctx->error_str = "Can't open socket to kernel. Permission denied or ipt_ACCOUNT module not loaded"; | |
28 | return -1; | |
29 | } | |
30 | ||
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) | |
34 | { | |
35 | close (ctx->sockfd); | |
36 | ctx->sockfd = -1; | |
37 | ctx->error_str = "Out of memory for data buffer"; | |
38 | return -1; | |
39 | } | |
40 | ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE; | |
41 | ||
42 | return 0; | |
43 | } | |
44 | ||
45 | void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx) | |
46 | { | |
47 | if (ctx->handle.handle_nr >= 0) | |
48 | { | |
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; | |
51 | } | |
52 | ||
53 | ctx->handle.itemcount = 0; | |
54 | ctx->pos = 0; | |
55 | } | |
56 | ||
57 | void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx) | |
58 | { | |
59 | free(ctx->data); | |
60 | ctx->data = NULL; | |
61 | ||
62 | ipt_ACCOUNT_free_entries(ctx); | |
63 | ||
64 | close(ctx->sockfd); | |
65 | ctx->sockfd =-1; | |
66 | } | |
67 | ||
68 | int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx, char *table, char dont_flush) | |
69 | { | |
70 | unsigned int s = sizeof (struct ipt_account_handle_sockopt); | |
71 | int rtn; | |
72 | ||
73 | strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1); | |
74 | ||
75 | // Get table information | |
76 | if (!dont_flush) | |
77 | rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s); | |
78 | else | |
79 | rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ, &ctx->handle, &s); | |
80 | ||
81 | if (rtn < 0) | |
82 | { | |
83 | ctx->error_str = "Can't get table information from kernel. Is the table existing?"; | |
84 | return -1; | |
85 | } | |
86 | ||
87 | // Check data buffer size | |
88 | ctx->pos = 0; | |
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; | |
93 | ||
94 | // Reallocate if it's too small or twice as big | |
95 | if (ctx->data_size < new_size || ctx->data_size > new_size*2) | |
96 | { | |
97 | // Free old buffer | |
98 | free (ctx->data); | |
99 | ctx->data_size = 0; | |
100 | ||
101 | if ((ctx->data = (void*)malloc(new_size)) == NULL) | |
102 | { | |
103 | ctx->error_str = "Out of memory for data buffer"; | |
104 | ipt_ACCOUNT_free_entries(ctx); | |
105 | return -1; | |
106 | } | |
107 | ||
108 | ctx->data_size = new_size; | |
109 | } | |
110 | ||
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); | |
114 | if (rtn < 0) | |
115 | { | |
116 | ctx->error_str = "Can't get data from kernel. Check /var/log/messages for details."; | |
117 | ipt_ACCOUNT_free_entries(ctx); | |
118 | return -1; | |
119 | } | |
120 | ||
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; | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | struct ipt_account_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx) | |
129 | { | |
130 | struct ipt_account_handle_ip *rtn; | |
131 | ||
132 | // Empty or no more items left to return? | |
133 | if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount) | |
134 | return NULL; | |
135 | ||
136 | // Get next entry | |
137 | rtn = (struct ipt_account_handle_ip *)(ctx->data + ctx->pos*sizeof(struct ipt_account_handle_ip)); | |
138 | ctx->pos++; | |
139 | ||
140 | return rtn; | |
141 | } |