ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/branches/ircd-hybrid-newconf/libio/net/hostmask.c
Revision: 1027
Committed: Sun Nov 8 13:01:13 2009 UTC (16 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 10137 byte(s)
Log Message:
- Move old 7.3 sources to branches/ircd-hybrid-newconf

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * hostmask.c: Provides support for IPv4/IPv6/DNS hostmasks.
4 *
5 * Copyright (C) 2002-2006 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26
27 #ifdef IPV6
28 static int try_parse_v6_netmask(const char *, struct irc_ssaddr *, int *);
29 #endif
30 static int try_parse_v4_netmask(const char *, struct irc_ssaddr *, int *);
31
32 #define DigitParse(ch) do { \
33 if (ch >= '0' && ch <= '9') \
34 ch = ch - '0'; \
35 else if (ch >= 'A' && ch <= 'F') \
36 ch = ch - 'A' + 10; \
37 else if (ch >= 'a' && ch <= 'f') \
38 ch = ch - 'a' + 10; \
39 } while(0);
40
41 /* The mask parser/type determination code... */
42
43 /* int try_parse_v6_netmask(const char *, struct irc_ssaddr *, int *);
44 * Input: A possible IPV6 address as a string.
45 * Output: An integer describing whether it is an IPV6 or hostmask,
46 * an address(if it is IPV6), a bitlength(if it is IPV6).
47 * Side effects: None
48 * Comments: Called from parse_netmask
49 */
50 /* Fixed so ::/0 (any IPv6 address) is valid
51 Also a bug in DigitParse above.
52 -Gozem 2002-07-19 gozem@linux.nu
53 */
54 #ifdef IPV6
55 static int
56 try_parse_v6_netmask(const char *text, struct irc_ssaddr *addr, int *b)
57 {
58 const char *p;
59 char c;
60 int d[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
61 int dp = 0;
62 int nyble = 4;
63 int finsert = -1;
64 int bits = 128;
65 int deficit = 0;
66 short dc[8];
67 struct sockaddr_in6 *v6 = (struct sockaddr_in6*) addr;
68
69 for (p = text; (c = *p); p++)
70 if (IsXDigit(c))
71 {
72 if (nyble == 0)
73 return HM_HOST;
74 DigitParse(c);
75 d[dp] |= c << (4 * --nyble);
76 }
77 else if (c == ':')
78 {
79 if (p > text && *(p - 1) == ':')
80 {
81 if (finsert >= 0)
82 return HM_HOST;
83 finsert = dp;
84 }
85 else
86 {
87 /* If there were less than 4 hex digits, e.g. :ABC: shift right
88 * so we don't interpret it as ABC0 -A1kmm */
89 d[dp] = d[dp] >> 4 * nyble;
90 nyble = 4;
91 if (++dp >= 8)
92 return HM_HOST;
93 }
94 }
95 else if (c == '*')
96 {
97 /* * must be last, and * is ambiguous if there is a ::... -A1kmm */
98 if (finsert >= 0 || *(p + 1) || dp == 0 || *(p - 1) != ':')
99 return HM_HOST;
100 bits = dp * 16;
101 }
102 else if (c == '/')
103 {
104 char *after;
105
106 d[dp] = d[dp] >> 4 * nyble;
107 dp++;
108 bits = strtoul(p + 1, &after, 10);
109 if (bits < 0 || *after)
110 return HM_HOST;
111 if (bits > dp * 4 && !(finsert >= 0 && bits <= 128))
112 return HM_HOST;
113 break;
114 }
115 else
116 return HM_HOST;
117
118 d[dp] = d[dp] >> 4 * nyble;
119 if (c == 0)
120 dp++;
121 if (finsert < 0 && bits == 0)
122 bits = dp * 16;
123 /* How many words are missing? -A1kmm */
124 deficit = bits / 16 + ((bits % 16) ? 1 : 0) - dp;
125 /* Now fill in the gaps(from ::) in the copied table... -A1kmm */
126 for (dp = 0, nyble = 0; dp < 8; dp++)
127 {
128 if (nyble == finsert && deficit)
129 {
130 dc[dp] = 0;
131 deficit--;
132 }
133 else
134 dc[dp] = d[nyble++];
135 }
136 /* Set unused bits to 0... -A1kmm */
137 if (bits < 128 && (bits % 16 != 0))
138 dc[bits / 16] &= ~((1 << (15 - bits % 16)) - 1);
139 for (dp = bits / 16 + (bits % 16 ? 1 : 0); dp < 8; dp++)
140 dc[dp] = 0;
141 /* And assign... -A1kmm */
142 if (addr)
143 {
144 v6->sin6_family = AF_INET6;
145 for (dp = 0; dp < 8; dp++)
146 /* The cast is a kludge to make netbsd work. */
147 ((unsigned short *)&v6->sin6_addr)[dp] = htons(dc[dp]);
148 }
149 if (b != NULL)
150 *b = bits;
151 return HM_IPV6;
152 }
153 #endif
154
155 /* int try_parse_v4_netmask(const char *, struct irc_ssaddr *, int *);
156 * Input: A possible IPV4 address as a string.
157 * Output: An integer describing whether it is an IPV4 or hostmask,
158 * an address(if it is IPV4), a bitlength(if it is IPV4).
159 * Side effects: None
160 * Comments: Called from parse_netmask
161 */
162 static int
163 try_parse_v4_netmask(const char *text, struct irc_ssaddr *addr, int *b)
164 {
165 const char *p;
166 const char *digits[4];
167 unsigned char addb[4];
168 int n = 0, bits = 0;
169 char c;
170 struct sockaddr_in *v4 = (struct sockaddr_in*) addr;
171
172 digits[n++] = text;
173
174 for (p = text; (c = *p); p++)
175 if (c >= '0' && c <= '9') /* empty */
176 ;
177 else if (c == '.')
178 {
179 if (n >= 4)
180 return HM_HOST;
181 digits[n++] = p + 1;
182 }
183 else if (c == '*')
184 {
185 if (*(p + 1) || n == 0 || *(p - 1) != '.')
186 return HM_HOST;
187 bits = (n - 1) * 8;
188 break;
189 }
190 else if (c == '/')
191 {
192 char *after;
193 bits = strtoul(p + 1, &after, 10);
194 if (!bits || bits > n*8 || *after)
195 return HM_HOST;
196 break;
197 }
198 else
199 return HM_HOST;
200
201 if (n < 4 && bits == 0)
202 bits = n * 8;
203 if (bits)
204 while (n < 4)
205 digits[n++] = "0";
206 for (n = 0; n < 4; n++)
207 addb[n] = strtoul(digits[n], NULL, 10);
208 if (bits == 0)
209 bits = 32;
210 /* Set unused bits to 0... -A1kmm */
211 if (bits < 32 && bits % 8)
212 addb[bits / 8] &= ~((1 << (8 - bits % 8)) - 1);
213 for (n = bits / 8 + (bits % 8 ? 1 : 0); n < 4; n++)
214 addb[n] = 0;
215 if (addr)
216 {
217 v4->sin_family = AF_INET;
218 v4->sin_addr.s_addr =
219 htonl(addb[0] << 24 | addb[1] << 16 | addb[2] << 8 | addb[3]);
220 }
221 if (b != NULL)
222 *b = bits;
223 return HM_IPV4;
224 }
225
226 /* int parse_netmask(const char *, struct irc_ssaddr *, int *);
227 * Input: A hostmask, or an IPV4/6 address.
228 * Output: An integer describing whether it is an IPV4, IPV6 address or a
229 * hostmask, an address(if it is an IP mask),
230 * a bitlength(if it is IP mask).
231 * Side effects: None
232 */
233 int
234 parse_netmask(const char *text, struct irc_ssaddr *addr, int *b)
235 {
236 addr->ss.sin_family = AF_UNSPEC;
237 #ifdef IPV6
238 if (strchr(text, ':'))
239 return try_parse_v6_netmask(text, addr, b);
240 #endif
241 if (strchr(text, '.'))
242 return try_parse_v4_netmask(text, addr, b);
243 return HM_HOST;
244 }
245
246 /* The address matching stuff... */
247 /* int match_ipv6(struct irc_ssaddr *, struct irc_ssaddr *, int)
248 * Input: An IP address, an IP mask, the number of bits in the mask.
249 * Output: if match, -1 else 0
250 * Side effects: None
251 */
252 #ifdef IPV6
253 int
254 match_ipv6(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits)
255 {
256 int i, m, n = bits / 8;
257 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
258 const struct sockaddr_in6 *v6mask = (const struct sockaddr_in6 *)mask;
259
260 for (i = 0; i < n; i++)
261 if (v6->sin6_addr.s6_addr[i] != v6mask->sin6_addr.s6_addr[i])
262 return 0;
263 if ((m = bits % 8) == 0)
264 return -1;
265 if ((v6->sin6_addr.s6_addr[n] & ~((1 << (8 - m)) - 1)) ==
266 v6mask->sin6_addr.s6_addr[n])
267 return -1;
268 return 0;
269 }
270 #endif
271
272 /* int match_ipv4(struct irc_ssaddr *, struct irc_ssaddr *, int)
273 * Input: An IP address, an IP mask, the number of bits in the mask.
274 * Output: if match, -1 else 0
275 * Side Effects: None
276 */
277 int
278 match_ipv4(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits)
279 {
280 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
281 const struct sockaddr_in *v4mask = (const struct sockaddr_in *)mask;
282
283 if ((ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1)) !=
284 ntohl(v4mask->sin_addr.s_addr))
285 return 0;
286
287 return -1;
288 }
289
290 /*
291 * mask_addr
292 *
293 * inputs - pointer to the ip to mask
294 * - bitlen
295 * output - NONE
296 * side effects -
297 */
298 void
299 mask_addr(struct irc_ssaddr *ip, int bits)
300 {
301 int mask;
302 #ifdef IPV6
303 struct sockaddr_in6 *v6_base_ip;
304 int i, m, n;
305 #endif
306 struct sockaddr_in *v4_base_ip;
307
308 #ifdef IPV6
309 if (ip->ss.ss_family != AF_INET6)
310 #endif
311 {
312 v4_base_ip = (struct sockaddr_in*)ip;
313 mask = ~((1 << (32 - bits)) - 1);
314 v4_base_ip->sin_addr.s_addr =
315 htonl(ntohl(v4_base_ip->sin_addr.s_addr) & mask);
316 }
317 #ifdef IPV6
318 else
319 {
320 n = bits / 8;
321 m = bits % 8;
322 v6_base_ip = (struct sockaddr_in6*)ip;
323
324 mask = ~((1 << (8 - m)) -1 );
325 v6_base_ip->sin6_addr.s6_addr[n] = v6_base_ip->sin6_addr.s6_addr[n] & mask;
326 for (i = n + 1; i < 16; i++)
327 v6_base_ip->sin6_addr.s6_addr[i] = 0;
328 }
329 #endif
330 }
331
332 /*
333 * hash_ip()
334 *
335 * Hash algorithm for IP addresses.
336 *
337 * inputs:
338 * addr - the address with sin_family set
339 * bits - number of significant bits
340 * size - number of buckets
341 * output: hash value
342 */
343 static unsigned int
344 hash_ipv4(struct sockaddr_in *v4, int bits, unsigned int size)
345 {
346 if (bits < 0)
347 bits = 32;
348
349 if (bits != 0)
350 {
351 unsigned int av = ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1);
352 return (av ^ (av >> 12) ^ (av >> 24)) % size;
353 }
354
355 return 0;
356 }
357
358 #ifdef IPV6
359 static unsigned int
360 hash_ipv6(struct sockaddr_in6 *v6, int bits, unsigned int size)
361 {
362 unsigned int av = 0, n;
363
364 if (bits < 0)
365 bits = 128;
366
367 for (n = 0; n < 16; n++)
368 if (bits >= 8)
369 {
370 av ^= v6->sin6_addr.s6_addr[n];
371 bits -= 8;
372 }
373 else
374 {
375 if (bits > 0)
376 av ^= v6->sin6_addr.s6_addr[n] & ~((1 << (8 - bits)) - 1);
377 break;
378 }
379
380 return av % size;
381 }
382 #endif
383
384 unsigned int
385 hash_ip(const struct irc_ssaddr *addr, int bits, unsigned int size)
386 {
387 switch (addr->ss.sin_family)
388 {
389 case AF_INET:
390 return hash_ipv4((struct sockaddr_in *) addr, bits, size);
391 #ifdef IPV6
392 case AF_INET6:
393 return hash_ipv6((struct sockaddr_in6 *) addr, bits, size);
394 #endif
395 default:
396 return 0;
397 }
398 }

Properties

Name Value
svn:eol-style native
svn:keywords "Id Revision"