1 /* Copyright 2007 Sven Wegener <sven.wegener@stealer.net>
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)
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
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
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
25 #include <linux/netfilter_ipv4/ip_set_iptreemap.h>
29 #define OPT_CREATE_GC 0x1
32 create_init(void *data)
34 struct ip_set_req_iptreemap_create *mydata = (struct ip_set_req_iptreemap_create *) data;
36 mydata->gc_interval = 0;
40 create_parse(int c, char *argv[], void *data, unsigned int *flags)
42 struct ip_set_req_iptreemap_create *mydata = (struct ip_set_req_iptreemap_create *) data;
46 string_to_number(optarg, 0, UINT_MAX, &mydata->gc_interval);
48 *flags |= OPT_CREATE_GC;
59 create_final(void *data, unsigned int flags)
63 static struct option create_opts[] = {
69 adt_parser(unsigned int cmd, const char *optarg, void *data)
71 struct ip_set_req_iptreemap *mydata = (struct ip_set_req_iptreemap *) data;
74 char *saved = ipset_strdup(optarg);
75 char *ptr, *tmp = saved;
77 if (strchr(tmp, '/')) {
78 parse_ipandmask(tmp, &mydata->start, &mask);
79 mydata->end = mydata->start | ~mask;
81 ptr = strsep(&tmp, ":");
82 parse_ip(ptr, &mydata->start);
85 parse_ip(tmp, &mydata->end);
87 mydata->end = mydata->start;
95 initheader(struct set *set, const void *data)
97 struct ip_set_req_iptreemap_create *header = (struct ip_set_req_iptreemap_create *) data;
98 struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->settype->header;
100 map->gc_interval = header->gc_interval;
104 printheader(struct set *set, unsigned int options)
106 struct ip_set_iptreemap *mysetdata = (struct ip_set_iptreemap *) set->settype->header;
108 if (mysetdata->gc_interval)
109 printf(" gc: %u", mysetdata->gc_interval);
115 printips_sorted(struct set *set, void *data, size_t len, unsigned int options)
117 struct ip_set_req_iptreemap *req;
120 while (len >= offset + sizeof(struct ip_set_req_iptreemap)) {
121 req = (struct ip_set_req_iptreemap *) (data + offset);
123 printf("%s", ip_tostring(req->start, options));
124 if (req->start != req->end)
125 printf(":%s", ip_tostring(req->end, options));
128 offset += sizeof(struct ip_set_req_iptreemap);
133 saveheader(struct set *set, unsigned int options)
135 struct ip_set_iptreemap *mysetdata = (struct ip_set_iptreemap *) set->settype->header;
137 printf("-N %s %s", set->name, set->settype->typename);
139 if (mysetdata->gc_interval)
140 printf(" --gc %u", mysetdata->gc_interval);
146 saveips(struct set *set, void *data, size_t len, unsigned int options)
148 struct ip_set_req_iptreemap *req;
151 while (len >= offset + sizeof(struct ip_set_req_iptreemap)) {
152 req = (struct ip_set_req_iptreemap *) (data + offset);
154 printf("-A %s %s", set->name, ip_tostring(req->start, options));
156 if (req->start != req->end)
157 printf(":%s", ip_tostring(req->end, options));
161 offset += sizeof(struct ip_set_req_iptreemap);
169 "-N set iptreemap --gc interval\n"
176 static struct settype settype_iptreemap = {
177 .typename = SETTYPE_NAME,
178 .protocol_version = IP_SET_PROTOCOL_VERSION,
180 .create_size = sizeof(struct ip_set_req_iptreemap_create),
181 .create_init = &create_init,
182 .create_parse = &create_parse,
183 .create_final = &create_final,
184 .create_opts = create_opts,
186 .adt_size = sizeof(struct ip_set_req_iptreemap),
187 .adt_parser = &adt_parser,
189 .header_size = sizeof(struct ip_set_iptreemap),
190 .initheader = &initheader,
191 .printheader = &printheader,
192 .printips = &printips_sorted,
193 .printips_sorted = &printips_sorted,
194 .saveheader = &saveheader,
197 .bindip_tostring = &binding_ip_tostring,
198 .bindip_parse = &parse_ip,
206 settype_register(&settype_iptreemap);