libxt_LED.c revision a19988f2795770ce470562c1795e1cf53e3aa54b
1/*
2 * libxt_LED.c - shared library add-on to iptables to add customized LED
3 *               trigger support.
4 *
5 * (C) 2008 Adam Nielsen <a.nielsen@shikadi.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12#include <stdio.h>
13#include <string.h>
14#include <stdlib.h>
15#include <xtables.h>
16#include <linux/netfilter/xt_LED.h>
17
18enum {
19	O_LED_TRIGGER_ID = 0,
20	O_LED_DELAY,
21	O_LED_ALWAYS_BLINK,
22};
23
24#define s struct xt_led_info
25static const struct xt_option_entry LED_opts[] = {
26	{.name = "led-trigger-id", .id = O_LED_TRIGGER_ID,
27	 .flags = XTOPT_MAND, .type = XTTYPE_STRING, .min = 0,
28	 .max = sizeof(((struct xt_led_info *)NULL)->id) -
29	        sizeof("netfilter-")},
30	{.name = "led-delay", .id = O_LED_DELAY, .type = XTTYPE_STRING},
31	{.name = "led-always-blink", .id = O_LED_ALWAYS_BLINK,
32	 .type = XTTYPE_NONE},
33	XTOPT_TABLEEND,
34};
35#undef s
36
37static void LED_help(void)
38{
39	printf(
40"LED target options:\n"
41"--led-trigger-id name           suffix for led trigger name\n"
42"--led-delay ms                  leave the LED on for this number of\n"
43"                                milliseconds after triggering.\n"
44"--led-always-blink              blink on arriving packets, even if\n"
45"                                the LED is already on.\n"
46	);
47}
48
49static void LED_parse(struct xt_option_call *cb)
50{
51	struct xt_led_info *led = cb->data;
52	unsigned int delay;
53
54	xtables_option_parse(cb);
55	switch (cb->entry->id) {
56	case O_LED_TRIGGER_ID:
57		strcpy(led->id, "netfilter-");
58		strcat(led->id, cb->arg);
59		break;
60	case O_LED_DELAY:
61		if (strncasecmp(cb->arg, "inf", 3) == 0)
62			led->delay = -1;
63		else if (!xtables_strtoui(cb->arg, NULL, &delay, 0, UINT32_MAX))
64			xtables_error(PARAMETER_PROBLEM,
65				"Delay value must be within range 0..%u",
66				UINT32_MAX);
67		break;
68	case O_LED_ALWAYS_BLINK:
69		led->always_blink = 1;
70		break;
71	}
72}
73
74static void LED_print(const void *ip, const struct xt_entry_target *target,
75		      int numeric)
76{
77	const struct xt_led_info *led = (void *)target->data;
78	const char *id = led->id + strlen("netfilter-"); /* trim off prefix */
79
80	printf(" led-trigger-id:\"");
81	/* Escape double quotes and backslashes in the ID */
82	while (*id != '\0') {
83		if (*id == '"' || *id == '\\')
84			printf("\\");
85		printf("%c", *id++);
86	}
87	printf("\"");
88
89	if (led->delay == -1)
90		printf(" led-delay:inf");
91	else
92		printf(" led-delay:%dms", led->delay);
93
94	if (led->always_blink)
95		printf(" led-always-blink");
96}
97
98static void LED_save(const void *ip, const struct xt_entry_target *target)
99{
100	const struct xt_led_info *led = (void *)target->data;
101	const char *id = led->id + strlen("netfilter-"); /* trim off prefix */
102
103	printf(" --led-trigger-id \"");
104	/* Escape double quotes and backslashes in the ID */
105	while (*id != '\0') {
106		if (*id == '"' || *id == '\\')
107			printf("\\");
108		printf("%c", *id++);
109	}
110	printf("\"");
111
112	/* Only print the delay if it's not zero (the default) */
113	if (led->delay > 0)
114		printf(" --led-delay %d", led->delay);
115	else if (led->delay == -1)
116		printf(" --led-delay inf");
117
118	/* Only print always_blink if it's not set to the default */
119	if (led->always_blink)
120		printf(" --led-always-blink");
121}
122
123static struct xtables_target led_tg_reg = {
124	.version       = XTABLES_VERSION,
125	.name          = "LED",
126	.family        = PF_UNSPEC,
127	.revision      = 0,
128	.size          = XT_ALIGN(sizeof(struct xt_led_info)),
129	.userspacesize = offsetof(struct xt_led_info, internal_data),
130	.help          = LED_help,
131	.print         = LED_print,
132	.save          = LED_save,
133	.x6_parse      = LED_parse,
134	.x6_options    = LED_opts,
135};
136
137void _init(void)
138{
139	xtables_register_target(&led_tg_reg);
140}
141