xref: /illumos-gate/usr/src/cmd/ipf/lib/genmask.c (revision f3ac678143127d4c6c1793fadabb5ded04e127b6)
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: genmask.c,v 1.7 2003/11/11 13:40:15 darrenr Exp $
7  */
8 
9 #include "ipf.h"
10 
11 
genmask(msk,mskp)12 int genmask(msk, mskp)
13 char *msk;
14 u_32_t *mskp;
15 {
16 	char *endptr = 0L;
17 	int bits;
18 
19 	if (strchr(msk, '.') || strchr(msk, 'x') || strchr(msk, ':')) {
20 		/* possibly of the form xxx.xxx.xxx.xxx
21 		 * or 0xYYYYYYYY */
22 #ifdef	USE_INET6
23 		if (use_inet6) {
24 			if (inet_pton(AF_INET6, msk, mskp) != 1)
25 				return -1;
26 		} else
27 #endif
28 		if (inet_aton(msk, (struct in_addr *)mskp) == 0)
29 			return -1;
30 	} else {
31 		/*
32 		 * set x most significant bits
33 		 */
34 		bits = (int)strtol(msk, &endptr, 0);
35 #ifdef	USE_INET6
36 		if ((*endptr != '\0') ||
37 		    ((bits > 32) && !use_inet6) || (bits < 0) ||
38 		    ((bits > 128) && use_inet6))
39 #else
40 		if (*endptr != '\0' || bits > 32 || bits < 0)
41 #endif
42 			return -1;
43 #ifdef	USE_INET6
44 		if (use_inet6)
45 			fill6bits(bits, mskp);
46 		else
47 #endif
48 		if (bits == 0)
49 			*mskp = 0;
50 		else
51 			*mskp = htonl(0xffffffff << (32 - bits));
52 	}
53 	return 0;
54 }
55