Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 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 | /* initialise match */ |
| 28 | static void |
| 29 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 30 | { |
| 31 | /* no can cache */ |
| 32 | *nfcache |= NFC_UNKNOWN; |
| 33 | } |
| 34 | |
| 35 | /* print matchinfo */ |
| 36 | static void |
| 37 | print(const struct ipt_ip *ip, const struct ipt_entry_match *match, int numeric) |
| 38 | { |
| 39 | struct ipt_quota_info *q = (struct ipt_quota_info *) match->data; |
| 40 | printf("quota: %llu bytes", (unsigned long long) q->quota); |
| 41 | } |
| 42 | |
| 43 | /* save matchinfo */ |
| 44 | static void |
| 45 | save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 46 | { |
| 47 | struct ipt_quota_info *q = (struct ipt_quota_info *) match->data; |
| 48 | printf("--quota %llu ", (unsigned long long) q->quota); |
| 49 | } |
| 50 | |
| 51 | /* parse quota option */ |
| 52 | static int |
| 53 | parse_quota(const char *s, u_int64_t * quota) |
| 54 | { |
| 55 | *quota = strtoull(s, (char **) NULL, 10); |
| 56 | |
| 57 | #ifdef DEBUG_IPT_QUOTA |
| 58 | printf("Quota: %llu\n", *quota); |
| 59 | #endif |
| 60 | |
| 61 | if (*quota == -1) |
| 62 | exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s); |
| 63 | else |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | /* parse all options, returning true if we found any for us */ |
| 68 | static int |
| 69 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 70 | const struct ipt_entry *entry, |
| 71 | unsigned int *nfcache, struct ipt_entry_match **match) |
| 72 | { |
| 73 | struct ipt_quota_info *info = (struct ipt_quota_info *) (*match)->data; |
| 74 | |
| 75 | switch (c) { |
| 76 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame^] | 77 | if (check_inverse(optarg, &invert, NULL, 0)) |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 78 | exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'"); |
| 79 | if (!parse_quota(optarg, &info->quota)) |
| 80 | exit_error(PARAMETER_PROBLEM, |
| 81 | "bad quota: '%s'", optarg); |
| 82 | break; |
| 83 | |
| 84 | default: |
| 85 | return 0; |
| 86 | } |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | /* no final check */ |
| 91 | static void |
| 92 | final_check(unsigned int flags) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | struct iptables_match quota = { NULL, |
| 97 | "quota", |
| 98 | NETFILTER_VERSION, |
| 99 | IPT_ALIGN(sizeof (struct ipt_quota_info)), |
| 100 | IPT_ALIGN(sizeof (struct ipt_quota_info)), |
| 101 | &help, |
| 102 | &init, |
| 103 | &parse, |
| 104 | &final_check, |
| 105 | &print, |
| 106 | &save, |
| 107 | opts |
| 108 | }; |
| 109 | |
| 110 | void |
| 111 | _init(void) |
| 112 | { |
| 113 | register_match("a); |
| 114 | } |