iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libipt_random.c
1 /* 
2    Shared library add-on to iptables to add match support for random 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    2001-10-14 Fabrice MARIE <fabrice@netfilter.org> : initial development.
9 */
10
11 #include <stdio.h>
12 #include <netdb.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <syslog.h>
16 #include <getopt.h>
17 #include <iptables.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
19 #include <linux/netfilter_ipv4/ipt_random.h>
20
21 /**
22  * The kernel random routing returns numbers between 0 and 255.
23  * To ease the task of the user in choosing the probability
24  * of matching, we want him to be able to use percentages.
25  * Therefore we have to accept numbers in percentage here,
26  * turn them into number between 0 and 255 for the kernel module,
27  * and turn them back to percentages when we print/save
28  * the rule.
29  */
30
31
32 /* Function which prints out usage message. */
33 static void
34 help(void)
35 {
36         printf(
37 "random v%s options:\n"
38 "  [--average]     percent      The probability in percentage of the match\n"
39 "                               If ommited, a probability of 50%% percent is set.\n"
40 "                               Percentage must be within : 1 <= percent <= 99.\n\n",
41 IPTABLES_VERSION);
42 }
43
44 static struct option opts[] = {
45         { "average", 1, 0, '1' },
46         { 0 }
47 };
48
49 /* Initialize the target. */
50 static void
51 init(struct ipt_entry_match *m, unsigned int *nfcache)
52 {
53         struct ipt_rand_info *randinfo = (struct ipt_rand_info *)(m)->data;
54         *nfcache |= NFC_UNKNOWN;
55
56         /* We assign the average to be 50 which is our default value */
57         /* 50 * 2.55 = 128 */
58         randinfo->average = 128;
59 }
60
61 #define IPT_RAND_OPT_AVERAGE    0x01
62
63 /* Function which parses command options; returns true if it
64    ate an option */
65 static int
66 parse(int c, char **argv, int invert, unsigned int *flags,
67       const struct ipt_entry *entry,
68       unsigned int *nfcache,
69       struct ipt_entry_match **match)
70 {
71         struct ipt_rand_info *randinfo = (struct ipt_rand_info *)(*match)->data;
72         unsigned int num;
73
74         switch (c) {
75         case '1':
76                 /* check for common mistakes... */
77                 if (invert)
78                         exit_error(PARAMETER_PROBLEM,
79                                    "Can't specify ! --average");
80                 if (*flags & IPT_RAND_OPT_AVERAGE)
81                         exit_error(PARAMETER_PROBLEM,
82                                    "Can't specify --average twice");
83
84                 /* Remember, this function will interpret a leading 0 to be 
85                    Octal, a leading 0x to be hexdecimal... */
86                 if (string_to_number(optarg, 1, 99, &num) == -1 || num < 1)
87                         exit_error(PARAMETER_PROBLEM,
88                                    "bad --average `%s', must be between 1 and 99", optarg);
89
90                 /* assign the values */
91                 randinfo->average = (int)(num * 2.55);
92                 *flags |= IPT_RAND_OPT_AVERAGE;
93                 break;
94         default:
95                 return 0;
96         }
97         return 1;
98 }
99
100 /* Final check; nothing. */
101 static void final_check(unsigned int flags)
102 {
103 }
104
105 /* Prints out the targinfo. */
106 static void
107 print(const struct ipt_ip *ip,
108       const struct ipt_entry_match *match,
109       int numeric)
110 {
111         const struct ipt_rand_info *randinfo
112                 = (const struct ipt_rand_info *)match->data;
113         div_t result = div((randinfo->average*100), 255);
114         if (result.rem > 127)  /* round up... */
115                 ++result.quot;
116
117         printf(" random %u%% ", result.quot);
118 }
119
120 /* Saves the union ipt_targinfo in parsable form to stdout. */
121 static void
122 save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
123 {
124         const struct ipt_rand_info *randinfo
125                 = (const struct ipt_rand_info *)match->data;
126         div_t result = div((randinfo->average *100), 255);
127         if (result.rem > 127)  /* round up... */
128                 ++result.quot;
129
130         printf("--average %u ", result.quot);
131 }
132
133 struct iptables_match rand_match
134 = { NULL,
135     "random",
136     IPTABLES_VERSION,
137     IPT_ALIGN(sizeof(struct ipt_rand_info)),
138     IPT_ALIGN(sizeof(struct ipt_rand_info)),
139     &help,
140     &init,
141     &parse,
142     &final_check,
143     &print,
144     &save,
145     opts
146 };
147
148 void _init(void)
149 {
150         register_match(&rand_match);
151 }