tizen 2.3.1 release
[external/iptables.git] / extensions / libxt_nfacct.c
1 /*
2  * (C) 2011 by Pablo Neira Ayuso <pablo@netfilter.org>
3  * (C) 2011 by Intra2Net AG <http://www.intra2net.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 (or
7  * any later at your option) as published by the Free Software Foundation.
8  */
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <getopt.h>
14 #include <xtables.h>
15
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_nfacct.h>
18
19 enum {
20         O_NAME = 0,
21 };
22
23 #define s struct xt_nfacct_match_info
24 static const struct xt_option_entry nfacct_opts[] = {
25         {.name = "nfacct-name", .id = O_NAME, .type = XTTYPE_STRING,
26          .min = 1, .flags = XTOPT_MAND|XTOPT_PUT, XTOPT_POINTER(s, name)},
27         XTOPT_TABLEEND,
28 };
29 #undef s
30
31 static void nfacct_help(void)
32 {
33         printf("nfacct match options:\n"
34                " --nfacct-name STRING           Name of accouting area\n");
35 }
36
37 static void nfacct_parse(struct xt_option_call *cb)
38 {
39         xtables_option_parse(cb);
40         switch (cb->entry->id) {
41         case O_NAME:
42                 if (strchr(cb->arg, '\n') != NULL)
43                         xtables_error(PARAMETER_PROBLEM,
44                                    "Newlines not allowed in --nfacct-name");
45                 break;
46         }
47 }
48
49 static void
50 nfacct_print_name(const struct xt_nfacct_match_info *info, char *name)
51 {
52         printf(" %snfacct-name ", name);
53         xtables_save_string(info->name);
54 }
55
56 static void nfacct_print(const void *ip, const struct xt_entry_match *match,
57                         int numeric)
58 {
59         const struct xt_nfacct_match_info *info =
60                 (struct xt_nfacct_match_info *)match->data;
61
62         nfacct_print_name(info, "");
63 }
64
65 static void nfacct_save(const void *ip, const struct xt_entry_match *match)
66 {
67         const struct xt_nfacct_match_info *info =
68                 (struct xt_nfacct_match_info *)match->data;
69
70         nfacct_print_name(info, "--");
71 }
72
73 static struct xtables_match nfacct_match = {
74         .family         = NFPROTO_UNSPEC,
75         .name           = "nfacct",
76         .version        = XTABLES_VERSION,
77         .size           = XT_ALIGN(sizeof(struct xt_nfacct_match_info)),
78         .userspacesize  = offsetof(struct xt_nfacct_match_info, nfacct),
79         .help           = nfacct_help,
80         .x6_parse       = nfacct_parse,
81         .print          = nfacct_print,
82         .save           = nfacct_save,
83         .x6_options     = nfacct_opts,
84 };
85
86 void _init(void)
87 {
88         xtables_register_match(&nfacct_match);
89 }