[RFC v0 1/6] iptables-test: Parse also netmask for src/dst addresses

Daniel Wagner wagi at monom.org
Wed Feb 1 09:51:42 PST 2012


From: Daniel Wagner <daniel.wagner at bmw-carit.de>

---
 tools/iptables-test.c |   48 +++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/tools/iptables-test.c b/tools/iptables-test.c
index a6780f1..b87339a 100644
--- a/tools/iptables-test.c
+++ b/tools/iptables-test.c
@@ -1456,6 +1456,42 @@ done:
 	return xt_m;
 }
 
+static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
+{
+	char **tokens;
+	uint32_t prefixlength;
+	uint32_t tmp;
+	int err;
+
+	tokens = g_strsplit(str, "/", 2);
+	if (tokens == NULL)
+		return -1;
+
+	if (!inet_pton(AF_INET, tokens[0], ip)) {
+		err = -1;
+		goto out;
+	}
+
+	if (tokens[1] != NULL) {
+		prefixlength = strtol(tokens[1], NULL, 10);
+		if (prefixlength > 31) {
+			err = -1;
+			goto out;
+		}
+
+		tmp = ~(0xffffffff >> prefixlength);
+	} else {
+		tmp = 0xffffffff;
+	}
+
+	mask->s_addr = htonl(tmp);
+	err = 0;
+out:
+	g_strfreev(tokens);
+
+	return err;
+}
+
 int main(int argc, char *argv[])
 {
 	struct connman_iptables *table;
@@ -1467,7 +1503,6 @@ int main(int argc, char *argv[])
 	char *delete_chain, *flush_chain, *policy;
 	int c, in_len, out_len;
 	gboolean dump, invert, delete, insert, delete_rule, compare_rule;
-	struct in_addr src, dst;
 
 	xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
 
@@ -1554,15 +1589,13 @@ int main(int argc, char *argv[])
 			break;
 
 		case 'd':
-			if (!inet_pton(AF_INET, optarg, &dst))
+			if (!parse_ip_and_mask(optarg, &ip.dst, &ip.dmsk))
 				break;
 
-			ip.dst = dst;
-			inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
-
 			if (invert)
 				ip.invflags |= IPT_INV_DSTIP;
 
+
 			break;
 
 		case 'i':
@@ -1610,12 +1643,9 @@ int main(int argc, char *argv[])
 			break;
 
 		case 's':
-			if (!inet_pton(AF_INET, optarg, &src))
+			if (!parse_ip_and_mask(optarg, &ip.src, &ip.smsk))
 				break;
 
-			ip.src = src;
-			inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
-
 			if (invert)
 				ip.invflags |= IPT_INV_SRCIP;
 
-- 
1.7.9.48.g85da4d




More information about the connman mailing list