]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
xlate-translate: split common parts into helper
authorFlorian Westphal <fw@strlen.de>
Thu, 12 Apr 2018 09:46:02 +0000 (11:46 +0200)
committerFlorian Westphal <fw@strlen.de>
Fri, 13 Apr 2018 18:06:47 +0000 (20:06 +0200)
iptables/xtables-translate.c

index 61d4da179cf7a5f12c0d82d1d05e4eb7d7521dfd..ecb490f98b2f2186c608ac05e9bd22baa141e2bf 100644 (file)
@@ -420,38 +420,50 @@ static struct nft_xt_restore_cb cb_xlate = {
        .abort          = commit,
 };
 
-static int xtables_xlate_main(int family, const char *progname, int argc,
-                             char *argv[])
+static int xtables_xlate_main_common(struct nft_handle *h,
+                                    int family,
+                                    const char *progname)
 {
        int ret;
-       char *table = "filter";
-       struct nft_handle h = {
-               .family = family,
-       };
 
        xtables_globals.program_name = progname;
        xtables_globals.compat_rev = dummy_compat_rev;
        ret = xtables_init_all(&xtables_globals, family);
        if (ret < 0) {
                fprintf(stderr, "%s/%s Failed to initialize xtables\n",
-                               xtables_globals.program_name,
-                               xtables_globals.program_version);
-                               exit(1);
+                       xtables_globals.program_name,
+                       xtables_globals.program_version);
+               return 1;
        }
 #if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
        init_extensions();
        init_extensions4();
 #endif
 
-       if (nft_init(&h, xtables_ipv4) < 0) {
+       if (nft_init(h, xtables_ipv4) < 0) {
                fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
                                xtables_globals.program_name,
                                xtables_globals.program_version,
                                strerror(errno));
-               nft_fini(&h);
-               exit(EXIT_FAILURE);
+               return 1;
        }
 
+       return 0;
+}
+
+static int xtables_xlate_main(int family, const char *progname, int argc,
+                             char *argv[])
+{
+       int ret;
+       char *table = "filter";
+       struct nft_handle h = {
+               .family = family,
+       };
+
+       ret = xtables_xlate_main_common(&h, family, progname);
+       if (ret < 0)
+               exit(EXIT_FAILURE);
+
        ret = do_command_xlate(&h, argc, argv, &table, false);
        if (!ret)
                fprintf(stderr, "Translation not implemented\n");
@@ -472,28 +484,9 @@ static int xtables_restore_xlate_main(int family, const char *progname,
        time_t now = time(NULL);
        int c;
 
-       xtables_globals.program_name = progname;
-       xtables_globals.compat_rev = dummy_compat_rev;
-       ret = xtables_init_all(&xtables_globals, family);
-       if (ret < 0) {
-               fprintf(stderr, "%s/%s Failed to initialize xtables\n",
-                               xtables_globals.program_name,
-                               xtables_globals.program_version);
-                               exit(1);
-       }
-#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
-       init_extensions();
-       init_extensions4();
-#endif
-
-       if (nft_init(&h, xtables_ipv4) < 0) {
-               fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
-                               xtables_globals.program_name,
-                               xtables_globals.program_version,
-                               strerror(errno));
-               nft_fini(&h);
+       ret = xtables_xlate_main_common(&h, family, progname);
+       if (ret < 0)
                exit(EXIT_FAILURE);
-       }
 
        opterr = 0;
        while ((c = getopt_long(argc, argv, "hf:", options, NULL)) != -1) {