iptables: fix regression with unintended free in need_protomatch
[project/firewall3.git] / utils.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_UTILS_H
20 #define __FW3_UTILS_H
21
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <sys/file.h>
31 #include <sys/types.h>
32 #include <ifaddrs.h>
33
34 #include <libubox/list.h>
35 #include <libubox/blob.h>
36 #include <uci.h>
37
38
39 #define FW3_STATEFILE "/var/run/fw3.state"
40 #define FW3_LOCKFILE "/var/run/fw3.lock"
41 #define FW3_HOTPLUG "/sbin/hotplug-call"
42
43 extern bool fw3_pr_debug;
44
45 void warn_elem(struct uci_element *e, const char *format, ...);
46 void warn(const char *format, ...);
47 void error(const char *format, ...);
48 void info(const char *format, ...);
49
50
51 #define warn_section(t, r, e, fmt, ...) \
52 do { \
53 if (e) \
54 warn_elem(e, fmt, ##__VA_ARGS__); \
55 else \
56 warn("Warning: ubus " t " (%s) " fmt, \
57 (r && r->name) ? r->name : "?", ##__VA_ARGS__); \
58 } while(0)
59
60 #define fw3_setbit(field, flag) field |= (1 << (flag))
61 #define fw3_delbit(field, flag) field &= ~(1 << (flag))
62 #define fw3_hasbit(field, flag) (field & (1 << (flag)))
63
64 #define set(field, family, flag) fw3_setbit(field[family == FW3_FAMILY_V6], flag)
65 #define del(field, family, flag) fw3_delbit(field[family == FW3_FAMILY_V6], flag)
66 #define has(field, family, flag) fw3_hasbit(field[family == FW3_FAMILY_V6], flag)
67
68 #define fw3_foreach(p, h) \
69 for (p = list_empty(h) ? NULL : list_first_entry(h, typeof(*p), list); \
70 list_empty(h) ? (p == NULL) : (&p->list != (h)); \
71 p = list_empty(h) ? list_first_entry(h, typeof(*p), list) \
72 : list_entry(p->list.next, typeof(*p), list))
73
74 #define fw3_is_family(p, f) \
75 (!p || (p)->family == FW3_FAMILY_ANY || (p)->family == f)
76
77 #define fw3_no_family(flags) \
78 (!(flags & ((1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6))))
79
80 #define fw3_no_table(flags) \
81 (!(flags & ((1<<FW3_TABLE_FILTER)|(1<<FW3_TABLE_NAT)| \
82 (1<<FW3_TABLE_MANGLE)|(1<<FW3_TABLE_RAW))))
83
84
85 void * fw3_alloc(size_t size);
86 char * fw3_strdup(const char *s);
87
88 const char * fw3_find_command(const char *cmd);
89
90 bool fw3_stdout_pipe(void);
91 bool __fw3_command_pipe(bool silent, const char *command, ...);
92 #define fw3_command_pipe(...) __fw3_command_pipe(__VA_ARGS__, NULL)
93
94 void fw3_command_close(void);
95 void fw3_pr(const char *fmt, ...);
96
97 bool fw3_has_table(bool ipv6, const char *table);
98
99 bool fw3_lock(void);
100 void fw3_unlock(void);
101
102
103 void fw3_write_statefile(void *state);
104
105 void fw3_free_object(void *obj, const void *opts);
106
107 void fw3_free_list(struct list_head *head);
108
109 bool fw3_hotplug(bool add, void *zone, void *device);
110
111 int fw3_netmask2bitlen(int family, void *mask);
112
113 bool fw3_bitlen2netmask(int family, int bits, void *mask);
114
115 void fw3_flush_conntrack(void *zone);
116
117 bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const char **type);
118
119 #endif