]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cidr.cpp
Remove last vestige of libircdfoo, by changing tag into a single identifier marking...
[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 */
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         else
104         {
105                 address_copy.assign(address);
106                 cidr_copy.assign(cidr_mask);
107         }
108
109         in_addr  address_in4;
110         in_addr  mask_in4;
111
112         std::string::size_type bits_chars = cidr_copy.rfind('/');
113
114         if (bits_chars != std::string::npos)
115         {
116                 bits = atoi(cidr_copy.substr(bits_chars + 1).c_str());
117                 cidr_copy.erase(bits_chars, cidr_copy.length() - bits_chars);
118         }
119         else
120         {
121                 /* No 'number of bits' field! */
122                 return false;
123         }
124
125 #ifdef SUPPORT_IP6LINKS
126         in6_addr address_in6;
127         in6_addr mask_in6;
128
129         if (inet_pton(AF_INET6, address_copy.c_str(), &address_in6) > 0)
130         {
131                 if (inet_pton(AF_INET6, cidr_copy.c_str(), &mask_in6) > 0)
132                 {
133                         memcpy(&addr_raw, &address_in6.s6_addr, 16);
134                         memcpy(&mask_raw, &mask_in6.s6_addr, 16);
135
136                         if (bits > 128)
137                                 bits = 128;
138                 }
139                 else
140                 {
141                         /* The address was valid ipv6, but the mask
142                          * that goes with it wasnt.
143                          */
144                         return false;
145                 }
146         }
147         else
148 #endif
149         if (inet_pton(AF_INET, address_copy.c_str(), &address_in4) > 0)
150         {
151                 if (inet_pton(AF_INET, cidr_copy.c_str(), &mask_in4) > 0)
152                 {
153                         memcpy(&addr_raw, &address_in4.s_addr, 4);
154                         memcpy(&mask_raw, &mask_in4.s_addr, 4);
155
156                         if (bits > 32)
157                                 bits = 32;
158                 }
159                 else
160                 {
161                         /* The address was valid ipv4,
162                          * but the mask that went with it wasnt.
163                          */
164                         return false;
165                 }
166         }
167         else
168         {
169                 /* The address was neither ipv4 or ipv6 */
170                 return false;
171         }
172
173         /* Low-level-match the bits in the raw data */
174         return MatchCIDRBits(addr_raw, mask_raw, bits);
175 }
176
177