libxt_LED.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. enum {
  18. O_LED_TRIGGER_ID = 0,
  19. O_LED_DELAY,
  20. O_LED_ALWAYS_BLINK,
  21. };
  22. #define s struct xt_led_info
  23. static const struct xt_option_entry LED_opts[] = {
  24. {.name = "led-trigger-id", .id = O_LED_TRIGGER_ID,
  25. .flags = XTOPT_MAND, .type = XTTYPE_STRING, .min = 0,
  26. .max = sizeof(((struct xt_led_info *)NULL)->id) -
  27. sizeof("netfilter-")},
  28. {.name = "led-delay", .id = O_LED_DELAY, .type = XTTYPE_STRING},
  29. {.name = "led-always-blink", .id = O_LED_ALWAYS_BLINK,
  30. .type = XTTYPE_NONE},
  31. XTOPT_TABLEEND,
  32. };
  33. #undef s
  34. static void LED_help(void)
  35. {
  36. printf(
  37. "LED target options:\n"
  38. "--led-trigger-id name suffix for led trigger name\n"
  39. "--led-delay ms leave the LED on for this number of\n"
  40. " milliseconds after triggering.\n"
  41. "--led-always-blink blink on arriving packets, even if\n"
  42. " the LED is already on.\n"
  43. );
  44. }
  45. static void LED_parse(struct xt_option_call *cb)
  46. {
  47. struct xt_led_info *led = cb->data;
  48. unsigned int delay;
  49. xtables_option_parse(cb);
  50. switch (cb->entry->id) {
  51. case O_LED_TRIGGER_ID:
  52. strcpy(led->id, "netfilter-");
  53. strcat(led->id, cb->arg);
  54. break;
  55. case O_LED_DELAY:
  56. if (strncasecmp(cb->arg, "inf", 3) == 0)
  57. led->delay = -1;
  58. else if (!xtables_strtoui(cb->arg, NULL, &delay, 0, UINT32_MAX))
  59. xtables_error(PARAMETER_PROBLEM,
  60. "Delay value must be within range 0..%u",
  61. UINT32_MAX);
  62. break;
  63. case O_LED_ALWAYS_BLINK:
  64. led->always_blink = 1;
  65. break;
  66. }
  67. }
  68. static void LED_print(const void *ip, const struct xt_entry_target *target,
  69. int numeric)
  70. {
  71. const struct xt_led_info *led = (void *)target->data;
  72. const char *id = led->id + strlen("netfilter-"); /* trim off prefix */
  73. printf(" led-trigger-id:\"");
  74. /* Escape double quotes and backslashes in the ID */
  75. while (*id != '\0') {
  76. if (*id == '"' || *id == '\\')
  77. printf("\\");
  78. printf("%c", *id++);
  79. }
  80. printf("\"");
  81. if (led->delay == -1)
  82. printf(" led-delay:inf");
  83. else
  84. printf(" led-delay:%dms", led->delay);
  85. if (led->always_blink)
  86. printf(" led-always-blink");
  87. }
  88. static void LED_save(const void *ip, const struct xt_entry_target *target)
  89. {
  90. const struct xt_led_info *led = (void *)target->data;
  91. const char *id = led->id + strlen("netfilter-"); /* trim off prefix */
  92. printf(" --led-trigger-id \"");
  93. /* Escape double quotes and backslashes in the ID */
  94. while (*id != '\0') {
  95. if (*id == '"' || *id == '\\')
  96. printf("\\");
  97. printf("%c", *id++);
  98. }
  99. printf("\"");
  100. /* Only print the delay if it's not zero (the default) */
  101. if (led->delay > 0)
  102. printf(" --led-delay %d", led->delay);
  103. else if (led->delay == -1)
  104. printf(" --led-delay inf");
  105. /* Only print always_blink if it's not set to the default */
  106. if (led->always_blink)
  107. printf(" --led-always-blink");
  108. }
  109. static struct xtables_target led_tg_reg = {
  110. .version = XTABLES_VERSION,
  111. .name = "LED",
  112. .family = PF_UNSPEC,
  113. .revision = 0,
  114. .size = XT_ALIGN(sizeof(struct xt_led_info)),
  115. .userspacesize = offsetof(struct xt_led_info, internal_data),
  116. .help = LED_help,
  117. .print = LED_print,
  118. .save = LED_save,
  119. .x6_parse = LED_parse,
  120. .x6_options = LED_opts,
  121. };
  122. void _init(void)
  123. {
  124. xtables_register_target(&led_tg_reg);
  125. }