1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 /* Used when comparing CIDR masks for the modulus bits left over.
19 * A lot of ircd's seem to do this:
20 * ((-1) << (8 - (mask % 8)))
21 * But imho, it sucks in comparison to a nice neat lookup table.
23 const unsigned char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits - never actually used */
24 0x80, /* 10000000 - 1 bits */
25 0xC0, /* 11000000 - 2 bits */
26 0xE0, /* 11100000 - 3 bits */
27 0xF0, /* 11110000 - 4 bits */
28 0xF8, /* 11111000 - 5 bits */
29 0xFC, /* 11111100 - 6 bits */
30 0xFE /* 11111110 - 7 bits */
34 /* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */
35 bool irc::sockets::MatchCIDRBits(const unsigned char* address, const unsigned char* mask, unsigned int mask_bits)
37 unsigned int divisor = mask_bits / 8; /* Number of whole bytes in the mask */
38 unsigned int modulus = mask_bits % 8; /* Remaining bits in the mask after whole bytes are dealt with */
40 /* First (this is faster) compare the odd bits with logic ops */
42 if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
43 /* If they dont match, return false */
46 /* Secondly (this is slower) compare the whole bytes */
47 if (memcmp(address, mask, divisor))
50 /* The address matches the mask, to mask_bits bits of mask */
54 /* Match CIDR, but dont attempt to match() against leading *!*@ sections */
55 bool irc::sockets::MatchCIDR(const std::string &address, const std::string &cidr_mask)
57 return MatchCIDR(address, cidr_mask, false);
60 /* 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
61 * If you have a lot of hosts to match, youre probably better off building your mask once
62 * and then using the lower level MatchCIDRBits directly.
64 * This will also attempt to match any leading usernames or nicknames on the mask, using
65 * match(), when match_with_username is true.
67 bool irc::sockets::MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username)
69 unsigned char addr_raw[16];
70 unsigned char mask_raw[16];
71 unsigned int bits = 0;
73 std::string address_copy;
74 std::string cidr_copy;
76 /* The caller is trying to match ident@<mask>/bits.
77 * Chop off the ident@ portion, use match() on it
80 if (match_with_username)
82 /* Use strchr not strrchr, because its going to be nearer to the left */
83 std::string::size_type username_mask_pos = cidr_mask.rfind('@');
84 std::string::size_type username_addr_pos = address.rfind('@');
86 /* Both strings have an @ symbol in them */
87 if (username_mask_pos != std::string::npos && username_addr_pos != std::string::npos)
89 /* Try and match() the strings before the @
90 * symbols, and recursively call MatchCIDR without
91 * username matching enabled to match the host part.
93 return (InspIRCd::Match(address.substr(0, username_addr_pos), cidr_mask.substr(0, username_mask_pos), ascii_case_insensitive_map) &&
94 MatchCIDR(address.substr(username_addr_pos + 1), cidr_mask.substr(username_mask_pos + 1), false));
98 address_copy = address.substr(username_addr_pos + 1);
99 cidr_copy = cidr_mask.substr(username_mask_pos + 1);
104 address_copy.assign(address);
105 cidr_copy.assign(cidr_mask);
111 std::string::size_type bits_chars = cidr_copy.rfind('/');
113 if (bits_chars != std::string::npos)
115 bits = atoi(cidr_copy.substr(bits_chars + 1).c_str());
116 cidr_copy.erase(bits_chars, cidr_copy.length() - bits_chars);
120 /* No 'number of bits' field! */
124 in6_addr address_in6;
127 if (inet_pton(AF_INET6, address_copy.c_str(), &address_in6) > 0)
129 if (inet_pton(AF_INET6, cidr_copy.c_str(), &mask_in6) > 0)
131 memcpy(&addr_raw, &address_in6.s6_addr, 16);
132 memcpy(&mask_raw, &mask_in6.s6_addr, 16);
139 /* The address was valid ipv6, but the mask
140 * that goes with it wasnt.
145 else if (inet_pton(AF_INET, address_copy.c_str(), &address_in4) > 0)
147 if (inet_pton(AF_INET, cidr_copy.c_str(), &mask_in4) > 0)
149 memcpy(&addr_raw, &address_in4.s_addr, 4);
150 memcpy(&mask_raw, &mask_in4.s_addr, 4);
157 /* The address was valid ipv4,
158 * but the mask that went with it wasnt.
165 /* The address was neither ipv4 or ipv6 */
169 /* Low-level-match the bits in the raw data */
170 return MatchCIDRBits(addr_raw, mask_raw, bits);