iptables-1.3.2-20050720
[iptables.git] / extensions / libipt_quota.c
1 /*
2  * Shared library add-on to iptables to add quota support
3  *
4  * Sam Johnston <samj@samj.net>
5  */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <getopt.h>
9 #include <iptables.h>
10
11 #include <linux/netfilter_ipv4/ipt_quota.h>
12 #include <linux/netfilter_ipv4/ip_tables.h>
13
14 static struct option opts[] = {
15         {"quota", 1, 0, '1'},
16         {0}
17 };
18
19 /* print usage */
20 static void
21 help(void)
22 {
23         printf("quota options:\n"
24                " --quota quota                  quota (bytes)\n" "\n");
25 }
26
27 /* print matchinfo */
28 static void
29 print(const struct ipt_ip *ip, const struct ipt_entry_match *match, int numeric)
30 {
31         struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
32         printf("quota: %llu bytes", (unsigned long long) q->quota);
33 }
34
35 /* save matchinfo */
36 static void
37 save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
38 {
39         struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
40         printf("--quota %llu ", (unsigned long long) q->quota);
41 }
42
43 /* parse quota option */
44 static int
45 parse_quota(const char *s, u_int64_t * quota)
46 {
47         *quota = strtoull(s, (char **) NULL, 10);
48
49 #ifdef DEBUG_IPT_QUOTA
50         printf("Quota: %llu\n", *quota);
51 #endif
52
53         if (*quota == -1)
54                 exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
55         else
56                 return 1;
57 }
58
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)
64 {
65         struct ipt_quota_info *info = (struct ipt_quota_info *) (*match)->data;
66
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;
75
76         default:
77                 return 0;
78         }
79         return 1;
80 }
81
82 /* no final check */
83 static void
84 final_check(unsigned int flags)
85 {
86 }
87
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
100 };
101
102 void
103 _init(void)
104 {
105         register_match(&quota);
106 }