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
18 #include <limits.h> /* UINT_MAX */
19 #include <stdio.h> /* *printf */
20 #include <string.h> /* mem* */
24 #include <linux/netfilter_ipv4/ip_set_iptree.h>
28 #define OPT_CREATE_TIMEOUT 0x01U
30 /* Initialize the create. */
32 iptree_create_init(void *data
)
34 struct ip_set_req_iptree_create
*mydata
= data
;
40 /* Function which parses command options; returns true if it ate an option */
42 iptree_create_parse(int c
, char *argv
[] UNUSED
, void *data
, unsigned *flags
)
44 struct ip_set_req_iptree_create
*mydata
= data
;
50 string_to_number(optarg
, 0, UINT_MAX
, &mydata
->timeout
);
52 *flags
|= OPT_CREATE_TIMEOUT
;
54 DP("--timeout %u", mydata
->timeout
);
64 /* Final check; exit if not ok. */
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'},
76 /* Add, del, test parser */
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
);
94 string_to_number(tmp
, 0, UINT_MAX
, &mydata
->timeout
);
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
;
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
);
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
;
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
),
139 printf("%s\n", ip_tostring(req
->ip
, options
));
140 offset
+= IPSET_VALIGN(sizeof(struct ip_set_req_iptree
), dont_align
);
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
,
155 set
->name
, set
->settype
->typename
);
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
;
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",
173 ip_tostring(req
->ip
, options
),
178 ip_tostring(req
->ip
, options
));
179 offset
+= IPSET_VALIGN(sizeof(struct ip_set_req_iptree
), dont_align
);
187 ("-N set iptree [--timeout value]\n"
188 "-A set IP[,timeout]\n"
193 static struct settype settype_iptree
= {
194 .typename
= SETTYPE_NAME
,
195 .protocol_version
= IP_SET_PROTOCOL_VERSION
,
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
,
205 .adt_size
= sizeof(struct ip_set_req_iptree
),
206 .adt_parser
= iptree_adt_parser
,
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
,
222 settype_register(&settype_iptree
);