This commit was manufactured by cvs2svn to create branch
[iptables.git] / extensions / libipt_pool.c.orig
1 /* Shared library add-on to iptables to add IP address pool matching. */
2 #include <stdio.h>
3 #include <netdb.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <getopt.h>
7 #include <ctype.h>
8
9 #include <iptables.h>
10 #include <linux/netfilter_ipv4/ip_conntrack.h>
11 #include <linux/netfilter_ipv4/ipt_pool.h>
12
13 #include <libippool/ip_pool_support.h>
14
15 /* FIXME --RR */
16 #include "../ippool/libippool.c"
17
18 /* Function which prints out usage message. */
19 static void
20 help(void)
21 {
22         printf(
23 "pool v%s options:\n"
24 " [!] --srcpool NAME|INDEX\n"
25 " [!] --dstpool NAME|INDEX\n"
26 "                       Pool index (or name from %s) to match\n"
27 "\n", IPTABLES_VERSION, IPPOOL_CONF);
28 }
29
30 static struct option opts[] = {
31         { "srcpool", 1, 0, '1' },
32         { "dstpool", 1, 0, '2' },
33         {0}
34 };
35
36 /* Initialize the match. */
37 static void
38 init(struct ipt_entry_match *match, unsigned int *nfcache)
39 {
40         struct ipt_pool_info *info =
41                 (struct ipt_pool_info *)match->data;
42
43         info->src = IP_POOL_NONE;
44         info->dst = IP_POOL_NONE;
45         info->flags = 0;
46         /* Can't cache this - XXX */
47         *nfcache |= NFC_UNKNOWN;
48 }
49
50 /* Function which parses command options; returns true if it ate an option */
51 static int
52 parse(int c, char **argv, int invert, unsigned int *flags,
53       const struct ipt_entry *entry,
54       unsigned int *nfcache,
55       struct ipt_entry_match **match)
56 {
57         struct ipt_pool_info *info =
58                 (struct ipt_pool_info *)(*match)->data;
59
60         switch (c) {
61         case '1':
62                 check_inverse(optarg, &invert, &optind, 0);
63                 info->src = ip_pool_get_index(argv[optind-1]);
64                 if (invert) info->flags |= IPT_POOL_INV_SRC;
65                 *flags = 1;
66                 break;
67         case '2':
68                 check_inverse(optarg, &invert, &optind, 0);
69                 info->dst = ip_pool_get_index(argv[optind-1]);
70                 if (invert) info->flags |= IPT_POOL_INV_DST;
71                 *flags = 1;
72                 break;
73
74         default:
75                 return 0;
76         }
77
78         return 1;
79 }
80
81 /* Final check; must have specified --srcpool or --dstpool. */
82 static void final_check(unsigned int flags)
83 {
84         if (!flags)
85                 exit_error(PARAMETER_PROBLEM, "You must specify either `--srcpool or --dstpool'");
86 }
87
88 /* Prints out the matchinfo. */
89 static void
90 print(const struct ipt_ip *ip,
91       const struct ipt_entry_match *match,
92       int numeric)
93 {
94         char buf[256];
95         struct ipt_pool_info *info =
96                 (struct ipt_pool_info *)match->data;
97
98         if (info->src != IP_POOL_NONE)
99                 printf("%ssrcpool %s ",
100                         (info->flags & IPT_POOL_INV_SRC) ? "!" : "",
101                         ip_pool_get_name(buf, sizeof(buf), info->src, 0));
102         if (info->dst != IP_POOL_NONE)
103                 printf("%sdstpool %s ",
104                         (info->flags & IPT_POOL_INV_DST) ? "!" : "",
105                         ip_pool_get_name(buf, sizeof(buf), info->dst, 0));
106 }
107
108 /* Saves the matchinfo in parsable form to stdout. */
109 static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
110 {
111         char buf[256];
112         struct ipt_pool_info *info =
113                 (struct ipt_pool_info *)match->data;
114
115         if (info->src != IP_POOL_NONE)
116                 printf("%s--srcpool %s",
117                         (info->flags & IPT_POOL_INV_SRC) ? "! " : "",
118                         ip_pool_get_name(buf, sizeof(buf), info->src, 0));
119         if (info->dst != IP_POOL_NONE)
120                 printf("%s--dstpool %s",
121                         (info->flags & IPT_POOL_INV_DST) ? "! " : "",
122                         ip_pool_get_name(buf, sizeof(buf), info->dst, 0));
123 }
124
125 static
126 struct iptables_match pool
127 = { NULL,
128     "pool",
129     IPTABLES_VERSION,
130     IPT_ALIGN(sizeof(struct ipt_pool_info)),
131     IPT_ALIGN(sizeof(struct ipt_pool_info)),
132     &help,
133     &init,
134     &parse,
135     &final_check,
136     &print,
137     &save,
138     opts
139 };
140
141 void _init(void)
142 {
143         register_match(&pool);
144 }