libxt_LED.c revision 73866357e4a7a0fdc1b293bf8863fee2bd56da9e
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 <stdbool.h> 13#include <stdio.h> 14#include <string.h> 15#include <stdlib.h> 16#include <getopt.h> 17#include <stddef.h> 18 19#include <xtables.h> 20 21#include <linux/netfilter/xt_LED.h> 22 23static 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'}, 27 XT_GETOPT_TABLEEND, 28}; 29 30static void LED_help(void) 31{ 32 printf( 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" 39 ); 40} 41 42static int LED_parse(int c, char **argv, int invert, unsigned int *flags, 43 const void *entry, struct xt_entry_target **target) 44{ 45 struct xt_led_info *led = (void *)(*target)->data; 46 47 switch (c) { 48 case 'i': 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"); 57 58 /* "netfilter-" + 16 char id == 26 == sizeof(led->id) */ 59 strcpy(led->id, "netfilter-"); 60 strcat(led->id, optarg); 61 *flags = 1; 62 return true; 63 64 case 'd': 65 xtables_param_act(XTF_NO_INVERT, "LED", "--led-delay", invert); 66 if (strncasecmp(optarg, "inf", 3) == 0) 67 led->delay = -1; 68 else 69 led->delay = strtoul(optarg, NULL, 0); 70 71 return true; 72 73 case 'a': 74 if (!invert) 75 led->always_blink = 1; 76 return true; 77 } 78 return false; 79} 80 81static void LED_final_check(unsigned int flags) 82{ 83 if (flags == 0) 84 xtables_error(PARAMETER_PROBLEM, 85 "--led-trigger-id must be specified"); 86} 87 88static void LED_print(const void *ip, const struct xt_entry_target *target, 89 int numeric) 90{ 91 const struct xt_led_info *led = (void *)target->data; 92 const char *id = led->id + strlen("netfilter-"); /* trim off prefix */ 93 94 printf(" led-trigger-id:\""); 95 /* Escape double quotes and backslashes in the ID */ 96 while (*id != '\0') { 97 if (*id == '"' || *id == '\\') 98 printf("\\"); 99 printf("%c", *id++); 100 } 101 printf("\""); 102 103 if (led->delay == -1) 104 printf(" led-delay:inf"); 105 else 106 printf(" led-delay:%dms", led->delay); 107 108 if (led->always_blink) 109 printf(" led-always-blink"); 110} 111 112static void LED_save(const void *ip, const struct xt_entry_target *target) 113{ 114 const struct xt_led_info *led = (void *)target->data; 115 const char *id = led->id + strlen("netfilter-"); /* trim off prefix */ 116 117 printf(" --led-trigger-id \""); 118 /* Escape double quotes and backslashes in the ID */ 119 while (*id != '\0') { 120 if (*id == '"' || *id == '\\') 121 printf("\\"); 122 printf("%c", *id++); 123 } 124 printf("\""); 125 126 /* Only print the delay if it's not zero (the default) */ 127 if (led->delay > 0) 128 printf(" --led-delay %d", led->delay); 129 else if (led->delay == -1) 130 printf(" --led-delay inf"); 131 132 /* Only print always_blink if it's not set to the default */ 133 if (led->always_blink) 134 printf(" --led-always-blink"); 135} 136 137static struct xtables_target led_tg_reg = { 138 .version = XTABLES_VERSION, 139 .name = "LED", 140 .family = PF_UNSPEC, 141 .revision = 0, 142 .size = XT_ALIGN(sizeof(struct xt_led_info)), 143 .userspacesize = offsetof(struct xt_led_info, internal_data), 144 .help = LED_help, 145 .parse = LED_parse, 146 .final_check = LED_final_check, 147 .extra_opts = LED_opts, 148 .print = LED_print, 149 .save = LED_save, 150}; 151 152void _init(void) 153{ 154 xtables_register_target(&led_tg_reg); 155} 156