|  |  | 
    | 
 
 
| User Contributed Notes 네트워크 관련 함수
 |  add a note |  
| 
| oliviert at videotron dot ca 18-Feb-1999 09:31
 | 
 |  | If u wanna do one of these function on windows95, u have to install the
lastest winsock before , if else php will crash with a message of
wsock32.dll
 ()
 |  |  
| 
| philip at cornado dot c()m 04-Jul-2001 09:18
 | 
 |  | View a few Networking PHP PEAR classes here :
 |  |  
| 
| philip at cornado dot c()m 04-Jul-2001 09:20
 | 
 |  | More socket functions can be seen here :
 |  |  
| 
| philippe-at-cyberabuse.org 15-Dec-2001 02:46
 | 
 |  | PHP miss CIDR functions.
 This one will convert a CIDR like
this:
 0.0.0.0/16 -> 0.0.0.0 - 0.0.255.255
 127.0/16 ->
127.0.0.0 - 127.0.255.255
 etc...
 
 function cidrconv($net)
{
 $start=strtok($net,"/");
 $n=3-substr_count($net,
".");
 if ($n>0) { for ($i=$n;$i>0;$i--)
$start.=".0";
}
 $bits1=str_pad(decbin(ip2long($start)),32,"0","STR_PAD_LEFT");
 $net=pow(2,(32-substr(strstr($net,"/"),1)))-1;
 $bits2=str_pad(decbin($net),32,"0","STR_PAD_LEFT");
 for
($i=0;$i<32;$i++) {
 if ($bits1[$i]==$bits2[$i])
$final.=$bits1[$i];
 if ($bits1[$i]==1 and $bits2[$i]==0)
$final.=$bits1[$i];
 if ($bits1[$i]==0 and $bits2[$i]==1)
$final.=$bits2[$i];
 }
 return $start." -
".long2ip(bindec($final));
 }
 |  |  
| 
| philippe-at-cyberabuse.org 12-Oct-2002 10:49
 | 
 |  | ... and this one will do the opposite (o return NULL for invalid netblocks)
:
 1.0.0.0 1.0.255.255 -> 1.0.0.0/16
 1.0.0.0 1.3.255.255 ->
1.0.0.0/14
 192.168.0.0 192.168.0.255 ->
192.168.0.0/24
 
 function ip2cidr($ip_start,$ip_end)
{
 if(long2ip(ip2long($ip_start))!=$ip_start or
long2ip(ip2long($ip_end))!=$ip_end) return NULL;
 $ipl_start=(int)ip2long($ip_start);
 $ipl_end=(int)ip2long($ip_end);
 if($ipl_start>0
&& $ipl_end<0) $delta=($ipl_end+4294967296)-$ipl_start;
 else
$delta=$ipl_end-$ipl_start;
 $netmask=str_pad(decbin($delta),32,"0","STR_PAD_LEFT");
 if(ip2long($ip_start)==0
&& substr_count($netmask,"1")==32) return
"0.0.0.0/0";
 if($delta<0 or ($delta>0 &&
$delta%2==0)) return NULL;
 for($mask=0;$mask<32;$mask++)
if($netmask[$mask]==1)
break;
 if(substr_count($netmask,"0")!=$mask) return
NULL;
 return "$ip_start/$mask";
 }
 |  |  
| 
| trevor-hemsley at nospam dot dial dot pipex dot com 16-Oct-2002 06:53
 | 
 |  | Previous example of IP range to CIDR list does not cope with ranges as well
as the perl Net::Netmask range2cidrlist() function. In PHP this looks
like
 <?
 function imask($this)
 {
 // use base_convert not
dechex because dechex is broken and returns 0x80000000 instead of
0xffffffff
 return base_convert((pow(2,32) - pow(2, (32-$this)))), 10,
16);
 }
 
 function imaxblock($ibase, $tbit)
 {
 while ($tbit
> 0)
 {
 $im = hexdec(imask($tbit-1));
 $imand = $ibase &
$im;
 if ($imand !=
$ibase)
 {
 break;
 }
 $tbit--;
 }
 return
$tbit;
 }
 
 function range2cidrlist($istart, $iend)
 {
 // this
function returns an array of cidr lists that map the range given
 $s =
explode(".", $istart);
 // PHP ip2long does not handle leading
zeros on IP addresses! 172.016 comes back as 172.14, seems to be treated
as octal!
 $start = "";
 $dot = "";
 while
(list($key,$val) = each($s))
 {
 $start =
sprintf("%s%s%d",$start,$dot,$val);
 $dot =
".";
 }
 $end = "";
 $dot = "";
 $e
= explode(".",$iend);
 while (list($key,$val) =
each($e))
 {
 $end =
sprintf("%s%s%d",$end,$dot,$val);
 $dot =
".";
 }
 $start = ip2long($start);
 $end =
ip2long($end);
 $result = array();
 while ($end >
$start)
 {
 $maxsize = imaxblock($start,32);
 $x = log($end - $start
+ 1)/log(2);
 $maxdiff = floor(32 - floor($x));
 $ip =
long2ip($start);
 if ($maxsize < $maxdiff)
 {
 $maxsize =
$maxdiff;
 }
 array_push($result,"$ip/$maxsize");
 $start
+= pow(2, (32-$maxsize));
 }
 return $result;
 }
 ?>
 |  |  
| 
| 03-Apr-2003 04:11 | 
 |  | Alternative cidr_conv function - a little easier to follow
 function
cidr_conv($cidr_address) {
 $first = substr($cidr_address, 0,
strpos($cidr_address, "/"));
 $netmask =
substr(strstr($cidr_address, "/"), 1);
 
 $first_bin =
str_pad(decbin(ip2long($first)), 32, "0", STR_PAD_LEFT);
 $netmask_bin = str_pad(str_repeat("1", (integer)$netmask), 32,
"0", STR_PAD_RIGHT);
 
 for ($i = 0; $i < 32; $i++)
{
 if ($netmask_bin[$i] == "1")
 $last_bin .=
$first_bin[$i];
 else
 $last_bin .= "1";
 }
 
 $last = long2ip(bindec($last_bin));
 
 return
"$first - $last";
 }
 |  |  
|  add a note |  
 |  |