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