2 * libxt_LED.c - shared library add-on to iptables to add customized LED
5 * (C) 2008 Adam Nielsen <a.nielsen@shikadi.net>
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.
21 #include <linux/netfilter/xt_LED.h>
23 static const struct option LED_opts[] = {
24 {.name = "led-trigger-id", .has_arg = true, .val = 'i'},
25 {.name = "led-delay", .has_arg = true, .val = 'd'},
26 {.name = "led-always-blink", .has_arg = false, .val = 'a'},
30 static void LED_help(void)
33 "LED target options:\n"
34 "--led-trigger-id name suffix for led trigger name\n"
35 "--led-delay ms leave the LED on for this number of\n"
36 " milliseconds after triggering.\n"
37 "--led-always-blink blink on arriving packets, even if\n"
38 " the LED is already on.\n"
42 static int LED_parse(int c, char **argv, int invert, unsigned int *flags,
43 const void *entry, struct xt_entry_target **target)
45 struct xt_led_info *led = (void *)(*target)->data;
49 xtables_param_act(XTF_NO_INVERT, "LED",
50 "--led-trigger-id", invert);
51 if (strlen("netfilter-") + strlen(optarg) > sizeof(led->id))
52 xtables_error(PARAMETER_PROBLEM,
53 "--led-trigger-id must be 16 chars or less");
54 if (optarg[0] == '\0')
55 xtables_error(PARAMETER_PROBLEM,
56 "--led-trigger-id cannot be blank");
58 /* "netfilter-" + 16 char id == 26 == sizeof(led->id) */
59 strcpy(led->id, "netfilter-");
60 strcat(led->id, optarg);
65 xtables_param_act(XTF_NO_INVERT, "LED", "--led-delay", invert);
66 if (strncasecmp(optarg, "inf", 3) == 0)
69 led->delay = strtoul(optarg, NULL, 0);
75 led->always_blink = 1;
81 static void LED_final_check(unsigned int flags)
84 xtables_error(PARAMETER_PROBLEM,
85 "--led-trigger-id must be specified");
88 static void LED_print(const void *ip, const struct xt_entry_target *target,
91 const struct xt_led_info *led = (void *)target->data;
92 const char *id = led->id + strlen("netfilter-"); /* trim off prefix */
94 printf("led-trigger-id:\"");
95 /* Escape double quotes and backslashes in the ID */
97 if (*id == '"' || *id == '\\')
103 if (led->delay == -1)
104 printf("led-delay:inf ");
106 printf("led-delay:%dms ", led->delay);
108 if (led->always_blink)
109 printf("led-always-blink ");
112 static void LED_save(const void *ip, const struct xt_entry_target *target)
114 const struct xt_led_info *led = (void *)target->data;
115 const char *id = led->id + strlen("netfilter-"); /* trim off prefix */
117 printf("--led-trigger-id \"");
118 /* Escape double quotes and backslashes in the ID */
119 while (*id != '\0') {
120 if (*id == '"' || *id == '\\')
126 /* Only print the delay if it's not zero (the default) */
128 printf("--led-delay %d ", led->delay);
129 else if (led->delay == -1)
130 printf("--led-delay inf ");
132 /* Only print always_blink if it's not set to the default */
133 if (led->always_blink)
134 printf("--led-always-blink ");
137 static struct xtables_target led_tg_reg = {
138 .version = XTABLES_VERSION,
142 .size = XT_ALIGN(sizeof(struct xt_led_info)),
143 .userspacesize = offsetof(struct xt_led_info, internal_data),
146 .final_check = LED_final_check,
147 .extra_opts = LED_opts,
154 xtables_register_target(&led_tg_reg);