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)
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
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* */
24 #include <linux/netfilter_ipv4/ip_set_iptreemap.h>
26 #define OPT_CREATE_GC 0x1
29 iptreemap_create_init(void *data
)
31 struct ip_set_req_iptreemap_create
*mydata
= data
;
33 mydata
->gc_interval
= 0;
37 iptreemap_create_parse(int c
, char *argv
[] UNUSED
, void *data
,
40 struct ip_set_req_iptreemap_create
*mydata
= data
;
44 string_to_number(optarg
, 0, UINT_MAX
, &mydata
->gc_interval
);
46 *flags
|= OPT_CREATE_GC
;
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'},
67 iptreemap_adt_parser(int cmd UNUSED
, const char *arg
, void *data
)
69 struct ip_set_req_iptreemap
*mydata
= data
;
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
;
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
);
86 parse_ip(tmp
, &mydata
->end
);
88 mydata
->end
= mydata
->ip
;
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
;
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
);
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
;
124 while (len
>= offset
+ sizeof(struct ip_set_req_iptreemap
)) {
127 printf("%s", ip_tostring(req
->ip
, options
));
128 if (req
->ip
!= req
->end
)
129 printf("-%s", ip_tostring(req
->end
, options
));
132 offset
+= IPSET_VALIGN(sizeof(struct ip_set_req_iptreemap
), dont_align
);
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
);
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
;
156 while (len
>= offset
+ sizeof(struct ip_set_req_iptreemap
)) {
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
));
166 offset
+= IPSET_VALIGN(sizeof(struct ip_set_req_iptreemap
), dont_align
);
171 iptreemap_usage(void)
174 "-N set iptreemap --gc interval\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
);