diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2009-12-20 02:30:32 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2009-12-20 02:30:32 +0000 |
commit | 1c2d85856bfcc6766073ac7466ead76b225e8ccf (patch) | |
tree | 75191665e7cf4bc8ec88abb4c1412fa2303090ce /contrib/fwd/src/fwd_utils.c | |
parent | ea9a0495b7b45d3c21ceb9bb7254f42826cc6a35 (diff) |
contrib/fwd: add utility functions
Diffstat (limited to 'contrib/fwd/src/fwd_utils.c')
-rw-r--r-- | contrib/fwd/src/fwd_utils.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/contrib/fwd/src/fwd_utils.c b/contrib/fwd/src/fwd_utils.c new file mode 100644 index 0000000000..170ecf4686 --- /dev/null +++ b/contrib/fwd/src/fwd_utils.c @@ -0,0 +1,78 @@ +/* + * fwd - OpenWrt firewall daemon - commmon utility functions + * + * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org> + * + * The fwd program is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * The fwd program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with the fwd program. If not, see http://www.gnu.org/licenses/. + */ + + +#include "fwd_utils.h" + + +void fwd_log_init(void) +{ + openlog("Firewall", 0, LOG_DAEMON | LOG_PERROR); +} + +void __fwd_log(int prio, const char *msg, ...) +{ + va_list ap; + va_start(ap, msg); + vsyslog(prio, msg, ap); + va_end(ap); +} + + + +int fwd_empty_cidr(struct fwd_cidr *c) +{ + if( (c == NULL) || ((c->addr.s_addr == 0) && (c->prefix == 0)) ) + return 1; + + return 0; +} + +int fwd_equal_cidr(struct fwd_cidr *a, struct fwd_cidr *b) +{ + if( fwd_empty_cidr(a) && fwd_empty_cidr(b) ) + return 1; + else if( (a->addr.s_addr == b->addr.s_addr) && (a->prefix == b->prefix) ) + return 1; + + return 0; +} + +void fwd_update_cidr(struct fwd_cidr *a, struct fwd_cidr *b) +{ + if( a != NULL ) + { + a->addr.s_addr = b ? b->addr.s_addr : 0; + a->prefix = b ? b->prefix : 0; + } +} + + +/* fwd_zmalloc(size_t) + * Allocates a zeroed buffer of the given size. */ +void * fwd_zmalloc(size_t s) +{ + void *b = malloc(s); + + if( b != NULL ) + memset(b, 0, s); + + return b; +} + + |