File: libxt_fuzzy.c

package info (click to toggle)
xtables-addons 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,296 kB
  • ctags: 1,415
  • sloc: sh: 11,559; ansic: 10,329; perl: 186; makefile: 121
file content (120 lines) | stat: -rw-r--r-- 3,452 bytes parent folder | download | duplicates (6)
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
/*
 *	"fuzzy" match extension for iptables
 *	Hime Aguiar e Oliveira Jr. <hime@engineer.com>, 2002 - 2003
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License;
 *	either version 2 of the License, or any later version, as
 *	published by the Free Software Foundation.
 */
#include <getopt.h>
#include <netdb.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xtables.h>
#include "xt_fuzzy.h"
#include "compat_user.h"

static void fuzzy_mt_help(void)
{
	printf(
"fuzzy match options:\n"
"  --lower-limit number (in packets per second)\n"
"  --upper-limit number\n");
};

static const struct option fuzzy_mt_opts[] = {
	{.name = "lower-limit", .has_arg = true, .val = '1'},
	{.name = "upper-limit", .has_arg = true, .val = '2'},
	{NULL},
};

/* Initialize data structures */
static void fuzzy_mt_init(struct xt_entry_match *m)
{
	struct xt_fuzzy_mtinfo *info = (void *)m->data;

	/*
	 * Default rates (I will improve this very soon with something based
	 * on real statistics of the running machine).
	 */
	info->minimum_rate = 1000;
	info->maximum_rate = 2000;
}

#define IPT_FUZZY_OPT_MINIMUM	0x01
#define IPT_FUZZY_OPT_MAXIMUM	0x02

static int fuzzy_mt_parse(int c, char **argv, int invert, unsigned int *flags,
                          const void *entry, struct xt_entry_match **match)
{
	struct xt_fuzzy_mtinfo *info = (void *)(*match)->data;
	uint32_t num;

	switch (c) {
	case '1':
		if (invert)
			xtables_error(PARAMETER_PROBLEM,"Can't specify ! --lower-limit");
		if (*flags & IPT_FUZZY_OPT_MINIMUM)
			xtables_error(PARAMETER_PROBLEM,"Can't specify --lower-limit twice");
		if (!xtables_strtoui(optarg, NULL, &num, 1, FUZZY_MAX_RATE) || num < 1)
			xtables_error(PARAMETER_PROBLEM,"BAD --lower-limit");
		info->minimum_rate = num;
		*flags |= IPT_FUZZY_OPT_MINIMUM;
		return true;

	case '2':
		if (invert)
			xtables_error(PARAMETER_PROBLEM,"Can't specify ! --upper-limit");
		if (*flags & IPT_FUZZY_OPT_MAXIMUM)
			xtables_error(PARAMETER_PROBLEM,"Can't specify --upper-limit twice");
		if (!xtables_strtoui(optarg, NULL, &num, 1, FUZZY_MAX_RATE) || num < 1)
			xtables_error(PARAMETER_PROBLEM,"BAD --upper-limit");
		info->maximum_rate = num;
		*flags |= IPT_FUZZY_OPT_MAXIMUM;
		return true;
	}
	return false;
}

static void fuzzy_mt_check(unsigned int flags)
{
}

static void fuzzy_mt_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_fuzzy_mtinfo *info = (const void *)match->data;

	printf(" --lower-limit %u ", info->minimum_rate);
	printf(" --upper-limit %u ", info->maximum_rate);
}

static void fuzzy_mt_print(const void *ip, const struct xt_entry_match *match,
                           int numeric)
{
	printf(" -m fuzzy");
	fuzzy_mt_save(ip, match);
}

static struct xtables_match fuzzy_mt_reg = {
	.name          = "fuzzy",
	.revision      = 1,
	.version       = XTABLES_VERSION,
	.family        = NFPROTO_UNSPEC,
	.size          = XT_ALIGN(sizeof(struct xt_fuzzy_mtinfo)),
	.userspacesize = offsetof(struct xt_fuzzy_mtinfo, packets_total),
	.help          = fuzzy_mt_help,
	.init          = fuzzy_mt_init,
	.parse         = fuzzy_mt_parse,
	.final_check   = fuzzy_mt_check,
	.print         = fuzzy_mt_print,
	.save          = fuzzy_mt_save,
	.extra_opts    = fuzzy_mt_opts,
};

static __attribute__((constructor)) void fuzzy_mt_ldr(void)
{
	xtables_register_match(&fuzzy_mt_reg);
}