libxt_IDLETIMER.c revision 67195a8c8a03d12994e91315e49e3d78c51a385a
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
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, .flag = 0, .val = 't' },
40	{ .name = "label",   .has_arg = true, .flag = 0, .val = 'l' },
41	{ .name = NULL }
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	default:
83		return false;
84	}
85
86	return true;
87}
88
89static void idletimer_tg_final_check(unsigned int flags)
90{
91	if (!(flags & IDLETIMER_TG_OPT_TIMEOUT))
92		xtables_error(PARAMETER_PROBLEM, "IDLETIMER target: "
93			      "--timeout parameter required");
94	if (!(flags & IDLETIMER_TG_OPT_LABEL))
95		xtables_error(PARAMETER_PROBLEM, "IDLETIMER target: "
96			      "--label parameter required");
97}
98
99static void idletimer_tg_print(const void *ip,
100			       const struct xt_entry_target *target,
101			       int numeric)
102{
103	struct idletimer_tg_info *info =
104		(struct idletimer_tg_info *) target->data;
105
106	printf("timeout:%u ", info->timeout);
107	printf("label:%s ", info->label);
108}
109
110static void idletimer_tg_save(const void *ip,
111			      const struct xt_entry_target *target)
112{
113	struct idletimer_tg_info *info =
114		(struct idletimer_tg_info *) target->data;
115
116	printf("--timeout %u ", info->timeout);
117	printf("--label %s ", info->label);
118}
119
120static struct xtables_target idletimer_tg_reg = {
121	.family	       = NFPROTO_UNSPEC,
122	.name	       = "IDLETIMER",
123	.version       = XTABLES_VERSION,
124	.revision      = 0,
125	.size	       = XT_ALIGN(sizeof(struct idletimer_tg_info)),
126	.userspacesize = offsetof(struct idletimer_tg_info, timer),
127	.help	       = idletimer_tg_help,
128	.parse	       = idletimer_tg_parse,
129	.final_check   = idletimer_tg_final_check,
130	.print	       = idletimer_tg_print,
131	.save	       = idletimer_tg_save,
132	.extra_opts    = idletimer_tg_opts,
133};
134
135static __attribute__((constructor)) void idletimer_tg_ldr(void)
136{
137	xtables_register_target(&idletimer_tg_reg);
138}
139