changing trunk/trunk to trunk
[iptables.git] / extensions / libxt_NFQUEUE.c
1 /* Shared library add-on to iptables for NFQ
2  *
3  * (C) 2005 by Harald Welte <laforge@netfilter.org>
4  *
5  * This program is distributed under the terms of GNU GPL v2, 1991
6  *
7  */
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <getopt.h>
12
13 #include <xtables.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter/xt_NFQUEUE.h>
16
17 static void NFQUEUE_help(void)
18 {
19         printf(
20 "NFQUEUE target options\n"
21 "  --queue-num value            Send packet to QUEUE number <value>.\n"
22 "                               Valid queue numbers are 0-65535\n"
23 );
24 }
25
26 static const struct option NFQUEUE_opts[] = {
27         { "queue-num", 1, NULL, 'F' },
28         { .name = NULL }
29 };
30
31 static void
32 parse_num(const char *s, struct xt_NFQ_info *tinfo)
33 {
34         unsigned int num;
35        
36         if (string_to_number(s, 0, 65535, &num) == -1)
37                 exit_error(PARAMETER_PROBLEM,
38                            "Invalid queue number `%s'\n", s);
39
40         tinfo->queuenum = num & 0xffff;
41         return;
42 }
43
44 static int
45 NFQUEUE_parse(int c, char **argv, int invert, unsigned int *flags,
46               const void *entry, struct xt_entry_target **target)
47 {
48         struct xt_NFQ_info *tinfo
49                 = (struct xt_NFQ_info *)(*target)->data;
50
51         switch (c) {
52         case 'F':
53                 if (*flags)
54                         exit_error(PARAMETER_PROBLEM, "NFQUEUE target: "
55                                    "Only use --queue-num ONCE!");
56                 parse_num(optarg, tinfo);
57                 break;
58         default:
59                 return 0;
60         }
61
62         return 1;
63 }
64
65 /* Prints out the targinfo. */
66 static void NFQUEUE_print(const void *ip,
67                           const struct xt_entry_target *target, int numeric)
68 {
69         const struct xt_NFQ_info *tinfo =
70                 (const struct xt_NFQ_info *)target->data;
71         printf("NFQUEUE num %u", tinfo->queuenum);
72 }
73
74 /* Saves the union ipt_targinfo in parsable form to stdout. */
75 static void NFQUEUE_save(const void *ip, const struct xt_entry_target *target)
76 {
77         const struct xt_NFQ_info *tinfo =
78                 (const struct xt_NFQ_info *)target->data;
79
80         printf("--queue-num %u ", tinfo->queuenum);
81 }
82
83 static struct xtables_target nfqueue_target = {
84         .family         = AF_INET,
85         .name           = "NFQUEUE",
86         .version        = XTABLES_VERSION,
87         .size           = XT_ALIGN(sizeof(struct xt_NFQ_info)),
88         .userspacesize  = XT_ALIGN(sizeof(struct xt_NFQ_info)),
89         .help           = NFQUEUE_help,
90         .parse          = NFQUEUE_parse,
91         .print          = NFQUEUE_print,
92         .save           = NFQUEUE_save,
93         .extra_opts     = NFQUEUE_opts
94 };
95
96 static struct xtables_target nfqueue_target6 = {
97         .family         = AF_INET6,
98         .name           = "NFQUEUE",
99         .version        = XTABLES_VERSION,
100         .size           = XT_ALIGN(sizeof(struct xt_NFQ_info)),
101         .userspacesize  = XT_ALIGN(sizeof(struct xt_NFQ_info)),
102         .help           = NFQUEUE_help,
103         .parse          = NFQUEUE_parse,
104         .print          = NFQUEUE_print,
105         .save           = NFQUEUE_save,
106         .extra_opts     = NFQUEUE_opts,
107 };
108
109 void _init(void)
110 {
111         xtables_register_target(&nfqueue_target);
112         xtables_register_target(&nfqueue_target6);
113 }