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