iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libipt_FTOS.c
1 /* Shared library add-on to iptables for FTOS
2  *
3  * (C) 2000 by Matthew G. Marsh <mgm@paktronix.com>
4  *
5  * This program is distributed under the terms of GNU GPL v2, 1991
6  *
7  * libipt_FTOS.c borrowed heavily from libipt_TOS.c  11/09/2000
8  *
9  */
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <getopt.h>
14
15 #include <iptables.h>
16 #include <linux/netfilter_ipv4/ip_tables.h>
17 #include <linux/netfilter_ipv4/ipt_FTOS.h>
18
19 struct finfo {
20         struct ipt_entry_target t;
21         u_int8_t ftos;
22 };
23
24 static void init(struct ipt_entry_target *t, unsigned int *nfcache) 
25 {
26 }
27
28 static void help(void) 
29 {
30         printf(
31 "FTOS target options\n"
32 "  --set-ftos value             Set TOS field in packet header to value\n"
33 "                               This value can be in decimal (ex: 32)\n"
34 "                               or in hex (ex: 0x20)\n"
35 );
36 }
37
38 static struct option opts[] = {
39         { "set-ftos", 1, 0, 'F' },
40         { 0 }
41 };
42
43 static void
44 parse_ftos(const unsigned char *s, struct ipt_FTOS_info *finfo)
45 {
46         unsigned int ftos;
47        
48         if (string_to_number(s, 0, 255, &ftos) == -1)
49                 exit_error(PARAMETER_PROBLEM,
50                            "Invalid ftos `%s'\n", s);
51         finfo->ftos = (u_int8_t )ftos;
52         return;
53 }
54
55 static int
56 parse(int c, char **argv, int invert, unsigned int *flags,
57       const struct ipt_entry *entry,
58       struct ipt_entry_target **target)
59 {
60         struct ipt_FTOS_info *finfo
61                 = (struct ipt_FTOS_info *)(*target)->data;
62
63         switch (c) {
64         case 'F':
65                 if (*flags)
66                         exit_error(PARAMETER_PROBLEM,
67                                    "FTOS target: Only use --set-ftos ONCE!");
68                 parse_ftos(optarg, finfo);
69                 *flags = 1;
70                 break;
71
72         default:
73                 return 0;
74         }
75
76         return 1;
77 }
78
79 static void
80 final_check(unsigned int flags)
81 {
82         if (!flags)
83                 exit_error(PARAMETER_PROBLEM,
84                            "FTOS target: Parameter --set-ftos is required");
85 }
86
87 static void
88 print_ftos(u_int8_t ftos, int numeric)
89 {
90         printf("0x%02x ", ftos);
91 }
92
93 /* Prints out the targinfo. */
94 static void
95 print(const struct ipt_ip *ip,
96       const struct ipt_entry_target *target,
97       int numeric)
98 {
99         const struct ipt_FTOS_info *finfo =
100                 (const struct ipt_FTOS_info *)target->data;
101         printf("TOS set ");
102         print_ftos(finfo->ftos, numeric);
103 }
104
105 /* Saves the union ipt_targinfo in parsable form to stdout. */
106 static void
107 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
108 {
109         const struct ipt_FTOS_info *finfo =
110                 (const struct ipt_FTOS_info *)target->data;
111
112         printf("--set-ftos 0x%02x ", finfo->ftos);
113 }
114
115 static
116 struct iptables_target ftos
117 = { NULL,
118     "FTOS",
119     IPTABLES_VERSION,
120     IPT_ALIGN(sizeof(struct ipt_FTOS_info)),
121     IPT_ALIGN(sizeof(struct ipt_FTOS_info)),
122     &help,
123     &init,
124     &parse,
125     &final_check,
126     &print,
127     &save,
128     opts
129 };
130
131 void _init(void)
132 {
133         register_target(&ftos);
134 }