diff options
author | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-08-06 15:10:40 +0000 |
---|---|---|
committer | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-08-06 15:10:40 +0000 |
commit | d07d41bf93ea6970e0b574c64c694abc6e3cd54c (patch) | |
tree | 2312555d106193b21c8d0d72d4b1d75192bf8252 | |
parent | 7363f470f07527e29fd94fdcea77965173cf332a (diff) |
Simple CIDR checking routine
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4730 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r-- | src/socket.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/socket.cpp b/src/socket.cpp index d826bfb8c..7ef7f225e 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -27,6 +27,32 @@ extern InspIRCd* ServerInstance; extern ServerConfig* Config; extern time_t TIME; +/* Used when comparing CIDR masks for the modulus bits left over */ + +char inverted_bits[8] = { 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 */ +}; + +bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits) +{ + unsigned int modulus = mask_bits % 8; + unsigned int divisor = mask_bits / 8; + + if (memcmp(address, mask, divisor)) + return false; + if (modulus) + if ((address[divisor] & inverted_bits[modulus]) != address[divisor]) + return false; + + return true; +} + /** This will bind a socket to a port. It works for UDP/TCP. * It can only bind to IP addresses, if you wish to bind to hostnames * you should first resolve them using class 'Resolver'. |