summaryrefslogtreecommitdiff
path: root/src/socket.cpp
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-08-06 16:09:29 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-08-06 16:09:29 +0000
commitf4a4901fee693791493c340fd380658a24d4cf26 (patch)
tree27a9d24c0d4bee19bfe0c4c33079e9e91a8b5912 /src/socket.cpp
parenta9621bc98996e08d86734e6848de13223341ea64 (diff)
Support CIDR, CIDR zline, /oper and CIDR <connect> tags. NOTE: With CIDR oper, ident field is not supported (yet)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4732 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src/socket.cpp')
-rw-r--r--src/socket.cpp64
1 files changed, 62 insertions, 2 deletions
diff --git a/src/socket.cpp b/src/socket.cpp
index 41c4fd1ca..14fe580ad 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -41,8 +41,8 @@ char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits */
bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)
{
- 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 */
+ 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 */
/* First compare the whole bytes, if they dont match, return false */
if (memcmp(address, mask, divisor))
@@ -58,6 +58,66 @@ bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mas
return true;
}
+bool MatchCIDR(const char* address, const char* cidr_mask)
+{
+ unsigned char addr_raw[16];
+ unsigned char mask_raw[16];
+ unsigned int bits = 0;
+ char* mask = strdup(cidr_mask);
+
+ in_addr address_in4;
+ in_addr mask_in4;
+
+ char* bits_chars = strchr(mask,'/');
+
+ if (bits_chars)
+ {
+ bits = atoi(bits_chars + 1);
+ *bits_chars = 0;
+ }
+
+#ifdef SUPPORT_IP6LINKS
+ in6_addr address_in6;
+ in6_addr mask_in6;
+
+ if (inet_pton(AF_INET6, address, &address_in6) > 0)
+ {
+ if (inet_pton(AF_INET6, mask, &mask_in6) > 0)
+ {
+ memcpy(&addr_raw, &address_in6.s6_addr, 16);
+ memcpy(&mask_raw, &mask_in6.s6_addr, 16);
+ }
+ else
+ {
+ free(mask);
+ return false;
+ }
+ }
+ else
+#endif
+ if (inet_pton(AF_INET, address, &address_in4) > 0)
+ {
+ if (inet_pton(AF_INET, mask, &mask_in4) > 0)
+ {
+ memcpy(&addr_raw, &address_in4.s_addr, 4);
+ memcpy(&mask_raw, &mask_in4.s_addr, 4);
+ }
+ else
+ {
+ free(mask);
+ return false;
+ }
+ }
+ else
+ {
+ free(mask);
+ return false;
+ }
+
+ free(mask);
+ return MatchCIDRBits(addr_raw, mask_raw, bits);
+}
+
/** 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'.