From: Phil Oester <kernel@linuxace.com>
To: netfilter-devel@vger.kernel.org
Cc: pablo@netfilter.org
Subject: [PATCH] iptables: Sort table names in ip[6]tables-save
Date: Wed, 26 Jun 2013 19:42:02 -0400 [thread overview]
Message-ID: <20130626234202.GA31099@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 404 bytes --]
Depending upon the load order of rules, the output from ip[6]tables-save
will vary, as ip[6]_tables_names is sorted LIFO. As reported by
Linus van Geuns, this makes comparing output from ip[6]tables-save across
reboots difficult. Fix this by sorting table names prior to walking
the tables, making output consistent.
This closes bugzilla #580.
Phil
Signed-off-by: Phil Oester <kernel@linuxace.com>
[-- Attachment #2: patch-sort-tables --]
[-- Type: text/plain, Size: 2917 bytes --]
diff --git a/include/xtables.h b/include/xtables.h
index c35a6e6..dc6e566 100644
--- a/include/xtables.h
+++ b/include/xtables.h
@@ -479,6 +479,8 @@ extern void xtables_ip6parse_any(const char *, struct in6_addr **,
extern void xtables_ip6parse_multiple(const char *, struct in6_addr **,
struct in6_addr **, unsigned int *);
+extern int stringcmp(const void *, const void *);
+
/**
* Print the specified value to standard output, quoting dangerous
* characters if required.
diff --git a/iptables/ip6tables-save.c b/iptables/ip6tables-save.c
index d819b30..7ec0f75 100644
--- a/iptables/ip6tables-save.c
+++ b/iptables/ip6tables-save.c
@@ -36,8 +36,9 @@ static const struct option options[] = {
/* Debugging prototype. */
static int for_each_table(int (*func)(const char *tablename))
{
- int ret = 1;
+ int i, count = 0, ret = 1;
FILE *procfile = NULL;
+ char **tables = NULL;
char tablename[XT_TABLE_MAXNAMELEN+1];
procfile = fopen("/proc/net/ip6_tables_names", "re");
@@ -50,10 +51,17 @@ static int for_each_table(int (*func)(const char *tablename))
"Badly formed tablename `%s'\n",
tablename);
tablename[strlen(tablename) - 1] = '\0';
- ret &= func(tablename);
+ count++;
+ tables = (char **)realloc(tables, sizeof(char*)*count);
+ tables[count-1] = strdup(tablename);
}
-
fclose(procfile);
+
+ qsort(tables, count, sizeof(char *), stringcmp);
+ for (i = 0 ; i < count ; i++) {
+ ret &= func(tables[i]);
+ }
+
return ret;
}
diff --git a/iptables/iptables-save.c b/iptables/iptables-save.c
index e599fce..802f94d 100644
--- a/iptables/iptables-save.c
+++ b/iptables/iptables-save.c
@@ -34,8 +34,9 @@ static const struct option options[] = {
/* Debugging prototype. */
static int for_each_table(int (*func)(const char *tablename))
{
- int ret = 1;
+ int i, count = 0, ret = 1;
FILE *procfile = NULL;
+ char **tables = NULL;
char tablename[XT_TABLE_MAXNAMELEN+1];
procfile = fopen("/proc/net/ip_tables_names", "re");
@@ -48,10 +49,17 @@ static int for_each_table(int (*func)(const char *tablename))
"Badly formed tablename `%s'\n",
tablename);
tablename[strlen(tablename) - 1] = '\0';
- ret &= func(tablename);
+ count++;
+ tables = (char **)realloc(tables, sizeof(char*)*count);
+ tables[count-1] = strdup(tablename);
}
-
fclose(procfile);
+
+ qsort(tables, count, sizeof(char *), stringcmp);
+ for (i = 0 ; i < count ; i++) {
+ ret &= func(tables[i]);
+ }
+
return ret;
}
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index ebc77b6..ca94f4e 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -1957,3 +1957,10 @@ void get_kernel_version(void)
sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
kernel_version = LINUX_VERSION(x, y, z);
}
+
+int stringcmp(const void *a, const void *b)
+{
+ const char **ia = (const char **)a;
+ const char **ib = (const char **)b;
+ return strcmp(*ia, *ib);
+}
next reply other threads:[~2013-06-27 20:33 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-26 23:42 Phil Oester [this message]
2013-07-02 4:43 ` [PATCH] iptables: Sort table names in ip[6]tables-save Maciej Żenczykowski
2013-07-01 14:01 ` Phil Oester
2013-07-02 21:08 ` Maciej Żenczykowski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130626234202.GA31099@gmail.com \
--to=kernel@linuxace.com \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).