xref: /illumos-gate/usr/src/cmd/ipf/lib/common/ntomask.c (revision 7663b816)
1 /*
2  * Copyright (C) 2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
7  * Use is subject to license terms.
8  */
9 
10 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11 
12 #include "ipf.h"
13 
ntomask(v,nbits,ap)14 int ntomask(v, nbits, ap)
15 int v, nbits;
16 u_32_t *ap;
17 {
18 	u_32_t mask;
19 
20 	if (nbits < 0)
21 		return -1;
22 
23 	switch (v)
24 	{
25 	case 4 :
26 		if (nbits > 32 || use_inet6 != 0)
27 			return -1;
28 		if (nbits == 0) {
29 			mask = 0;
30 		} else {
31 			mask = 0xffffffff;
32 			mask <<= (32 - nbits);
33 		}
34 		*ap = htonl(mask);
35 		break;
36 
37 	case 6 :
38 		if (nbits > 128)
39 			return -1;
40 		fill6bits(nbits, ap);
41 		break;
42 
43 	default :
44 		return -1;
45 	}
46 	return 0;
47 }
48