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