ntomask.c revision 170268
1/* $FreeBSD: head/contrib/ipfilter/lib/ntomask.c 170268 2007-06-04 02:54:36Z darrenr $ */ 2 3/* 4 * Copyright (C) 2002-2005 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 * 8 * $Id: ntomask.c,v 1.6.2.1 2006/06/16 17:21:07 darrenr Exp $ 9 */ 10 11#include "ipf.h" 12 13int ntomask(v, nbits, ap) 14int v, nbits; 15u_32_t *ap; 16{ 17 u_32_t mask; 18 19 if (nbits < 0) 20 return -1; 21 22 switch (v) 23 { 24 case 4 : 25 if (nbits > 32 || use_inet6 != 0) 26 return -1; 27 if (nbits == 0) { 28 mask = 0; 29 } else { 30 mask = 0xffffffff; 31 mask <<= (32 - nbits); 32 } 33 *ap = htonl(mask); 34 break; 35 36 case 6 : 37 if ((nbits > 128) || (use_inet6 == 0)) 38 return -1; 39 fill6bits(nbits, ap); 40 break; 41 42 default : 43 return -1; 44 } 45 return 0; 46} 47