redirects: fix segmentation fault
[oweals/firewall3.git] / xtables-10.h
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
19 #ifndef __FW3_XTABLES_10_H
20 #define __FW3_XTABLES_10_H
21
22 extern struct xtables_match *xtables_pending_matches;
23 extern struct xtables_target *xtables_pending_targets;
24
25 static inline void
26 fw3_xt_reset(void)
27 {
28         xtables_matches = NULL;
29         xtables_targets = NULL;
30
31         xtables_pending_matches = NULL;
32         xtables_pending_targets = NULL;
33 }
34
35
36 static inline const char *
37 fw3_xt_get_match_name(struct xtables_match *m)
38 {
39     if (m->alias)
40         return m->alias(m->m);
41
42     return m->m->u.user.name;
43 }
44
45 static inline void
46 fw3_xt_set_match_name(struct xtables_match *m)
47 {
48     snprintf(m->m->u.user.name, sizeof(m->m->u.user.name), "%s",
49              m->real_name ? m->real_name : m->name);
50 }
51
52 static inline bool
53 fw3_xt_has_match_parse(struct xtables_match *m)
54 {
55     return (m->parse || m->x6_parse);
56 }
57
58 static inline void
59 fw3_xt_free_match_udata(struct xtables_match *m)
60 {
61     if (m->udata_size)
62     {
63         free(m->udata);
64         m->udata = fw3_alloc(m->udata_size);
65     }
66 }
67
68 static inline void
69 fw3_xt_merge_match_options(struct xtables_globals *g, struct xtables_match *m)
70 {
71         if (m->x6_options)
72                 g->opts = xtables_options_xfrm(g->orig_opts, g->opts,
73                                                                            m->x6_options, &m->option_offset);
74
75         if (m->extra_opts)
76                 g->opts = xtables_merge_options(g->orig_opts, g->opts,
77                                                                                 m->extra_opts, &m->option_offset);
78 }
79
80
81 static inline const char *
82 fw3_xt_get_target_name(struct xtables_target *t)
83 {
84     if (t->alias)
85         return t->alias(t->t);
86
87     return t->t->u.user.name;
88 }
89
90 static inline void
91 fw3_xt_set_target_name(struct xtables_target *t, const char *name)
92 {
93     snprintf(t->t->u.user.name, sizeof(t->t->u.user.name), "%s",
94              t->real_name ? t->real_name : name);
95 }
96
97 static inline bool
98 fw3_xt_has_target_parse(struct xtables_target *t)
99 {
100     return (t->parse || t->x6_parse);
101 }
102
103 static inline void
104 fw3_xt_free_target_udata(struct xtables_target *t)
105 {
106     if (t->udata_size)
107     {
108         free(t->udata);
109         t->udata = fw3_alloc(t->udata_size);
110     }
111 }
112
113 static inline void
114 fw3_xt_merge_target_options(struct xtables_globals *g, struct xtables_target *t)
115 {
116         if (t->x6_options)
117                 g->opts = xtables_options_xfrm(g->orig_opts, g->opts,
118                                                t->x6_options, &t->option_offset);
119         else
120                 g->opts = xtables_merge_options(g->orig_opts, g->opts,
121                                                 t->extra_opts, &t->option_offset);
122 }
123
124 static inline void
125 fw3_xt_print_matches(void *ip, struct xtables_rule_match *matches)
126 {
127         struct xtables_rule_match *rm;
128         struct xtables_match *m;
129
130         for (rm = matches; rm; rm = rm->next)
131         {
132                 m = rm->match;
133                 printf(" -m %s", fw3_xt_get_match_name(m));
134
135                 if (m->save)
136                         m->save(ip, m->m);
137         }
138 }
139
140 static inline void
141 fw3_xt_print_target(void *ip, struct xtables_target *target)
142 {
143         if (target)
144         {
145                 printf(" -j %s", fw3_xt_get_target_name(target));
146
147                 if (target->save)
148                         target->save(ip, target->t);
149         }
150 }
151
152 #endif