xref: /illumos-gate/usr/src/cmd/ipf/lib/ntomask.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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 #include "ipf.h"
11 
12 int ntomask(v, nbits, ap)
13 int v, nbits;
14 u_32_t *ap;
15 {
16 	u_32_t mask;
17 
18 	if (nbits < 0)
19 		return -1;
20 
21 	switch (v)
22 	{
23 	case 4 :
24 		if (nbits > 32 || use_inet6 != 0)
25 			return -1;
26 		if (nbits == 0) {
27 			mask = 0;
28 		} else {
29 			mask = 0xffffffff;
30 			mask <<= (32 - nbits);
31 		}
32 		*ap = htonl(mask);
33 		break;
34 
35 	case 6 :
36 		if (nbits > 128)
37 			return -1;
38 		fill6bits(nbits, ap);
39 		break;
40 
41 	default :
42 		return -1;
43 	}
44 	return 0;
45 }
46