xref: /netbsd-src/external/bsd/ipf/dist/lib/ntomask.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: ntomask.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
2bc4097aaSchristos 
3bc4097aaSchristos /*
4c9d5dc6cSdarrenr  * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos  *
6bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos  *
8*13885a66Sdarrenr  * Id: ntomask.c,v 1.1.1.2 2012/07/22 13:44:39 darrenr Exp $
9bc4097aaSchristos  */
10bc4097aaSchristos 
11bc4097aaSchristos #include "ipf.h"
12bc4097aaSchristos 
ntomask(family,nbits,ap)13bc4097aaSchristos int ntomask(family, nbits, ap)
14bc4097aaSchristos 	int family, nbits;
15bc4097aaSchristos 	u_32_t *ap;
16bc4097aaSchristos {
17bc4097aaSchristos 	u_32_t mask;
18bc4097aaSchristos 
19bc4097aaSchristos 	if (nbits < 0)
20bc4097aaSchristos 		return -1;
21bc4097aaSchristos 
22bc4097aaSchristos 	switch (family)
23bc4097aaSchristos 	{
24bc4097aaSchristos 	case AF_INET :
25bc4097aaSchristos 		if (nbits > 32 || use_inet6 == 1)
26bc4097aaSchristos 			return -1;
27bc4097aaSchristos 		if (nbits == 0) {
28bc4097aaSchristos 			mask = 0;
29bc4097aaSchristos 		} else {
30bc4097aaSchristos 			mask = 0xffffffff;
31bc4097aaSchristos 			mask <<= (32 - nbits);
32bc4097aaSchristos 		}
33bc4097aaSchristos 		*ap = htonl(mask);
34bc4097aaSchristos 		break;
35bc4097aaSchristos 
36bc4097aaSchristos 	case 0 :
37bc4097aaSchristos 	case AF_INET6 :
38bc4097aaSchristos 		if ((nbits > 128) || (use_inet6 == -1))
39bc4097aaSchristos 			return -1;
40bc4097aaSchristos 		fill6bits(nbits, ap);
41bc4097aaSchristos 		break;
42bc4097aaSchristos 
43bc4097aaSchristos 	default :
44bc4097aaSchristos 		return -1;
45bc4097aaSchristos 	}
46bc4097aaSchristos 	return 0;
47bc4097aaSchristos }
48