contrib/fwd: add utility functions
[project/luci.git] / contrib / fwd / src / fwd_utils.c
1 /*
2 * fwd - OpenWrt firewall daemon - commmon utility functions
3 *
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * The fwd program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * The fwd program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with the fwd program. If not, see http://www.gnu.org/licenses/.
17 */
18
19
20 #include "fwd_utils.h"
21
22
23 void fwd_log_init(void)
24 {
25 openlog("Firewall", 0, LOG_DAEMON | LOG_PERROR);
26 }
27
28 void __fwd_log(int prio, const char *msg, ...)
29 {
30 va_list ap;
31 va_start(ap, msg);
32 vsyslog(prio, msg, ap);
33 va_end(ap);
34 }
35
36
37
38 int fwd_empty_cidr(struct fwd_cidr *c)
39 {
40 if( (c == NULL) || ((c->addr.s_addr == 0) && (c->prefix == 0)) )
41 return 1;
42
43 return 0;
44 }
45
46 int fwd_equal_cidr(struct fwd_cidr *a, struct fwd_cidr *b)
47 {
48 if( fwd_empty_cidr(a) && fwd_empty_cidr(b) )
49 return 1;
50 else if( (a->addr.s_addr == b->addr.s_addr) && (a->prefix == b->prefix) )
51 return 1;
52
53 return 0;
54 }
55
56 void fwd_update_cidr(struct fwd_cidr *a, struct fwd_cidr *b)
57 {
58 if( a != NULL )
59 {
60 a->addr.s_addr = b ? b->addr.s_addr : 0;
61 a->prefix = b ? b->prefix : 0;
62 }
63 }
64
65
66 /* fwd_zmalloc(size_t)
67 * Allocates a zeroed buffer of the given size. */
68 void * fwd_zmalloc(size_t s)
69 {
70 void *b = malloc(s);
71
72 if( b != NULL )
73 memset(b, 0, s);
74
75 return b;
76 }
77
78