X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fsocket.cpp;h=2a33442fed32097c6bf2ff6a846aa3f62a2f48e6;hb=3e50e674c3aab0827e62d21e75b828bff0e88da7;hp=41c4fd1ca60f5f4d2ba532220c307175978ebd31;hpb=a9621bc98996e08d86734e6848de13223341ea64;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/socket.cpp b/src/socket.cpp index 41c4fd1ca..2a33442fe 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -21,6 +21,7 @@ #include "inspstring.h" #include "helperfuncs.h" #include "socketengine.h" +#include "wildcard.h" #include "message.h" extern InspIRCd* ServerInstance; @@ -39,10 +40,15 @@ char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits */ 0xFE /* 11111110 - 7 bits */ }; +/* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */ 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 */ + + /* We shouldnt match anything, /0 is always valid */ + if (!mask_bits) + return true; /* First compare the whole bytes, if they dont match, return false */ if (memcmp(address, mask, divisor)) @@ -58,6 +64,124 @@ bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mas return true; } +bool MatchCIDR(const char* address, const char* cidr_mask) +{ + return MatchCIDR(address, cidr_mask, false); +} + +/* Match CIDR strings, e.g. 127.0.0.1 to 127.0.0.0/8 or 3ffe:1:5:6::8 to 3ffe:1::0/32 + * If you have a lot of hosts to match, youre probably better off building your mask once + * and then using the lower level MatchCIDRBits directly. + */ +bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username) +{ + unsigned char addr_raw[16]; + unsigned char mask_raw[16]; + unsigned int bits = 0; + char* mask = NULL; + + /* The caller is trying to match ident@/bits. + * Chop off the ident@ portion, use match() on it + * seperately. + */ + if (match_with_username) + { + /* Duplicate the strings, and try to find the position + * of the @ symbol in each */ + char* address_dupe = strdup(address); + char* cidr_dupe = strdup(cidr_mask); + + char* username_mask_pos = strchr(cidr_dupe, '@'); + char* username_addr_pos = strchr(address_dupe, '@'); + + if (username_mask_pos && username_addr_pos) + { + *username_mask_pos = *username_addr_pos = 0; + + bool result = (match(address_dupe, cidr_dupe) && MatchCIDR(username_addr_pos + 1, username_mask_pos + 1)); + + free(address_dupe); + free(cidr_dupe); + + return result; + } + else + { + free(address_dupe); + free(cidr_dupe); + mask = strdup(cidr_mask); + } + } + else + { + 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; + } + else + { + /* No 'number of bits' field! */ + return false; + } + +#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); + + if (bits > 128) + bits = 128; + } + 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); + + if (bits > 32) + bits = 32; + } + 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'.