1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 /* Used when comparing CIDR masks for the modulus bits left over.
20 * A lot of ircd's seem to do this:
21 * ((-1) << (8 - (mask % 8)))
22 * But imho, it sucks in comparison to a nice neat lookup table.
24 const unsigned char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits - never actually used */
25 0x80, /* 10000000 - 1 bits */
26 0xC0, /* 11000000 - 2 bits */
27 0xE0, /* 11100000 - 3 bits */
28 0xF0, /* 11110000 - 4 bits */
29 0xF8, /* 11111000 - 5 bits */
30 0xFC, /* 11111100 - 6 bits */
31 0xFE /* 11111110 - 7 bits */
35 /* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */
36 bool irc::sockets::MatchCIDRBits(const unsigned char* address, const unsigned char* mask, unsigned int mask_bits)
38 unsigned int divisor = mask_bits / 8; /* Number of whole bytes in the mask */
39 unsigned int modulus = mask_bits % 8; /* Remaining bits in the mask after whole bytes are dealt with */
41 /* First (this is faster) compare the odd bits with logic ops */
43 if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
44 /* If they dont match, return false */
47 /* Secondly (this is slower) compare the whole bytes */
48 if (memcmp(address, mask, divisor))
51 /* The address matches the mask, to mask_bits bits of mask */
55 /* Match CIDR, but dont attempt to match() against leading *!*@ sections */
56 bool irc::sockets::MatchCIDR(const std::string &address, const std::string &cidr_mask)
58 return MatchCIDR(address, cidr_mask, false);
61 /* 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
62 * If you have a lot of hosts to match, youre probably better off building your mask once
63 * and then using the lower level MatchCIDRBits directly.
65 * This will also attempt to match any leading usernames or nicknames on the mask, using
66 * match(), when match_with_username is true.
68 bool irc::sockets::MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username)
70 unsigned char addr_raw[16];
71 unsigned char mask_raw[16];
72 unsigned int bits = 0;
74 std::string address_copy;
75 std::string cidr_copy;
77 /* The caller is trying to match ident@<mask>/bits.
78 * Chop off the ident@ portion, use match() on it
81 if (match_with_username)
83 /* Use strchr not strrchr, because its going to be nearer to the left */
84 std::string::size_type username_mask_pos = cidr_mask.rfind('@');
85 std::string::size_type username_addr_pos = address.rfind('@');
87 /* Both strings have an @ symbol in them */
88 if (username_mask_pos != std::string::npos && username_addr_pos != std::string::npos)
90 /* Try and match() the strings before the @
91 * symbols, and recursively call MatchCIDR without
92 * username matching enabled to match the host part.
94 return (match(address.substr(0, username_addr_pos), cidr_mask.substr(0, username_mask_pos)) &&
95 MatchCIDR(address.substr(username_addr_pos + 1), cidr_mask.substr(username_mask_pos + 1), false));
99 address_copy = address.substr(username_addr_pos + 1);
100 cidr_copy = cidr_mask.substr(username_mask_pos + 1);
105 address_copy.assign(address);
106 cidr_copy.assign(cidr_mask);
112 std::string::size_type bits_chars = cidr_copy.rfind('/');
114 if (bits_chars != std::string::npos)
116 bits = atoi(cidr_copy.substr(bits_chars + 1).c_str());
117 cidr_copy.erase(bits_chars, cidr_copy.length() - bits_chars);
121 /* No 'number of bits' field! */
125 #ifdef SUPPORT_IP6LINKS
126 in6_addr address_in6;
129 if (inet_pton(AF_INET6, address_copy.c_str(), &address_in6) > 0)
131 if (inet_pton(AF_INET6, cidr_copy.c_str(), &mask_in6) > 0)
133 memcpy(&addr_raw, &address_in6.s6_addr, 16);
134 memcpy(&mask_raw, &mask_in6.s6_addr, 16);
141 /* The address was valid ipv6, but the mask
142 * that goes with it wasnt.
149 if (inet_pton(AF_INET, address_copy.c_str(), &address_in4) > 0)
151 if (inet_pton(AF_INET, cidr_copy.c_str(), &mask_in4) > 0)
153 memcpy(&addr_raw, &address_in4.s_addr, 4);
154 memcpy(&mask_raw, &mask_in4.s_addr, 4);
161 /* The address was valid ipv4,
162 * but the mask that went with it wasnt.
169 /* The address was neither ipv4 or ipv6 */
173 /* Low-level-match the bits in the raw data */
174 return MatchCIDRBits(addr_raw, mask_raw, bits);