Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / extensions / libipt_quota.c
bloba9c138c5d8fdd7b16fd81a3a7f583ec2fe2e1e01
1 /*
2 * Shared library add-on to iptables to add quota support
4 * Sam Johnston <samj@samj.net>
5 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <getopt.h>
9 #include <iptables.h>
11 #include <linux/netfilter_ipv4/ipt_quota.h>
12 #include <linux/netfilter_ipv4/ip_tables.h>
14 static struct option opts[] = {
15 {"quota", 1, 0, '1'},
16 {0}
19 /* print usage */
20 static void
21 help(void)
23 printf("quota options:\n"
24 " --quota quota quota (bytes)\n" "\n");
27 /* print matchinfo */
28 static void
29 print(const struct ipt_ip *ip, const struct ipt_entry_match *match, int numeric)
31 struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
32 printf("quota: %llu bytes", (unsigned long long) q->quota);
35 /* save matchinfo */
36 static void
37 save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
39 struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
40 printf("--quota %llu ", (unsigned long long) q->quota);
43 /* parse quota option */
44 static int
45 parse_quota(const char *s, u_int64_t * quota)
47 *quota = strtoull(s, (char **) NULL, 10);
49 #ifdef DEBUG_IPT_QUOTA
50 printf("Quota: %llu\n", *quota);
51 #endif
53 if (*quota == -1)
54 exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
55 else
56 return 1;
59 /* parse all options, returning true if we found any for us */
60 static int
61 parse(int c, char **argv, int invert, unsigned int *flags,
62 const struct ipt_entry *entry,
63 unsigned int *nfcache, struct ipt_entry_match **match)
65 struct ipt_quota_info *info = (struct ipt_quota_info *) (*match)->data;
67 switch (c) {
68 case '1':
69 if (check_inverse(optarg, &invert, NULL, 0))
70 exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
71 if (!parse_quota(optarg, &info->quota))
72 exit_error(PARAMETER_PROBLEM,
73 "bad quota: '%s'", optarg);
74 break;
76 default:
77 return 0;
79 return 1;
82 /* no final check */
83 static void
84 final_check(unsigned int flags)
88 struct iptables_match quota = {
89 .next = NULL,
90 .name = "quota",
91 .version = IPTABLES_VERSION,
92 .size = IPT_ALIGN(sizeof (struct ipt_quota_info)),
93 .userspacesize = IPT_ALIGN(sizeof (struct ipt_quota_info)),
94 .help = &help,
95 .parse = &parse,
96 .final_check = &final_check,
97 .print = &print,
98 .save = &save,
99 .extra_opts = opts
102 void
103 _init(void)
105 register_match(&quota);