This commit was manufactured by cvs2svn to create branch
[iptables.git] / extensions / libipt_POOL.c
1 /* Shared library add-on to iptables to add IP pool mangling target. */
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_tables.h>
11 #include <linux/netfilter_ipv4/ip_nat_rule.h>
12 #include <linux/netfilter_ipv4/ip_pool.h>
13 #include <linux/netfilter_ipv4/ipt_pool.h>
14
15 #include <libippool/ip_pool_support.h>
16
17 /* FIXME --RR */
18 #define ip_pool_init           ip_POOL_init
19 #define ip_pool_get_index      ip_POOL_get_index
20 #define ip_pool_get_name       ip_POOL_get_name
21 #include "../ippool/libippool.c"
22
23 /* Function which prints out usage message. */
24 static void
25 help(void)
26 {
27         printf(
28 "POOL v%s options:\n"
29 " --add-srcip <pool>\n"
30 " --del-srcip <pool>\n"
31 " --add-dstip <pool>\n"
32 " --del-dstip <pool>\n"
33 "                               add/del src/dst IP from pool.\n\n",
34 IPTABLES_VERSION);
35 }
36
37 static struct option opts[] = {
38         { "add-srcip", 1, 0, '1' },
39         { "del-srcip", 1, 0, '2' },
40         { "add-dstip", 1, 0, '3' },
41         { "del-dstip", 1, 0, '4' },
42         { 0 }
43 };
44
45 /* Initialize the target. */
46 static void
47 init(struct ipt_entry_target *target, unsigned int *nfcache)
48 {
49         struct ipt_pool_info *ipi = (struct ipt_pool_info *) target->data;
50
51         ipi->src = ipi->dst = IP_POOL_NONE;
52         ipi->flags = 0;
53
54 }
55
56 /* Function which parses command options; returns true if it
57    ate an option */
58 static int
59 parse(int c, char **argv, int invert, unsigned int *flags,
60       const struct ipt_entry *entry,
61       struct ipt_entry_target **target)
62 {
63         struct ipt_pool_info *ipi = (struct ipt_pool_info *) (*target)->data;
64         switch (c) {
65         case '1':       /* --add-srcip <pool> */
66                 ipi->src = ip_pool_get_index(optarg);
67                 ipi->flags &= ~IPT_POOL_DEL_SRC;
68                 break;
69         case '2':       /* --del-srcip <pool> */
70                 ipi->src = ip_pool_get_index(optarg);
71                 ipi->flags |= IPT_POOL_DEL_SRC;
72                 break;
73         case '3':       /* --add-dstip <pool> */
74                 ipi->dst = ip_pool_get_index(optarg);
75                 ipi->flags &= ~IPT_POOL_DEL_DST;
76                 break;
77         case '4':       /* --del-dstip <pool> */
78                 ipi->dst = ip_pool_get_index(optarg);
79                 ipi->flags |= IPT_POOL_DEL_DST;
80                 break;
81         default:
82                 return 0;
83         }
84         return 1;
85 }
86
87 /* Final check; don't care. */
88 static void final_check(unsigned int flags)
89 {
90 }
91
92 /* Prints out the targinfo. */
93 static void
94 print(const struct ipt_ip *ip,
95       const struct ipt_entry_target *target,
96       int numeric)
97 {
98         char buf[256];
99         struct ipt_pool_info *ipi = (struct ipt_pool_info *) target->data;
100
101         printf("POOL");
102         if (ipi->src != IP_POOL_NONE) {
103                 printf(" --%s-srcip %s",
104                         (ipi->flags & IPT_POOL_DEL_SRC) ? "del" : "add",
105                         ip_pool_get_name(buf, sizeof(buf), ipi->src, numeric));
106         }
107         if (ipi->dst != IP_POOL_NONE) {
108                 printf(" --%s-dstip %s",
109                         (ipi->flags & IPT_POOL_DEL_DST) ? "del" : "add",
110                         ip_pool_get_name(buf, sizeof(buf), ipi->dst, numeric));
111         }
112 }
113
114 /* Saves the union ipt_targinfo in parsable form to stdout. */
115 static void
116 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
117 {
118         char buf[256];
119         struct ipt_pool_info *ipi = (struct ipt_pool_info *) target->data;
120
121         printf("-j POOL");
122         if (ipi->src != IP_POOL_NONE) {
123                 printf(" --%s-srcip %s",
124                         (ipi->flags & IPT_POOL_DEL_SRC) ? "del" : "add",
125                         ip_pool_get_name(buf, sizeof(buf), ipi->src, 0));
126         }
127         if (ipi->dst != IP_POOL_NONE) {
128                 printf(" --%s-dstip %s",
129                         (ipi->flags & IPT_POOL_DEL_DST) ? "del" : "add",
130                         ip_pool_get_name(buf, sizeof(buf), ipi->dst, 0));
131         }
132 }
133
134 static struct iptables_target ipt_pool_target = { 
135         .next           = NULL,
136         .name           = "POOL",
137         .version        = IPTABLES_VERSION,
138         .size           = IPT_ALIGN(sizeof(struct ipt_pool_info)),
139         .userspacesize  = IPT_ALIGN(sizeof(struct ipt_pool_info)),
140         .help           = &help,
141         .init           = &init,
142         .parse          = &parse,
143         .final_check    = &final_check,
144         .print          = &print,
145         .save           = &save,
146         .extra_opts     = opts
147 };
148
149 void _init(void)
150 {
151         register_target(&ipt_pool_target);
152 }