fix for f12, gcc4.4
[iptables.git] / ipset / ipset_iptree.c
1 /* Copyright 2005 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
2  *
3  * This program is free software; you can redistribute it and/or modify   
4  * it under the terms of the GNU General Public License as published by   
5  * the Free Software Foundation; either version 2 of the License, or      
6  * (at your option) any later version.                                    
7  *                                                                         
8  * This program is distributed in the hope that it will be useful,        
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of         
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
11  * GNU General Public License for more details.                           
12  *                                                                         
13  * You should have received a copy of the GNU General Public License      
14  * along with this program; if not, write to the Free Software            
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <limits.h>
25
26 #include <linux/netfilter_ipv4/ip_set_iptree.h>
27 #include "ipset.h"
28
29 #define BUFLEN 30;
30
31 #define OPT_CREATE_TIMEOUT    0x01U
32
33 /* Initialize the create. */
34 void create_init(void *data)
35 {
36         struct ip_set_req_iptree_create *mydata =
37             (struct ip_set_req_iptree_create *) data;
38
39         DP("create INIT");
40         mydata->timeout = 0;
41 }
42
43 /* Function which parses command options; returns true if it ate an option */
44 int create_parse(int c, char *argv[], void *data, unsigned *flags)
45 {
46         struct ip_set_req_iptree_create *mydata =
47             (struct ip_set_req_iptree_create *) data;
48
49         DP("create_parse");
50
51         switch (c) {
52         case '1':
53                 string_to_number(optarg, 0, UINT_MAX, &mydata->timeout);
54
55                 *flags |= OPT_CREATE_TIMEOUT;
56
57                 DP("--timeout %u", mydata->timeout);
58
59                 break;
60         default:
61                 return 0;
62         }
63
64         return 1;
65 }
66
67 /* Final check; exit if not ok. */
68 void create_final(void *data, unsigned int flags)
69 {
70 }
71
72 /* Create commandline options */
73 static struct option create_opts[] = {
74         {"timeout", 1, 0, '1'},
75         {0}
76 };
77
78 /* Add, del, test parser */
79 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
80 {
81         struct ip_set_req_iptree *mydata =
82             (struct ip_set_req_iptree *) data;
83         char *saved = ipset_strdup(optarg);
84         char *ptr, *tmp = saved;
85
86         DP("iptree: %p %p", optarg, data);
87
88         ptr = strsep(&tmp, ":%");
89         parse_ip(ptr, &mydata->ip);
90
91         if (tmp)
92                 string_to_number(tmp, 0, UINT_MAX, &mydata->timeout);
93         else
94                 mydata->timeout = 0;    
95
96         free(saved);
97         return 1;       
98 }
99
100 /*
101  * Print and save
102  */
103
104 void initheader(struct set *set, const void *data)
105 {
106         struct ip_set_req_iptree_create *header =
107             (struct ip_set_req_iptree_create *) data;
108         struct ip_set_iptree *map =
109                 (struct ip_set_iptree *) set->settype->header;
110                 
111         map->timeout = header->timeout;
112 }
113
114 void printheader(struct set *set, unsigned options)
115 {
116         struct ip_set_iptree *mysetdata =
117             (struct ip_set_iptree *) set->settype->header;
118
119         if (mysetdata->timeout)
120                 printf(" timeout: %u", mysetdata->timeout);
121         printf("\n");
122 }
123
124 void printips_sorted(struct set *set, void *data, size_t len, unsigned options)
125 {
126         struct ip_set_iptree *mysetdata =
127             (struct ip_set_iptree *) set->settype->header;
128         struct ip_set_req_iptree *req;
129         size_t offset = 0;
130
131         while (len >= offset + sizeof(struct ip_set_req_iptree)) {
132                 req = (struct ip_set_req_iptree *)(data + offset);
133                 if (mysetdata->timeout)
134                         printf("%s:%u\n", ip_tostring(req->ip, options),
135                                           req->timeout);
136                 else
137                         printf("%s\n", ip_tostring(req->ip, options));
138                 offset += sizeof(struct ip_set_req_iptree);
139         }
140 }
141
142 void saveheader(struct set *set, unsigned options)
143 {
144         struct ip_set_iptree *mysetdata =
145             (struct ip_set_iptree *) set->settype->header;
146
147         if (mysetdata->timeout)
148                 printf("-N %s %s --timeout %u\n",
149                        set->name, set->settype->typename,
150                        mysetdata->timeout);
151         else
152                 printf("-N %s %s\n",
153                        set->name, set->settype->typename);
154 }
155
156 void saveips(struct set *set, void *data, size_t len, unsigned options)
157 {
158         struct ip_set_iptree *mysetdata =
159             (struct ip_set_iptree *) set->settype->header;
160         struct ip_set_req_iptree *req;
161         size_t offset = 0;
162
163         DP("%s", set->name);
164
165         while (len >= offset + sizeof(struct ip_set_req_iptree)) {
166                 req = (struct ip_set_req_iptree *)(data + offset);
167                 if (mysetdata->timeout)
168                         printf("-A %s %s:%u\n",
169                                 set->name, 
170                                 ip_tostring(req->ip, options),
171                                 req->timeout);
172                 else
173                         printf("-A %s %s\n", 
174                                 set->name,
175                                 ip_tostring(req->ip, options));
176                 offset += sizeof(struct ip_set_req_iptree);
177         }
178 }
179
180 void usage(void)
181 {
182         printf
183             ("-N set iptree [--timeout value]\n"
184              "-A set IP[:timeout]\n"
185              "-D set IP\n"
186              "-T set IP\n");
187 }
188
189 static struct settype settype_iptree = {
190         .typename = SETTYPE_NAME,
191         .protocol_version = IP_SET_PROTOCOL_VERSION,
192
193         /* Create */
194         .create_size = sizeof(struct ip_set_req_iptree_create),
195         .create_init = &create_init,
196         .create_parse = &create_parse,
197         .create_final = &create_final,
198         .create_opts = create_opts,
199
200         /* Add/del/test */
201         .adt_size = sizeof(struct ip_set_req_iptree),
202         .adt_parser = &adt_parser,
203
204         /* Printing */
205         .header_size = sizeof(struct ip_set_iptree),
206         .initheader = &initheader,
207         .printheader = &printheader,
208         .printips = &printips_sorted,   /* We only have sorted version */
209         .printips_sorted = &printips_sorted,
210         .saveheader = &saveheader,
211         .saveips = &saveips,
212         
213         /* Bindings */
214         .bindip_tostring = &binding_ip_tostring,
215         .bindip_parse   = &parse_ip,
216
217         .usage = &usage,
218 };
219
220 void _init(void)
221 {
222         settype_register(&settype_iptree);
223
224 }