summaryrefslogtreecommitdiff
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
parentd07d41bf93ea6970e0b574c64c694abc6e3cd54c (diff)
Fix CIDR functions so they work
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4731 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r--include/socket.h2
-rw-r--r--src/inspircd.cpp59
-rw-r--r--src/socket.cpp19
3 files changed, 20 insertions, 60 deletions
diff --git a/include/socket.h b/include/socket.h
index f9a39ded5..399f84397 100644
--- a/include/socket.h
+++ b/include/socket.h
@@ -49,6 +49,8 @@ typedef struct in_addr insp_inaddr;
#endif
+bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits);
+
const char* insp_ntoa(insp_inaddr n);
int insp_aton(const char* a, insp_inaddr* n);
diff --git a/src/inspircd.cpp b/src/inspircd.cpp
index a34f438f9..09cd74d69 100644
--- a/src/inspircd.cpp
+++ b/src/inspircd.cpp
@@ -940,61 +940,14 @@ int InspIRCd::Run()
int main(int argc, char** argv)
{
- /* TEST SUITE FOR TOKENSTREAM
- *
- * Expected output:
- *
- * String: 'PRIVMSG #test FOO BAR'
- * Token 0 = 'PRIVMSG'
- * Token 1 = '#test'
- * Token 2 = 'FOO'
- * Token 3 = 'BAR'
- * String: 'PRIVMSG #test :FOO BAR BAZ'
- * Token 0 = 'PRIVMSG'
- * Token 1 = '#test'
- * Token 2 = 'FOO BAR BAZ'
- * String: ':PRIVMSG #test :FOO BAR BAZ'
- * Token 0 = ':PRIVMSG'
- * String: 'AAAAAAA'
- * Token 0 = 'AAAAAAA'
- * String: ''
- * NumItems = 0
- *
- std::string a = "PRIVMSG #test FOO BAR";
- printf("String: '%s'\n",a.c_str());
- irc::tokenstream test(a);
- printf("Token 0 = '%s'\n",test.GetToken().c_str());
- printf("Token 1 = '%s'\n",test.GetToken().c_str());
- printf("Token 2 = '%s'\n",test.GetToken().c_str());
- printf("Token 3 = '%s'\n",test.GetToken().c_str());
- printf("Token 4 = '%s'\n",test.GetToken().c_str());
-
- std::string b = "PRIVMSG #test :FOO BAR BAZ";
- printf("String: '%s'\n",b.c_str());
- irc::tokenstream test2(b);
- printf("Token 0 = '%s'\n",test2.GetToken().c_str());
- printf("Token 1 = '%s'\n",test2.GetToken().c_str());
- printf("Token 2 = '%s'\n",test2.GetToken().c_str());
- printf("Token 3 = '%s'\n",test2.GetToken().c_str());
-
- std::string c = ":PRIVMSG #test :FOO BAR BAZ";
- printf("String: '%s'\n",c.c_str());
- irc::tokenstream test3(c);
- printf("Token 0 = '%s'\n",test3.GetToken().c_str());
-
- c = "AAAAAAA";
- printf("String: '%s'\n",c.c_str());
- irc::tokenstream test4(c);
- printf("Token 0 = '%s'\n",test4.GetToken().c_str());
- printf("Token 1 = '%s'\n",test4.GetToken().c_str());
-
- c = "";
- printf("String: '%s'\n",c.c_str());
- irc::tokenstream test5(c);
- printf("Token 0 = '%s'\n",test5.GetToken().c_str());
+
+ unsigned char addr[] = {0xCC,0xAA,0xCC,0xAA};
+ unsigned char mask[] = {0xCC,0xAA,0xC0,0xAA};
+
+ printf("%d",MatchCIDRBits(addr, mask, 20));
exit(0);
- */
+
try
{
ServerInstance = new InspIRCd(argc, argv);
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;
}