iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libipt_XOR.c
1 /* Shared library add-on to iptables for the XOR target
2  * (C) 2000 by Tim Vandermeersch <Tim.Vandermeersch@pandora.be>
3  * Based on libipt_TTL.c
4  *
5  * Version 1.0
6  *
7  * This program is distributed under the terms of GNU GPL
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <getopt.h>
14 #include <iptables.h>
15
16 #include <linux/netfilter_ipv4/ip_tables.h>
17 #include <linux/netfilter_ipv4/ipt_XOR.h>
18
19 #define IPT_KEY_SET             1
20 #define IPT_BLOCKSIZE_SET       2
21
22 static void init(struct ipt_entry_target *t, unsigned int *nfcache) 
23 {
24 }
25
26 static void help(void) 
27 {
28         printf(
29                 "XOR target v%s options\n"
30                 "  --key string           Set key to \"string\"\n"
31                 "  --block-size           Set block size\n",
32                 IPTABLES_VERSION);
33 }
34
35 static int parse(int c, char **argv, int invert, unsigned int *flags,
36                 const struct ipt_entry *entry, 
37                 struct ipt_entry_target **target)
38 {
39         struct ipt_XOR_info *info = (struct ipt_XOR_info *) (*target)->data;
40         
41         if (!optarg)
42                 exit_error(PARAMETER_PROBLEM, "XOR: too few arguments");
43         
44         if (check_inverse(optarg, &invert, NULL, 0))
45                 exit_error(PARAMETER_PROBLEM, "XOR: unexpected '!'");
46
47         switch (c) {    
48                 case '1':
49                         strncpy(info->key, optarg, 30);
50                         *flags |= IPT_KEY_SET;
51                         break;
52                 case '2':
53                         info->block_size = atoi(optarg);
54                         *flags |= IPT_BLOCKSIZE_SET;
55                         break;
56                 default:
57                         return 0;
58         }
59         
60         return 1;
61 }
62
63 static void final_check(unsigned int flags)
64 {
65         if (!(flags & IPT_KEY_SET))
66                 exit_error(PARAMETER_PROBLEM, "XOR: You must specify a key");
67         if (!(flags & IPT_BLOCKSIZE_SET))
68                 exit_error(PARAMETER_PROBLEM, "XOR: You must specify a block-size");
69 }
70
71 static void save (const struct ipt_ip *ip,
72                 const struct ipt_entry_target *target)
73 {
74         const struct ipt_XOR_info *info = (struct ipt_XOR_info *) target->data;
75
76         printf("--key %s ", info->key);
77         printf("--block-size %u ", info->block_size);
78 }
79
80 static void print (const struct ipt_ip *ip,
81         const struct ipt_entry_target *target, int numeric)
82 {
83         const struct ipt_XOR_info *info = (struct ipt_XOR_info *) target->data;
84
85         printf("key: %s ", info->key);
86         printf("block-size: %u ", info->block_size);
87 }
88
89 static struct option opts[] = {
90         { "key", 1, 0, '1' },
91         { "block-size", 1, 0, '2' },
92         { 0 }
93 };
94
95 static struct iptables_target XOR = { NULL, 
96         "XOR",
97         IPTABLES_VERSION,
98         IPT_ALIGN(sizeof(struct ipt_XOR_info)),
99         IPT_ALIGN(sizeof(struct ipt_XOR_info)),
100         &help,
101         &init,
102         &parse,
103         &final_check,
104         &print,
105         &save,
106         opts 
107 };
108
109 void _init(void)
110 {
111         register_target(&XOR);
112 }