New routers supported
[tomato.git] / release / src / router / ipset / ipset_iptreemap.c
blob22bdcb3840dccc04be2b5734d274000b89cef2fc
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)
6 * any later version.
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.
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
18 #include <limits.h> /* UINT_MAX */
19 #include <stdio.h> /* *printf */
20 #include <string.h> /* mem* */
22 #include "ipset.h"
24 #include <linux/netfilter_ipv4/ip_set_iptreemap.h>
26 #define OPT_CREATE_GC 0x1
28 static void
29 iptreemap_create_init(void *data)
31 struct ip_set_req_iptreemap_create *mydata = data;
33 mydata->gc_interval = 0;
36 static int
37 iptreemap_create_parse(int c, char *argv[] UNUSED, void *data,
38 unsigned int *flags)
40 struct ip_set_req_iptreemap_create *mydata = data;
42 switch (c) {
43 case 'g':
44 string_to_number(optarg, 0, UINT_MAX, &mydata->gc_interval);
46 *flags |= OPT_CREATE_GC;
47 break;
48 default:
49 return 0;
50 break;
53 return 1;
56 static void
57 iptreemap_create_final(void *data UNUSED, unsigned int flags UNUSED)
61 static const struct option create_opts[] = {
62 {.name = "gc", .has_arg = required_argument, .val = 'g'},
63 {NULL},
66 static ip_set_ip_t
67 iptreemap_adt_parser(int cmd UNUSED, const char *arg, void *data)
69 struct ip_set_req_iptreemap *mydata = data;
70 ip_set_ip_t mask;
72 char *saved = ipset_strdup(arg);
73 char *ptr, *tmp = saved;
75 if (strchr(tmp, '/')) {
76 parse_ipandmask(tmp, &mydata->ip, &mask);
77 mydata->end = mydata->ip | ~mask;
78 } else {
79 if ((ptr = strchr(tmp, ':')) != NULL && ++warn_once == 1)
80 fprintf(stderr, "Warning: please use '-' separator token between IP range.\n"
81 "Next release won't support old separator token.\n");
82 ptr = strsep(&tmp, "-:");
83 parse_ip(ptr, &mydata->ip);
85 if (tmp) {
86 parse_ip(tmp, &mydata->end);
87 } else {
88 mydata->end = mydata->ip;
92 ipset_free(saved);
94 return 1;
97 static void
98 iptreemap_initheader(struct set *set, const void *data)
100 const struct ip_set_req_iptreemap_create *header = data;
101 struct ip_set_iptreemap *map = set->settype->header;
103 map->gc_interval = header->gc_interval;
106 static void
107 iptreemap_printheader(struct set *set, unsigned int options UNUSED)
109 struct ip_set_iptreemap *mysetdata = set->settype->header;
111 if (mysetdata->gc_interval)
112 printf(" gc: %u", mysetdata->gc_interval);
114 printf("\n");
117 static void
118 iptreemap_printips_sorted(struct set *set UNUSED, void *data,
119 u_int32_t len, unsigned int options, char dont_align)
121 struct ip_set_req_iptreemap *req;
122 size_t offset = 0;
124 while (len >= offset + sizeof(struct ip_set_req_iptreemap)) {
125 req = data + offset;
127 printf("%s", ip_tostring(req->ip, options));
128 if (req->ip != req->end)
129 printf("-%s", ip_tostring(req->end, options));
130 printf("\n");
132 offset += IPSET_VALIGN(sizeof(struct ip_set_req_iptreemap), dont_align);
136 static void
137 iptreemap_saveheader(struct set *set, unsigned int options UNUSED)
139 struct ip_set_iptreemap *mysetdata = set->settype->header;
141 printf("-N %s %s", set->name, set->settype->typename);
143 if (mysetdata->gc_interval)
144 printf(" --gc %u", mysetdata->gc_interval);
146 printf("\n");
149 static void
150 iptreemap_saveips(struct set *set UNUSED, void *data,
151 u_int32_t len, unsigned int options, char dont_align)
153 struct ip_set_req_iptreemap *req;
154 size_t offset = 0;
156 while (len >= offset + sizeof(struct ip_set_req_iptreemap)) {
157 req = data + offset;
159 printf("-A %s %s", set->name, ip_tostring(req->ip, options));
161 if (req->ip != req->end)
162 printf("-%s", ip_tostring(req->end, options));
164 printf("\n");
166 offset += IPSET_VALIGN(sizeof(struct ip_set_req_iptreemap), dont_align);
170 static void
171 iptreemap_usage(void)
173 printf(
174 "-N set iptreemap --gc interval\n"
175 "-A set IP\n"
176 "-D set IP\n"
177 "-T set IP\n"
181 static struct settype settype_iptreemap = {
182 .typename = SETTYPE_NAME,
183 .protocol_version = IP_SET_PROTOCOL_VERSION,
185 .create_size = sizeof(struct ip_set_req_iptreemap_create),
186 .create_init = iptreemap_create_init,
187 .create_parse = iptreemap_create_parse,
188 .create_final = iptreemap_create_final,
189 .create_opts = create_opts,
191 .adt_size = sizeof(struct ip_set_req_iptreemap),
192 .adt_parser = iptreemap_adt_parser,
194 .header_size = sizeof(struct ip_set_iptreemap),
195 .initheader = iptreemap_initheader,
196 .printheader = iptreemap_printheader,
197 .printips = iptreemap_printips_sorted,
198 .printips_sorted = iptreemap_printips_sorted,
199 .saveheader = iptreemap_saveheader,
200 .saveips = iptreemap_saveips,
202 .usage = iptreemap_usage,
205 CONSTRUCTOR(iptreemap)
207 settype_register(&settype_iptreemap);