summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2007-08-23 16:35:59 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2007-08-23 16:35:59 +0000
commit8214184319ccd044376cb2a9a30518c9bfeebab3 (patch)
tree63bad2396f8ee9754e5c33c8708b116ad2fcd961
parent05716503f30bbb386b3e98e4e22ce232eae570d9 (diff)
Switch around the two operations in MatchCIDRBits to make negative matches faster (and not change the speed of positive matches)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@7797 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r--src/socket.cpp14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/socket.cpp b/src/socket.cpp
index b28414d4a..976c0585b 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -133,19 +133,19 @@ void ListenSocket::HandleEvent(EventType et, int errornum)
/* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */
bool irc::sockets::MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)
{
- unsigned int modulus = mask_bits % 8; /* Number of whole bytes in the mask */
- unsigned int divisor = mask_bits / 8; /* Remaining bits in the mask after whole bytes are dealt with */
+ unsigned int divisor = mask_bits / 8; /* Number of whole bytes in the mask */
+ unsigned int modulus = mask_bits % 8; /* Remaining bits in the mask after whole bytes are dealt with */
- /* First compare the whole bytes, if they dont match, return false */
- if (memcmp(address, mask, divisor))
- return false;
-
- /* Now if there are any remainder bits, we compare them with logic AND */
+ /* First (this is faster) compare the odd bits with logic ops */
if (modulus)
if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
/* If they dont match, return false */
return false;
+ /* Secondly (this is slower) compare the whole bytes */
+ if (memcmp(address, mask, divisor))
+ return false;
+
/* The address matches the mask, to mask_bits bits of mask */
return true;
}