changing trunk/trunk to trunk
[iptables.git] / extensions / libxt_CLASSIFY.c.orig
1 /* Shared library add-on to iptables to add CLASSIFY target support. */
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <getopt.h>
6
7 #include <xtables.h>
8 #include <linux/netfilter/x_tables.h>
9 #include <linux/netfilter/xt_CLASSIFY.h>
10 #include <linux/types.h>
11 #include <linux/pkt_sched.h>
12
13 /* Function which prints out usage message. */
14 static void
15 CLASSIFY_help(void)
16 {
17         printf(
18 "CLASSIFY target options:\n"
19 "  --set-class [MAJOR:MINOR]    Set skb->priority value\n");
20 }
21
22 static const struct option CLASSIFY_opts[] = {
23         { "set-class", 1, NULL, '1' },
24         { .name = NULL }
25 };
26
27 static int CLASSIFY_string_to_priority(const char *s, unsigned int *p)
28 {
29         unsigned int i, j;
30
31         if (sscanf(s, "%x:%x", &i, &j) != 2)
32                 return 1;
33         
34         *p = TC_H_MAKE(i<<16, j);
35         return 0;
36 }
37
38 /* Function which parses command options; returns true if it
39    ate an option */
40 static int
41 CLASSIFY_parse(int c, char **argv, int invert, unsigned int *flags,
42       const void *entry,
43       struct xt_entry_target **target)
44 {
45         struct xt_classify_target_info *clinfo
46                 = (struct xt_classify_target_info *)(*target)->data;
47
48         switch (c) {
49         case '1':
50                 if (CLASSIFY_string_to_priority(optarg, &clinfo->priority))
51                         exit_error(PARAMETER_PROBLEM,
52                                    "Bad class value `%s'", optarg);
53                 if (*flags)
54                         exit_error(PARAMETER_PROBLEM,
55                                    "CLASSIFY: Can't specify --set-class twice");
56                 *flags = 1;
57                 break;
58
59         default:
60                 return 0;
61         }
62
63         return 1;
64 }
65
66 static void
67 CLASSIFY_final_check(unsigned int flags)
68 {
69         if (!flags)
70                 exit_error(PARAMETER_PROBLEM,
71                            "CLASSIFY: Parameter --set-class is required");
72 }
73
74 static void
75 CLASSIFY_print_class(unsigned int priority, int numeric)
76 {
77         printf("%x:%x ", TC_H_MAJ(priority)>>16, TC_H_MIN(priority));
78 }
79
80 /* Prints out the targinfo. */
81 static void
82 CLASSIFY_print(const void *ip,
83       const struct xt_entry_target *target,
84       int numeric)
85 {
86         const struct xt_classify_target_info *clinfo =
87                 (const struct xt_classify_target_info *)target->data;
88         printf("CLASSIFY set ");
89         CLASSIFY_print_class(clinfo->priority, numeric);
90 }
91
92 /* Saves the union ipt_targinfo in parsable form to stdout. */
93 static void
94 CLASSIFY_save(const void *ip, const struct xt_entry_target *target)
95 {
96         const struct xt_classify_target_info *clinfo =
97                 (const struct xt_classify_target_info *)target->data;
98
99         printf("--set-class %.4x:%.4x ",
100                TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority));
101 }
102
103 static struct xtables_target classify_target = { 
104         .family         = AF_UNSPEC,
105         .name           = "CLASSIFY",
106         .version        = XTABLES_VERSION,
107         .size           = XT_ALIGN(sizeof(struct xt_classify_target_info)),
108         .userspacesize  = XT_ALIGN(sizeof(struct xt_classify_target_info)),
109         .help           = CLASSIFY_help,
110         .parse          = CLASSIFY_parse,
111         .final_check    = CLASSIFY_final_check,
112         .print          = CLASSIFY_print,
113         .save           = CLASSIFY_save,
114         .extra_opts     = CLASSIFY_opts,
115 };
116
117 void _init(void)
118 {
119         xtables_register_target(&classify_target);
120 }