--- netfilter.same2/userspace/extensions/libipt_BALANCE.c Mon Oct 23 08:41:41 2000 +++ netfilter/userspace/extensions/libipt_BALANCE.c Sun Jul 29 18:21:44 2001 @@ -7,6 +7,7 @@ #include #include #include +#include #define BREAKUP_IP(x) (x)>>24, ((x)>>16) & 0xFF, ((x)>>8) & 0xFF, (x) & 0xFF @@ -17,7 +18,9 @@ printf( "BALANCE v%s options:\n" " --to-destination -\n" -" Addresses to map destination to.\n", +" Addresses to map destination to.\n" +" May be specified more than\n" +" once for multiple ranges.\n", NETFILTER_VERSION); } @@ -30,10 +33,11 @@ static void init(struct ipt_entry_target *t, unsigned int *nfcache) { - struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data; + struct ipt_balance_info *mr = (struct ipt_balance_info *)t->data; - /* Actually, it's 0, but it's ignored at the moment. */ - mr->rangesize = 1; + /* Set default to 0 */ + mr->rangesize = 0; + mr->ipnum = 0; /* Can't cache this */ *nfcache |= NFC_UNKNOWN; @@ -50,19 +54,24 @@ dash = strchr(arg, '-'); if (dash) *dash = '\0'; - else - exit_error(PARAMETER_PROBLEM, "Bad IP range `%s'\n", arg); ip = dotted_to_addr(arg); if (!ip) exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", arg); range->min_ip = ip->s_addr; - ip = dotted_to_addr(dash+1); - if (!ip) - exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", - dash+1); + + if (dash) { + ip = dotted_to_addr(dash+1); + if (!ip) + exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", + dash+1); + } range->max_ip = ip->s_addr; + if (dash) + if (range->min_ip > range->max_ip) + exit_error(PARAMETER_PROBLEM, "Bad IP range `%s-%s'\n", + arg, dash+1); } /* Function which parses command options; returns true if it @@ -72,16 +81,22 @@ const struct ipt_entry *entry, struct ipt_entry_target **target) { - struct ip_nat_multi_range *mr - = (struct ip_nat_multi_range *)(*target)->data; + struct ipt_balance_info *mr + = (struct ipt_balance_info *)(*target)->data; switch (c) { case '1': + if (mr->rangesize > IPT_BALANCE_MAX_RANGE) + exit_error(PARAMETER_PROBLEM, + "Too many ranges specified, maximum " + "is %i ranges.\n", + IPT_BALANCE_MAX_RANGE); if (check_inverse(optarg, &invert)) exit_error(PARAMETER_PROBLEM, "Unexpected `!' after --to-destination"); - parse_to(optarg, &mr->range[0]); + parse_to(optarg, &mr->range[mr->rangesize]); + mr->rangesize++; *flags = 1; return 1; @@ -104,39 +119,57 @@ const struct ipt_entry_target *target, int numeric) { - struct ip_nat_multi_range *mr - = (struct ip_nat_multi_range *)target->data; - struct ip_nat_range *r = &mr->range[0]; - struct in_addr a; - - a.s_addr = r->min_ip; - - printf("balance %s", addr_to_dotted(&a)); - a.s_addr = r->max_ip; - printf("-%s ", addr_to_dotted(&a)); + int count; + struct ipt_balance_info *mr + = (struct ipt_balance_info *)target->data; + + printf("balance:"); + + for (count = 0; count < mr->rangesize; count++) { + struct ip_nat_range *r = &mr->range[count]; + struct in_addr a; + + a.s_addr = r->min_ip; + + printf("%s", addr_to_dotted(&a)); + a.s_addr = r->max_ip; + + if (r->min_ip == r->max_ip) + printf(" "); + else + printf("-%s ", addr_to_dotted(&a)); + } } /* Saves the union ipt_targinfo in parsable form to stdout. */ static void save(const struct ipt_ip *ip, const struct ipt_entry_target *target) { - struct ip_nat_multi_range *mr - = (struct ip_nat_multi_range *)target->data; - struct ip_nat_range *r = &mr->range[0]; - struct in_addr a; - - a.s_addr = r->min_ip; - printf("--to-destination %s", addr_to_dotted(&a)); - a.s_addr = r->max_ip; - printf("-%s ", addr_to_dotted(&a)); + int count; + struct ipt_balance_info *mr + = (struct ipt_balance_info *)target->data; + + for (count = 0; count < mr->rangesize; count++) { + struct ip_nat_range *r = &mr->range[count]; + struct in_addr a; + + a.s_addr = r->min_ip; + printf("--to-destination %s", addr_to_dotted(&a)); + a.s_addr = r->max_ip; + + if (r->min_ip == r->max_ip) + printf(" "); + else + printf("-%s ", addr_to_dotted(&a)); + } } struct iptables_target balance = { NULL, "BALANCE", NETFILTER_VERSION, - IPT_ALIGN(sizeof(struct ip_nat_multi_range)), - IPT_ALIGN(sizeof(struct ip_nat_multi_range)), + IPT_ALIGN(sizeof(struct ipt_balance_info)), + IPT_ALIGN(sizeof(struct ipt_balance_info)), &help, &init, &parse,