utils: Fix string format message
[project/firewall3.git] / utils.c
diff --git a/utils.c b/utils.c
index 4f892a7aa465bae393f7266a9773971d2bb5703c..d9b1f7dc2d78e827786fe36c1a928e2aa3a6613c 100644 (file)
--- a/utils.c
+++ b/utils.c
  */
 
 #define _GNU_SOURCE
+
+#include <net/if.h>
+#include <sys/ioctl.h>
+
 #include "utils.h"
 #include "options.h"
 
@@ -24,7 +28,7 @@
 #include "ipsets.h"
 
 
-static int lock_fd = -1;
+static int fw3_lock_fd = -1;
 static pid_t pipe_pid = -1;
 static FILE *pipe_fd = NULL;
 
@@ -141,7 +145,7 @@ fw3_alloc(size_t size)
        mem = calloc(1, size);
 
        if (!mem)
-               error("Out of memory while allocating %d bytes", size);
+               error("Out of memory while allocating %zd bytes", size);
 
        return mem;
 }
@@ -252,6 +256,7 @@ __fw3_command_pipe(bool silent, const char *command, ...)
        switch ((pid = fork()))
        {
        case -1:
+               free(args);
                return false;
 
        case 0:
@@ -275,6 +280,7 @@ __fw3_command_pipe(bool silent, const char *command, ...)
        }
 
        pipe_fd = fdopen(pfds[1], "w");
+       free(args);
        return true;
 }
 
@@ -340,13 +346,13 @@ fw3_has_table(bool ipv6, const char *table)
 
 
 bool
-fw3_lock(void)
+fw3_lock_path(int *fd, const char *path)
 {
-       lock_fd = open(FW3_LOCKFILE, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
+       int lock_fd = open(path, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
 
        if (lock_fd < 0)
        {
-               warn("Cannot create lock file %s: %s", FW3_LOCKFILE, strerror(errno));
+               warn("Cannot create lock file %s: %s", path, strerror(errno));
                return false;
        }
 
@@ -356,22 +362,38 @@ fw3_lock(void)
                return false;
        }
 
+       *fd = lock_fd;
+
        return true;
 }
 
+bool
+fw3_lock()
+{
+       return fw3_lock_path(&fw3_lock_fd, FW3_LOCKFILE);
+}
+
+
 void
-fw3_unlock(void)
+fw3_unlock_path(int *fd, const char *lockpath)
 {
-       if (lock_fd < 0)
+       if (*fd < 0)
                return;
 
-       if (flock(lock_fd, LOCK_UN))
+       if (flock(*fd, LOCK_UN))
                warn("Cannot release exclusive lock: %s", strerror(errno));
 
-       close(lock_fd);
+       close(*fd);
        unlink(FW3_LOCKFILE);
 
-       lock_fd = -1;
+       *fd = -1;
+}
+
+
+void
+fw3_unlock(void)
+{
+       fw3_unlock_path(&fw3_lock_fd, FW3_LOCKFILE);
 }
 
 
@@ -579,6 +601,14 @@ write_ipset_uci(struct uci_context *ctx, struct fw3_ipset *s,
        ptr.value  = s->name;
        uci_set(ctx, &ptr);
 
+       ptr.o      = NULL;
+       ptr.option = "family";
+       if (s->family == FW3_FAMILY_V4)
+               ptr.value = "ipv4";
+       else
+               ptr.value = "ipv6";
+       uci_set(ctx, &ptr);
+
        ptr.o      = NULL;
        ptr.option = "storage";
        ptr.value  = fw3_ipset_method_names[s->method];
@@ -933,3 +963,43 @@ fw3_protoname(void *proto)
 
        return pe->p_name;
 }
+
+bool
+fw3_check_loopback_dev(const char *name)
+{
+       struct ifreq ifr;
+       int s;
+       bool rv = false;
+
+       s = socket(AF_LOCAL, SOCK_DGRAM, 0);
+
+       if (s < 0)
+               return false;
+
+       memset(&ifr, 0, sizeof(ifr));
+       strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
+
+       if (ioctl(s, SIOCGIFFLAGS, &ifr) >= 0) {
+               if (ifr.ifr_flags & IFF_LOOPBACK)
+                       rv = true;
+       }
+
+       close(s);
+
+       return rv;
+}
+
+bool
+fw3_check_loopback_addr(struct fw3_address *addr)
+{
+       if (addr->family == FW3_FAMILY_V4 &&
+           (ntohl(addr->address.v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
+               return true;
+
+       if (addr->family == FW3_FAMILY_V6 && !addr->range &&
+           fw3_netmask2bitlen(FW3_FAMILY_V6, &addr->mask.v6) == 128 &&
+           IN6_IS_ADDR_LOOPBACK(&addr->address.v6))
+               return true;
+
+       return false;
+}