1 /* $FreeBSD$ */ 2 3 /* 4 * Copyright (C) 2012 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 * 8 * $Id$ 9 */ 10 11 #include "ipf.h" 12 13 int 14 ntomask(int family, int nbits, u_32_t *ap) 15 { 16 u_32_t mask; 17 18 if (nbits < 0) 19 return (-1); 20 21 switch (family) 22 { 23 case AF_INET : 24 if (nbits > 32 || use_inet6 == 1) 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 0 : 36 case AF_INET6 : 37 if ((nbits > 128) || (use_inet6 == -1)) 38 return (-1); 39 fill6bits(nbits, ap); 40 break; 41 42 default : 43 return (-1); 44 } 45 return (0); 46 } 47