141edb306SCy Schubert 241edb306SCy Schubert /* 341edb306SCy Schubert * Copyright (C) 2012 by Darren Reed. 441edb306SCy Schubert * 541edb306SCy Schubert * See the IPFILTER.LICENCE file for details on licencing. 641edb306SCy Schubert * 741edb306SCy Schubert * $Id$ 841edb306SCy Schubert */ 941edb306SCy Schubert 1041edb306SCy Schubert #include "ipf.h" 1141edb306SCy Schubert 1241edb306SCy Schubert 1341edb306SCy Schubert /* 1441edb306SCy Schubert * count consecutive 1's in bit mask. If the mask generated by counting 1541edb306SCy Schubert * consecutive 1's is different to that passed, return -1, else return # 1641edb306SCy Schubert * of bits. 1741edb306SCy Schubert */ 18efeb8bffSCy Schubert int count4bits(u_int ip)19efeb8bffSCy Schubertcount4bits(u_int ip) 2041edb306SCy Schubert { 2141edb306SCy Schubert int cnt = 0, i, j; 2241edb306SCy Schubert u_int ipn; 2341edb306SCy Schubert 2441edb306SCy Schubert ip = ipn = ntohl(ip); 2541edb306SCy Schubert for (i = 32; i; i--, ipn *= 2) 2641edb306SCy Schubert if (ipn & 0x80000000) 2741edb306SCy Schubert cnt++; 2841edb306SCy Schubert else 2941edb306SCy Schubert break; 3041edb306SCy Schubert ipn = 0; 3141edb306SCy Schubert for (i = 32, j = cnt; i; i--, j--) { 3241edb306SCy Schubert ipn *= 2; 3341edb306SCy Schubert if (j > 0) 3441edb306SCy Schubert ipn++; 3541edb306SCy Schubert } 3641edb306SCy Schubert if (ipn == ip) 372582ae57SCy Schubert return (cnt); 382582ae57SCy Schubert return (-1); 3941edb306SCy Schubert } 40