iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libipt_fuzzy.c
1 /* 
2    Shared library add-on to iptables to add match support for the fuzzy match.
3    
4    This file is distributed under the terms of the GNU General Public
5    License (GPL). Copies of the GPL can be obtained from:
6    ftp://prep.ai.mit.edu/pub/gnu/GPL
7
8 2002-08-07 Hime Aguiar e Oliveira Jr. <hime@engineer.com> : Initial version.
9 2003-06-09 Hime Aguiar e Oliveira Jr. <hime@engineer.com> : Bug corrections in
10 the save function , thanks to information given by Jean-Francois Patenaude .
11
12 */
13
14 #include <stdio.h>
15 #include <netdb.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <syslog.h>
19 #include <getopt.h>
20 #include <iptables.h>
21 #include <linux/netfilter_ipv4/ip_tables.h>
22 #include <linux/netfilter_ipv4/ipt_fuzzy.h>
23
24
25 static void
26 help(void)
27 {
28         printf(
29 "fuzzy v%s options:\n"
30 "                      --lower-limit number (in packets per second)\n"
31 "                      --upper-limit number\n"
32 ,IPTABLES_VERSION);
33 };
34
35 static struct option opts[] = {
36         { "lower-limit", 1 , 0 , '1' } ,
37         { "upper-limit", 1 , 0 , '2' } ,
38         { 0 }
39 };
40
41 /* Initialize data structures */
42 static void
43 init(struct ipt_entry_match *m, unsigned int *nfcache)
44 {
45         struct ipt_fuzzy_info *presentinfo = (struct ipt_fuzzy_info *)(m)->data;
46         *nfcache |= NFC_UNKNOWN;
47
48         /*
49          * Default rates ( I'll improve this very soon with something based 
50          * on real statistics of the running machine ) .
51         */
52
53         presentinfo->minimum_rate = 1000;
54         presentinfo->maximum_rate = 2000;
55 }
56
57 #define IPT_FUZZY_OPT_MINIMUM   0x01
58 #define IPT_FUZZY_OPT_MAXIMUM   0x02
59
60 static int
61 parse(int c, char **argv, int invert, unsigned int *flags,
62       const struct ipt_entry *entry,
63       unsigned int *nfcache,
64       struct ipt_entry_match **match)
65 {
66
67 struct ipt_fuzzy_info *fuzzyinfo = (struct ipt_fuzzy_info *)(*match)->data;
68
69         u_int32_t num;
70
71         switch (c) {
72
73         case '1':
74                 
75         if (invert)
76                 exit_error(PARAMETER_PROBLEM,"Can't specify ! --lower-limit");
77
78         if (*flags & IPT_FUZZY_OPT_MINIMUM)
79               exit_error(PARAMETER_PROBLEM,"Can't specify --lower-limit twice");
80         
81         if (string_to_number(optarg,1,MAXFUZZYRATE,&num) == -1 || num < 1)
82                         exit_error(PARAMETER_PROBLEM,"BAD --lower-limit");
83
84                 fuzzyinfo->minimum_rate = num ;
85
86                 *flags |= IPT_FUZZY_OPT_MINIMUM;
87                 
88                 break;
89
90         case '2':
91
92         if (invert)
93                 exit_error(PARAMETER_PROBLEM,"Can't specify ! --upper-limit");
94
95         if (*flags & IPT_FUZZY_OPT_MAXIMUM)
96            exit_error(PARAMETER_PROBLEM,"Can't specify --upper-limit twice");
97
98         if (string_to_number(optarg,1,MAXFUZZYRATE,&num) == -1 || num < 1)
99                 exit_error(PARAMETER_PROBLEM,"BAD --upper-limit");
100
101                 fuzzyinfo->maximum_rate = num ;
102
103                 *flags |= IPT_FUZZY_OPT_MAXIMUM;
104
105                 break ;
106
107         default:
108                 return 0;
109         }
110         return 1;
111 }
112
113 static void final_check(unsigned int flags)
114 {
115 }
116
117 static void
118 print(const struct ipt_ip *ip,
119       const struct ipt_entry_match *match,
120       int numeric)
121 {
122         const struct ipt_fuzzy_info *fuzzyinfo
123                 = (const struct ipt_fuzzy_info *)match->data;
124
125         printf(" fuzzy: lower limit = %u pps - upper limit = %u pps ",fuzzyinfo->minimum_rate,fuzzyinfo->maximum_rate);
126
127 }
128
129 /* Saves the union ipt_targinfo in parsable form to stdout. */
130 static void
131 save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
132 {
133         const struct ipt_fuzzy_info *fuzzyinfo
134                 = (const struct ipt_fuzzy_info *)match->data;
135
136         printf("--lower-limit %u ",fuzzyinfo->minimum_rate);
137         printf("--upper-limit %u ",fuzzyinfo->maximum_rate);
138
139 }
140
141 struct iptables_match fuzzy_match
142 = { NULL,
143     "fuzzy",
144     IPTABLES_VERSION,
145     IPT_ALIGN(sizeof(struct ipt_fuzzy_info)),
146     IPT_ALIGN(sizeof(struct ipt_fuzzy_info)),
147     &help,
148     &init,
149     &parse,
150     &final_check,
151     &print,
152     &save,
153     opts
154 };
155
156 void _init(void)
157 {
158         register_match(&fuzzy_match);
159 }