summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRalf Baechle <ralf@linux-mips.org>2017-08-01 11:50:34 +0200
committerRalf Baechle <ralf@linux-mips.org>2017-08-03 04:21:00 +0200
commit3c8c639c2a8863260b171c1a99b7a7286030b42c (patch)
tree2965cca47906a5b32bcd32e1558e4e48c2e455bd
parent875cc7a37dc6b3fb9d29473d6c70f23160887ee9 (diff)
rip98d: Replace naive mask2bits and bits2mask implementations.
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
-rw-r--r--tcpip/rip98d.c57
-rw-r--r--tcpip/rip98r.c6
2 files changed, 10 insertions, 53 deletions
diff --git a/tcpip/rip98d.c b/tcpip/rip98d.c
index f98be6f..43d31ba 100644
--- a/tcpip/rip98d.c
+++ b/tcpip/rip98d.c
@@ -39,45 +39,6 @@ int logging = FALSE;
struct route_struct *first_route;
-static struct mask_struct {
- unsigned long int mask;
- unsigned int bits;
-} mask_table[] = {
- {0xFFFFFFFF, 32},
- {0xFFFFFFFE, 31},
- {0xFFFFFFFC, 30},
- {0xFFFFFFF8, 29},
- {0xFFFFFFF0, 28},
- {0xFFFFFFE0, 27},
- {0xFFFFFFC0, 26},
- {0xFFFFFF80, 25},
- {0xFFFFFF00, 24},
- {0xFFFFFE00, 23},
- {0xFFFFFC00, 22},
- {0xFFFFF800, 21},
- {0xFFFFF000, 20},
- {0xFFFFE000, 19},
- {0xFFFFC000, 18},
- {0xFFFF8000, 17},
- {0xFFFF0000, 16},
- {0xFFFE0000, 15},
- {0xFFFC0000, 14},
- {0xFFF80000, 13},
- {0xFFF00000, 12},
- {0xFFE00000, 11},
- {0xFFC00000, 10},
- {0xFF800000, 9},
- {0xFF000000, 8},
- {0xFE000000, 7},
- {0xFC000000, 6},
- {0xF8000000, 5},
- {0xF0000000, 4},
- {0xE0000000, 3},
- {0xC0000000, 2},
- {0x80000000, 1},
- {0x00000000, 0},
-};
-
static void terminate(int sig)
{
if (logging) {
@@ -90,24 +51,20 @@ static void terminate(int sig)
unsigned int mask2bits(unsigned int mask)
{
- struct mask_struct *t;
+ mask = ~mask;
- for (t = mask_table; t->mask != 0; t++)
- if (mask == t->mask)
- return t->bits;
+ if (!mask)
+ return 32;
- return 0;
+ return __builtin_clz(mask);
}
unsigned int bits2mask(unsigned int bits)
{
- struct mask_struct *t;
-
- for (t = mask_table; t->mask != 0; t++)
- if (bits == t->bits)
- return htonl(t->mask);
+ if (!bits)
+ return 0;
- return 0;
+ return ~0 << (sizeof(unsigned int) * 8 - bits);
}
static unsigned long int hex2intrev(char *buffer)
diff --git a/tcpip/rip98r.c b/tcpip/rip98r.c
index 57afe4e..8332944 100644
--- a/tcpip/rip98r.c
+++ b/tcpip/rip98r.c
@@ -26,8 +26,8 @@ static int cmp_route(struct route_struct *route, struct in_addr addr, int bits,
unsigned long int old_mask, new_mask;
unsigned long int old_addr, new_addr;
- old_mask = ntohl(bits2mask(route->bits));
- new_mask = ntohl(bits2mask(bits));
+ old_mask = bits2mask(route->bits);
+ new_mask = bits2mask(bits);
old_addr = route->addr.s_addr;
new_addr = addr.s_addr;
@@ -234,7 +234,7 @@ void receive_routes(int s)
if (route->bits == 32) {
rt.rt_flags |= RTF_HOST;
} else {
- netmask = bits2mask(route->bits);
+ netmask = htonl(bits2mask(route->bits));
trg.sin_family = AF_INET;
memcpy((char *)&trg.sin_addr, (char *)&netmask, sizeof(struct in_addr));