From d96993e50b44b358ea5bd15f3944674eafd62542 Mon Sep 17 00:00:00 2001 From: Luciano Coelho Date: Tue, 15 Jun 2010 16:54:50 +0200 Subject: extensions: add idletimer xt target extension Add the extension plugin for the IDLETIMER x_tables target. Signed-off-by: Luciano Coelho Signed-off-by: Patrick McHardy --- extensions/libxt_IDLETIMER.c | 141 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 extensions/libxt_IDLETIMER.c (limited to 'extensions/libxt_IDLETIMER.c') diff --git a/extensions/libxt_IDLETIMER.c b/extensions/libxt_IDLETIMER.c new file mode 100644 index 00000000..565f8e39 --- /dev/null +++ b/extensions/libxt_IDLETIMER.c @@ -0,0 +1,141 @@ +/* + * Shared library add-on for iptables to add IDLETIMER support. + * + * Copyright (C) 2010 Nokia Corporation. All rights reserved. + * + * Contact: Luciano Coelho + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ + +#include +#include +#include +#include +#include + +#include +#include + +enum { + IDLETIMER_TG_OPT_TIMEOUT = 1 << 0, + IDLETIMER_TG_OPT_LABEL = 1 << 1, +}; + +static const struct option idletimer_tg_opts[] = { + { .name = "timeout", .has_arg = true, .flag = 0, .val = 't' }, + { .name = "label", .has_arg = true, .flag = 0, .val = 'l' }, + { .name = NULL } +}; + +static void idletimer_tg_help(void) +{ + printf( +"IDLETIMER target options:\n" +" --timeout time Timeout until the notification is sent (in seconds)\n" +" --label string Unique rule identifier\n" +"\n"); +} + +static int idletimer_tg_parse(int c, char **argv, int invert, + unsigned int *flags, + const void *entry, + struct xt_entry_target **target) +{ + struct idletimer_tg_info *info = + (struct idletimer_tg_info *)(*target)->data; + + switch (c) { + case 't': + if (*flags & IDLETIMER_TG_OPT_TIMEOUT) + xtables_error(PARAMETER_PROBLEM, + "Cannot specify timeout more than once"); + + info->timeout = atoi(optarg); + *flags |= IDLETIMER_TG_OPT_TIMEOUT; + break; + + case 'l': + if (*flags & IDLETIMER_TG_OPT_LABEL) + xtables_error(PARAMETER_PROBLEM, + "Cannot specify label more than once"); + + if (strlen(optarg) > MAX_IDLETIMER_LABEL_SIZE - 1) + xtables_error(PARAMETER_PROBLEM, + "Maximum label length is %u for --label", + MAX_IDLETIMER_LABEL_SIZE - 1); + + strcpy(info->label, optarg); + *flags |= IDLETIMER_TG_OPT_LABEL; + break; + + default: + return false; + } + + return true; +} + +static void idletimer_tg_final_check(unsigned int flags) +{ + if (!(flags & IDLETIMER_TG_OPT_TIMEOUT)) + xtables_error(PARAMETER_PROBLEM, "IDLETIMER target: " + "--timeout parameter required"); + if (!(flags & IDLETIMER_TG_OPT_LABEL)) + xtables_error(PARAMETER_PROBLEM, "IDLETIMER target: " + "--label parameter required"); +} + +static void idletimer_tg_print(const void *ip, + const struct xt_entry_target *target, + int numeric) +{ + struct idletimer_tg_info *info = + (struct idletimer_tg_info *) target->data; + + printf("timeout:%u ", info->timeout); + printf("label:%s ", info->label); +} + +static void idletimer_tg_save(const void *ip, + const struct xt_entry_target *target) +{ + struct idletimer_tg_info *info = + (struct idletimer_tg_info *) target->data; + + printf("--timeout %u ", info->timeout); + printf("--label %s ", info->label); +} + +static struct xtables_target idletimer_tg_reg = { + .family = NFPROTO_UNSPEC, + .name = "IDLETIMER", + .version = XTABLES_VERSION, + .revision = 0, + .size = XT_ALIGN(sizeof(struct idletimer_tg_info)), + .userspacesize = offsetof(struct idletimer_tg_info, timer), + .help = idletimer_tg_help, + .parse = idletimer_tg_parse, + .final_check = idletimer_tg_final_check, + .print = idletimer_tg_print, + .save = idletimer_tg_save, + .extra_opts = idletimer_tg_opts, +}; + +static __attribute__((constructor)) void idletimer_tg_ldr(void) +{ + xtables_register_target(&idletimer_tg_reg); +} -- cgit v1.2.3 From 67195a8c8a03d12994e91315e49e3d78c51a385a Mon Sep 17 00:00:00 2001 From: Luciano Coelho Date: Thu, 15 Jul 2010 17:12:56 +0200 Subject: extensions: libxt_IDLETIMER: use xtables_param_act when checking options This patch changes custom error messages for illegal options into the default iptables messages, by using xtables_param_act(). Signed-off-by: Luciano Coelho Signed-off-by: Patrick McHardy --- extensions/libxt_IDLETIMER.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'extensions/libxt_IDLETIMER.c') diff --git a/extensions/libxt_IDLETIMER.c b/extensions/libxt_IDLETIMER.c index 565f8e39..c931d0ee 100644 --- a/extensions/libxt_IDLETIMER.c +++ b/extensions/libxt_IDLETIMER.c @@ -60,23 +60,20 @@ static int idletimer_tg_parse(int c, char **argv, int invert, switch (c) { case 't': - if (*flags & IDLETIMER_TG_OPT_TIMEOUT) - xtables_error(PARAMETER_PROBLEM, - "Cannot specify timeout more than once"); + xtables_param_act(XTF_ONLY_ONCE, "IDLETIMER", "--timeout", + *flags & IDLETIMER_TG_OPT_TIMEOUT); info->timeout = atoi(optarg); *flags |= IDLETIMER_TG_OPT_TIMEOUT; break; case 'l': - if (*flags & IDLETIMER_TG_OPT_LABEL) - xtables_error(PARAMETER_PROBLEM, - "Cannot specify label more than once"); + xtables_param_act(XTF_ONLY_ONCE, "IDLETIMER", "--label", + *flags & IDLETIMER_TG_OPT_TIMEOUT); if (strlen(optarg) > MAX_IDLETIMER_LABEL_SIZE - 1) - xtables_error(PARAMETER_PROBLEM, - "Maximum label length is %u for --label", - MAX_IDLETIMER_LABEL_SIZE - 1); + xtables_param_act(XTF_BAD_VALUE, "IDLETIMER", "--label", + optarg); strcpy(info->label, optarg); *flags |= IDLETIMER_TG_OPT_LABEL; -- cgit v1.2.3 From 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Fri, 23 Jul 2010 21:16:14 +0200 Subject: all: consistent syntax use in struct option Try to inhibit copypasting old stuff. Signed-off-by: Jan Engelhardt --- extensions/libxt_IDLETIMER.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'extensions/libxt_IDLETIMER.c') diff --git a/extensions/libxt_IDLETIMER.c b/extensions/libxt_IDLETIMER.c index c931d0ee..12573a4f 100644 --- a/extensions/libxt_IDLETIMER.c +++ b/extensions/libxt_IDLETIMER.c @@ -20,7 +20,7 @@ * 02110-1301 USA * */ - +#include #include #include #include @@ -36,9 +36,9 @@ enum { }; static const struct option idletimer_tg_opts[] = { - { .name = "timeout", .has_arg = true, .flag = 0, .val = 't' }, - { .name = "label", .has_arg = true, .flag = 0, .val = 'l' }, - { .name = NULL } + {.name = "timeout", .has_arg = true, .val = 't'}, + {.name = "label", .has_arg = true, .val = 'l'}, + XT_GETOPT_TABLEEND, }; static void idletimer_tg_help(void) -- cgit v1.2.3