libxt_IDLETIMER.c revision 73866357e4a7a0fdc1b293bf8863fee2bd56da9e
1/* 2 * Shared library add-on for iptables to add IDLETIMER support. 3 * 4 * Copyright (C) 2010 Nokia Corporation. All rights reserved. 5 * 6 * Contact: Luciano Coelho <luciano.coelho@nokia.com> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * version 2 as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 20 * 02110-1301 USA 21 * 22 */ 23#include <stdbool.h> 24#include <stdio.h> 25#include <string.h> 26#include <stdlib.h> 27#include <getopt.h> 28#include <stddef.h> 29 30#include <xtables.h> 31#include <linux/netfilter/xt_IDLETIMER.h> 32 33enum { 34 IDLETIMER_TG_OPT_TIMEOUT = 1 << 0, 35 IDLETIMER_TG_OPT_LABEL = 1 << 1, 36}; 37 38static const struct option idletimer_tg_opts[] = { 39 {.name = "timeout", .has_arg = true, .val = 't'}, 40 {.name = "label", .has_arg = true, .val = 'l'}, 41 XT_GETOPT_TABLEEND, 42}; 43 44static void idletimer_tg_help(void) 45{ 46 printf( 47"IDLETIMER target options:\n" 48" --timeout time Timeout until the notification is sent (in seconds)\n" 49" --label string Unique rule identifier\n" 50"\n"); 51} 52 53static int idletimer_tg_parse(int c, char **argv, int invert, 54 unsigned int *flags, 55 const void *entry, 56 struct xt_entry_target **target) 57{ 58 struct idletimer_tg_info *info = 59 (struct idletimer_tg_info *)(*target)->data; 60 61 switch (c) { 62 case 't': 63 xtables_param_act(XTF_ONLY_ONCE, "IDLETIMER", "--timeout", 64 *flags & IDLETIMER_TG_OPT_TIMEOUT); 65 66 info->timeout = atoi(optarg); 67 *flags |= IDLETIMER_TG_OPT_TIMEOUT; 68 break; 69 70 case 'l': 71 xtables_param_act(XTF_ONLY_ONCE, "IDLETIMER", "--label", 72 *flags & IDLETIMER_TG_OPT_TIMEOUT); 73 74 if (strlen(optarg) > MAX_IDLETIMER_LABEL_SIZE - 1) 75 xtables_param_act(XTF_BAD_VALUE, "IDLETIMER", "--label", 76 optarg); 77 78 strcpy(info->label, optarg); 79 *flags |= IDLETIMER_TG_OPT_LABEL; 80 break; 81 } 82 83 return true; 84} 85 86static void idletimer_tg_final_check(unsigned int flags) 87{ 88 if (!(flags & IDLETIMER_TG_OPT_TIMEOUT)) 89 xtables_error(PARAMETER_PROBLEM, "IDLETIMER target: " 90 "--timeout parameter required"); 91 if (!(flags & IDLETIMER_TG_OPT_LABEL)) 92 xtables_error(PARAMETER_PROBLEM, "IDLETIMER target: " 93 "--label parameter required"); 94} 95 96static void idletimer_tg_print(const void *ip, 97 const struct xt_entry_target *target, 98 int numeric) 99{ 100 struct idletimer_tg_info *info = 101 (struct idletimer_tg_info *) target->data; 102 103 printf(" timeout:%u", info->timeout); 104 printf(" label:%s", info->label); 105} 106 107static void idletimer_tg_save(const void *ip, 108 const struct xt_entry_target *target) 109{ 110 struct idletimer_tg_info *info = 111 (struct idletimer_tg_info *) target->data; 112 113 printf(" --timeout %u", info->timeout); 114 printf(" --label %s", info->label); 115} 116 117static struct xtables_target idletimer_tg_reg = { 118 .family = NFPROTO_UNSPEC, 119 .name = "IDLETIMER", 120 .version = XTABLES_VERSION, 121 .revision = 0, 122 .size = XT_ALIGN(sizeof(struct idletimer_tg_info)), 123 .userspacesize = offsetof(struct idletimer_tg_info, timer), 124 .help = idletimer_tg_help, 125 .parse = idletimer_tg_parse, 126 .final_check = idletimer_tg_final_check, 127 .print = idletimer_tg_print, 128 .save = idletimer_tg_save, 129 .extra_opts = idletimer_tg_opts, 130}; 131 132void _init(void) 133{ 134 xtables_register_target(&idletimer_tg_reg); 135} 136