From f4a4901fee693791493c340fd380658a24d4cf26 Mon Sep 17 00:00:00 2001 From: brain Date: Sun, 6 Aug 2006 16:09:29 +0000 Subject: Support CIDR, CIDR zline, /oper and CIDR 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 --- src/socket.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) (limited to 'src/socket.cpp') 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'. -- cgit v1.2.3