]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cidr.cpp
Add support for blacklists and whitelists, just http password auth to go (the most...
[user/henk/code/inspircd.git] / src / cidr.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDcidr */
15
16 #include "inspircd.h"
17 #include "wildcard.h"
18
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.
23  */
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 */
32 };
33
34
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)
37 {
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 */
40
41         /* First (this is faster) compare the odd bits with logic ops */
42         if (modulus)
43                 if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
44                         /* If they dont match, return false */
45                         return false;
46
47         /* Secondly (this is slower) compare the whole bytes */
48         if (memcmp(address, mask, divisor))
49                 return false;
50
51         /* The address matches the mask, to mask_bits bits of mask */
52         return true;
53 }
54
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)
57 {
58         return MatchCIDR(address, cidr_mask, false);
59 }
60
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.
64  *
65  * This will also attempt to match any leading usernames or nicknames on the mask, using
66  * match(), when match_with_username is true.
67  */
68 bool irc::sockets::MatchCIDR(const std::string &address, const std::string &cidr_mask, bool match_with_username)
69 {
70         unsigned char addr_raw[16];
71         unsigned char mask_raw[16];
72         unsigned int bits = 0;
73
74         std::string address_copy;
75         std::string cidr_copy;
76
77         /* The caller is trying to match ident@<mask>/bits.
78          * Chop off the ident@ portion, use match() on it
79          * seperately.
80          */
81         if (match_with_username)
82         {
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('@');
86
87                 /* Both strings have an @ symbol in them */
88                 if (username_mask_pos != std::string::npos && username_addr_pos != std::string::npos)
89                 {
90                         /* Try and match() the strings before the @
91                          * symbols, and recursively call MatchCIDR without
92                          * username matching enabled to match the host part.
93                          */
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));
96                 }
97                 else
98                 {
99                         address_copy = address.substr(username_addr_pos + 1);
100                         cidr_copy = cidr_mask.substr(username_mask_pos + 1);
101                 }
102         }
103
104         in_addr  address_in4;
105         in_addr  mask_in4;
106
107         std::string::size_type bits_chars = cidr_copy.rfind('/');
108
109         if (bits_chars != std::string::npos)
110         {
111                 bits = atoi(cidr_copy.substr(bits_chars + 1).c_str());
112         }
113         else
114         {
115                 /* No 'number of bits' field! */
116                 return false;
117         }
118
119 #ifdef SUPPORT_IP6LINKS
120         in6_addr address_in6;
121         in6_addr mask_in6;
122
123         if (inet_pton(AF_INET6, address_copy.c_str(), &address_in6) > 0)
124         {
125                 if (inet_pton(AF_INET6, cidr_copy.c_str(), &mask_in6) > 0)
126                 {
127                         memcpy(&addr_raw, &address_in6.s6_addr, 16);
128                         memcpy(&mask_raw, &mask_in6.s6_addr, 16);
129
130                         if (bits > 128)
131                                 bits = 128;
132                 }
133                 else
134                 {
135                         /* The address was valid ipv6, but the mask
136                          * that goes with it wasnt.
137                          */
138                         return false;
139                 }
140         }
141         else
142 #endif
143         if (inet_pton(AF_INET, address_copy.c_str(), &address_in4) > 0)
144         {
145                 if (inet_pton(AF_INET, cidr_copy.c_str(), &mask_in4) > 0)
146                 {
147                         memcpy(&addr_raw, &address_in4.s_addr, 4);
148                         memcpy(&mask_raw, &mask_in4.s_addr, 4);
149
150                         if (bits > 32)
151                                 bits = 32;
152                 }
153                 else
154                 {
155                         /* The address was valid ipv4,
156                          * but the mask that went with it wasnt.
157                          */
158                         return false;
159                 }
160         }
161         else
162         {
163                 /* The address was neither ipv4 or ipv6 */
164                 return false;
165         }
166
167         /* Low-level-match the bits in the raw data */
168         return MatchCIDRBits(addr_raw, mask_raw, bits);
169 }
170
171