summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/genmask.c
blob: 75193e3ea398291c9b78c98cdf1b566492f094d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright (C) 2012 by Darren Reed.
 *
 * See the IPFILTER.LICENCE file for details on licencing.
 *
 * $Id$
 */

#include "ipf.h"


int genmask(family, msk, mskp)
	int family;
	char *msk;
	i6addr_t *mskp;
{
	char *endptr = 0L;
	u_32_t addr;
	int bits;

	if (strchr(msk, '.') || strchr(msk, 'x') || strchr(msk, ':')) {
		/* possibly of the form xxx.xxx.xxx.xxx
		 * or 0xYYYYYYYY */
		switch (family)
		{
#ifdef USE_INET6
		case AF_INET6 :
			if (inet_pton(AF_INET6, msk, &mskp->in4) != 1)
				return -1;
			break;
#endif
		case AF_INET :
			if (inet_aton(msk, &mskp->in4) == 0)
				return -1;
			break;
		default :
			return -1;
			/*NOTREACHED*/
		}
	} else {
		/*
		 * set x most significant bits
		 */
		bits = (int)strtol(msk, &endptr, 0);

		switch (family)
		{
		case AF_INET6 :
			if ((*endptr != '\0') || (bits < 0) || (bits > 128))
				return -1;
			fill6bits(bits, mskp->i6);
			break;
		case AF_INET :
			if (*endptr != '\0' || bits > 32 || bits < 0)
				return -1;
			if (bits == 0)
				addr = 0;
			else
				addr = htonl(0xffffffff << (32 - bits));
			mskp->in4.s_addr = addr;
			break;
		default :
			return -1;
			/*NOTREACHED*/
		}
	}
	return 0;
}
OpenPOWER on IntegriCloud