diff options
author | Daniel Wagner <daniel.wagner@bmw-carit.de> | 2012-02-01 18:51:42 +0100 |
---|---|---|
committer | Samuel Ortiz <sameo@linux.intel.com> | 2012-02-13 11:33:17 +0100 |
commit | 2e0ccdb9c0685f9d73aa72224bb84665ba0cc054 (patch) | |
tree | 5e23477e36e845c797824aaa57f3fd9fb0e7e937 | |
parent | cf33699115ff48f9cbf70f45585d1b1df9946650 (diff) | |
download | connman-2e0ccdb9c0685f9d73aa72224bb84665ba0cc054.tar.gz connman-2e0ccdb9c0685f9d73aa72224bb84665ba0cc054.tar.bz2 connman-2e0ccdb9c0685f9d73aa72224bb84665ba0cc054.zip |
iptables-test: Parse also netmask for src/dst addresses
-rw-r--r-- | 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 a6780f15..b87339aa 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; |