iptables-1.3.2-20050720
[iptables.git] / extensions / libipt_BALANCE.c
1 /* Shared library add-on to iptables to add simple load-balance support. */
2 #include <stdio.h>
3 #include <netdb.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <getopt.h>
7 #include <iptables.h>
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter_ipv4/ip_nat_rule.h>
10
11 #define BREAKUP_IP(x) (x)>>24, ((x)>>16) & 0xFF, ((x)>>8) & 0xFF, (x) & 0xFF
12
13 /* Function which prints out usage message. */
14 static void
15 help(void)
16 {
17         printf(
18 "BALANCE v%s options:\n"
19 " --to-destination <ipaddr>-<ipaddr>\n"
20 "                               Addresses to map destination to.\n",
21 IPTABLES_VERSION);
22 }
23
24 static struct option opts[] = {
25         { "to-destination", 1, 0, '1' },
26         { 0 }
27 };
28
29 /* Initialize the target. */
30 static void
31 init(struct ipt_entry_target *t, unsigned int *nfcache)
32 {
33         struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
34
35         /* Actually, it's 0, but it's ignored at the moment. */
36         mr->rangesize = 1;
37
38 }
39
40 /* Parses range of IPs */
41 static void
42 parse_to(char *arg, struct ip_nat_range *range)
43 {
44         char *dash;
45         struct in_addr *ip;
46
47         range->flags |= IP_NAT_RANGE_MAP_IPS;
48         dash = strchr(arg, '-');
49         if (dash)
50                 *dash = '\0';
51         else
52                 exit_error(PARAMETER_PROBLEM, "Bad IP range `%s'\n", arg);
53
54         ip = dotted_to_addr(arg);
55         if (!ip)
56                 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
57                            arg);
58         range->min_ip = ip->s_addr;
59         ip = dotted_to_addr(dash+1);
60         if (!ip)
61                 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
62                            dash+1);
63         range->max_ip = ip->s_addr;
64 }
65
66 /* Function which parses command options; returns true if it
67    ate an option */
68 static int
69 parse(int c, char **argv, int invert, unsigned int *flags,
70       const struct ipt_entry *entry,
71       struct ipt_entry_target **target)
72 {
73         struct ip_nat_multi_range *mr
74                 = (struct ip_nat_multi_range *)(*target)->data;
75
76         switch (c) {
77         case '1':
78                 if (check_inverse(optarg, &invert, NULL, 0))
79                         exit_error(PARAMETER_PROBLEM,
80                                    "Unexpected `!' after --to-destination");
81
82                 parse_to(optarg, &mr->range[0]);
83                 *flags = 1;
84                 return 1;
85
86         default:
87                 return 0;
88         }
89 }
90
91 /* Final check; need --to-dest. */
92 static void final_check(unsigned int flags)
93 {
94         if (!flags)
95                 exit_error(PARAMETER_PROBLEM,
96                            "BALANCE needs --to-destination");
97 }
98
99 /* Prints out the targinfo. */
100 static void
101 print(const struct ipt_ip *ip,
102       const struct ipt_entry_target *target,
103       int numeric)
104 {
105         struct ip_nat_multi_range *mr
106                 = (struct ip_nat_multi_range *)target->data;
107         struct ip_nat_range *r = &mr->range[0];
108         struct in_addr a;
109
110         a.s_addr = r->min_ip;
111
112         printf("balance %s", addr_to_dotted(&a));
113         a.s_addr = r->max_ip;
114         printf("-%s ", addr_to_dotted(&a));
115 }
116
117 /* Saves the union ipt_targinfo in parsable form to stdout. */
118 static void
119 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
120 {
121         struct ip_nat_multi_range *mr
122                 = (struct ip_nat_multi_range *)target->data;
123         struct ip_nat_range *r = &mr->range[0];
124         struct in_addr a;
125
126         a.s_addr = r->min_ip;
127         printf("--to-destination %s", addr_to_dotted(&a));
128         a.s_addr = r->max_ip;
129         printf("-%s ", addr_to_dotted(&a));
130 }
131
132 static struct iptables_target balance = { 
133         .next           = NULL,
134         .name           = "BALANCE",
135         .version        = IPTABLES_VERSION,
136         .size           = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
137         .userspacesize  = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
138         .help           = &help,
139         .init           = &init,
140         .parse          = &parse,
141         .final_check    = &final_check,
142         .print          = &print,
143         .save           = &save,
144         .extra_opts     = opts
145 };
146
147 void _init(void)
148 {
149         register_target(&balance);
150 }