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 13 /* 14 * count consecutive 1's in bit mask. If the mask generated by counting 15 * consecutive 1's is different to that passed, return -1, else return # 16 * of bits. 17 */ 18 int 19 count4bits(u_int ip) 20 { 21 int cnt = 0, i, j; 22 u_int ipn; 23 24 ip = ipn = ntohl(ip); 25 for (i = 32; i; i--, ipn *= 2) 26 if (ipn & 0x80000000) 27 cnt++; 28 else 29 break; 30 ipn = 0; 31 for (i = 32, j = cnt; i; i--, j--) { 32 ipn *= 2; 33 if (j > 0) 34 ipn++; 35 } 36 if (ipn == ip) 37 return (cnt); 38 return (-1); 39 } 40