1 /* Copyright 2008 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_setlist.h>
28 /* Initialize the create. */
30 setlist_create_init(void *data
)
32 struct ip_set_req_setlist_create
*mydata
= data
;
37 /* Function which parses command options; returns true if it ate an option */
39 setlist_create_parse(int c
, char *argv
[] UNUSED
, void *data
,
40 unsigned *flags UNUSED
)
42 struct ip_set_req_setlist_create
*mydata
= data
;
47 if (string_to_number(optarg
, 1, 255, &size
))
48 exit_error(PARAMETER_PROBLEM
,
49 "Invalid size '%s specified: must be "
50 "between 1-255", optarg
);
59 /* Final check; exit if not ok. */
61 setlist_create_final(void *data UNUSED
, unsigned int flags UNUSED
)
65 /* Create commandline options */
66 static const struct option create_opts
[] = {
67 {.name
= "size", .has_arg
= required_argument
, .val
= '1'},
72 check_setname(const char *name
)
74 if (strlen(name
) > IP_SET_MAXNAMELEN
- 1)
75 exit_error(PARAMETER_PROBLEM
,
76 "Setname %s is longer than %d characters.",
77 name
, IP_SET_MAXNAMELEN
- 1);
80 /* Add, del, test parser */
82 setlist_adt_parser(int cmd UNUSED
, const char *arg
, void *data
)
84 struct ip_set_req_setlist
*mydata
= data
;
85 char *saved
= ipset_strdup(arg
);
86 char *ptr
, *tmp
= saved
;
88 DP("setlist: %p %p", arg
, data
);
90 ptr
= strsep(&tmp
, ",");
92 strcpy(mydata
->name
, ptr
);
96 mydata
->ref
[0] = '\0';
100 ptr
= strsep(&tmp
, ",");
102 if (tmp
== NULL
|| !(strcmp(ptr
, "before") == 0 || strcmp(ptr
, "after") == 0))
103 exit_error(PARAMETER_PROBLEM
,
104 "Syntax error, you must specify elements as setname,[before|after],setname");
107 strcpy(mydata
->ref
, tmp
);
108 mydata
->before
= !strcmp(ptr
, "before");
120 setlist_initheader(struct set
*set
, const void *data
)
122 const struct ip_set_req_setlist_create
*header
= data
;
123 struct ip_set_setlist
*map
= set
->settype
->header
;
125 memset(map
, 0, sizeof(struct ip_set_setlist
));
126 map
->size
= header
->size
;
130 setlist_printheader(struct set
*set
, unsigned options UNUSED
)
132 struct ip_set_setlist
*mysetdata
= set
->settype
->header
;
134 printf(" size: %u\n", mysetdata
->size
);
138 setlist_printips_sorted(struct set
*set
, void *data
,
139 u_int32_t len UNUSED
, unsigned options UNUSED
,
142 struct ip_set_setlist
*mysetdata
= set
->settype
->header
;
147 asize
= IPSET_VALIGN(sizeof(ip_set_id_t
), dont_align
);
148 for (i
= 0; i
< mysetdata
->size
; i
++ ) {
150 id
= (ip_set_id_t
*)(data
+ i
* asize
);
151 DP("Try %u, check", i
);
152 if (*id
== IP_SET_INVALID_ID
)
154 elem
= set_find_byid(*id
);
155 printf("%s\n", elem
->name
);
160 setlist_saveheader(struct set
*set
, unsigned options UNUSED
)
162 struct ip_set_setlist
*mysetdata
= set
->settype
->header
;
164 printf("-N %s %s --size %u\n",
165 set
->name
, set
->settype
->typename
,
170 setlist_saveips(struct set
*set
, void *data
,
171 u_int32_t len UNUSED
, unsigned options UNUSED
, char dont_align
)
173 struct ip_set_setlist
*mysetdata
= set
->settype
->header
;
178 asize
= IPSET_VALIGN(sizeof(ip_set_id_t
), dont_align
);
179 for (i
= 0; i
< mysetdata
->size
; i
++ ) {
180 id
= (ip_set_id_t
*)(data
+ i
* asize
);
181 if (*id
== IP_SET_INVALID_ID
)
183 elem
= set_find_byid(*id
);
184 printf("-A %s %s\n", set
->name
, elem
->name
);
192 ("-N set setlist --size size\n"
193 "-A set setname[,before|after,setname]\n"
198 static struct settype settype_setlist
= {
199 .typename
= SETTYPE_NAME
,
200 .protocol_version
= IP_SET_PROTOCOL_VERSION
,
203 .create_size
= sizeof(struct ip_set_req_setlist_create
),
204 .create_init
= setlist_create_init
,
205 .create_parse
= setlist_create_parse
,
206 .create_final
= setlist_create_final
,
207 .create_opts
= create_opts
,
210 .adt_size
= sizeof(struct ip_set_req_setlist
),
211 .adt_parser
= setlist_adt_parser
,
214 .header_size
= sizeof(struct ip_set_setlist
),
215 .initheader
= setlist_initheader
,
216 .printheader
= setlist_printheader
,
217 .printips
= setlist_printips_sorted
,
218 .printips_sorted
= setlist_printips_sorted
,
219 .saveheader
= setlist_saveheader
,
220 .saveips
= setlist_saveips
,
222 .usage
= setlist_usage
,
227 settype_register(&settype_setlist
);