#include <stdarg.h>
#include <stdlib.h>
#include <getopt.h>
+#include <signal.h>
#include <netinet/in.h>
-#if 0
-#include "fwd_addr.h"
-#include "fwd_rules.h"
-#include "fwd_config.h"
-#endif
enum fwd_policy {
FWD_P_UNSPEC = 0,
int code;
};
-struct fwd_network_list {
+struct fwd_network {
char *name;
char *ifname;
int isalias;
struct fwd_cidr *addr;
- struct fwd_network_list *next;
+ struct fwd_network *next;
};
struct fwd_defaults {
struct fwd_zone {
char *name;
- struct fwd_network_list *networks;
+ struct fwd_network *networks;
struct fwd_data *forwardings;
struct fwd_data *redirects;
struct fwd_data *rules;
struct fwd_handle {
int rtnl_socket;
+ int unix_socket;
struct fwd_data *conf;
- struct fwd_addr_list *addrs;
};
-/* fwd_zmalloc(size_t)
- * Allocates a zeroed buffer of the given size. */
-static void * fwd_zmalloc(size_t s)
-{
- void *b = malloc(s);
-
- if( b != NULL )
- memset(b, 0, s);
-
- return b;
-}
-
/* fwd_fatal(fmt, ...)
* Prints message to stderr and termintes program. */
#define fwd_fatal(...) do { \
exit(1); \
} while(0)
-/* fwd_alloc_ptr(type)
- * Allocates a buffer with the size of the given datatype
- * and returns a pointer to it. */
-#define fwd_alloc_ptr(t) (t *) fwd_zmalloc(sizeof(t))
-
-/* fwd_free_ptr(void *)
- * Frees the given pointer and sets it to NULL.
- * Safe for NULL values. */
-#define fwd_free_ptr(x) do { if(x != NULL) free(x); x = NULL; } while(0)
#endif
#include "fwd.h"
#include "fwd_addr.h"
+#include "fwd_utils.h"
-struct fwd_addr_list * fwd_get_addrs(int fd, int family)
+struct fwd_addr * fwd_get_addrs(int fd, int family)
{
struct {
struct nlmsghdr n;
struct nlmsghdr *nlmp;
struct ifaddrmsg *rtmp;
- struct fwd_addr_list *head, *entry;
+ struct fwd_addr *head, *entry;
/* Build request */
memset(&req, 0, sizeof(req));
rtmp = (struct ifaddrmsg *) NLMSG_DATA(nlmp);
rtatp = (struct rtattr *) IFA_RTA(rtmp);
- if( !(entry = fwd_alloc_ptr(struct fwd_addr_list)) )
+ if( !(entry = fwd_alloc_ptr(struct fwd_addr)) )
goto error;
entry->index = rtmp->ifa_index;
return NULL;
}
-void fwd_free_addrs(struct fwd_addr_list *head)
+struct fwd_cidr * fwd_lookup_addr(struct fwd_addr *head, const char *ifname)
{
- struct fwd_addr_list *entry = head;
+ struct fwd_addr *entry;
+
+ for( entry = head; entry; entry = entry->next )
+ if( !strncmp(entry->ifname, ifname, IFNAMSIZ) )
+ return &entry->ipaddr;
+
+ return NULL;
+}
+
+void fwd_free_addrs(struct fwd_addr *head)
+{
+ struct fwd_addr *entry = head;
while( entry != NULL )
{
head = entry = NULL;
}
-struct fwd_addr_list * fwd_append_addrs(struct fwd_addr_list *head, struct fwd_addr_list *add)
+struct fwd_addr * fwd_append_addrs(struct fwd_addr *head, struct fwd_addr *add)
{
- struct fwd_addr_list *entry = head;
+ struct fwd_addr *entry = head;
while( entry->next != NULL )
entry = entry->next;
#include <arpa/inet.h>
-struct fwd_addr_list {
+struct fwd_addr {
char ifname[IFNAMSIZ];
char label[IFNAMSIZ];
int family;
int index;
struct fwd_cidr ipaddr;
- struct fwd_addr_list *next;
+ struct fwd_addr *next;
};
-struct fwd_addr_list * fwd_get_addrs(int, int);
-struct fwd_addr_list * fwd_append_addrs(struct fwd_addr_list *, struct fwd_addr_list *);
-void fwd_free_addrs(struct fwd_addr_list *);
+struct fwd_addr * fwd_get_addrs(int, int);
+struct fwd_addr * fwd_append_addrs(struct fwd_addr *, struct fwd_addr *);
+void fwd_free_addrs(struct fwd_addr *);
+
+struct fwd_cidr * fwd_lookup_addr(struct fwd_addr *, const char *);
#define fwd_foreach_addrs(head, entry) for(entry = head; entry; entry = entry->next)
#include "fwd.h"
#include "fwd_xtables.h"
+#include "fwd_utils.h"
/* Required by certain extensions like SNAT and DNAT */
}
void fwd_xt_parse_in(
- struct fwd_xt_rule *r, struct fwd_network_list *n, int inv
+ struct fwd_xt_rule *r, struct fwd_network *n, int inv
) {
if( n != NULL )
{
}
void fwd_xt_parse_out(
- struct fwd_xt_rule *r, struct fwd_network_list *n, int inv
+ struct fwd_xt_rule *r, struct fwd_network *n, int inv
) {
if( n != NULL )
{
struct fwd_xt_rule * fwd_xt_init_rule(struct iptc_handle *h);
void fwd_xt_parse_proto(struct fwd_xt_rule *r, struct fwd_proto *p, int inv);
-void fwd_xt_parse_in(struct fwd_xt_rule *r, struct fwd_network_list *n, int inv);
-void fwd_xt_parse_out(struct fwd_xt_rule *r, struct fwd_network_list *n, int inv);
+void fwd_xt_parse_in(struct fwd_xt_rule *r, struct fwd_network *n, int inv);
+void fwd_xt_parse_out(struct fwd_xt_rule *r, struct fwd_network *n, int inv);
void fwd_xt_parse_src(struct fwd_xt_rule *r, struct fwd_cidr *c, int inv);
void fwd_xt_parse_dest(struct fwd_xt_rule *r, struct fwd_cidr *c, int inv);
void fwd_xt_parse_frag(struct fwd_xt_rule *r, int frag, int inv);