{"id":1906,"date":"2011-04-27T11:16:44","date_gmt":"2011-04-27T15:16:44","guid":{"rendered":"http:\/\/www.kayak2u.com\/blog\/?p=1906"},"modified":"2011-04-27T11:16:44","modified_gmt":"2011-04-27T15:16:44","slug":"optimizin-ip-range-to-cidr-functions","status":"publish","type":"post","link":"http:\/\/www.kayak2u.com\/?p=1906","title":{"rendered":"Optimizin&#8217; IP range to CIDR functions"},"content":{"rendered":"<p>Looking for helper functions for the latest enhancements to my almost-Alpha version <a href=\"http:\/\/wordpress.org\/extend\/plugins\/ecstatic\/stats\/\" target=\"_blank\">opus<\/a> led <a href=\"http:\/\/www.php.net\/manual\/en\/ref.network.php#26063\" target=\"_blank\">here<\/a> and <a href=\"http:\/\/www.php.net\/manual\/en\/ref.network.php#25958\" target=\"_blank\">here<\/a>, before I settled on the code <a href=\"http:\/\/snipplr.com\/view\/15557\/cidr-class-for-ipv4\/\" target=\"_blank\">here<\/a>. &nbsp;It has to do with turning an IP range (123.456.789.0 &#8211; 123.456.789.255) into its CIDR equivalent(s) (123.456.789.0\/24), a task somewhat more difficult than it might first appear.<\/p>\n<p>The class below, which I&#39;ve stripped of extraneities (and also of three of its methods) gets right to business in a fast, efficient way without some of the convolutions and conditionals of the other examples.<\/p>\n<p>However, there does seem to be an exuberant number of ip2long()s and long2ip()s in there. &nbsp;The <code>rangeToCIDRList()<\/code> function starts by converting dotted decimal IP numbers to long integers, then converts them back to dotted decimals to pass along, which each receiving function promptly converts into long integers. &nbsp;Is all that converting really necessary? &nbsp;According to my investigations: &nbsp;Nope.<\/p>\n<pre>class CIDR {\r\npublic static function countSetbits($int){\r\n\t$int = $int - (($int &gt;&gt; 1) &amp; 0x55555555);\r\n\t$int = ($int &amp; 0x33333333) + (($int &gt;&gt; 2) &amp; 0x33333333);\r\n\treturn (($int + ($int &gt;&gt; 4) &amp; 0xF0F0F0F) * 0x1010101) &gt;&gt; 24;\r\n\t}\r\n\r\npublic static function validNetMask($netmask){\r\n<code>\/*****\t$netmask = ip2long($netmask); remove this line *\/<\/code>\r\n\t$neg = ((~(int)$netmask) &amp; 0xFFFFFFFF);\r\n\treturn (($neg + 1) &amp; $neg) === 0;\r\n\t}\r\n\r\npublic static function maskToCIDR($netmask){\r\n\tif(self::validNetMask($netmask))\r\n<code>\/*****\t\treturn self::countSetBits(ip2long($netmask)); remove *\/<\/code>\r\n\t\treturn self::countSetBits($netmask); <code>\/\/replaces line above<\/code>\r\n\telse\r\n\t\tthrow new Exception(&#39;Invalid Netmask&#39;);\r\n\t}\r\n\r\npublic static function maxBlock($ipinput) {\r\n<code>\/*****\treturn self::maskToCIDR(long2ip(-(ip2long($ipinput) &amp; -(ip2long($ipinput))))); remove *\/<\/code>\r\n\treturn self::maskToCIDR(-($ipinput &amp; -$ipinput)); <code>\/\/replaces line above<\/code>\r\n\t}\r\n\r\npublic static function rangeToCIDRList($startIPinput,$endIPinput) {\r\n\t$start = ip2long($startIPinput);\r\n\t$end = ip2long($endIPinput);\r\n\twhile($end &gt;= $start) {\r\n<code>\/*****\t\t$maxsize = self::maxBlock(long2ip($start)); remove *\/<\/code>\r\n\t\t$maxsize = self::maxBlock($start); <code>\/\/replaces line above<\/code>\r\n\t\t$maxdiff = 32 - intval(log($end - $start + 1) \/ log(2));\r\n\t\t$size = ($maxsize &gt; $maxdiff) ? $maxsize : $maxdiff;\r\n\t\t$listCIDRs[] = long2ip($start) . &quot;\/$size&quot;;\r\n\t\t$start += pow(2, (32 - $size));\r\n\t\t}\r\n\treturn $listCIDRs;\r\n\t}\r\n}\r\n<\/pre>\n<p>That saves like four ip2long()s and two long2ip()s each iteration. It don&#39;t get no better than that in my book.<\/p>\n<p>Still testing&#8230;&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking for helper functions for the latest enhancements to my almost-Alpha version opus led here and here, before I settled on the code here. &nbsp;It has to do with turning an IP range (123.456.789.0 &#8211; 123.456.789.255) into its CIDR equivalent(s) (123.456.789.0\/24), a task somewhat more difficult than it might first appear. The class below, which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1906","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"http:\/\/www.kayak2u.com\/index.php?rest_route=\/wp\/v2\/posts\/1906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.kayak2u.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.kayak2u.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.kayak2u.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.kayak2u.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1906"}],"version-history":[{"count":0,"href":"http:\/\/www.kayak2u.com\/index.php?rest_route=\/wp\/v2\/posts\/1906\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.kayak2u.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.kayak2u.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1906"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.kayak2u.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}