libxt_nfacct.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * (C) 2011 by Pablo Neira Ayuso <pablo@netfilter.org>
  3. * (C) 2011 by Intra2Net AG <http://www.intra2net.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 (or
  7. * any later at your option) as published by the Free Software Foundation.
  8. */
  9. #include <stdbool.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <getopt.h>
  14. #include <xtables.h>
  15. #include <linux/netfilter/x_tables.h>
  16. #include <linux/netfilter/xt_nfacct.h>
  17. enum {
  18. O_NAME = 0,
  19. };
  20. #define s struct xt_nfacct_match_info
  21. static const struct xt_option_entry nfacct_opts[] = {
  22. {.name = "nfacct-name", .id = O_NAME, .type = XTTYPE_STRING,
  23. .min = 1, .flags = XTOPT_MAND|XTOPT_PUT, XTOPT_POINTER(s, name)},
  24. XTOPT_TABLEEND,
  25. };
  26. #undef s
  27. static void nfacct_help(void)
  28. {
  29. printf("nfacct match options:\n"
  30. " --nfacct-name STRING Name of accouting area\n");
  31. }
  32. static void nfacct_parse(struct xt_option_call *cb)
  33. {
  34. xtables_option_parse(cb);
  35. switch (cb->entry->id) {
  36. case O_NAME:
  37. if (strchr(cb->arg, '\n') != NULL)
  38. xtables_error(PARAMETER_PROBLEM,
  39. "Newlines not allowed in --nfacct-name");
  40. break;
  41. }
  42. }
  43. static void
  44. nfacct_print_name(const struct xt_nfacct_match_info *info, char *name)
  45. {
  46. printf(" %snfacct-name ", name);
  47. xtables_save_string(info->name);
  48. }
  49. static void nfacct_print(const void *ip, const struct xt_entry_match *match,
  50. int numeric)
  51. {
  52. const struct xt_nfacct_match_info *info =
  53. (struct xt_nfacct_match_info *)match->data;
  54. nfacct_print_name(info, "");
  55. }
  56. static void nfacct_save(const void *ip, const struct xt_entry_match *match)
  57. {
  58. const struct xt_nfacct_match_info *info =
  59. (struct xt_nfacct_match_info *)match->data;
  60. nfacct_print_name(info, "--");
  61. }
  62. static struct xtables_match nfacct_match = {
  63. .family = NFPROTO_UNSPEC,
  64. .name = "nfacct",
  65. .version = XTABLES_VERSION,
  66. .size = XT_ALIGN(sizeof(struct xt_nfacct_match_info)),
  67. .userspacesize = offsetof(struct xt_nfacct_match_info, nfacct),
  68. .help = nfacct_help,
  69. .x6_parse = nfacct_parse,
  70. .print = nfacct_print,
  71. .save = nfacct_save,
  72. .x6_options = nfacct_opts,
  73. };
  74. void _init(void)
  75. {
  76. xtables_register_match(&nfacct_match);
  77. }