iptables-1.3.2-20050720
[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                         info->key[29] = '\0';
51                         *flags |= IPT_KEY_SET;
52                         break;
53                 case '2':
54                         info->block_size = atoi(optarg);
55                         *flags |= IPT_BLOCKSIZE_SET;
56                         break;
57                 default:
58                         return 0;
59         }
60         
61         return 1;
62 }
63
64 static void final_check(unsigned int flags)
65 {
66         if (!(flags & IPT_KEY_SET))
67                 exit_error(PARAMETER_PROBLEM, "XOR: You must specify a key");
68         if (!(flags & IPT_BLOCKSIZE_SET))
69                 exit_error(PARAMETER_PROBLEM, "XOR: You must specify a block-size");
70 }
71
72 static void save (const struct ipt_ip *ip,
73                 const struct ipt_entry_target *target)
74 {
75         const struct ipt_XOR_info *info = (struct ipt_XOR_info *) target->data;
76
77         printf("--key %s ", info->key);
78         printf("--block-size %u ", info->block_size);
79 }
80
81 static void print (const struct ipt_ip *ip,
82         const struct ipt_entry_target *target, int numeric)
83 {
84         const struct ipt_XOR_info *info = (struct ipt_XOR_info *) target->data;
85
86         printf("key: %s ", info->key);
87         printf("block-size: %u ", info->block_size);
88 }
89
90 static struct option opts[] = {
91         { "key", 1, 0, '1' },
92         { "block-size", 1, 0, '2' },
93         { 0 }
94 };
95
96 static struct iptables_target XOR = {
97         .next           = NULL, 
98         .name           = "XOR",
99         .version        = IPTABLES_VERSION,
100         .size           = IPT_ALIGN(sizeof(struct ipt_XOR_info)),
101         .userspacesize  = IPT_ALIGN(sizeof(struct ipt_XOR_info)),
102         .help           = &help,
103         .init           = &init,
104         .parse          = &parse,
105         .final_check    = &final_check,
106         .print          = &print,
107         .save           = &save,
108         .extra_opts     = opts 
109 };
110
111 void _init(void)
112 {
113         register_target(&XOR);
114 }