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.
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
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
25 #include <linux/netfilter_ipv4/ip_set_iptree.h>
30 #define OPT_CREATE_TIMEOUT 0x01U
32 /* Initialize the create. */
33 void create_init(void *data)
35 struct ip_set_req_iptree_create *mydata =
36 (struct ip_set_req_iptree_create *) data;
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)
45 struct ip_set_req_iptree_create *mydata =
46 (struct ip_set_req_iptree_create *) data;
52 string_to_number(optarg, 0, UINT_MAX, &mydata->timeout);
54 *flags |= OPT_CREATE_TIMEOUT;
56 DP("--timeout %u", mydata->timeout);
66 /* Final check; exit if not ok. */
67 void create_final(void *data, unsigned int flags)
71 /* Create commandline options */
72 static struct option create_opts[] = {
73 {"timeout", 1, 0, '1'},
77 /* Add, del, test parser */
78 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
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;
85 DP("iptree: %p %p", optarg, data);
87 ptr = strsep(&tmp, "%");
88 parse_ip(ptr, &mydata->ip);
91 string_to_number(tmp, 0, UINT_MAX, &mydata->timeout);
103 void initheader(struct set *set, const void *data)
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;
110 map->timeout = header->timeout;
113 void printheader(struct set *set, unsigned options)
115 struct ip_set_iptree *mysetdata =
116 (struct ip_set_iptree *) set->settype->header;
118 if (mysetdata->timeout)
119 printf(" timeout: %u", mysetdata->timeout);
123 void printips_sorted(struct set *set, void *data, size_t len, unsigned options)
125 struct ip_set_iptree *mysetdata =
126 (struct ip_set_iptree *) set->settype->header;
127 struct ip_set_req_iptree *req;
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),
136 printf("%s\n", ip_tostring(req->ip, options));
137 offset += sizeof(struct ip_set_req_iptree);
141 void saveheader(struct set *set, unsigned options)
143 struct ip_set_iptree *mysetdata =
144 (struct ip_set_iptree *) set->settype->header;
146 if (mysetdata->timeout)
147 printf("-N %s %s --timeout %u\n",
148 set->name, set->settype->typename,
152 set->name, set->settype->typename);
155 void saveips(struct set *set, void *data, size_t len, unsigned options)
157 struct ip_set_iptree *mysetdata =
158 (struct ip_set_iptree *) set->settype->header;
159 struct ip_set_req_iptree *req;
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",
169 ip_tostring(req->ip, options),
174 ip_tostring(req->ip, options));
175 offset += sizeof(struct ip_set_req_iptree);
182 ("-N set iptree [--timeout value]\n"
183 "-A set IP[%%timeout]\n"
188 static struct settype settype_iptree = {
189 .typename = SETTYPE_NAME,
190 .protocol_version = IP_SET_PROTOCOL_VERSION,
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,
200 .adt_size = sizeof(struct ip_set_req_iptree),
201 .adt_parser = &adt_parser,
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,
213 .bindip_tostring = &binding_ip_tostring,
214 .bindip_parse = &parse_ip,
221 settype_register(&settype_iptree);