summaryrefslogtreecommitdiffstats
path: root/ipset_nethash.c
blob: 7de5a2995aed9b7cb81c2cf94caa13f9571fccf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* Copyright 2004 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
 *
 * This program is free software; you can redistribute it and/or modify   
 * it under the terms of the GNU General Public License as published by   
 * the Free Software Foundation; either version 2 of the License, or      
 * (at your option) any later version.                                    
 *                                                                         
 * This program is distributed in the hope that it will be useful,        
 * but WITHOUT ANY WARRANTY; without even the implied warranty of         
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
 * GNU General Public License for more details.                           
 *                                                                         
 * You should have received a copy of the GNU General Public License      
 * along with this program; if not, write to the Free Software            
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <limits.h>			/* UINT_MAX */
#include <stdio.h>			/* *printf */
#include <string.h>			/* mem*, str* */

#include "ipset.h"

#include <linux/netfilter_ipv4/ip_set_nethash.h>

#define BUFLEN 30;

#define OPT_CREATE_HASHSIZE	0x01U
#define OPT_CREATE_PROBES	0x02U
#define OPT_CREATE_RESIZE	0x04U

/* Initialize the create. */
static void
nethash_create_init(void *data)
{
	struct ip_set_req_nethash_create *mydata = data;

	DP("create INIT");

	/* Default create parameters */	
	mydata->hashsize = IP_NF_SET_HASHSIZE;
	mydata->probes = 4;
	mydata->resize = 50;
}

/* Function which parses command options; returns true if it ate an option */
static int
nethash_create_parse(int c, char *argv[] UNUSED, void *data, unsigned *flags)
{
	struct ip_set_req_nethash_create *mydata = data;
	ip_set_ip_t value;

	DP("create_parse");

	switch (c) {
	case '1':

		if (string_to_number(optarg, 1, UINT_MAX - 1, &mydata->hashsize))
			exit_error(PARAMETER_PROBLEM, "Invalid hashsize `%s' specified", optarg);

		*flags |= OPT_CREATE_HASHSIZE;

		DP("--hashsize %u", mydata->hashsize);
		
		break;

	case '2':

		if (string_to_number(optarg, 1, 65535, &value))
			exit_error(PARAMETER_PROBLEM, "Invalid probes `%s' specified", optarg);

		mydata->probes = value;
		*flags |= OPT_CREATE_PROBES;

		DP("--probes %u", mydata->probes);
		
		break;

	case '3':

		if (string_to_number(optarg, 0, 65535, &value))
			exit_error(PARAMETER_PROBLEM, "Invalid resize `%s' specified", optarg);

		mydata->resize = value;
		*flags |= OPT_CREATE_RESIZE;

		DP("--resize %u", mydata->resize);
		
		break;

	default:
		return 0;
	}

	return 1;
}

/* Final check; exit if not ok. */
static void
nethash_create_final(void *data UNUSED, unsigned int flags UNUSED)
{
}

/* Create commandline options */
static const struct option create_opts[] = {
	{.name = "hashsize",	.has_arg = required_argument,	.val = '1'},
	{.name = "probes",	.has_arg = required_argument,	.val = '2'},
	{.name = "resize",	.has_arg = required_argument,	.val = '3'},
	{NULL},
};

/* Add, del, test parser */
static ip_set_ip_t
nethash_adt_parser(int cmd, const char *arg, void *data)
{
	struct ip_set_req_nethash *mydata = data;
	char *saved = ipset_strdup(arg);
	char *ptr, *tmp = saved;
	ip_set_ip_t cidr;

	ptr = strsep(&tmp, "/");
	
	if (tmp == NULL) {
		if (cmd == CMD_TEST)
			cidr = 32;
		else
			exit_error(PARAMETER_PROBLEM,
				   "Missing cidr from `%s'", arg);
	} else
		if (string_to_number(tmp, 1, 31, &cidr))
			exit_error(PARAMETER_PROBLEM,
				   "Out of range cidr `%s' specified", arg);
	
	mydata->cidr = cidr;
	parse_ip(ptr, &mydata->ip);
#if 0
	if (!mydata->ip)
		exit_error(PARAMETER_PROBLEM,
			  "Zero valued IP address `%s' specified", ptr);
#endif
	ipset_free(saved);

	return 1;	
};

/*
 * Print and save
 */

static void
nethash_initheader(struct set *set, const void *data)
{
	const struct ip_set_req_nethash_create *header = data;
	struct ip_set_nethash *map = set->settype->header;

	memset(map, 0, sizeof(struct ip_set_nethash));
	map->hashsize = header->hashsize;
	map->probes = header->probes;
	map->resize = header->resize;
}

static void
nethash_printheader(struct set *set, unsigned options UNUSED)
{
	struct ip_set_nethash *mysetdata = set->settype->header;

	printf(" hashsize: %u", mysetdata->hashsize);
	printf(" probes: %u", mysetdata->probes);
	printf(" resize: %u\n", mysetdata->resize);
}

static char buf[20];

static char *
unpack_ip_tostring(ip_set_ip_t ip, unsigned options UNUSED)
{
	int i, j = 3;
	unsigned char a, b;

	ip = htonl(ip);	
	for (i = 3; i >= 0; i--)
		if (((unsigned char *)&ip)[i] != 0) {
			j = i;
			break;
		}
			
	a = ((unsigned char *)&ip)[j];
	if (a <= 128) {
		a = (a - 1) * 2;
		b = 7;
	} else if (a <= 192) {
		a = (a - 129) * 4;
		b = 6;
	} else if (a <= 224) {
		a = (a - 193) * 8;
		b = 5;
	} else if (a <= 240) {
		a = (a - 225) * 16;
		b = 4;
	} else if (a <= 248) {
		a = (a - 241) * 32;
		b = 3;
	} else if (a <= 252) {
		a = (a - 249) * 64;
		b = 2;
	} else if (a <= 254) {
		a = (a - 253) * 128;
		b = 1;
	} else {
		a = b = 0;
	}
	((unsigned char *)&ip)[j] = a;
	b += j * 8;
	
	sprintf(buf, "%u.%u.%u.%u/%u",
		((unsigned char *)&ip)[0],
		((unsigned char *)&ip)[1],
		((unsigned char *)&ip)[2],
		((unsigned char *)&ip)[3],
		b);

	DP("%s %s", ip_tostring(ntohl(ip), 0), buf);
	return buf;
}

static void
nethash_printips(struct set *set UNUSED, void *data, u_int32_t len,
		 unsigned options, char dont_align)
{
	size_t offset = 0;
	ip_set_ip_t *ip;

	while (offset < len) {
		ip = data + offset;
		printf("%s\n", unpack_ip_tostring(*ip, options));
		offset += IPSET_VALIGN(sizeof(ip_set_ip_t), dont_align);
	}
}

static void
nethash_saveheader(struct set *set, unsigned options UNUSED)
{
	struct ip_set_nethash *mysetdata = set->settype->header;

	printf("-N %s %s --hashsize %u --probes %u --resize %u\n",
	       set->name, set->settype->typename,
	       mysetdata->hashsize, mysetdata->probes, mysetdata->resize);
}

/* Print save for an IP */
static void
nethash_saveips(struct set *set UNUSED, void *data, u_int32_t len,
		unsigned options, char dont_align)
{
	size_t offset = 0;
	ip_set_ip_t *ip;

	while (offset < len) {
		ip = data + offset;
		printf("-A %s %s\n", set->name,
		       unpack_ip_tostring(*ip, options));
		offset += IPSET_VALIGN(sizeof(ip_set_ip_t), dont_align);
	}
}

static void
nethash_usage(void)
{
	printf
	    ("-N set nethash [--hashsize hashsize] [--probes probes ]\n"
	     "               [--resize resize]\n"
	     "-A set IP/cidr\n"
	     "-D set IP/cidr\n"
	     "-T set IP/cidr\n");
}

static struct settype settype_nethash = {
	.typename = SETTYPE_NAME,
	.protocol_version = IP_SET_PROTOCOL_VERSION,

	/* Create */
	.create_size = sizeof(struct ip_set_req_nethash_create),
	.create_init = nethash_create_init,
	.create_parse = nethash_create_parse,
	.create_final = nethash_create_final,
	.create_opts = create_opts,

	/* Add/del/test */
	.adt_size = sizeof(struct ip_set_req_nethash),
	.adt_parser = nethash_adt_parser,

	/* Printing */
	.header_size = sizeof(struct ip_set_nethash),
	.initheader = nethash_initheader,
	.printheader = nethash_printheader,
	.printips = nethash_printips,
	.printips_sorted = nethash_printips,
	.saveheader = nethash_saveheader,
	.saveips = nethash_saveips,
	
	.usage = nethash_usage,
};

CONSTRUCTOR(nethash)
{
	settype_register(&settype_nethash);

}