changing trunk/trunk to trunk
[iptables.git] / extensions / libxt_quota.c
1 /*
2  * Shared library add-on to iptables to add quota support
3  *
4  * Sam Johnston <samj@samj.net>
5  */
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <getopt.h>
10 #include <xtables.h>
11
12 #include <linux/netfilter/xt_quota.h>
13
14 static const struct option quota_opts[] = {
15         {"quota", 1, NULL, '1'},
16         { .name = NULL }
17 };
18
19 /* print usage */
20 static void quota_help(void)
21 {
22         printf("quota match options:\n"
23                " --quota quota                  quota (bytes)\n");
24 }
25
26 /* print matchinfo */
27 static void
28 quota_print(const void *ip, const struct xt_entry_match *match, int numeric)
29 {
30         struct xt_quota_info *q = (struct xt_quota_info *) match->data;
31         printf("quota: %llu bytes", (unsigned long long) q->quota);
32 }
33
34 /* save matchinfo */
35 static void
36 quota_save(const void *ip, const struct xt_entry_match *match)
37 {
38         struct xt_quota_info *q = (struct xt_quota_info *) match->data;
39         printf("--quota %llu ", (unsigned long long) q->quota);
40 }
41
42 /* parse quota option */
43 static int
44 parse_quota(const char *s, u_int64_t * quota)
45 {
46         *quota = strtoull(s, NULL, 10);
47
48 #ifdef DEBUG_XT_QUOTA
49         printf("Quota: %llu\n", *quota);
50 #endif
51
52         if (*quota == (u_int64_t)-1)
53                 exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
54         else
55                 return 1;
56 }
57
58 /* parse all options, returning true if we found any for us */
59 static int
60 quota_parse(int c, char **argv, int invert, unsigned int *flags,
61             const void *entry, struct xt_entry_match **match)
62 {
63         struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data;
64
65         switch (c) {
66         case '1':
67                 if (check_inverse(optarg, &invert, NULL, 0))
68                         exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
69                 if (!parse_quota(optarg, &info->quota))
70                         exit_error(PARAMETER_PROBLEM,
71                                    "bad quota: '%s'", optarg);
72                 break;
73
74         default:
75                 return 0;
76         }
77         return 1;
78 }
79
80 static struct xtables_match quota_match = {
81         .family         = AF_UNSPEC,
82         .name           = "quota",
83         .version        = XTABLES_VERSION,
84         .size           = XT_ALIGN(sizeof (struct xt_quota_info)),
85         .userspacesize  = offsetof(struct xt_quota_info, quota),
86         .help           = quota_help,
87         .parse          = quota_parse,
88         .print          = quota_print,
89         .save           = quota_save,
90         .extra_opts     = quota_opts,
91 };
92
93 void
94 _init(void)
95 {
96         xtables_register_match(&quota_match);
97 }