libxt_IDLETIMER.c revision d96993e50b44b358ea5bd15f3944674eafd62542
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		if (*flags & IDLETIMER_TG_OPT_TIMEOUT)
64			xtables_error(PARAMETER_PROBLEM,
65				      "Cannot specify timeout more than once");
66
67		info->timeout = atoi(optarg);
68		*flags |= IDLETIMER_TG_OPT_TIMEOUT;
69		break;
70
71	case 'l':
72		if (*flags & IDLETIMER_TG_OPT_LABEL)
73			xtables_error(PARAMETER_PROBLEM,
74				      "Cannot specify label more than once");
75
76		if (strlen(optarg) > MAX_IDLETIMER_LABEL_SIZE - 1)
77			xtables_error(PARAMETER_PROBLEM,
78				      "Maximum label length is %u for --label",
79				      MAX_IDLETIMER_LABEL_SIZE - 1);
80
81		strcpy(info->label, optarg);
82		*flags |= IDLETIMER_TG_OPT_LABEL;
83		break;
84
85	default:
86		return false;
87	}
88
89	return true;
90}
91
92static void idletimer_tg_final_check(unsigned int flags)
93{
94	if (!(flags & IDLETIMER_TG_OPT_TIMEOUT))
95		xtables_error(PARAMETER_PROBLEM, "IDLETIMER target: "
96			      "--timeout parameter required");
97	if (!(flags & IDLETIMER_TG_OPT_LABEL))
98		xtables_error(PARAMETER_PROBLEM, "IDLETIMER target: "
99			      "--label parameter required");
100}
101
102static void idletimer_tg_print(const void *ip,
103			       const struct xt_entry_target *target,
104			       int numeric)
105{
106	struct idletimer_tg_info *info =
107		(struct idletimer_tg_info *) target->data;
108
109	printf("timeout:%u ", info->timeout);
110	printf("label:%s ", info->label);
111}
112
113static void idletimer_tg_save(const void *ip,
114			      const struct xt_entry_target *target)
115{
116	struct idletimer_tg_info *info =
117		(struct idletimer_tg_info *) target->data;
118
119	printf("--timeout %u ", info->timeout);
120	printf("--label %s ", info->label);
121}
122
123static struct xtables_target idletimer_tg_reg = {
124	.family	       = NFPROTO_UNSPEC,
125	.name	       = "IDLETIMER",
126	.version       = XTABLES_VERSION,
127	.revision      = 0,
128	.size	       = XT_ALIGN(sizeof(struct idletimer_tg_info)),
129	.userspacesize = offsetof(struct idletimer_tg_info, timer),
130	.help	       = idletimer_tg_help,
131	.parse	       = idletimer_tg_parse,
132	.final_check   = idletimer_tg_final_check,
133	.print	       = idletimer_tg_print,
134	.save	       = idletimer_tg_save,
135	.extra_opts    = idletimer_tg_opts,
136};
137
138static __attribute__((constructor)) void idletimer_tg_ldr(void)
139{
140	xtables_register_target(&idletimer_tg_reg);
141}
142