xref: /illumos-gate/usr/src/cmd/ipf/lib/common/ntomask.c (revision 7663b816)
1*7663b816Sml /*
2*7663b816Sml  * Copyright (C) 2002 by Darren Reed.
3*7663b816Sml  *
4*7663b816Sml  * See the IPFILTER.LICENCE file for details on licencing.
5*7663b816Sml  *
6*7663b816Sml  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
7*7663b816Sml  * Use is subject to license terms.
8*7663b816Sml  */
9*7663b816Sml 
10*7663b816Sml #pragma ident	"%Z%%M%	%I%	%E% SMI"
11*7663b816Sml 
127c478bd9Sstevel@tonic-gate #include "ipf.h"
137c478bd9Sstevel@tonic-gate 
ntomask(v,nbits,ap)147c478bd9Sstevel@tonic-gate int ntomask(v, nbits, ap)
157c478bd9Sstevel@tonic-gate int v, nbits;
167c478bd9Sstevel@tonic-gate u_32_t *ap;
177c478bd9Sstevel@tonic-gate {
187c478bd9Sstevel@tonic-gate 	u_32_t mask;
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate 	if (nbits < 0)
217c478bd9Sstevel@tonic-gate 		return -1;
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate 	switch (v)
247c478bd9Sstevel@tonic-gate 	{
257c478bd9Sstevel@tonic-gate 	case 4 :
267c478bd9Sstevel@tonic-gate 		if (nbits > 32 || use_inet6 != 0)
277c478bd9Sstevel@tonic-gate 			return -1;
287c478bd9Sstevel@tonic-gate 		if (nbits == 0) {
297c478bd9Sstevel@tonic-gate 			mask = 0;
307c478bd9Sstevel@tonic-gate 		} else {
317c478bd9Sstevel@tonic-gate 			mask = 0xffffffff;
327c478bd9Sstevel@tonic-gate 			mask <<= (32 - nbits);
337c478bd9Sstevel@tonic-gate 		}
347c478bd9Sstevel@tonic-gate 		*ap = htonl(mask);
357c478bd9Sstevel@tonic-gate 		break;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 	case 6 :
38*7663b816Sml 		if (nbits > 128)
397c478bd9Sstevel@tonic-gate 			return -1;
407c478bd9Sstevel@tonic-gate 		fill6bits(nbits, ap);
417c478bd9Sstevel@tonic-gate 		break;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 	default :
447c478bd9Sstevel@tonic-gate 		return -1;
457c478bd9Sstevel@tonic-gate 	}
467c478bd9Sstevel@tonic-gate 	return 0;
477c478bd9Sstevel@tonic-gate }
48