]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cidr.cpp
Port from hottpd: Split src/socket.cpp out into src/cidr.cpp
[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(unsigned char* address, 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 char* address, const char* 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 char* address, const char* 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         char* mask = NULL;
74
75         /* The caller is trying to match ident@<mask>/bits.
76          * Chop off the ident@ portion, use match() on it
77          * seperately.
78          */
79         if (match_with_username)
80         {
81                 /* Duplicate the strings, and try to find the position
82                  * of the @ symbol in each
83                  */
84                 char* address_dupe = strdup(address);
85                 char* cidr_dupe = strdup(cidr_mask);
86         
87                 /* Use strchr not strrchr, because its going to be nearer to the left */
88                 char* username_mask_pos = strrchr(cidr_dupe, '@');
89                 char* username_addr_pos = strrchr(address_dupe, '@');
90
91                 /* Both strings have an @ symbol in them */
92                 if (username_mask_pos && username_addr_pos)
93                 {
94                         /* Zero out the location of the @ symbol */
95                         *username_mask_pos = *username_addr_pos = 0;
96
97                         /* Try and match() the strings before the @
98                          * symbols, and recursively call MatchCIDR without
99                          * username matching enabled to match the host part.
100                          */
101                         bool result = (match(address_dupe, cidr_dupe) && MatchCIDR(username_addr_pos + 1, username_mask_pos + 1, false));
102
103                         /* Free the stuff we created */
104                         free(address_dupe);
105                         free(cidr_dupe);
106
107                         /* Return a result */
108                         return result;
109                 }
110                 else
111                 {
112                         /* One or both didnt have an @ in,
113                          * just match as CIDR
114                          */
115                         free(address_dupe);
116                         free(cidr_dupe);
117                         mask = strdup(cidr_mask);
118                 }
119         }
120         else
121         {
122                 /* Make a copy of the cidr mask string,
123                  * we're going to change it
124                  */
125                 mask = strdup(cidr_mask);
126         }
127
128         in_addr  address_in4;
129         in_addr  mask_in4;
130
131
132         /* Use strrchr for this, its nearer to the right */
133         char* bits_chars = strrchr(mask,'/');
134
135         if (bits_chars)
136         {
137                 bits = atoi(bits_chars + 1);
138                 *bits_chars = 0;
139         }
140         else
141         {
142                 /* No 'number of bits' field! */
143                 free(mask);
144                 return false;
145         }
146
147 #ifdef SUPPORT_IP6LINKS
148         in6_addr address_in6;
149         in6_addr mask_in6;
150
151         if (inet_pton(AF_INET6, address, &address_in6) > 0)
152         {
153                 if (inet_pton(AF_INET6, mask, &mask_in6) > 0)
154                 {
155                         memcpy(&addr_raw, &address_in6.s6_addr, 16);
156                         memcpy(&mask_raw, &mask_in6.s6_addr, 16);
157
158                         if (bits > 128)
159                                 bits = 128;
160                 }
161                 else
162                 {
163                         /* The address was valid ipv6, but the mask
164                          * that goes with it wasnt.
165                          */
166                         free(mask);
167                         return false;
168                 }
169         }
170         else
171 #endif
172         if (inet_pton(AF_INET, address, &address_in4) > 0)
173         {
174                 if (inet_pton(AF_INET, mask, &mask_in4) > 0)
175                 {
176                         memcpy(&addr_raw, &address_in4.s_addr, 4);
177                         memcpy(&mask_raw, &mask_in4.s_addr, 4);
178
179                         if (bits > 32)
180                                 bits = 32;
181                 }
182                 else
183                 {
184                         /* The address was valid ipv4,
185                          * but the mask that went with it wasnt.
186                          */
187                         free(mask);
188                         return false;
189                 }
190         }
191         else
192         {
193                 /* The address was neither ipv4 or ipv6 */
194                 free(mask);
195                 return false;
196         }
197
198         /* Low-level-match the bits in the raw data */
199         free(mask);
200         return MatchCIDRBits(addr_raw, mask_raw, bits);
201 }
202
203
204