summaryrefslogtreecommitdiff
path: root/ipset_setlist.c
diff options
context:
space:
mode:
author/C=EU/ST=EU/CN=Jozsef Kadlecsik/emailAddress=kadlec@blackhole.kfki.hu </C=EU/ST=EU/CN=Jozsef Kadlecsik/emailAddress=kadlec@blackhole.kfki.hu>2008-10-20 10:00:26 +0000
committer/C=EU/ST=EU/CN=Jozsef Kadlecsik/emailAddress=kadlec@blackhole.kfki.hu </C=EU/ST=EU/CN=Jozsef Kadlecsik/emailAddress=kadlec@blackhole.kfki.hu>2008-10-20 10:00:26 +0000
commita96e4fca10506462df4ee4035f0f86f09bd9dc34 (patch)
tree103bed0a7ae3608675f371d2ac91f3fa7f3a58cc /ipset_setlist.c
parentbc2ddd2d8da1252e78a1f25bd91c1e3cd8016ead (diff)
downloadipset-a96e4fca10506462df4ee4035f0f86f09bd9dc34.tar.gz
ipset 2.4 release
userspace changes: - Added KBUILD_OUTPUT support (Sven Wegener) - Fix memory leak in ipset_iptreemap (Sven Wegener) - Fix multiple compiler warnings (Sven Wegener) - ipportiphash, ipportnethash and setlist types added - binding marked as deprecated functionality - element separator token changed to ',' in anticipating IPv6 addresses, old separator tokens are still supported - unnecessary includes removed - ipset does not try to resolve IP addresses when listing the content of sets (default changed) - manpage updated - ChangeLog forked for kernel part kernel part changes: - ipportiphash, ipportnethash and setlist types added - set type modules reworked to avoid code duplication as much as possible, code unification macros - expand_macros Makefile target added to help debugging code unification macros - ip_set_addip_kernel and ip_set_delip_kernel changed from void to int, __ip_set_get_byname and __ip_set_put_byid added for the sake of setlist type - unnecessary includes removed - compatibility fix for kernels >= 2.6.27: semaphore.h was moved from asm/ to linux/ (James King) - ChangeLog forked for kernel part
Diffstat (limited to 'ipset_setlist.c')
-rw-r--r--ipset_setlist.c219
1 files changed, 219 insertions, 0 deletions
diff --git a/ipset_setlist.c b/ipset_setlist.c
new file mode 100644
index 0000000..b82cd6c
--- /dev/null
+++ b/ipset_setlist.c
@@ -0,0 +1,219 @@
+/* Copyright 2008 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <linux/netfilter_ipv4/ip_set_setlist.h>
+#include "ipset.h"
+
+/* Initialize the create. */
+static void
+create_init(void *data)
+{
+ struct ip_set_req_setlist_create *mydata = data;
+
+ mydata->size = 8;
+}
+
+/* Function which parses command options; returns true if it ate an option */
+static int
+create_parse(int c, char *argv[], void *data, unsigned *flags)
+{
+ struct ip_set_req_setlist_create *mydata = data;
+ unsigned int size;
+
+ switch (c) {
+ case '1':
+ if (string_to_number(optarg, 1, 255, &size))
+ exit_error(PARAMETER_PROBLEM,
+ "Invalid size '%s specified: must be "
+ "between 1-255", optarg);
+ mydata->size = size;
+ break;
+ default:
+ return 0;
+ }
+ return 1;
+}
+
+/* Final check; exit if not ok. */
+static void
+create_final(void *data, unsigned int flags)
+{
+}
+
+/* Create commandline options */
+static const struct option create_opts[] = {
+ {.name = "size", .has_arg = required_argument, .val = '1'},
+ {NULL},
+};
+
+static void check_setname(const char *name)
+{
+ if (strlen(name) > IP_SET_MAXNAMELEN - 1)
+ exit_error(PARAMETER_PROBLEM,
+ "Setname %s is longer than %d characters.",
+ name, IP_SET_MAXNAMELEN - 1);
+}
+
+/* Add, del, test parser */
+static ip_set_ip_t
+adt_parser(unsigned cmd, const char *optarg, void *data)
+{
+ struct ip_set_req_setlist *mydata = data;
+ char *saved = ipset_strdup(optarg);
+ char *ptr, *tmp = saved;
+
+ DP("setlist: %p %p", optarg, data);
+
+ ptr = strsep(&tmp, ",");
+ check_setname(ptr);
+ strcpy(mydata->name, ptr);
+
+ if (!tmp) {
+ mydata->before = 0;
+ mydata->ref[0] = '\0';
+ return 1;
+ }
+
+ ptr = strsep(&tmp, ",");
+
+ if (tmp == NULL || !(strcmp(ptr, "before") == 0 || strcmp(ptr, "after") == 0))
+ exit_error(PARAMETER_PROBLEM,
+ "Syntax error, you must specify elements as setname,[before|after],setname");
+
+ check_setname(tmp);
+ strcpy(mydata->ref, tmp);
+ mydata->before = !strcmp(ptr, "before");
+
+ free(saved);
+
+ return 1;
+}
+
+/*
+ * Print and save
+ */
+
+static void
+initheader(struct set *set, const void *data)
+{
+ const struct ip_set_req_setlist_create *header = data;
+ struct ip_set_setlist *map = set->settype->header;
+
+ memset(map, 0, sizeof(struct ip_set_setlist));
+ map->size = header->size;
+}
+
+static void
+printheader(struct set *set, unsigned options)
+{
+ struct ip_set_setlist *mysetdata = set->settype->header;
+
+ printf(" size: %u\n", mysetdata->size);
+}
+
+static void
+printips_sorted(struct set *set, void *data, size_t len, unsigned options)
+{
+ struct ip_set_setlist *mysetdata = set->settype->header;
+ int i;
+ ip_set_id_t id;
+ struct set *elem;
+
+ for (i = 0; i < mysetdata->size; i++ ) {
+ id = *((ip_set_id_t *)data + i);
+ if (id == IP_SET_INVALID_ID)
+ return;
+ elem = set_find_byid(id);
+ printf("%s\n", elem->name);
+ }
+}
+
+static void
+saveheader(struct set *set, unsigned options)
+{
+ struct ip_set_setlist *mysetdata = set->settype->header;
+
+ printf("-N %s %s --size %u\n",
+ set->name, set->settype->typename,
+ mysetdata->size);
+}
+
+static void
+saveips(struct set *set, void *data, size_t len, unsigned options)
+{
+ struct ip_set_setlist *mysetdata = set->settype->header;
+ int i;
+ ip_set_id_t id;
+ struct set *elem;
+
+ for (i = 0; i < mysetdata->size; i++ ) {
+ id = *((ip_set_id_t *)data + i);
+ if (id == IP_SET_INVALID_ID)
+ return;
+ elem = set_find_byid(id);
+ printf("-A %s %s\n", set->name, elem->name);
+ }
+}
+
+static void usage(void)
+{
+ printf
+ ("-N set setlist --size size\n"
+ "-A set setname[,before|after,setname]\n"
+ "-D set setname\n"
+ "-T set setname\n");
+}
+
+static struct settype settype_setlist = {
+ .typename = SETTYPE_NAME,
+ .protocol_version = IP_SET_PROTOCOL_VERSION,
+
+ /* Create */
+ .create_size = sizeof(struct ip_set_req_setlist_create),
+ .create_init = &create_init,
+ .create_parse = &create_parse,
+ .create_final = &create_final,
+ .create_opts = create_opts,
+
+ /* Add/del/test */
+ .adt_size = sizeof(struct ip_set_req_setlist),
+ .adt_parser = &adt_parser,
+
+ /* Printing */
+ .header_size = sizeof(struct ip_set_setlist),
+ .initheader = &initheader,
+ .printheader = &printheader,
+ .printips = &printips_sorted, /* We only have sorted version */
+ .printips_sorted = &printips_sorted,
+ .saveheader = &saveheader,
+ .saveips = &saveips,
+
+ .usage = &usage,
+};
+
+void _init(void)
+{
+ settype_register(&settype_setlist);
+
+}