New routers supported
[tomato.git] / release / src / router / ipset / ipset_iptree.c
blob508e67ae4820512c9c25e4cef25a0949c691a8bc
1 /* Copyright 2005 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
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.
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
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_iptree.h>
26 #define BUFLEN 30;
28 #define OPT_CREATE_TIMEOUT 0x01U
30 /* Initialize the create. */
31 static void
32 iptree_create_init(void *data)
34 struct ip_set_req_iptree_create *mydata = data;
36 DP("create INIT");
37 mydata->timeout = 0;
40 /* Function which parses command options; returns true if it ate an option */
41 static int
42 iptree_create_parse(int c, char *argv[] UNUSED, void *data, unsigned *flags)
44 struct ip_set_req_iptree_create *mydata = data;
46 DP("create_parse");
48 switch (c) {
49 case '1':
50 string_to_number(optarg, 0, UINT_MAX, &mydata->timeout);
52 *flags |= OPT_CREATE_TIMEOUT;
54 DP("--timeout %u", mydata->timeout);
56 break;
57 default:
58 return 0;
61 return 1;
64 /* Final check; exit if not ok. */
65 static void
66 iptree_create_final(void *data UNUSED, unsigned int flags UNUSED)
70 /* Create commandline options */
71 static const struct option create_opts[] = {
72 {.name = "timeout", .has_arg = required_argument, .val = '1'},
73 {NULL},
76 /* Add, del, test parser */
77 static ip_set_ip_t
78 iptree_adt_parser(int cmd UNUSED, const char *arg, void *data)
80 struct ip_set_req_iptree *mydata = data;
81 char *saved = ipset_strdup(arg);
82 char *ptr, *tmp = saved;
84 DP("iptree: %p %p", arg, data);
86 if (((ptr = strchr(tmp, ':')) || (ptr = strchr(tmp, '%'))) && ++warn_once == 1)
87 fprintf(stderr, "Warning: please use ',' separator token between ip,timeout.\n"
88 "Next release won't support old separator tokens.\n");
90 ptr = strsep(&tmp, ":%,");
91 parse_ip(ptr, &mydata->ip);
93 if (tmp)
94 string_to_number(tmp, 0, UINT_MAX, &mydata->timeout);
95 else
96 mydata->timeout = 0;
98 ipset_free(saved);
99 return 1;
103 * Print and save
106 static void
107 iptree_initheader(struct set *set, const void *data)
109 const struct ip_set_req_iptree_create *header = data;
110 struct ip_set_iptree *map = set->settype->header;
112 map->timeout = header->timeout;
115 static void
116 iptree_printheader(struct set *set, unsigned options UNUSED)
118 struct ip_set_iptree *mysetdata = set->settype->header;
120 if (mysetdata->timeout)
121 printf(" timeout: %u", mysetdata->timeout);
122 printf("\n");
125 static void
126 iptree_printips_sorted(struct set *set, void *data, u_int32_t len,
127 unsigned options, char dont_align)
129 struct ip_set_iptree *mysetdata = set->settype->header;
130 struct ip_set_req_iptree *req;
131 size_t offset = 0;
133 while (len >= offset + sizeof(struct ip_set_req_iptree)) {
134 req = (struct ip_set_req_iptree *)(data + offset);
135 if (mysetdata->timeout)
136 printf("%s,%u\n", ip_tostring(req->ip, options),
137 req->timeout);
138 else
139 printf("%s\n", ip_tostring(req->ip, options));
140 offset += IPSET_VALIGN(sizeof(struct ip_set_req_iptree), dont_align);
144 static void
145 iptree_saveheader(struct set *set, unsigned options UNUSED)
147 struct ip_set_iptree *mysetdata = set->settype->header;
149 if (mysetdata->timeout)
150 printf("-N %s %s --timeout %u\n",
151 set->name, set->settype->typename,
152 mysetdata->timeout);
153 else
154 printf("-N %s %s\n",
155 set->name, set->settype->typename);
158 static void
159 iptree_saveips(struct set *set, void *data, u_int32_t len,
160 unsigned options, char dont_align)
162 struct ip_set_iptree *mysetdata = set->settype->header;
163 struct ip_set_req_iptree *req;
164 size_t offset = 0;
166 DP("%s", set->name);
168 while (len >= offset + sizeof(struct ip_set_req_iptree)) {
169 req = (struct ip_set_req_iptree *)(data + offset);
170 if (mysetdata->timeout)
171 printf("-A %s %s,%u\n",
172 set->name,
173 ip_tostring(req->ip, options),
174 req->timeout);
175 else
176 printf("-A %s %s\n",
177 set->name,
178 ip_tostring(req->ip, options));
179 offset += IPSET_VALIGN(sizeof(struct ip_set_req_iptree), dont_align);
183 static void
184 iptree_usage(void)
186 printf
187 ("-N set iptree [--timeout value]\n"
188 "-A set IP[,timeout]\n"
189 "-D set IP\n"
190 "-T set IP\n");
193 static struct settype settype_iptree = {
194 .typename = SETTYPE_NAME,
195 .protocol_version = IP_SET_PROTOCOL_VERSION,
197 /* Create */
198 .create_size = sizeof(struct ip_set_req_iptree_create),
199 .create_init = iptree_create_init,
200 .create_parse = iptree_create_parse,
201 .create_final = iptree_create_final,
202 .create_opts = create_opts,
204 /* Add/del/test */
205 .adt_size = sizeof(struct ip_set_req_iptree),
206 .adt_parser = iptree_adt_parser,
208 /* Printing */
209 .header_size = sizeof(struct ip_set_iptree),
210 .initheader = iptree_initheader,
211 .printheader = iptree_printheader,
212 .printips = iptree_printips_sorted, /* We only have sorted version */
213 .printips_sorted = iptree_printips_sorted,
214 .saveheader = iptree_saveheader,
215 .saveips = iptree_saveips,
217 .usage = iptree_usage,
220 CONSTRUCTOR(iptree)
222 settype_register(&settype_iptree);