libxt_quota.c revision 661f112072bc13a1625c4eb5983695e122ea97da
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 14static const struct option opts[] = { 15 {"quota", 1, 0, '1'}, 16 {0} 17}; 18 19/* print usage */ 20static void 21help(void) 22{ 23 printf("quota options:\n" 24 " --quota quota quota (bytes)\n" "\n"); 25} 26 27/* print matchinfo */ 28static void 29print(const void *ip, const struct xt_entry_match *match, int numeric) 30{ 31 struct xt_quota_info *q = (struct xt_quota_info *) match->data; 32 printf("quota: %llu bytes", (unsigned long long) q->quota); 33} 34 35/* save matchinfo */ 36static void 37save(const void *ip, const struct xt_entry_match *match) 38{ 39 struct xt_quota_info *q = (struct xt_quota_info *) match->data; 40 printf("--quota %llu ", (unsigned long long) q->quota); 41} 42 43/* parse quota option */ 44static int 45parse_quota(const char *s, u_int64_t * quota) 46{ 47 *quota = strtoull(s, (char **) NULL, 10); 48 49#ifdef DEBUG_XT_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 */ 60static int 61parse(int c, char **argv, int invert, unsigned int *flags, 62 const void *entry, 63 unsigned int *nfcache, struct xt_entry_match **match) 64{ 65 struct xt_quota_info *info = (struct xt_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 */ 83static void 84final_check(unsigned int flags) 85{ 86} 87 88struct xtables_match quota = { 89 .family = AF_INET, 90 .name = "quota", 91 .version = IPTABLES_VERSION, 92 .size = XT_ALIGN(sizeof (struct xt_quota_info)), 93 .userspacesize = offsetof(struct xt_quota_info, quota), 94 .help = &help, 95 .parse = &parse, 96 .final_check = &final_check, 97 .print = &print, 98 .save = &save, 99 .extra_opts = opts 100}; 101 102struct xtables_match quota6 = { 103 .family = AF_INET6, 104 .name = "quota", 105 .version = IPTABLES_VERSION, 106 .size = XT_ALIGN(sizeof (struct xt_quota_info)), 107 .userspacesize = offsetof(struct xt_quota_info, quota), 108 .help = &help, 109 .parse = &parse, 110 .final_check = &final_check, 111 .print = &print, 112 .save = &save, 113 .extra_opts = opts 114}; 115 116void 117_init(void) 118{ 119 xtables_register_match("a); 120 xtables_register_match("a6); 121} 122