xtables-10.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * firewall3 - 3rd OpenWrt UCI firewall implementation
  3. *
  4. * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef __FW3_XTABLES_10_H
  19. #define __FW3_XTABLES_10_H
  20. extern struct xtables_match *xtables_pending_matches;
  21. extern struct xtables_target *xtables_pending_targets;
  22. static inline void
  23. fw3_xt_reset(void)
  24. {
  25. xtables_matches = NULL;
  26. xtables_targets = NULL;
  27. xtables_pending_matches = NULL;
  28. xtables_pending_targets = NULL;
  29. }
  30. static inline const char *
  31. fw3_xt_get_match_name(struct xtables_match *m)
  32. {
  33. if (m->alias)
  34. return m->alias(m->m);
  35. return m->m->u.user.name;
  36. }
  37. static inline void
  38. fw3_xt_set_match_name(struct xtables_match *m)
  39. {
  40. snprintf(m->m->u.user.name, sizeof(m->m->u.user.name), "%s",
  41. m->real_name ? m->real_name : m->name);
  42. }
  43. static inline bool
  44. fw3_xt_has_match_parse(struct xtables_match *m)
  45. {
  46. return (m->parse || m->x6_parse);
  47. }
  48. static inline void
  49. fw3_xt_free_match_udata(struct xtables_match *m)
  50. {
  51. if (m->udata_size)
  52. {
  53. free(m->udata);
  54. m->udata = fw3_alloc(m->udata_size);
  55. }
  56. }
  57. static inline void
  58. fw3_xt_merge_match_options(struct xtables_globals *g, struct xtables_match *m)
  59. {
  60. if (m->x6_options)
  61. g->opts = xtables_options_xfrm(g->orig_opts, g->opts,
  62. m->x6_options, &m->option_offset);
  63. if (m->extra_opts)
  64. g->opts = xtables_merge_options(g->orig_opts, g->opts,
  65. m->extra_opts, &m->option_offset);
  66. }
  67. static inline const char *
  68. fw3_xt_get_target_name(struct xtables_target *t)
  69. {
  70. if (t->alias)
  71. return t->alias(t->t);
  72. return t->t->u.user.name;
  73. }
  74. static inline void
  75. fw3_xt_set_target_name(struct xtables_target *t, const char *name)
  76. {
  77. snprintf(t->t->u.user.name, sizeof(t->t->u.user.name), "%s",
  78. t->real_name ? t->real_name : name);
  79. }
  80. static inline bool
  81. fw3_xt_has_target_parse(struct xtables_target *t)
  82. {
  83. return (t->parse || t->x6_parse);
  84. }
  85. static inline void
  86. fw3_xt_free_target_udata(struct xtables_target *t)
  87. {
  88. if (t->udata_size)
  89. {
  90. free(t->udata);
  91. t->udata = fw3_alloc(t->udata_size);
  92. }
  93. }
  94. static inline void
  95. fw3_xt_merge_target_options(struct xtables_globals *g, struct xtables_target *t)
  96. {
  97. if (t->x6_options)
  98. g->opts = xtables_options_xfrm(g->orig_opts, g->opts,
  99. t->x6_options, &t->option_offset);
  100. else
  101. g->opts = xtables_merge_options(g->orig_opts, g->opts,
  102. t->extra_opts, &t->option_offset);
  103. }
  104. static inline void
  105. fw3_xt_print_matches(void *ip, struct xtables_rule_match *matches)
  106. {
  107. struct xtables_rule_match *rm;
  108. struct xtables_match *m;
  109. for (rm = matches; rm; rm = rm->next)
  110. {
  111. m = rm->match;
  112. printf(" -m %s", fw3_xt_get_match_name(m));
  113. if (m->save)
  114. m->save(ip, m->m);
  115. }
  116. }
  117. static inline void
  118. fw3_xt_print_target(void *ip, struct xtables_target *target)
  119. {
  120. if (target)
  121. {
  122. printf(" -j %s", fw3_xt_get_target_name(target));
  123. if (target->save)
  124. target->save(ip, target->t);
  125. }
  126. }
  127. #endif