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>
26 #include <linux/netfilter_ipv4/ip_set_iptree.h>
31 #define OPT_CREATE_TIMEOUT 0x01U
33 /* Initialize the create. */
34 void create_init(void *data)
36 struct ip_set_req_iptree_create *mydata =
37 (struct ip_set_req_iptree_create *) data;
43 /* Function which parses command options; returns true if it ate an option */
44 int create_parse(int c, char *argv[], void *data, unsigned *flags)
46 struct ip_set_req_iptree_create *mydata =
47 (struct ip_set_req_iptree_create *) data;
53 string_to_number(optarg, 0, UINT_MAX, &mydata->timeout);
55 *flags |= OPT_CREATE_TIMEOUT;
57 DP("--timeout %u", mydata->timeout);
67 /* Final check; exit if not ok. */
68 void create_final(void *data, unsigned int flags)
72 /* Create commandline options */
73 static struct option create_opts[] = {
74 {"timeout", 1, 0, '1'},
78 /* Add, del, test parser */
79 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
81 struct ip_set_req_iptree *mydata =
82 (struct ip_set_req_iptree *) data;
83 char *saved = ipset_strdup(optarg);
84 char *ptr, *tmp = saved;
86 DP("iptree: %p %p", optarg, data);
88 ptr = strsep(&tmp, ":%");
89 parse_ip(ptr, &mydata->ip);
92 string_to_number(tmp, 0, UINT_MAX, &mydata->timeout);
104 void initheader(struct set *set, const void *data)
106 struct ip_set_req_iptree_create *header =
107 (struct ip_set_req_iptree_create *) data;
108 struct ip_set_iptree *map =
109 (struct ip_set_iptree *) set->settype->header;
111 map->timeout = header->timeout;
114 void printheader(struct set *set, unsigned options)
116 struct ip_set_iptree *mysetdata =
117 (struct ip_set_iptree *) set->settype->header;
119 if (mysetdata->timeout)
120 printf(" timeout: %u", mysetdata->timeout);
124 void printips_sorted(struct set *set, void *data, size_t len, unsigned options)
126 struct ip_set_iptree *mysetdata =
127 (struct ip_set_iptree *) set->settype->header;
128 struct ip_set_req_iptree *req;
131 while (len >= offset + sizeof(struct ip_set_req_iptree)) {
132 req = (struct ip_set_req_iptree *)(data + offset);
133 if (mysetdata->timeout)
134 printf("%s:%u\n", ip_tostring(req->ip, options),
137 printf("%s\n", ip_tostring(req->ip, options));
138 offset += sizeof(struct ip_set_req_iptree);
142 void saveheader(struct set *set, unsigned options)
144 struct ip_set_iptree *mysetdata =
145 (struct ip_set_iptree *) set->settype->header;
147 if (mysetdata->timeout)
148 printf("-N %s %s --timeout %u\n",
149 set->name, set->settype->typename,
153 set->name, set->settype->typename);
156 void saveips(struct set *set, void *data, size_t len, unsigned options)
158 struct ip_set_iptree *mysetdata =
159 (struct ip_set_iptree *) set->settype->header;
160 struct ip_set_req_iptree *req;
165 while (len >= offset + sizeof(struct ip_set_req_iptree)) {
166 req = (struct ip_set_req_iptree *)(data + offset);
167 if (mysetdata->timeout)
168 printf("-A %s %s:%u\n",
170 ip_tostring(req->ip, options),
175 ip_tostring(req->ip, options));
176 offset += sizeof(struct ip_set_req_iptree);
183 ("-N set iptree [--timeout value]\n"
184 "-A set IP[:timeout]\n"
189 static struct settype settype_iptree = {
190 .typename = SETTYPE_NAME,
191 .protocol_version = IP_SET_PROTOCOL_VERSION,
194 .create_size = sizeof(struct ip_set_req_iptree_create),
195 .create_init = &create_init,
196 .create_parse = &create_parse,
197 .create_final = &create_final,
198 .create_opts = create_opts,
201 .adt_size = sizeof(struct ip_set_req_iptree),
202 .adt_parser = &adt_parser,
205 .header_size = sizeof(struct ip_set_iptree),
206 .initheader = &initheader,
207 .printheader = &printheader,
208 .printips = &printips_sorted, /* We only have sorted version */
209 .printips_sorted = &printips_sorted,
210 .saveheader = &saveheader,
214 .bindip_tostring = &binding_ip_tostring,
215 .bindip_parse = &parse_ip,
220 void __attribute__ ((constructor)) my_init(void)
222 settype_register(&settype_iptree);