summaryrefslogtreecommitdiff
path: root/src/socket.cpp
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-08-06 15:26:16 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-08-06 15:26:16 +0000
commita9621bc98996e08d86734e6848de13223341ea64 (patch)
tree640e5bf984b9efbc53be3f7f4e62f34f62eee3da /src/socket.cpp
parentd07d41bf93ea6970e0b574c64c694abc6e3cd54c (diff)
Fix CIDR functions so they work
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4731 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src/socket.cpp')
-rw-r--r--src/socket.cpp19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/socket.cpp b/src/socket.cpp
index 7ef7f225e..41c4fd1ca 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -29,27 +29,32 @@ extern time_t TIME;
/* Used when comparing CIDR masks for the modulus bits left over */
-char inverted_bits[8] = { 0x80, /* 10000000 - 1 bits */
+char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits */
+ 0x80, /* 10000000 - 1 bits */
0xC0, /* 11000000 - 2 bits */
0xE0, /* 11100000 - 3 bits */
0xF0, /* 11110000 - 4 bits */
0xF8, /* 11111000 - 5 bits */
0xFC, /* 11111100 - 6 bits */
- 0xFE, /* 11111110 - 7 bits */
- 0xFF, /* 11111111 - 8 bits */
+ 0xFE /* 11111110 - 7 bits */
};
bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)
{
- unsigned int modulus = mask_bits % 8;
- unsigned int divisor = mask_bits / 8;
+ unsigned int modulus = mask_bits & 0x07; /* Number of whole bytes in the mask */
+ unsigned int divisor = mask_bits >> 0x04; /* 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;
+ return false;
+
+ /* Now if there are any remainder bits, we compare them with logic AND */
if (modulus)
- if ((address[divisor] & inverted_bits[modulus]) != address[divisor])
+ if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
+ /* If they dont match, return false */
return false;
+ /* The address matches the mask, to mask_bits bits of mask */
return true;
}