Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / extensions / libipt_bcount.c
blob57c8c2ba8ddaa401f29391ecb2fdadb78169009c
1 /*
3 bcount match (experimental)
4 Copyright (C) 2006 Jonathan Zarate
6 Licensed under GNU GPL v2 or later.
8 */
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <getopt.h>
14 #include <iptables.h>
15 #include <linux/netfilter_ipv4/ipt_bcount.h>
18 #undef IPTABLES_SAVE
21 static void help(void)
23 printf(
24 "bcount match v0.00\n"
25 "[!] --range min:max (upper range of 0x0FFFFFFF or more means unlimited)\n"
29 static void init(struct ipt_entry_match *match, unsigned int *nfcache)
31 struct ipt_bcount_match *info = (struct ipt_bcount_match *)match->data;
32 info->min = 0xFFFFFFFF;
33 info->max = 0x0FFFFFFF;
34 *nfcache |= NFC_UNKNOWN;
37 static struct option opts[] = {
38 { .name = "range", .has_arg = 1, .flag = 0, .val = '1' },
39 { .name = NULL }
42 static int parse(int c, char **argv, int invert, unsigned int *flags,
43 const struct ipt_entry *entry, unsigned int *nfcache,
44 struct ipt_entry_match **match)
46 char *p;
47 struct ipt_bcount_match *info;
49 if (c != '1') return 0;
51 info = (struct ipt_bcount_match *)(*match)->data;
53 *flags = 1;
54 check_inverse(optarg, &invert, &optind, 0);
55 if (invert) info->invert = 1;
57 info->min = strtoul(argv[optind - 1], &p, 0);
58 if (*p == '+') {
59 ++p;
61 else if ((*p == '-') || (*p == ':')) {
62 ++p;
63 if ((*p == 0) || (strcmp(p, "max") == 0)) {
64 info->max = 0x0FFFFFFF;
66 else {
67 info->max = strtoul(p, &p, 0);
68 if (info->max > 0x0FFFFFFF) info->max = 0x0FFFFFFF;
72 if ((*p != 0) || (info->min > info->max))
73 exit_error(PARAMETER_PROBLEM, "Invalid range");
74 return 1;
77 static void final_check(unsigned int flags)
79 if (!flags) {
80 exit_error(PARAMETER_PROBLEM, "Invalid range");
84 static void print_match(const struct ipt_bcount_match *info)
86 if (info->min != 0xFFFFFFFF) {
87 if (info->invert) printf("! ");
88 printf("--range %u", info->min);
89 if (info->max == 0x0FFFFFFF) printf("+ ");
90 else printf(":%u ", info->max);
94 static void print(const struct ipt_ip *ip, const struct ipt_entry_match *match, int numeric)
96 printf("bcount ");
97 print_match((const struct ipt_bcount_match *)match->data);
100 static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
102 #ifdef IPTABLES_SAVE
103 print_match((const struct ipt_bcount_match *)match->data);
104 #endif
108 static struct iptables_match bcount_match = {
109 .name = "bcount",
110 .version = IPTABLES_VERSION,
111 .size = IPT_ALIGN(sizeof(struct ipt_bcount_match)),
112 .userspacesize = IPT_ALIGN(sizeof(struct ipt_bcount_match)),
113 .help = &help,
114 .init = &init,
115 .parse = &parse,
116 .final_check = &final_check,
117 .print = &print,
118 .save = &save,
119 .extra_opts = opts
122 void _init(void)
124 register_match(&bcount_match);