defaults: robustify flow table detection.
[project/firewall3.git] / utils.c
diff --git a/utils.c b/utils.c
index 441dbd26020a7864411688e2ac8f127b051903db..da6563243c0673f8393c16f65116aeb88ba0ea6c 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -344,6 +344,33 @@ fw3_has_table(bool ipv6, const char *table)
        return seen;
 }
 
+bool
+fw3_has_target(const bool ipv6, const char *target)
+{
+       FILE *f;
+
+       char line[12];
+       bool seen = false;
+
+       const char *path = ipv6
+               ? "/proc/net/ip6_tables_targets" : "/proc/net/ip_tables_targets";
+
+       if (!(f = fopen(path, "r")))
+               return false;
+
+       while (fgets(line, sizeof(line), f))
+       {
+               if (!strcmp(line, target))
+               {
+                       seen = true;
+                       break;
+               }
+       }
+
+       fclose(f);
+
+       return seen;
+}
 
 bool
 fw3_lock_path(int *fd, const char *path)