ipset-2.2.8-20051203
[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
25 #include <linux/netfilter_ipv4/ip_set_iptree.h>
26 #include "ipset.h"
27
28 #define BUFLEN 30;
29
30 #define OPT_CREATE_TIMEOUT    0x01U
31
32 /* Initialize the create. */
33 void create_init(void *data)
34 {
35         struct ip_set_req_iptree_create *mydata =
36             (struct ip_set_req_iptree_create *) data;
37
38         DP("create INIT");
39         mydata->timeout = 0;
40 }
41
42 /* Function which parses command options; returns true if it ate an option */
43 int create_parse(int c, char *argv[], void *data, unsigned *flags)
44 {
45         struct ip_set_req_iptree_create *mydata =
46             (struct ip_set_req_iptree_create *) data;
47
48         DP("create_parse");
49
50         switch (c) {
51         case '1':
52                 string_to_number(optarg, 0, UINT_MAX, &mydata->timeout);
53
54                 *flags |= OPT_CREATE_TIMEOUT;
55
56                 DP("--timeout %u", mydata->timeout);
57
58                 break;
59         default:
60                 return 0;
61         }
62
63         return 1;
64 }
65
66 /* Final check; exit if not ok. */
67 void create_final(void *data, unsigned int flags)
68 {
69 }
70
71 /* Create commandline options */
72 static struct option create_opts[] = {
73         {"timeout", 1, 0, '1'},
74         {0}
75 };
76
77 /* Add, del, test parser */
78 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
79 {
80         struct ip_set_req_iptree *mydata =
81             (struct ip_set_req_iptree *) data;
82         char *saved = ipset_strdup(optarg);
83         char *ptr, *tmp = saved;
84
85         DP("iptree: %p %p", optarg, data);
86
87         ptr = strsep(&tmp, "%");
88         parse_ip(ptr, &mydata->ip);
89
90         if (tmp)
91                 string_to_number(tmp, 0, UINT_MAX, &mydata->timeout);
92         else
93                 mydata->timeout = 0;    
94
95         free(saved);
96         return 1;       
97 }
98
99 /*
100  * Print and save
101  */
102
103 void initheader(struct set *set, const void *data)
104 {
105         struct ip_set_req_iptree_create *header =
106             (struct ip_set_req_iptree_create *) data;
107         struct ip_set_iptree *map =
108                 (struct ip_set_iptree *) set->settype->header;
109                 
110         map->timeout = header->timeout;
111 }
112
113 void printheader(struct set *set, unsigned options)
114 {
115         struct ip_set_iptree *mysetdata =
116             (struct ip_set_iptree *) set->settype->header;
117
118         if (mysetdata->timeout)
119                 printf(" timeout: %u", mysetdata->timeout);
120         printf("\n");
121 }
122
123 void printips_sorted(struct set *set, void *data, size_t len, unsigned options)
124 {
125         struct ip_set_iptree *mysetdata =
126             (struct ip_set_iptree *) set->settype->header;
127         struct ip_set_req_iptree *req;
128         size_t offset = 0;
129
130         while (len >= offset + sizeof(struct ip_set_req_iptree)) {
131                 req = (struct ip_set_req_iptree *)(data + offset);
132                 if (mysetdata->timeout)
133                         printf("%s%%%u\n", ip_tostring(req->ip, options),
134                                            req->timeout);
135                 else
136                         printf("%s\n", ip_tostring(req->ip, options));
137                 offset += sizeof(struct ip_set_req_iptree);
138         }
139 }
140
141 void saveheader(struct set *set, unsigned options)
142 {
143         struct ip_set_iptree *mysetdata =
144             (struct ip_set_iptree *) set->settype->header;
145
146         if (mysetdata->timeout)
147                 printf("-N %s %s --timeout %u\n",
148                        set->name, set->settype->typename,
149                        mysetdata->timeout);
150         else
151                 printf("-N %s %s\n",
152                        set->name, set->settype->typename);
153 }
154
155 void saveips(struct set *set, void *data, size_t len, unsigned options)
156 {
157         struct ip_set_iptree *mysetdata =
158             (struct ip_set_iptree *) set->settype->header;
159         struct ip_set_req_iptree *req;
160         size_t offset = 0;
161
162         DP("%s", set->name);
163
164         while (len >= offset + sizeof(struct ip_set_req_iptree)) {
165                 req = (struct ip_set_req_iptree *)(data + offset);
166                 if (mysetdata->timeout)
167                         printf("-A %s %s%%%u\n",
168                                 set->name, 
169                                 ip_tostring(req->ip, options),
170                                 req->timeout);
171                 else
172                         printf("-A %s %s\n", 
173                                 set->name,
174                                 ip_tostring(req->ip, options));
175                 offset += sizeof(struct ip_set_req_iptree);
176         }
177 }
178
179 void usage(void)
180 {
181         printf
182             ("-N set iptree [--timeout value]\n"
183              "-A set IP[%%timeout]\n"
184              "-D set IP\n"
185              "-T set IP\n");
186 }
187
188 static struct settype settype_iptree = {
189         .typename = SETTYPE_NAME,
190         .protocol_version = IP_SET_PROTOCOL_VERSION,
191
192         /* Create */
193         .create_size = sizeof(struct ip_set_req_iptree_create),
194         .create_init = &create_init,
195         .create_parse = &create_parse,
196         .create_final = &create_final,
197         .create_opts = create_opts,
198
199         /* Add/del/test */
200         .adt_size = sizeof(struct ip_set_req_iptree),
201         .adt_parser = &adt_parser,
202
203         /* Printing */
204         .header_size = sizeof(struct ip_set_iptree),
205         .initheader = &initheader,
206         .printheader = &printheader,
207         .printips = &printips_sorted,   /* We only have sorted version */
208         .printips_sorted = &printips_sorted,
209         .saveheader = &saveheader,
210         .saveips = &saveips,
211         
212         /* Bindings */
213         .bindip_tostring = &binding_ip_tostring,
214         .bindip_parse   = &parse_ip,
215
216         .usage = &usage,
217 };
218
219 void _init(void)
220 {
221         settype_register(&settype_iptree);
222
223 }