summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2021-03-02 15:07:13 +0100
committerPhil Sutter <phil@nwl.cc>2021-03-09 09:27:40 +0100
commit831f57c7fbdc8d79e34b1d7d81ca6f6a8e6bae87 (patch)
treef85fc91ef9f8a78ad0854c1546ef62ad12ddc844
parent46f9d3a9a61ee80fa94b7fa7b3b36045c92606ae (diff)
libxtables: Simplify xtables_ipmask_to_cidr() a bit
Reduce the whole mask matching into a single for-loop. No need for a shortcut, /32 masks will match in the first iteration. Signed-off-by: Phil Sutter <phil@nwl.cc>
-rw-r--r--libxtables/xtables.c15
1 files changed, 5 insertions, 10 deletions
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index bc42ba82..fc3f6220 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -1430,16 +1430,11 @@ int xtables_ipmask_to_cidr(const struct in_addr *mask)
int i;
maskaddr = ntohl(mask->s_addr);
- /* shortcut for /32 networks */
- if (maskaddr == 0xFFFFFFFFL)
- return 32;
-
- i = 32;
- bits = 0xFFFFFFFEL;
- while (--i >= 0 && maskaddr != bits)
- bits <<= 1;
- if (i >= 0)
- return i;
+
+ for (i = 32, bits = (uint32_t)-1; i >= 0; i--, bits <<= 1) {
+ if (bits == maskaddr)
+ return i;
+ }
/* this mask cannot be converted to CIDR notation */
return -1;