#!/usr/local/bin/php -q $end) continue; $tand = bindec($tbit & sprintf('%032b',$tend)); if($start == $tand) { //echo inet_ntoa($start)."\t".inet_ntoa($tend)."\t". // inet_ntoa($tnet)."\n"; $nstart = $tend + 1; // change break; } } return array($tend,$tnet); } function is_subnet($some) { $some = trim($some); if(preg_match('/^[\d]+\.[\d]+\.[\d]+\.[\d]+$/',$some)) { $some = sprintf('%u',ip2long($some)); } $dec = abs($some); $bits = decbin($dec); //echo $bits."\n"; // debug return (boolean) preg_match('/^[1]+[0]*$/',$bits); } function is_network($some) { return FALSE; // all false $some = trim($some); if(!preg_match('/^[\d]+\.[\d]+\.[\d]+\.[\d]+$/',$some)) { $some = long2ip($some); } $tmp = explode('.',$some); $r = TRUE; if($tmp[3] % 2) $r = FALSE; else if($tmp[2] % 2) $r = FALSE; else if($tmp[1] % 2) $r = FALSE; else if($tmp[0] % 2) $r = FALSE; return (boolean) $r; } function inet_ntocidr($netmask) { $netmask = trim($netmask); $bits = decbin(abs(sprintf('%u',ip2long($netmask)))); $cidr = strpos($bits,'0') + 0; return ($cidr > 0) ? $cidr : 32; } function inet_aton($ipaddr) { return sprintf('%u',ip2long($ipaddr)); } function inet_ntoa($base) { return long2ip($base); } function inet_ntob($base) { return sprintf('%032b',$base); } function inet_atob($ipaddr) { return sprintf('%032b',ip2long($ipaddr)); } function inet_btoa($bits) { return long2ip(sprintf('%u',$bits)); } function _file($file) { if(!$fp = fopen($file,'r')) return; while(!feof($fp)) $r[] = trim(fgets($fp,1024)); fclose($fp); return $r; // array } set_time_limit(0); //ini_set('memory_limit','512M'); ## file format: ## korean-ispname 6KANet 203.254.0.0 203.254.63.255 19940401 ## if(!file_exists($file=$_SERVER['argv'][1])) exit; $lists = file($file); // dot.ipv4 //$lists = _file($file); $size = sizeof($lists); ## get array and get block group ## for($i=0; $i<$size; $i++) { list($kblock,$eblock,$s,$e) = preg_split('/\t+/',trim($lists[$i])); $kblock = trim($kblock); $eblock = trim($eblock); $e = trim($e); $s = trim($s); if(preg_match('/^#/',$kblock) || !$kblock) continue; $s = inet_aton($s); $e = inet_aton($e); //$netmask = inet_ntoa($j + inet_aton('255.255.255.255') - inet_aton($e)); //$krblock[$eblock][$s] = $kblock."\t".$eblock."\t".$s."\t".$e."\t".$netmask; //$krblock[$eblock][$s] = $kblock."\t".$eblock."\t".$s."\t".$e; $krblock[$eblock][$s] = array($kblock,$eblock,$s,$e); } //print_r($krblock); //exit; $merge = array(); ## merge ## foreach($krblock AS $arr) { unset($start); // reset ksort($arr); $new = array_values($arr); $size = sizeof($new); for($i=0; $i<$size; $i++) { $total++; $kname = $new[$i][0]; $ename = $new[$i][1]; $start = $start ? $start : $new[$i][2]; $end = $new[$i][3]; if(($new[$i][3] + 1) == $new[$i+1][2]) { continue; // not next skip } $m++; // count of merge ## patch 2005.11.07, is if(TRUE) ## $nn = $start + inet_aton('255.255.255.255') - $end; $nu = $end - $start + 1; // number of host //if(!is_subnet($nn = $start + inet_aton('255.255.255.255') - $end) || !is_network($start)) //{ // $nu = $end - $start + 1; // number of host //} //$ps = inet_ntoa($start); //$pe = inet_ntoa($end); //$netmask = inet_ntoa($nn); //echo "=>$kname\t$ename\t$ps\t$pe\t$netmask\t<--$ck ($nu)\n\n"; $merge[$ename][] = array($kname,$ename,$start,$end,$nu,$nn); unset($nu); unset($start); } } //print_r($merge); //exit; $class_a = inet_aton('127.0.0.0'); $class_b = inet_aton('192.0.0.0'); $me = 0; foreach($merge AS $arr) { foreach($arr AS $tmp) { //list($kname,$ename,$start,$end,$nu) = each($tmp); $kname = $tmp[0]; $ename = $tmp[1]; $start = $tmp[2]; $end = $tmp[3]; $nu = $tmp[4]; $nn = $tmp[5]; if($nu) { $ln = strlen(preg_replace('/^([1]+).*$/','\1',decbin($nn))); $sbit = decbin($start); if($start < $class_a) $ln = 1; // A class else if($start < $class_b) $ln = 8; // B class while($start < $end) { list($tend,$mask) = search_net($ln,$start,$end); $ps = inet_ntoa($start); $pe = inet_ntoa($tend); $netmask = inet_ntoa($mask); $idx = explode('.',$ps); //echo implode("\t",$tmp)."\n"; //echo "$kname\t$ename\t$ps\t$pe\t$netmask\n"; //echo "\$_IPNETWORK['$ename'][] = array('$ps','$pe','$netmask','$kname');\n"; //$_IPNETWORK[$idx[0]]['netmask'][] = (string) inet_ntob($mask); $_IPNETWORK[$idx[0]]['netmask'][] = $netmask; $_IPNETWORK[$idx[0]][inet_aton($ps)] = array($ps,$pe,$netmask,$kname,$ename); $start = $tend + 1; // change $me++; } } else { $ps = inet_ntoa($start); $pe = inet_ntoa($end); $mask = $start + inet_aton('255.255.255.255') - $end; $netmask = inet_ntoa($mask); $idx = explode('.',$ps); //echo implode("\t",$tmp)."\n"; //echo "$kname\t$ename\t$ps\t$pe\t$netmask\n"; //echo "\$_IPNETWORK['$ename'][] = array('$ps','$pe','$netmask','$kname');\n"; //$_IPNETWORK[$idx[0]]['netmask'][] = (string) inet_ntob($mask); $_IPNETWORK[$idx[0]]['netmask'][] = $netmask; $_IPNETWORK[$idx[0]][inet_aton($ps)] = array($ps,$pe,$netmask,$kname,$ename); $me++; } } } //print_r($_IPNETWORK); //exit; $ipas = array_keys($_IPNETWORK); sort($ipas); foreach($ipas AS $ipa) { //$_IPNETWORK[$ipa]['netmask'] = array_values(array_unique($_IPNETWORK[$ipa]['netmask'])); $_NETMASK = array_unique($_IPNETWORK[$ipa]['netmask']); sort($_NETMASK); $v = array_values($_NETMASK); $s = sizeof($v) - 1; echo "\$_NET[$ipa] = array("; for($i=0; $i<$s; $i++) echo "'".$v[$i]."',"; echo "'".$v[$s]."');\n"; } foreach($ipas AS $ipa) { unset($_IPNETWORK[$ipa]['netmask']); ksort($_IPNETWORK[$ipa]); foreach($_IPNETWORK[$ipa] AS $k=>$arr) { echo "\$_ISP['$k'] = array('$arr[0]','$arr[1]','$arr[2]','$arr[3]','$arr[4]');\n"; } } //print_r($ipas); //print_r($_IPNETWORK); echo $total.' '.$me."\n"; exit; ?>